pull/1/head
parent
f3f13a3fae
commit
43890c342e
13 changed files with 119 additions and 69 deletions
@ -0,0 +1,22 @@ |
|||||||
|
services: |
||||||
|
main-services: |
||||||
|
port: 3422 |
||||||
|
base_url: http://localhost |
||||||
|
endpoints: |
||||||
|
get: |
||||||
|
- /user |
||||||
|
- /item |
||||||
|
- /testing |
||||||
|
put: |
||||||
|
- /user/*/*/fdf |
||||||
|
- /item/:query/:query |
||||||
|
delete: |
||||||
|
- /user/ |
||||||
|
- /item/ |
||||||
|
post: |
||||||
|
- /user/create |
||||||
|
- /item/create |
||||||
|
enable_auth: false |
||||||
|
ip_blacklist: |
||||||
|
- "103.77.77.91" |
||||||
|
- "103.77.78.111" |
@ -1,65 +1,83 @@ |
|||||||
const {validateAPIKey, grabRequest} = require('../helpers/index') // Helpers
|
const {validateAPIKey, grabRequest} = require('../helpers/index') // Helpers
|
||||||
|
const fs = require('fs') |
||||||
|
const yaml = require('yaml') |
||||||
|
|
||||||
function __resolveRequest(req, logModel, serviceModel, callback) { |
|
||||||
|
function __getServiceInformation(service_name) { |
||||||
|
return new Promise((resolve, reject) => { |
||||||
|
const file = fs.readFileSync('./app/gateway_conf.yml', 'utf8') |
||||||
|
const conf = yaml.parse(file) |
||||||
|
if ( conf.services.hasOwnProperty(service_name) ) { |
||||||
|
resolve(conf.services[service_name]) |
||||||
|
} else { |
||||||
|
const err = { |
||||||
|
type: 'NOT_FOUND', |
||||||
|
module_source: 'request_resolver', |
||||||
|
message: 'Service method is not found.' |
||||||
|
} |
||||||
|
reject(err) |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
function __resolveRequest(req, logModel, callback) { |
||||||
let request = grabRequest(req) |
let request = grabRequest(req) |
||||||
if ( validateAPIKey(request.api_key) ) { |
__getServiceInformation(request.app_id || '') |
||||||
serviceModel.getByID(request.app_id) |
.then(service => { |
||||||
.then(result => { |
let flag = false |
||||||
if ( result ) { |
const availableEndPoints = service.endpoints[request.method.toLowerCase()] || [] |
||||||
let flag = false |
const splittedRequestPath = request.path.replace(/^\/|\/$/g, '').split('/') |
||||||
for ( let i = 0; i < result.resourcePaths.length; i++ ) { |
for ( let i = 0; i < availableEndPoints.length; i++ ) { |
||||||
if ( request.path === result.resourcePaths[i].path |
let splittedEndPointPath = availableEndPoints[i].replace(/^\/|\/$/g, '').split('/') |
||||||
&& request.method == result.resourcePaths[i].method) { |
if ( splittedRequestPath.length === splittedEndPointPath.length ) { |
||||||
flag = true |
let fractalCheckFlag = true |
||||||
|
for ( let j = 0; j < splittedEndPointPath.length; j++ ) { |
||||||
|
if ( splittedEndPointPath[j] !== splittedRequestPath[j] && splittedEndPointPath[j] !== '*' ) { |
||||||
|
fractalCheckFlag = false |
||||||
break |
break |
||||||
} |
} |
||||||
} |
} |
||||||
if ( flag ) { |
if ( fractalCheckFlag ) { |
||||||
logModel.addLog(new logModel({ |
flag = true |
||||||
path: request.path, |
break |
||||||
service: request.app_id, |
|
||||||
ip_address: request.ip_address |
|
||||||
})) |
|
||||||
callback(result, request, null) |
|
||||||
} else { |
|
||||||
const err = { |
|
||||||
type: 'UNAUTHORIZED', |
|
||||||
module_source: 'request_resolver', |
|
||||||
message: 'Request method is not found.' |
|
||||||
} |
|
||||||
callback(null, null, err) |
|
||||||
} |
} |
||||||
|
} |
||||||
|
} |
||||||
|
if ( service.enable_auth && !validateAPIKey(request.api_key) ) { |
||||||
|
const err = { |
||||||
|
type: 'UNAUTHORIZED', |
||||||
|
module_source: 'request_resolver', |
||||||
|
message: 'You\'re not allowed to do this action.' |
||||||
|
} |
||||||
|
callback(null, null, err)
|
||||||
|
} else { |
||||||
|
if ( flag ) { |
||||||
|
logModel.addLog(new logModel({ |
||||||
|
path: request.path, |
||||||
|
service: request.app_id, |
||||||
|
ip_address: request.ip_address |
||||||
|
})) |
||||||
|
callback(request, service, null) |
||||||
} else { |
} else { |
||||||
const err = { |
const err = { |
||||||
type: 'NOT_FOUND', |
type: 'NOT_FOUND', |
||||||
module_source: 'request_resolver', |
module_source: 'request_resolver', |
||||||
message: 'Host not found in the database' |
message: 'Request method is not found.' |
||||||
} |
} |
||||||
callback(null, null, err) |
callback(null, null, err) |
||||||
} |
} |
||||||
}) |
|
||||||
.catch(_ => { |
|
||||||
const err = { |
|
||||||
type: 'SERVER_ERROR', |
|
||||||
module_source: 'request_resolver', |
|
||||||
message: 'Internal server Error. Please back later.' |
|
||||||
} |
|
||||||
callback(null, null, err) |
|
||||||
}) |
|
||||||
} else { |
|
||||||
const err = { |
|
||||||
type: 'UNAUTHORIZED', |
|
||||||
module_source: 'request_resolver', |
|
||||||
message: 'You\'re not allowed to do this action.' |
|
||||||
} |
} |
||||||
|
}) |
||||||
|
.catch(err => { |
||||||
|
console.log(err) |
||||||
callback(null, null, err) |
callback(null, null, err) |
||||||
} |
}) |
||||||
} |
} |
||||||
|
|
||||||
module.exports = (logModel, serviceModel) => { |
module.exports = (logModel) => { |
||||||
return { |
return { |
||||||
resolveRequest: (req, callback) => { |
resolveRequest: (req, callback) => { |
||||||
return __resolveRequest(req, logModel, serviceModel, callback) |
return __resolveRequest(req, logModel, callback) |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
@ -1,12 +1,10 @@ |
|||||||
const logModel = require('./logs/index') |
const logModel = require('./logs/index') |
||||||
const serviceModel = require('./services/index') |
|
||||||
|
|
||||||
function __getLogModel(mongoose) { return logModel(mongoose) } |
function __getLogModel(mongoose) { return logModel(mongoose) } |
||||||
function __getServiceModel(mongoose) { return serviceModel(mongoose) } |
function __getServiceModel(mongoose) { return serviceModel(mongoose) } |
||||||
|
|
||||||
module.exports = (mongoose) => { |
module.exports = (mongoose) => { |
||||||
return { |
return { |
||||||
logModel: __getLogModel(mongoose), |
logModel: __getLogModel(mongoose) |
||||||
serviceModel: __getServiceModel(mongoose) |
|
||||||
} |
} |
||||||
} |
} |
@ -1,6 +1,3 @@ |
|||||||
const { validateAPIKey } = require('./validation/http_signature_validation') |
const { validateAPIKey } = require('./validation/http_signature_validation') |
||||||
const { grabRequest } = require('./webhooks/http_headers_grab') |
const { grabRequest } = require('./webhooks/http_headers_grab') |
||||||
|
module.exports = { validateAPIKey, grabRequest } |
||||||
module.exports = { |
|
||||||
validateAPIKey, grabRequest |
|
||||||
} |
|
@ -1,3 +0,0 @@ |
|||||||
/** |
|
||||||
* IP whitelist module |
|
||||||
*/ |
|
Loading…
Reference in new issue