Commit dd3e93681557ed8a8fb4f1bfcc043682144a2987

Authored by Adhidarma Hadiwinoto
1 parent ebb5b7873c
Exists in master

Add config.do_not_reply_warning_on_empty_message

Showing 2 changed files with 7 additions and 3 deletions Inline Diff

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 "disable_claim_bonus": false 16 "disable_claim_bonus": false,
17 "do_not_reply_warning_on_empty_message": false
17 } 18 }
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
74 return; 74 if (!config.do_not_reply_warning_on_empty_message) {
75 res.end(`ERROR. Undefined parameter: msg. XID: ${xid}`);
76 return;
77 }
75 } 78 }
76 79
77 logger.verbose( 80 logger.verbose(
78 `${MODULE_NAME} 1E9D2388: Saving message history`, 81 `${MODULE_NAME} 1E9D2388: Saving message history`,
79 { 82 {
80 xid, 83 xid,
81 direction: req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming', 84 direction: req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming',
82 transport: req.body.origin_transport || req.query.origin_transport, 85 transport: req.body.origin_transport || req.query.origin_transport,
83 partner: req.body.partner || req.query.partner || req.body.from || req.query.from 86 partner: req.body.partner || req.query.partner || req.body.from || req.query.from
84 || req.body.to || req.query.to, 87 || req.body.to || req.query.to,
85 msg, 88 msg,
86 }, 89 },
87 ); 90 );
88 91
89 messagesArchive.insert( 92 messagesArchive.insert(
90 { 93 {
91 origin_label: req.body.origin_label || req.query.origin_label 94 origin_label: req.body.origin_label || req.query.origin_label
92 || req.body.origin || req.query.origin, 95 || req.body.origin || req.query.origin,
93 origin_transport: req.body.origin_transport || req.query.origin_transport, 96 origin_transport: req.body.origin_transport || req.query.origin_transport,
94 partner: req.body.partner_raw || req.query.partner_raw 97 partner: req.body.partner_raw || req.query.partner_raw
95 || req.body.from_raw || req.query.from_raw 98 || req.body.from_raw || req.query.from_raw
96 || req.body.from || req.query.from 99 || req.body.from || req.query.from
97 || req.body.to || req.query.to || req.body.partner || req.query.partner, 100 || req.body.to || req.query.to || req.body.partner || req.query.partner,
98 msg, 101 msg,
99 }, 102 },
100 (req.body.is_outgoing || req.query.is_outgoing) ? messagesArchive.DIRECTION_OUTGOING 103 (req.body.is_outgoing || req.query.is_outgoing) ? messagesArchive.DIRECTION_OUTGOING
101 : messagesArchive.DIRECTION_INCOMING, 104 : messagesArchive.DIRECTION_INCOMING,
102 ); 105 );
103 106
104 if ( 107 if (
105 req.body.do_not_forward_to_core || req.query.do_not_forward_to_core 108 req.body.do_not_forward_to_core || req.query.do_not_forward_to_core
106 || req.body.is_outgoing || req.query.is_outgoing 109 || req.body.is_outgoing || req.query.is_outgoing
107 ) { 110 ) {
108 logger.verbose('Ignoring message', { 111 logger.verbose('Ignoring message', {
109 xid, 112 xid,
110 from: req.body.from || req.query.from, 113 from: req.body.from || req.query.from,
111 msg, 114 msg,
112 do_not_forward_to_core: req.body.do_not_forward_to_core 115 do_not_forward_to_core: req.body.do_not_forward_to_core
113 || req.query.do_not_forward_to_core, 116 || req.query.do_not_forward_to_core,
114 }); 117 });
115 118
116 res.end('OK'); 119 res.end('OK');
117 return; 120 return;
118 } 121 }
119 122
120 if (!req.query.report_port && !req.body.report_port) { 123 if (!req.query.report_port && !req.body.report_port) {
121 res.end('ERROR. Undefined parameter: report_port'); 124 res.end('ERROR. Undefined parameter: report_port');
122 return; 125 return;
123 } 126 }
124 127
125 const params = { 128 const params = {
126 origin: req.body.origin || req.query.origin || 'MESSAGING', 129 origin: req.body.origin || req.query.origin || 'MESSAGING',
127 origin_transport: req.body.origin_transport || req.query.origin_transport, 130 origin_transport: req.body.origin_transport || req.query.origin_transport,
128 report_ip: req.body.report_ip || req.query.report_ip || req.ip, 131 report_ip: req.body.report_ip || req.query.report_ip || req.ip,
129 report_port: req.body.report_port || req.query.report_port, 132 report_port: req.body.report_port || req.query.report_port,
130 from: req.body.partner || req.query.partner || req.body.from || req.query.from, 133 from: req.body.partner || req.query.partner || req.body.from || req.query.from,
131 msg, 134 msg,
132 }; 135 };
133 136
134 commandHandler(msg, params, (err, coreResponseObject, responseParams) => { 137 commandHandler(msg, params, (err, coreResponseObject, responseParams) => {
135 if (err) { 138 if (err) {
136 res.end(`ERROR. ${err.message || err}`); 139 res.end(`ERROR. ${err.message || err}`);
137 } else if (coreResponseObject) { 140 } else if (coreResponseObject) {
138 res.json(coreResponseObject); 141 res.json(coreResponseObject);
139 } else { 142 } else {
140 res.end(responseParams.body); 143 res.end(responseParams.body);
141 } 144 }
142 }); 145 });
143 } 146 }
144 147
145 if (config.ip_whitelist && config.ip_whitelist.length) { 148 if (config.ip_whitelist && config.ip_whitelist.length) {
146 app.use(ipfilter(config.ip_whitelist, { mode: 'allow', log: false })); 149 app.use(ipfilter(config.ip_whitelist, { mode: 'allow', log: false }));
147 } 150 }
148 151
149 app.get('/', mainHandler); 152 app.get('/', mainHandler);
150 app.post('/', express.urlencoded({ extended: true }), mainHandler); 153 app.post('/', express.urlencoded({ extended: true }), mainHandler);
151 154
152 app.listen(port, () => { 155 app.listen(port, () => {
153 logger.info('HTTP-LISTENER: started', { 156 logger.info('HTTP-LISTENER: started', {
154 port, app_env: app.get('env'), 157 port, app_env: app.get('env'),
155 }); 158 });
156 }); 159 });
157 160