Commit dcc1925c99400d0683a18bdefa38a5f6fc855197
1 parent
22b6472d9b
Exists in
master
Change log message on message history
Showing 1 changed file with 2 additions and 1 deletions Inline Diff
lib/http-listener.js
1 | const MODULE_NAME = 'HTTP-LISTENER'; | 1 | const MODULE_NAME = 'HTTP-LISTENER'; |
2 | 2 | ||
3 | const express = require('express'); | 3 | const express = require('express'); |
4 | const ipfilter = require('express-ipfilter').IpFilter; | 4 | const ipfilter = require('express-ipfilter').IpFilter; |
5 | const removeAccents = require('remove-accents'); | 5 | const removeAccents = require('remove-accents'); |
6 | const uniqid = require('uniqid'); | 6 | const uniqid = require('uniqid'); |
7 | 7 | ||
8 | const config = require('komodo-sdk/config'); | 8 | const config = require('komodo-sdk/config'); |
9 | const logger = require('tektrans-logger'); | 9 | const logger = require('tektrans-logger'); |
10 | 10 | ||
11 | const commandHandler = require('./command-handler'); | 11 | const commandHandler = require('./command-handler'); |
12 | const messagesArchive = require('./messages-archive'); | 12 | const messagesArchive = require('./messages-archive'); |
13 | 13 | ||
14 | const app = express(); | 14 | const app = express(); |
15 | const port = (config && config.listener && config.listener.http | 15 | const port = (config && config.listener && config.listener.http |
16 | && config.listener.http.port && config.listener.http.port | 16 | && config.listener.http.port && config.listener.http.port |
17 | ) || 32979; | 17 | ) || 32979; |
18 | 18 | ||
19 | /** | 19 | /** |
20 | * Handler utama. | 20 | * Handler utama. |
21 | * | 21 | * |
22 | * @param {object} req - Express request object | 22 | * @param {object} req - Express request object |
23 | * @param {object} req.query - Express query string object | 23 | * @param {object} req.query - Express query string object |
24 | * @param {string} [req.query.partner] - Partner (pengirim atau penerima) | 24 | * @param {string} [req.query.partner] - Partner (pengirim atau penerima) |
25 | * @param {string} [req.query.from] - Pengirim, OBSOLETED: gunakan parameter partner | 25 | * @param {string} [req.query.from] - Pengirim, OBSOLETED: gunakan parameter partner |
26 | * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED by partner_raw | 26 | * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED by partner_raw |
27 | * @param {string} [req.query.to] - Tujuan, jika is_outgoing, OBSOLETED: gunakan parameter partner | 27 | * @param {string} [req.query.to] - Tujuan, jika is_outgoing, OBSOLETED: gunakan parameter partner |
28 | * @param {string} req.query.msg - Isi pesan | 28 | * @param {string} req.query.msg - Isi pesan |
29 | * @param {string} req.query.origin - Nama origin | 29 | * @param {string} req.query.origin - Nama origin |
30 | * @param {string} [req.query.origin_label] - Nama origin untuk ditulis di histori pesan | 30 | * @param {string} [req.query.origin_label] - Nama origin untuk ditulis di histori pesan |
31 | * @param {string} [req.query.do_not_forward_to_core] - Apakah teruskan pesan ke CORE | 31 | * @param {string} [req.query.do_not_forward_to_core] - Apakah teruskan pesan ke CORE |
32 | * @param {string} [req.query.is_outgoing] - Apakah pesan keluar | 32 | * @param {string} [req.query.is_outgoing] - Apakah pesan keluar |
33 | * @param {object} res - Express response object | 33 | * @param {object} res - Express response object |
34 | */ | 34 | */ |
35 | function mainHandler(req, res) { | 35 | function mainHandler(req, res) { |
36 | if (!req.body) req.body = {}; | 36 | if (!req.body) req.body = {}; |
37 | 37 | ||
38 | const xid = uniqid(); | 38 | const xid = uniqid(); |
39 | 39 | ||
40 | logger.verbose(`${MODULE_NAME} 72AFD326: Got a request`, { | 40 | logger.verbose(`${MODULE_NAME} 72AFD326: Got a request`, { |
41 | xid, | 41 | xid, |
42 | query: req.query, | 42 | query: req.query, |
43 | body: req.body, | 43 | body: req.body, |
44 | }); | 44 | }); |
45 | 45 | ||
46 | if ( | 46 | if ( |
47 | (!req.body.partner && !req.query.partner) | 47 | (!req.body.partner && !req.query.partner) |
48 | && ( | 48 | && ( |
49 | (!req.query.is_outgoing && !req.body.is_outgoing && !req.query.from && !req.body.from) | 49 | (!req.query.is_outgoing && !req.body.is_outgoing && !req.query.from && !req.body.from) |
50 | || ((req.query.is_outgoing || req.body.is_outgoing) && !req.query.to && !req.body.to) | 50 | || ((req.query.is_outgoing || req.body.is_outgoing) && !req.query.to && !req.body.to) |
51 | ) | 51 | ) |
52 | ) { | 52 | ) { |
53 | logger.warn(`${MODULE_NAME} D254B7B454DB: Undefined parameter partner or from or to`, { | 53 | logger.warn(`${MODULE_NAME} D254B7B454DB: Undefined parameter partner or from or to`, { |
54 | xid, | 54 | xid, |
55 | partner: req.body.partner || req.query.partner, | 55 | partner: req.body.partner || req.query.partner, |
56 | is_outgoing: req.body.is_outgoing || req.query.is_outgoing, | 56 | is_outgoing: req.body.is_outgoing || req.query.is_outgoing, |
57 | from: req.body.from || req.query.from, | 57 | from: req.body.from || req.query.from, |
58 | to: req.body.to || req.query.to, | 58 | to: req.body.to || req.query.to, |
59 | }); | 59 | }); |
60 | res.end('ERROR. Undefined parameter: partner or from or to'); | 60 | res.end('ERROR. Undefined parameter: partner or from or to'); |
61 | return; | 61 | return; |
62 | } | 62 | } |
63 | 63 | ||
64 | // message cleansing | 64 | // message cleansing |
65 | const msg = removeAccents(req.query.msg || req.body.msg || '') | 65 | const msg = removeAccents(req.query.msg || req.body.msg || '') |
66 | .replace(/[\u{0080}-\u{FFFF}]/gu, '') | 66 | .replace(/[\u{0080}-\u{FFFF}]/gu, '') |
67 | .trim(); | 67 | .trim(); |
68 | 68 | ||
69 | if (!msg) { | 69 | if (!msg) { |
70 | logger.warn(`${MODULE_NAME} #92996A497D12: Undefined parameter msg`, { | 70 | logger.warn(`${MODULE_NAME} #92996A497D12: Undefined parameter msg`, { |
71 | xid, | 71 | xid, |
72 | }); | 72 | }); |
73 | res.end(`ERROR. Undefined parameter: msg. XID: ${xid}`); | 73 | res.end(`ERROR. Undefined parameter: msg. XID: ${xid}`); |
74 | return; | 74 | return; |
75 | } | 75 | } |
76 | 76 | ||
77 | logger.verbose( | 77 | logger.verbose( |
78 | `Saving ${req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming'} message history`, | 78 | `${MODULE_NAME} 1E9D2388: Saving message history`, |
79 | { | 79 | { |
80 | xid, | 80 | xid, |
81 | direction: req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming', | ||
81 | transport: req.body.origin_transport || req.query.origin_transport, | 82 | transport: req.body.origin_transport || req.query.origin_transport, |
82 | partner: req.body.partner || req.query.partner || req.body.from || req.query.from | 83 | partner: req.body.partner || req.query.partner || req.body.from || req.query.from |
83 | || req.body.to || req.query.to, | 84 | || req.body.to || req.query.to, |
84 | msg, | 85 | msg, |
85 | }, | 86 | }, |
86 | ); | 87 | ); |
87 | 88 | ||
88 | messagesArchive.insert( | 89 | messagesArchive.insert( |
89 | { | 90 | { |
90 | origin_label: req.body.origin_label || req.query.origin_label | 91 | origin_label: req.body.origin_label || req.query.origin_label |
91 | || req.body.origin || req.query.origin, | 92 | || req.body.origin || req.query.origin, |
92 | origin_transport: req.body.origin_transport || req.query.origin_transport, | 93 | origin_transport: req.body.origin_transport || req.query.origin_transport, |
93 | partner: req.body.partner_raw || req.query.partner_raw | 94 | partner: req.body.partner_raw || req.query.partner_raw |
94 | || req.body.from_raw || req.query.from_raw | 95 | || req.body.from_raw || req.query.from_raw |
95 | || req.body.from || req.query.from | 96 | || req.body.from || req.query.from |
96 | || req.body.to || req.query.to || req.body.partner || req.query.partner, | 97 | || req.body.to || req.query.to || req.body.partner || req.query.partner, |
97 | msg, | 98 | msg, |
98 | }, | 99 | }, |
99 | (req.body.is_outgoing || req.query.is_outgoing) ? messagesArchive.DIRECTION_OUTGOING | 100 | (req.body.is_outgoing || req.query.is_outgoing) ? messagesArchive.DIRECTION_OUTGOING |
100 | : messagesArchive.DIRECTION_INCOMING, | 101 | : messagesArchive.DIRECTION_INCOMING, |
101 | ); | 102 | ); |
102 | 103 | ||
103 | if ( | 104 | if ( |
104 | req.body.do_not_forward_to_core || req.query.do_not_forward_to_core | 105 | req.body.do_not_forward_to_core || req.query.do_not_forward_to_core |
105 | || req.body.is_outgoing || req.query.is_outgoing | 106 | || req.body.is_outgoing || req.query.is_outgoing |
106 | ) { | 107 | ) { |
107 | logger.verbose('Ignoring message', { | 108 | logger.verbose('Ignoring message', { |
108 | xid, | 109 | xid, |
109 | from: req.body.from || req.query.from, | 110 | from: req.body.from || req.query.from, |
110 | msg, | 111 | msg, |
111 | do_not_forward_to_core: req.body.do_not_forward_to_core | 112 | do_not_forward_to_core: req.body.do_not_forward_to_core |
112 | || req.query.do_not_forward_to_core, | 113 | || req.query.do_not_forward_to_core, |
113 | }); | 114 | }); |
114 | 115 | ||
115 | res.end('OK'); | 116 | res.end('OK'); |
116 | return; | 117 | return; |
117 | } | 118 | } |
118 | 119 | ||
119 | if (!req.query.report_port && !req.body.report_port) { | 120 | if (!req.query.report_port && !req.body.report_port) { |
120 | res.end('ERROR. Undefined parameter: report_port'); | 121 | res.end('ERROR. Undefined parameter: report_port'); |
121 | return; | 122 | return; |
122 | } | 123 | } |
123 | 124 | ||
124 | const params = { | 125 | const params = { |
125 | origin: req.body.origin || req.query.origin || 'MESSAGING', | 126 | origin: req.body.origin || req.query.origin || 'MESSAGING', |
126 | origin_transport: req.body.origin_transport || req.query.origin_transport, | 127 | origin_transport: req.body.origin_transport || req.query.origin_transport, |
127 | report_ip: req.body.report_ip || req.query.report_ip || req.ip, | 128 | report_ip: req.body.report_ip || req.query.report_ip || req.ip, |
128 | report_port: req.body.report_port || req.query.report_port, | 129 | report_port: req.body.report_port || req.query.report_port, |
129 | from: req.body.partner || req.query.partner || req.body.from || req.query.from, | 130 | from: req.body.partner || req.query.partner || req.body.from || req.query.from, |
130 | msg, | 131 | msg, |
131 | }; | 132 | }; |
132 | 133 | ||
133 | commandHandler(msg, params, (err, coreResponseObject, responseParams) => { | 134 | commandHandler(msg, params, (err, coreResponseObject, responseParams) => { |
134 | if (err) { | 135 | if (err) { |
135 | res.end(`ERROR. ${err.message || err}`); | 136 | res.end(`ERROR. ${err.message || err}`); |
136 | } else if (coreResponseObject) { | 137 | } else if (coreResponseObject) { |
137 | res.json(coreResponseObject); | 138 | res.json(coreResponseObject); |
138 | } else { | 139 | } else { |
139 | res.end(responseParams.body); | 140 | res.end(responseParams.body); |
140 | } | 141 | } |
141 | }); | 142 | }); |
142 | } | 143 | } |
143 | 144 | ||
144 | if (config.ip_whitelist && config.ip_whitelist.length) { | 145 | if (config.ip_whitelist && config.ip_whitelist.length) { |
145 | app.use(ipfilter(config.ip_whitelist, { mode: 'allow', log: false })); | 146 | app.use(ipfilter(config.ip_whitelist, { mode: 'allow', log: false })); |
146 | } | 147 | } |
147 | 148 | ||
148 | app.get('/', mainHandler); | 149 | app.get('/', mainHandler); |
149 | app.post('/', express.urlencoded({ extended: true }), mainHandler); | 150 | app.post('/', express.urlencoded({ extended: true }), mainHandler); |
150 | 151 | ||
151 | app.listen(port, () => { | 152 | app.listen(port, () => { |
152 | logger.info('HTTP-LISTENER: started', { | 153 | logger.info('HTTP-LISTENER: started', { |
153 | port, app_env: app.get('env'), | 154 | port, app_env: app.get('env'), |
154 | }); | 155 | }); |
155 | }); | 156 | }); |
156 | 157 |