Commit a5f8107819f2122fb1b69b121679c06ddf90613f
1 parent
70f5b4a142
Exists in
master
Batal config.do_not_forward_rc68_to_centers
Showing 3 changed files with 16 additions and 19 deletions Inline Diff
config.sample.json
1 | { | 1 | { |
2 | "listener": { | 2 | "listener": { |
3 | "http": { | 3 | "http": { |
4 | "port": 32979 | 4 | "port": 32979 |
5 | } | 5 | } |
6 | }, | 6 | }, |
7 | "ip_whitelist": [ | 7 | "ip_whitelist": [ |
8 | "127.0.0.1", | 8 | "127.0.0.1", |
9 | "::ffff:127.0.0.1", | 9 | "::ffff:127.0.0.1", |
10 | "::1" | 10 | "::1" |
11 | ], | 11 | ], |
12 | "ascending_deposit": false, | 12 | "ascending_deposit": false, |
13 | "ascending_mutation": false, | 13 | "ascending_mutation": false, |
14 | "blacklist_help_for_origins": [], | 14 | "blacklist_help_for_origins": [], |
15 | "blacklist_help_for_origin_transports": [], | 15 | "blacklist_help_for_origin_transports": [], |
16 | "do_not_forward_rc68_to_centers": [], | ||
17 | "disable_claim_bonus": false | 16 | "disable_claim_bonus": false |
18 | } | 17 | } |
lib/coreapi/request.js
1 | // const PRODUCTION = process.env.NODE_ENV === 'production'; | 1 | // const PRODUCTION = process.env.NODE_ENV === 'production'; |
2 | 2 | ||
3 | const request = require('request'); | 3 | const request = require('request'); |
4 | const uniqid = require('uniqid'); | 4 | const uniqid = require('uniqid'); |
5 | 5 | ||
6 | const coreUrl = require('komodo-sdk/core-url'); | 6 | const coreUrl = require('komodo-sdk/core-url'); |
7 | const config = require('komodo-sdk/config'); | ||
8 | const logger = require('komodo-sdk/logger'); | 7 | const logger = require('komodo-sdk/logger'); |
9 | const commandError = require('../command-handler/error'); | 8 | const commandError = require('../command-handler/error'); |
10 | 9 | ||
11 | function execute(coreEndpoint, params, httpMethod, cb) { | 10 | function execute(coreEndpoint, params, httpMethod, cb) { |
12 | const xid = uniqid(); | 11 | const xid = uniqid(); |
13 | 12 | ||
14 | const requestOptions = { | 13 | const requestOptions = { |
15 | url: coreUrl + coreEndpoint, | 14 | url: coreUrl + coreEndpoint, |
16 | method: httpMethod || 'GET' | 15 | method: httpMethod || 'GET' |
17 | } | 16 | } |
18 | 17 | ||
19 | if (requestOptions.method === 'GET' && params) { | 18 | if (requestOptions.method === 'GET' && params) { |
20 | requestOptions.qs = params; | 19 | requestOptions.qs = params; |
21 | } | 20 | } |
22 | else if (requestOptions.method === 'POST' && params) { | 21 | else if (requestOptions.method === 'POST' && params) { |
23 | requestOptions.data = params; | 22 | requestOptions.data = params; |
24 | } | 23 | } |
25 | 24 | ||
26 | logger.verbose('COREAPI-REQUEST: Requesting to core', { | 25 | logger.verbose('COREAPI-REQUEST: Requesting to core', { |
27 | xid, url: requestOptions.url, http_method: httpMethod, params: params, | 26 | xid, url: requestOptions.url, http_method: httpMethod, params: params, |
28 | }); | 27 | }); |
29 | 28 | ||
30 | request(requestOptions, function(err, res, body) { | 29 | request(requestOptions, function(err, res, body) { |
31 | const responseParams = { | 30 | const responseParams = { |
32 | directResponse: true, | 31 | directResponse: true, |
33 | httpStatusCode: res ? res.statusCode : null, | 32 | httpStatusCode: res ? res.statusCode : null, |
34 | body: body | 33 | body: body |
35 | } | 34 | } |
36 | 35 | ||
37 | if (err) { | 36 | if (err) { |
38 | logger.warn('ERROR on requesting to CORE', { | 37 | logger.warn('ERROR on requesting to CORE', { |
39 | xid, eCode: err.code, eMessage: err.message, | 38 | xid, eCode: err.code, eMessage: err.message, |
40 | }); | 39 | }); |
41 | cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams); | 40 | cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams); |
42 | return; | 41 | return; |
43 | } | 42 | } |
44 | 43 | ||
45 | if (res.statusCode !== 200) { | 44 | if (res.statusCode !== 200) { |
46 | logger.warn('Invalid HTTP status from CORE', { | 45 | logger.warn('Invalid HTTP status from CORE', { |
47 | xid, httpStatus: res.statusCode, | 46 | xid, httpStatus: res.statusCode, |
48 | }); | 47 | }); |
49 | 48 | ||
50 | cb(commandError.ERR_INVALID_CORE_HTTP_STATUS_RESPONSE, null, responseParams); | 49 | cb(commandError.ERR_INVALID_CORE_HTTP_STATUS_RESPONSE, null, responseParams); |
51 | return; | 50 | return; |
52 | } | 51 | } |
53 | 52 | ||
54 | try { | 53 | try { |
55 | var coreResponseObject = JSON.parse(body); | 54 | var coreResponseObject = JSON.parse(body); |
56 | } | 55 | } |
57 | catch(e) { | 56 | catch(e) { |
58 | logger.warn(commandError.ERR_INVALID_CORE_RESPONSE, { | 57 | logger.warn(commandError.ERR_INVALID_CORE_RESPONSE, { |
59 | xid, eCode: e.code, eMessage: e.message, body: body | 58 | xid, eCode: e.code, eMessage: e.message, body: body |
60 | }); | 59 | }); |
61 | cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams); | 60 | cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams); |
62 | return; | 61 | return; |
63 | } | 62 | } |
64 | 63 | ||
65 | logger.verbose('Got CORE response', { | 64 | logger.verbose('Got CORE response', { |
66 | xid, coreResponseObject, | 65 | xid, coreResponseObject, |
67 | }); | 66 | }); |
68 | 67 | ||
69 | // jangan kirim reply rc 68 jika origin/center ada di do_not_forward_rc68_to_centers | 68 | // jangan kirim reply rc 68 jika origin/center ada di do_not_forward_rc68_to_centers. |
70 | if ( | 69 | // tidak jadi, ditangani di center masing2 via komodo-center-messaging-lib |
71 | coreResponseObject && coreResponseObject.rc === '68' | 70 | // if ( |
72 | && config && config.do_not_forward_rc68_to_centers | 71 | // coreResponseObject && coreResponseObject.rc === '68' |
73 | && params && params.origin && params.origin.trim() | 72 | // && config && config.do_not_forward_rc68_to_centers |
74 | && typeof params.origin === 'string' | 73 | // && params && params.origin && params.origin.trim() |
75 | && Array.isArray(config.do_not_forward_rc68_to_centers) | 74 | // && typeof params.origin === 'string' |
76 | && config.do_not_forward_rc68_to_centers | 75 | // && Array.isArray(config.do_not_forward_rc68_to_centers) |
77 | .map((item) => item && (typeof item === 'string') && item.trim().toUpperCase()) | 76 | // && config.do_not_forward_rc68_to_centers |
78 | .indexOf(params.origin.trim().toUpperCase()) >= 0 | 77 | // .map((item) => item && (typeof item === 'string') && item.trim().toUpperCase()) |
79 | ) { | 78 | // .indexOf(params.origin.trim().toUpperCase()) >= 0 |
80 | return; | 79 | // ) { |
81 | } | 80 | // return; |
81 | // } | ||
82 | 82 | ||
83 | cb(err, coreResponseObject, responseParams); | 83 | cb(err, coreResponseObject, responseParams); |
84 | }) | 84 | }) |
85 | 85 | ||
86 | } | 86 | } |
87 | 87 |
lib/http-listener.js
1 | const express = require('express'); | 1 | const express = require('express'); |
2 | const bodyParser = require('body-parser'); | 2 | const bodyParser = require('body-parser'); |
3 | const ipfilter = require('express-ipfilter').IpFilter | 3 | const ipfilter = require('express-ipfilter').IpFilter |
4 | 4 | ||
5 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
6 | const logger = require('komodo-sdk/logger'); | 6 | const logger = require('komodo-sdk/logger'); |
7 | 7 | ||
8 | const commandHandler = require('./command-handler'); | 8 | const commandHandler = require('./command-handler'); |
9 | const messagesArchive = require('./messages-archive'); | 9 | const messagesArchive = require('./messages-archive'); |
10 | 10 | ||
11 | const app = express(); | 11 | const app = express(); |
12 | const port = config && config.listener && config.listener.http && config.listener.http.port ? config.listener.http.port : 32979; | 12 | const port = config && config.listener && config.listener.http && config.listener.http.port ? config.listener.http.port : 32979; |
13 | 13 | ||
14 | /** | 14 | /** |
15 | * Handler utama. | 15 | * Handler utama. |
16 | * | 16 | * |
17 | * @param {object} req - Express request object | 17 | * @param {object} req - Express request object |
18 | * @param {object} req.query - Express query string object | 18 | * @param {object} req.query - Express query string object |
19 | * @param {string} [req.query.partner] - Partner (pengirim atau penerima) | 19 | * @param {string} [req.query.partner] - Partner (pengirim atau penerima) |
20 | * @param {string} [req.query.from] - Pengirim, OBSOLETED: gunakan parameter partner | 20 | * @param {string} [req.query.from] - Pengirim, OBSOLETED: gunakan parameter partner |
21 | * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED: gunakan parameter partner_raw | 21 | * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED: gunakan parameter partner_raw |
22 | * @param {string} [req.query.to] - Tujuan, jika is_outgoing, OBSOLETED: gunakan parameter partner | 22 | * @param {string} [req.query.to] - Tujuan, jika is_outgoing, OBSOLETED: gunakan parameter partner |
23 | * @param {string} req.query.msg - Isi pesan | 23 | * @param {string} req.query.msg - Isi pesan |
24 | * @param {string} req.query.origin - Nama origin | 24 | * @param {string} req.query.origin - Nama origin |
25 | * @param {string} [req.query.origin_label] - Nama origin untuk ditulis di histori pesan | 25 | * @param {string} [req.query.origin_label] - Nama origin untuk ditulis di histori pesan |
26 | * @param {string} [req.query.do_not_forward_to_core] - Apakah teruskan pesan ke CORE | 26 | * @param {string} [req.query.do_not_forward_to_core] - Apakah teruskan pesan ke CORE |
27 | * @param {string} [req.query.is_outgoing] - Apakah pesan keluar | 27 | * @param {string} [req.query.is_outgoing] - Apakah pesan keluar |
28 | * @param {object} res - Express response object | 28 | * @param {object} res - Express response object |
29 | */ | 29 | */ |
30 | function mainHandler(req, res) { | 30 | function mainHandler(req, res) { |
31 | if (!req.body) req.body = {}; | 31 | if (!req.body) req.body = {}; |
32 | 32 | ||
33 | if ( | 33 | if ( |
34 | ( !req.body.partner && !req.query.partner ) | 34 | ( !req.body.partner && !req.query.partner ) |
35 | && | 35 | && |
36 | ( | 36 | ( |
37 | ( (!req.query.is_outgoing && !req.body.is_outgoing) && (!req.query.from && !req.body.from) ) | 37 | ( (!req.query.is_outgoing && !req.body.is_outgoing) && (!req.query.from && !req.body.from) ) |
38 | || | 38 | || |
39 | ( (req.query.is_outgoing || req.body.is_outgoing) && (!req.query.to && !req.body.to) ) | 39 | ( (req.query.is_outgoing || req.body.is_outgoing) && (!req.query.to && !req.body.to) ) |
40 | ) | 40 | ) |
41 | ) { | 41 | ) { |
42 | logger.warn('Undefined parameter partner or from or to. #D254B7B454DB', { | 42 | logger.warn('Undefined parameter partner or from or to. #D254B7B454DB', { |
43 | partner: req.body.partner || req.query.partner, | 43 | partner: req.body.partner || req.query.partner, |
44 | is_outgoing: req.body.is_outgoing || req.query.is_outgoing, | 44 | is_outgoing: req.body.is_outgoing || req.query.is_outgoing, |
45 | from: req.body.from || req.query.from, | 45 | from: req.body.from || req.query.from, |
46 | to: req.body.to || req.query.to, | 46 | to: req.body.to || req.query.to, |
47 | }); | 47 | }); |
48 | res.end('ERROR. Undefined parameter: partner or from or to'); | 48 | res.end('ERROR. Undefined parameter: partner or from or to'); |
49 | return; | 49 | return; |
50 | } | 50 | } |
51 | 51 | ||
52 | if (!req.query.msg && !req.body.msg) { | 52 | if (!req.query.msg && !req.body.msg) { |
53 | logger.warn('Undefined parameter msg. #92996A497D12') | 53 | logger.warn('Undefined parameter msg. #92996A497D12') |
54 | res.end('ERROR. Undefined parameter: msg'); | 54 | res.end('ERROR. Undefined parameter: msg'); |
55 | return; | 55 | return; |
56 | } | 56 | } |
57 | 57 | ||
58 | logger.verbose( | 58 | logger.verbose( |
59 | `Saving ${req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming' } message history`, | 59 | `Saving ${req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming' } message history`, |
60 | { | 60 | { |
61 | transport: req.body.origin_transport || req.query.origin_transport, | 61 | transport: req.body.origin_transport || req.query.origin_transport, |
62 | partner: req.body.partner || req.query.partner || req.body.from || req.query.from || req.body.to || req.query.to, | 62 | partner: req.body.partner || req.query.partner || req.body.from || req.query.from || req.body.to || req.query.to, |
63 | msg: req.body.msg || req.query.msg, | 63 | msg: req.body.msg || req.query.msg, |
64 | } | 64 | } |
65 | ); | 65 | ); |
66 | 66 | ||
67 | messagesArchive.insert( | 67 | messagesArchive.insert( |
68 | { | 68 | { |
69 | origin_label: req.body.origin_label || req.query.origin_label || req.body.origin || req.query.origin, | 69 | origin_label: req.body.origin_label || req.query.origin_label || req.body.origin || req.query.origin, |
70 | origin_transport: req.body.origin_transport || req.query.origin_transport, | 70 | origin_transport: req.body.origin_transport || req.query.origin_transport, |
71 | partner: req.body.partner_raw || req.query.partner_raw || req.body.from_raw || req.query.from_raw || req.body.from || req.query.from || req.body.to || req.query.to || req.body.partner || req.query.partner, | 71 | partner: req.body.partner_raw || req.query.partner_raw || req.body.from_raw || req.query.from_raw || req.body.from || req.query.from || req.body.to || req.query.to || req.body.partner || req.query.partner, |
72 | msg: req.body.msg || req.query.msg, | 72 | msg: req.body.msg || req.query.msg, |
73 | }, | 73 | }, |
74 | ( req.body.is_outgoing || req.query.is_outgoing ) ? messagesArchive.DIRECTION_OUTGOING : messagesArchive.DIRECTION_INCOMING | 74 | ( req.body.is_outgoing || req.query.is_outgoing ) ? messagesArchive.DIRECTION_OUTGOING : messagesArchive.DIRECTION_INCOMING |
75 | ); | 75 | ); |
76 | 76 | ||
77 | if ( | 77 | if ( |
78 | req.body.do_not_forward_to_core || req.query.do_not_forward_to_core | 78 | req.body.do_not_forward_to_core || req.query.do_not_forward_to_core |
79 | || req.body.is_outgoing || req.query.is_outgoing | 79 | || req.body.is_outgoing || req.query.is_outgoing |
80 | ) { | 80 | ) { |
81 | logger.verbose( 'Ignoring message', { | 81 | logger.verbose( 'Ignoring message', { |
82 | from: req.body.from || req.query.from, | 82 | from: req.body.from || req.query.from, |
83 | msg: req.body.msg || req.query.msg, | 83 | msg: req.body.msg || req.query.msg, |
84 | do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core | 84 | do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core |
85 | }); | 85 | }); |
86 | 86 | ||
87 | res.end('OK'); | 87 | res.end('OK'); |
88 | return; | 88 | return; |
89 | } | 89 | } |
90 | 90 | ||
91 | if (!req.query.report_port && !req.body.report_port) { | 91 | if (!req.query.report_port && !req.body.report_port) { |
92 | res.end('ERROR. Undefined parameter: report_port'); | 92 | res.end('ERROR. Undefined parameter: report_port'); |
93 | return; | 93 | return; |
94 | } | 94 | } |
95 | 95 | ||
96 | const params = { | 96 | const params = { |
97 | origin: req.body.origin || req.query.origin || 'MESSAGING', | 97 | origin: req.body.origin || req.query.origin || 'MESSAGING', |
98 | origin_transport: req.body.origin_transport || req.query.origin_transport, | 98 | origin_transport: req.body.origin_transport || req.query.origin_transport, |
99 | report_ip: req.body.report_ip || req.query.report_ip || req.ip, | 99 | report_ip: req.body.report_ip || req.query.report_ip || req.ip, |
100 | report_port: req.body.report_port || req.query.report_port, | 100 | report_port: req.body.report_port || req.query.report_port, |
101 | from: req.body.partner || req.query.partner || req.body.from || req.query.from, | 101 | from: req.body.partner || req.query.partner || req.body.from || req.query.from, |
102 | msg: req.body.msg || req.query.msg | 102 | msg: req.body.msg || req.query.msg |
103 | } | 103 | } |
104 | 104 | ||
105 | commandHandler(req.body.msg || req.query.msg, params, function(err, coreResponseObject, responseParams) { | 105 | commandHandler(req.body.msg || req.query.msg, params, function(err, coreResponseObject, responseParams) { |
106 | if (err) { | 106 | if (err) { |
107 | res.end('ERROR. ' + err); | 107 | res.end('ERROR. ' + err); |
108 | } | 108 | } else if (coreResponseObject) { |
109 | else if (coreResponseObject) { | ||
110 | res.json(coreResponseObject); | 109 | res.json(coreResponseObject); |
111 | } | 110 | } else { |
112 | else { | ||
113 | res.end(responseParams.body); | 111 | res.end(responseParams.body); |
114 | } | 112 | } |
115 | }) | 113 | }) |
116 | } | 114 | } |
117 | 115 | ||
118 | config.ip_whitelist && config.ip_whitelist.length && app.use(ipfilter(config.ip_whitelist, {mode: 'allow', log: false})); | 116 | config.ip_whitelist && config.ip_whitelist.length && app.use(ipfilter(config.ip_whitelist, {mode: 'allow', log: false})); |
119 | 117 | ||
120 | app.get('/', mainHandler); | 118 | app.get('/', mainHandler); |
121 | app.post('/', bodyParser.urlencoded({extended: true}), mainHandler); | 119 | app.post('/', bodyParser.urlencoded({extended: true}), mainHandler); |
122 | 120 | ||
123 | app.listen(port, function() { | 121 | app.listen(port, function() { |
124 | logger.info('HTTP-LISTENER: started', {port: port, app_env: app.get('env')}); | 122 | logger.info('HTTP-LISTENER: started', {port: port, app_env: app.get('env')}); |
125 | }); | 123 | }); |