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 => { |
|
||||||
if ( result ) { |
|
||||||
let flag = false |
let flag = false |
||||||
for ( let i = 0; i < result.resourcePaths.length; i++ ) { |
const availableEndPoints = service.endpoints[request.method.toLowerCase()] || [] |
||||||
if ( request.path === result.resourcePaths[i].path |
const splittedRequestPath = request.path.replace(/^\/|\/$/g, '').split('/') |
||||||
&& request.method == result.resourcePaths[i].method) { |
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 ( fractalCheckFlag ) { |
||||||
flag = true |
flag = true |
||||||
break |
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 ) { |
if ( flag ) { |
||||||
logModel.addLog(new logModel({ |
logModel.addLog(new logModel({ |
||||||
path: request.path, |
path: request.path, |
||||||
service: request.app_id, |
service: request.app_id, |
||||||
ip_address: request.ip_address |
ip_address: request.ip_address |
||||||
})) |
})) |
||||||
callback(result, request, null) |
callback(request, service, null) |
||||||
} else { |
} else { |
||||||
const err = { |
const err = { |
||||||
type: 'UNAUTHORIZED', |
type: 'NOT_FOUND', |
||||||
module_source: 'request_resolver', |
module_source: 'request_resolver', |
||||||
message: 'Request method is not found.' |
message: 'Request method is not found.' |
||||||
} |
} |
||||||
callback(null, null, err) |
callback(null, null, err) |
||||||
} |
} |
||||||
} else { |
|
||||||
const err = { |
|
||||||
type: 'NOT_FOUND', |
|
||||||
module_source: 'request_resolver', |
|
||||||
message: 'Host not found in the database' |
|
||||||
} |
|
||||||
callback(null, null, err) |
|
||||||
} |
} |
||||||
}) |
}) |
||||||
.catch(_ => { |
.catch(err => { |
||||||
const err = { |
console.log(err) |
||||||
type: 'SERVER_ERROR', |
|
||||||
module_source: 'request_resolver', |
|
||||||
message: 'Internal server Error. Please back later.' |
|
||||||
} |
|
||||||
callback(null, null, err) |
callback(null, null, err) |
||||||
}) |
}) |
||||||
} else { |
|
||||||
const err = { |
|
||||||
type: 'UNAUTHORIZED', |
|
||||||
module_source: 'request_resolver', |
|
||||||
message: 'You\'re not allowed to do this action.' |
|
||||||
} |
|
||||||
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