Commit 178d09609414889ba37b21555f3c690f4b803b05
1 parent
85a4549acb
Exists in
master
Sanitize destination
Showing 1 changed file with 17 additions and 2 deletions Inline Diff
lib/transport.js
1 | const MODULE_NAME = 'TRANSPORT'; | 1 | const MODULE_NAME = 'TRANSPORT'; |
2 | 2 | ||
3 | const axios = require('axios').default; | 3 | const axios = require('axios').default; |
4 | const uniqid = require('uniqid'); | 4 | const uniqid = require('uniqid'); |
5 | const urljoin = require('url-join'); | 5 | const urljoin = require('url-join'); |
6 | const config = require('komodo-sdk/config'); | 6 | const config = require('komodo-sdk/config'); |
7 | const logger = require('tektrans-logger'); | 7 | const logger = require('tektrans-logger'); |
8 | 8 | ||
9 | const axiosConfig = { | 9 | const axiosConfig = { |
10 | headers: { | 10 | headers: { |
11 | 'content-type': 'application/json', | 11 | 'content-type': 'application/json', |
12 | }, | 12 | }, |
13 | timeout: 60 * 1000, | 13 | timeout: 60 * 1000, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | const send = async (partner, msg, callerXid) => { | 16 | const send = async (partner, msg, callerXid) => { |
17 | const xid = callerXid || uniqid(); | 17 | const xid = callerXid || uniqid(); |
18 | const modemApiServer = config.modem; | 18 | const modemApiServer = config.modem; |
19 | if (!modemApiServer || !modemApiServer.url) { | 19 | if (!modemApiServer || !modemApiServer.url) { |
20 | logger.warn(`${MODULE_NAME} 4111A9BA: Missing modem`, { xid, partner, msg }); | 20 | logger.warn(`${MODULE_NAME} 4111A9BA: Missing modem`, { xid, partner, msg }); |
21 | return; | 21 | return; |
22 | } | 22 | } |
23 | 23 | ||
24 | const endpointUrl = urljoin( | 24 | const endpointUrl = urljoin( |
25 | modemApiServer.url, | 25 | modemApiServer.url, |
26 | '/sms/send', | 26 | '/sms/send', |
27 | ); | 27 | ); |
28 | 28 | ||
29 | if (!partner || typeof partner !== 'string' || !partner.trim()) { | ||
30 | logger.verbose(`${MODULE_NAME} E5308BDD: Missing destination`); | ||
31 | return; | ||
32 | } | ||
33 | |||
34 | if (!msg || typeof msg !== 'string' || !msg.trim()) { | ||
35 | logger.verbose(`${MODULE_NAME} EC8FF515: Missing msg`); | ||
36 | return; | ||
37 | } | ||
38 | |||
39 | const destination = (partner || '') | ||
40 | .trim() | ||
41 | .replace(/^0/, '+62') | ||
42 | .replace(/^62/, '+62'); | ||
43 | |||
29 | const payload = { | 44 | const payload = { |
30 | destination: partner, | 45 | destination, |
31 | message: msg, | 46 | message: (msg || '').trim(), |
32 | }; | 47 | }; |
33 | 48 | ||
34 | logger.verbose(`${MODULE_NAME} 8D4F8A3A: Sending message to modem apiserver`, { | 49 | logger.verbose(`${MODULE_NAME} 8D4F8A3A: Sending message to modem apiserver`, { |
35 | xid, | 50 | xid, |
36 | endpointUrl, | 51 | endpointUrl, |
37 | payload, | 52 | payload, |
38 | }); | 53 | }); |
39 | 54 | ||
40 | try { | 55 | try { |
41 | const response = await axios.post(endpointUrl, payload, axiosConfig); | 56 | const response = await axios.post(endpointUrl, payload, axiosConfig); |
42 | 57 | ||
43 | logger.verbose(`${MODULE_NAME} 6D71CC75: Message sent to modem apiserver`, { | 58 | logger.verbose(`${MODULE_NAME} 6D71CC75: Message sent to modem apiserver`, { |
44 | xid, | 59 | xid, |
45 | partner, | 60 | partner, |
46 | msg, | 61 | msg, |
47 | endpointUrl, | 62 | endpointUrl, |
48 | httpStatus: response.status, | 63 | httpStatus: response.status, |
49 | responseBody: response.data, | 64 | responseBody: response.data, |
50 | }); | 65 | }); |
51 | } catch (e) { | 66 | } catch (e) { |
52 | logger.warn(`${MODULE_NAME} C0124404: Exception on send`, { | 67 | logger.warn(`${MODULE_NAME} C0124404: Exception on send`, { |
53 | xid, | 68 | xid, |
54 | eCode: e.code, | 69 | eCode: e.code, |
55 | eMessage: e.message, | 70 | eMessage: e.message, |
56 | httpStatus: e.response && e.response.status, | 71 | httpStatus: e.response && e.response.status, |
57 | responseBody: e.response && e.response.data, | 72 | responseBody: e.response && e.response.data, |
58 | }); | 73 | }); |
59 | } | 74 | } |
60 | }; | 75 | }; |
61 | exports.send = send; | 76 | exports.send = send; |
62 | 77 |