-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
79 lines (72 loc) · 2.62 KB
/
app.js
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
const assert = require('assert');
if (process.env.JAMBONES_REDIS_SENTINELS) {
assert.ok(process.env.JAMBONES_REDIS_SENTINEL_MASTER_NAME,
'missing JAMBONES_REDIS_SENTINEL_MASTER_NAME env var, JAMBONES_REDIS_SENTINEL_PASSWORD env var is optional');
} else {
assert.ok(process.env.JAMBONES_REDIS_HOST, 'missing JAMBONES_REDIS_HOST env var');
}
const smpp = require('@jambonz/node-smpp');
const express = require('express');
const app = express();
const opts = Object.assign({
timestamp: () => `, "time": "${new Date().toISOString()}"`,
level: process.env.JAMBONES_LOGLEVEL || 'info'
});
const logger = require('pino')(opts);
const port = process.env.HTTP_PORT || 3020;
const smppPort = process.env.SMPP_PORT || 2775;
const {retrieveSet} = require('@jambonz/realtimedb-helpers')({}, logger);
const {
lookupSmppGatewaysByBindCredentials,
lookupAppByPhoneNumber
} = require('@jambonz/db-helpers')({
host: process.env.JAMBONES_MYSQL_HOST,
user: process.env.JAMBONES_MYSQL_USER,
port: process.env.JAMBONES_MYSQL_PORT || 3306,
password: process.env.JAMBONES_MYSQL_PASSWORD,
database: process.env.JAMBONES_MYSQL_DATABASE,
connectionLimit: process.env.JAMBONES_MYSQL_CONNECTION_LIMIT || 10
}, logger);
const sessionHandler = require('./lib/session-handler');
app.locals = {
...app.locals,
dbHelpers: {
lookupSmppGatewaysByBindCredentials,
lookupAppByPhoneNumber,
retrieveSet
},
logger,
};
const routes = require('./lib/routes');
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use('/', routes);
app.use((err, req, res, next) => {
logger.error(err, 'burped error');
res.status(err.status || 500).json({msg: err.message});
});
app.listen(port, () => {
logger.info(`jambonz-smpp-esme listening for api requests at http://localhost:${port}`);
});
/* create smpp server */
const server = smpp.createServer((session) => {
session.on('bind_transceiver', async(pdu) => {
session.pause();
try {
const gw = await lookupSmppGatewaysByBindCredentials(pdu.system_id, pdu.password);
if (0 === gw.length) throw new Error('credentials not found');
session.send(pdu.response());
session.resume();
sessionHandler(logger, session, gw);
} catch (err) {
logger.info({err}, `Incoming connection rejected for system_id ${pdu.system_id}`);
await session.send(pdu.response({command_status: smpp.ESME_RBINDFAIL}))
.catch((err) => logger.error({err}, 'Error sending ESME_RBINDFAIL'));
session.close();
}
});
});
server.listen(smppPort, () => {
logger.info(`jambonz-smpp-esme listening for smpp at http://localhost:${smppPort}`);
});
module.exports = app;