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 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) |
||||
if ( validateAPIKey(request.api_key) ) { |
||||
serviceModel.getByID(request.app_id) |
||||
.then(result => { |
||||
if ( result ) { |
||||
let flag = false |
||||
for ( let i = 0; i < result.resourcePaths.length; i++ ) { |
||||
if ( request.path === result.resourcePaths[i].path |
||||
&& request.method == result.resourcePaths[i].method) { |
||||
flag = true |
||||
__getServiceInformation(request.app_id || '') |
||||
.then(service => { |
||||
let flag = false |
||||
const availableEndPoints = service.endpoints[request.method.toLowerCase()] || [] |
||||
const splittedRequestPath = request.path.replace(/^\/|\/$/g, '').split('/') |
||||
for ( let i = 0; i < availableEndPoints.length; i++ ) { |
||||
let splittedEndPointPath = availableEndPoints[i].replace(/^\/|\/$/g, '').split('/') |
||||
if ( splittedRequestPath.length === splittedEndPointPath.length ) { |
||||
let fractalCheckFlag = true |
||||
for ( let j = 0; j < splittedEndPointPath.length; j++ ) { |
||||
if ( splittedEndPointPath[j] !== splittedRequestPath[j] && splittedEndPointPath[j] !== '*' ) { |
||||
fractalCheckFlag = false |
||||
break |
||||
} |
||||
} |
||||
if ( flag ) { |
||||
logModel.addLog(new logModel({ |
||||
path: request.path, |
||||
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 ( fractalCheckFlag ) { |
||||
flag = true |
||||
break |
||||
} |
||||
} |
||||
} |
||||
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 { |
||||
const err = { |
||||
type: 'NOT_FOUND', |
||||
module_source: 'request_resolver', |
||||
message: 'Host not found in the database' |
||||
message: 'Request method is not found.' |
||||
} |
||||
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) |
||||
} |
||||
}) |
||||
} |
||||
|
||||
module.exports = (logModel, serviceModel) => { |
||||
module.exports = (logModel) => { |
||||
return { |
||||
resolveRequest: (req, callback) => { |
||||
return __resolveRequest(req, logModel, serviceModel, callback) |
||||
return __resolveRequest(req, logModel, callback) |
||||
} |
||||
} |
||||
|
||||
|
@ -1,12 +1,10 @@ |
||||
const logModel = require('./logs/index') |
||||
const serviceModel = require('./services/index') |
||||
|
||||
function __getLogModel(mongoose) { return logModel(mongoose) } |
||||
function __getServiceModel(mongoose) { return serviceModel(mongoose) } |
||||
|
||||
module.exports = (mongoose) => { |
||||
return { |
||||
logModel: __getLogModel(mongoose), |
||||
serviceModel: __getServiceModel(mongoose) |
||||
logModel: __getLogModel(mongoose) |
||||
} |
||||
} |
@ -1,6 +1,3 @@ |
||||
const { validateAPIKey } = require('./validation/http_signature_validation') |
||||
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