index.js
4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use strict";
/**
* Modul untuk menerima callback dari modem handler jika ada SMS masuk.
*/
const express = require('express');
const moment = require('moment');
const messagingService = require('komodo-center-messaging-client-lib');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const transport = require('../transport');
const partnerLastSeen = require('../partner-last-seen');
const history = require('../history');
// const modems = require('../modems2');
const routerConfigSenders = require('./router-config-senders');
const routerModems = require('./router-modems');
if (config.handler_callback_server) {
logger.warn('Deprecated config.handler_callback_server. Please migrate it to config.apiserver!');
}
const app = express();
messagingService.setTransport(transport);
function apikeyChecker(req, res, next) {
res.locals.has_valid_apikey = req.params.apikey === ((config.apiserver && config.apiserver.apikey ? config.apiserver.apikey : null) || config.handler_callback_server.apikey);
if (res.locals.has_valid_apikey) {
next();
}
else {
logger.warn('Invalid apikey', { ip: req.ip });
res.end('APISERVER: Invalid apikey');
}
}
function onIncomingSms(req, res) {
res.end('OK');
if (!req.query.number) return;
const numberWithSuffix = req.query.number.replace(/^\+/, '') + (config.number_suffix || '');
partnerLastSeen.set(req.query.number, req.query.modem_imsi);
history.push({
ts: req.query.ts || moment().format('YYYY-MM-DD HH:mm:ss'),
modem: {
name: req.query.modem,
imsi: req.query.modem_imsi,
msisdn: req.query.modem_msisdn,
},
direction: 'INCOMING',
partner: req.query.number,
message: req.query.msg,
});
/*
modems.set({
name: req.query.modem,
device: req.query.modem_device,
imsi: req.query.modem_imsi,
msisdn: req.query.modem_msisdn,
networkId: req.query.modem.network_id,
networkName: req.query.modem_network_name,
signalStrength: req.query.modem_signal_strength,
uptime: req.query.uptime,
reportIp: req.query.report_ip || req.ip,
reportPort: req.query.report_port,
reportApikey: req.query.report_apikey,
reportPathSms: req.query.report_path_sms || '/sms',
});
*/
const doNotForwardToCore = (req.query.number.search(/(\+)*62/) !== 0) || (req.query.number.length <= 8);
logger.info('APISERVER: Incoming SMS', { modem: req.query.modem, from: req.query.number, from_with_suffix: numberWithSuffix, msg: req.query.msg, doNotForwardToCore });
messagingService.onIncomingMessage({
me: req.query.modem,
partner: numberWithSuffix,
partner_raw: req.query.number,
msg: req.query.msg,
origin_label: req.query.modem_imsi || 'UNKNOWN',
origin_transport: 'SMS',
origin_partner: req.query.number,
do_not_forward_to_core: doNotForwardToCore,
});
}
async function pageHistory(req, res) {
res.json(await history.dump());
}
app.use(function(req, res, next) {
if (
req && req.path && typeof req.path === 'string'
&& (
req.path.search(/\/modems$/) >= 0
|| req.path.search(/\/modems\/set$/) >= 0
)
) {
next();
return;
}
logger.verbose('APISERVER: Incoming http request', { ip: req.ip, path: req.path, url: req.url });
next();
})
app.use('/apikey/:apikey', apikeyChecker);
app.get('/apikey/:apikey/on-sms', onIncomingSms);
app.get('/apikey/:apikey/inbox', onIncomingSms);
app.get('/apikey/:apikey/on-sms/inbox', onIncomingSms);
app.get('/apikey/:apikey/history', pageHistory);
app.use('/apikey/:apikey/modems', routerModems);
app.use('/apikey/:apikey/config/senders', routerConfigSenders);
const listenPort = (config && config.apiserver && config.apiserver.listen_port ? config.apiserver.listen_port : null)
|| (config && config.handler_callback_server ? config.handler_callback_server.listen_port : null);
if (listenPort) {
app.listen(listenPort, () => {
logger.info('HTTP Handler Callback server listening on port ' + listenPort);
});
} else {
logger.warn('Undefined config.apiserver.listen_port for APISERVER. Not listening for command.');
}