Commit c7425b7ec662cb8573d89653f43f6a7a94aa77cf
1 parent
1fc809c905
Exists in
master
Multilne
Showing 1 changed file with 10 additions and 2 deletions Inline Diff
lib/http-listener.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const express = require('express'); | 3 | const express = require('express'); |
4 | const bodyParser = require('body-parser'); | 4 | const bodyParser = require('body-parser'); |
5 | const ipfilter = require('express-ipfilter').IpFilter | 5 | const ipfilter = require('express-ipfilter').IpFilter |
6 | 6 | ||
7 | const config = require('komodo-sdk/config'); | 7 | const config = require('komodo-sdk/config'); |
8 | const logger = require('komodo-sdk/logger'); | 8 | const logger = require('komodo-sdk/logger'); |
9 | 9 | ||
10 | const commandHandler = require('./command-handler'); | 10 | const commandHandler = require('./command-handler'); |
11 | const messagesArchive = require('./messages-archive'); | 11 | const messagesArchive = require('./messages-archive'); |
12 | 12 | ||
13 | const app = express(); | 13 | const app = express(); |
14 | const port = config && config.listener && config.listener.http && config.listener.http.port ? config.listener.http.port : 32979; | 14 | const port = config && config.listener && config.listener.http && config.listener.http.port ? config.listener.http.port : 32979; |
15 | 15 | ||
16 | /** | 16 | /** |
17 | * Handler utama. | 17 | * Handler utama. |
18 | * | 18 | * |
19 | * @param {object} req - Express request object | 19 | * @param {object} req - Express request object |
20 | * @param {object} req.query - Express query string object | 20 | * @param {object} req.query - Express query string object |
21 | * @param {string} [req.query.partner] - Partner (pengirim atau penerima) | 21 | * @param {string} [req.query.partner] - Partner (pengirim atau penerima) |
22 | * @param {string} [req.query.from] - Pengirim, OBSOLETED: gunakan parameter partner | 22 | * @param {string} [req.query.from] - Pengirim, OBSOLETED: gunakan parameter partner |
23 | * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED: gunakan parameter partner_raw | 23 | * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED: gunakan parameter partner_raw |
24 | * @param {string} [req.query.to] - Tujuan, jika is_outgoing, OBSOLETED: gunakan parameter partner | 24 | * @param {string} [req.query.to] - Tujuan, jika is_outgoing, OBSOLETED: gunakan parameter partner |
25 | * @param {string} req.query.msg - Isi pesan | 25 | * @param {string} req.query.msg - Isi pesan |
26 | * @param {string} req.query.origin - Nama origin | 26 | * @param {string} req.query.origin - Nama origin |
27 | * @param {string} [req.query.origin_label] - Nama origin untuk ditulis di histori pesan | 27 | * @param {string} [req.query.origin_label] - Nama origin untuk ditulis di histori pesan |
28 | * @param {string} [req.query.do_not_forward_to_core] - Apakah teruskan pesan ke CORE | 28 | * @param {string} [req.query.do_not_forward_to_core] - Apakah teruskan pesan ke CORE |
29 | * @param {string} [req.query.is_outgoing] - Apakah pesan keluar | 29 | * @param {string} [req.query.is_outgoing] - Apakah pesan keluar |
30 | * @param {object} res - Express response object | 30 | * @param {object} res - Express response object |
31 | */ | 31 | */ |
32 | function mainHandler(req, res) { | 32 | function mainHandler(req, res) { |
33 | if (!req.body) req.body = {}; | 33 | if (!req.body) req.body = {}; |
34 | 34 | ||
35 | if ( | 35 | if ( |
36 | ( !req.body.partner && !req.query.partner ) | 36 | ( !req.body.partner && !req.query.partner ) |
37 | && | 37 | && |
38 | ( | 38 | ( |
39 | ( (!req.query.is_outgoing && !req.body.is_outgoing) && (!req.query.from && !req.body.from) ) | 39 | ( (!req.query.is_outgoing && !req.body.is_outgoing) && (!req.query.from && !req.body.from) ) |
40 | || | 40 | || |
41 | ( (req.query.is_outgoing || req.body.is_outgoing) && (!req.query.to && !req.body.to) ) | 41 | ( (req.query.is_outgoing || req.body.is_outgoing) && (!req.query.to && !req.body.to) ) |
42 | ) | 42 | ) |
43 | ) { | 43 | ) { |
44 | logger.warn('Undefined parameter partner or from or to. #D254B7B454DB', { | 44 | logger.warn('Undefined parameter partner or from or to. #D254B7B454DB', { |
45 | partner: req.body.partner || req.query.partner, | 45 | partner: req.body.partner || req.query.partner, |
46 | is_outgoing: req.body.is_outgoing || req.query.is_outgoing, | 46 | is_outgoing: req.body.is_outgoing || req.query.is_outgoing, |
47 | from: req.body.from || req.query.from, | 47 | from: req.body.from || req.query.from, |
48 | to: req.body.to || req.query.to, | 48 | to: req.body.to || req.query.to, |
49 | }); | 49 | }); |
50 | res.end('ERROR. Undefined parameter: partner or from or to'); | 50 | res.end('ERROR. Undefined parameter: partner or from or to'); |
51 | return; | 51 | return; |
52 | } | 52 | } |
53 | 53 | ||
54 | if (!req.query.msg && !req.body.msg) { | 54 | if (!req.query.msg && !req.body.msg) { |
55 | logger.warn('Undefined parameter msg. #92996A497D12') | 55 | logger.warn('Undefined parameter msg. #92996A497D12') |
56 | res.end('ERROR. Undefined parameter: msg'); | 56 | res.end('ERROR. Undefined parameter: msg'); |
57 | return; | 57 | return; |
58 | } | 58 | } |
59 | 59 | ||
60 | logger.info(`Saving ${req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming' } message history`); | 60 | logger.info(`Saving ${req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming' } message history`); |
61 | 61 | ||
62 | messagesArchive.insert( | 62 | messagesArchive.insert( |
63 | { | 63 | { |
64 | origin_label: req.body.origin_label || req.query.origin_label || req.body.origin || req.query.origin, | 64 | origin_label: req.body.origin_label || req.query.origin_label || req.body.origin || req.query.origin, |
65 | origin_transport: req.body.origin_transport || req.query.origin_transport, | 65 | origin_transport: req.body.origin_transport || req.query.origin_transport, |
66 | 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, | 66 | 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, |
67 | msg: req.body.msg || req.query.msg, | 67 | msg: req.body.msg || req.query.msg, |
68 | }, | 68 | }, |
69 | ( req.body.is_outgoing || req.query.is_outgoing ) ? messagesArchive.DIRECTION_OUTGOING : messagesArchive.DIRECTION_INCOMING | 69 | ( req.body.is_outgoing || req.query.is_outgoing ) ? messagesArchive.DIRECTION_OUTGOING : messagesArchive.DIRECTION_INCOMING |
70 | ); | 70 | ); |
71 | 71 | ||
72 | if (req.body.do_not_forward_to_core || req.query.do_not_forward_to_core || req.body.is_outgoing || req.query.is_outgoing) { | 72 | if ( |
73 | logger.verbose('Ignoring message', { from: req.body.from || req.query.from, msg: req.body.msg || req.query.msg, do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core }); | 73 | req.body.do_not_forward_to_core || req.query.do_not_forward_to_core |
74 | || req.body.is_outgoing || req.query.is_outgoing | ||
75 | ) { | ||
76 | logger.verbose( 'Ignoring message', { | ||
77 | from: req.body.from || req.query.from, | ||
78 | msg: req.body.msg || req.query.msg, | ||
79 | do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core | ||
80 | }); | ||
81 | |||
74 | res.end('OK'); | 82 | res.end('OK'); |
75 | return; | 83 | return; |
76 | } | 84 | } |
77 | 85 | ||
78 | if (!req.query.report_port && !req.body.report_port) { | 86 | if (!req.query.report_port && !req.body.report_port) { |
79 | res.end('ERROR. Undefined parameter: report_port'); | 87 | res.end('ERROR. Undefined parameter: report_port'); |
80 | return; | 88 | return; |
81 | } | 89 | } |
82 | 90 | ||
83 | const params = { | 91 | const params = { |
84 | origin: req.body.origin || req.query.origin || 'MESSAGING', | 92 | origin: req.body.origin || req.query.origin || 'MESSAGING', |
85 | report_ip: req.body.report_ip || req.query.report_ip || req.ip, | 93 | report_ip: req.body.report_ip || req.query.report_ip || req.ip, |
86 | report_port: req.body.report_port || req.query.report_port, | 94 | report_port: req.body.report_port || req.query.report_port, |
87 | from: req.body.partner || req.query.partner || req.body.from || req.query.from, | 95 | from: req.body.partner || req.query.partner || req.body.from || req.query.from, |
88 | msg: req.body.msg || req.query.msg | 96 | msg: req.body.msg || req.query.msg |
89 | } | 97 | } |
90 | 98 | ||
91 | commandHandler(req.body.msg || req.query.msg, params, function(err, coreResponseObject, responseParams) { | 99 | commandHandler(req.body.msg || req.query.msg, params, function(err, coreResponseObject, responseParams) { |
92 | if (err) { | 100 | if (err) { |
93 | res.end('ERROR. ' + err); | 101 | res.end('ERROR. ' + err); |
94 | } | 102 | } |
95 | else if (coreResponseObject) { | 103 | else if (coreResponseObject) { |
96 | res.json(coreResponseObject); | 104 | res.json(coreResponseObject); |
97 | } | 105 | } |
98 | else { | 106 | else { |
99 | res.end(responseParams.body); | 107 | res.end(responseParams.body); |
100 | } | 108 | } |
101 | }) | 109 | }) |
102 | } | 110 | } |
103 | 111 | ||
104 | config.ip_whitelist && config.ip_whitelist.length && app.use(ipfilter(config.ip_whitelist, {mode: 'allow', log: false})); | 112 | config.ip_whitelist && config.ip_whitelist.length && app.use(ipfilter(config.ip_whitelist, {mode: 'allow', log: false})); |
105 | 113 | ||
106 | app.get('/', mainHandler); | 114 | app.get('/', mainHandler); |
107 | app.post('/', bodyParser.urlencoded({extended: true}), mainHandler); | 115 | app.post('/', bodyParser.urlencoded({extended: true}), mainHandler); |
108 | 116 | ||
109 | app.listen(port, function() { | 117 | app.listen(port, function() { |
110 | logger.info('HTTP-LISTENER: started', {port: port, app_env: app.get('env')}); | 118 | logger.info('HTTP-LISTENER: started', {port: port, app_env: app.get('env')}); |
111 | }); | 119 | }); |