-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
200 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
users.json | ||
blocked_ips.csv | ||
users.csv | ||
|
||
# Logs | ||
logs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
const { AuthApiKey, Validator } = require("./middlewares"); | ||
const Model = require("./model"); | ||
const Validator = require("./validator"); | ||
const router = require("express").Router(); | ||
|
||
const validator = new Validator(); | ||
const model = new Model(); | ||
|
||
// Send your api username and password in json format to generate api_key | ||
router.post("/token", validator.token, (req, res) => { | ||
const result = model.token(); | ||
|
||
return res.json({ api_key: result }); | ||
return res.json(result); | ||
}); | ||
|
||
// Send your proxy email and limit data in json format to be applied | ||
router.post("/add", AuthApiKey, validator.add, (req, res) => { | ||
const result = model.add(req.body); | ||
|
||
return res.json(result); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const { response } = require("./utils"); | ||
const crypto = require("crypto-js"); | ||
const Joi = require("joi"); | ||
|
||
const AuthApiKey = (req, res, next) => { | ||
let apiKey = (req.headers["api_key"] || "").trim(); | ||
|
||
if (!apiKey) | ||
return res.status(403).json( | ||
response({ | ||
error: { | ||
type: "AUTH", | ||
reason: "api_key Not found!", | ||
}, | ||
}), | ||
); | ||
|
||
let decryptedKey = crypto.AES.decrypt( | ||
apiKey, | ||
process.env.API_SECRET, | ||
).toString(crypto.enc.Utf8); | ||
|
||
const parseKey = JSON.parse(decryptedKey); | ||
|
||
if (Date.now() > +parseKey.expireAt) | ||
return res.status(403).json( | ||
response({ | ||
error: { | ||
type: "AUTH", | ||
reason: "api_key expired Please get a new api_key", | ||
}, | ||
}), | ||
); | ||
|
||
return next(); | ||
}; | ||
|
||
class Validator { | ||
token(req, res, next) { | ||
/** | ||
* @type {ApiSetTokenType} | ||
*/ | ||
const data = req.body; | ||
|
||
const schema = Joi.object({ | ||
username: Joi.string().required(), | ||
password: Joi.string().required(), | ||
}); | ||
|
||
const { error } = schema.validate(data); | ||
|
||
if (error) { | ||
return res.status(403).json( | ||
response({ | ||
error: { | ||
type: "INVALID", | ||
reason: error.message, | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
if (process.env.API_LOGIN !== `${data.username}:${data.password}`) | ||
return res.status(403).json( | ||
response({ | ||
error: { | ||
type: "NOT_MATCH", | ||
reason: "Username or password doesn't match", | ||
}, | ||
}), | ||
); | ||
|
||
return next(); | ||
} | ||
|
||
add(req, res, next) { | ||
const schema = Joi.object({ | ||
email: Joi.string().required(), | ||
limit: Joi.number().required(), | ||
}); | ||
|
||
const data = req.body; | ||
|
||
const { error } = schema.validate(data); | ||
|
||
if (error) { | ||
return res.status(403).json( | ||
response({ | ||
error: { | ||
type: "INVALID", | ||
reason: error.message, | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
return next(); | ||
} | ||
} | ||
|
||
module.exports = { | ||
AuthApiKey, | ||
Validator, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* | ||
* @param {ApiResponseType} params | ||
*/ | ||
|
||
const response = (params) => { | ||
if (!params?.data) params.data = null; | ||
if (!params?.error) params.error = null; | ||
if (!params?.status) params.status = 0; | ||
|
||
return params; | ||
}; | ||
|
||
module.exports = { response }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters