forked from suderman/smsglue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
198 lines (148 loc) · 5.68 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require('fs');
const path = require('path');
const log = require('npmlog');
var SMSglue = require('./smsglue');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var server = require('http').createServer(app);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var TIMER = {};
app.post('/enable', (req, res) => {
log.info('Action', 'enable');
let token = req.body.did.substring(6) + '-' +
SMSglue.encrypt({
user: req.body.user || '',
pass: req.body.pass || '',
did: req.body.did || ''
});
let glue = new SMSglue(token, req.body.origin || '');
glue.enable( (err, r, body) => {
if (body = SMSglue.parseBody(body)) {
// log.info('Action', body);
SMSglue.save('provisions', glue.id, SMSglue.encrypt(glue.accountXML()), () => {
// Auto-empty this xml file (only "<account></account>") after 10 minutes of waiting...
if (TIMER[glue.id]) clearTimeout(TIMER[glue.id]);
TIMER[glue.id] = setTimeout(() => {
SMSglue.save('provisions', glue.id, SMSglue.encrypt('<account></account>'));
log.info('Provision', 'Cleared after 10 minute timeout');
}, 600000)
res.setHeader('Content-Type', 'application/json');
res.send({ response: { error: 0, description: 'Success', hooks: glue.hooks }});
});
} else {
res.setHeader('Content-Type', 'application/json');
res.send({ response: { error: 400, description: 'Invalid parameters' }});
}
});
});
app.get('/provision/:id', (req, res) => {
log.info('Action', 'provision');
SMSglue.load('provisions', req.params.id, (err, encrypted) => {
var xml = SMSglue.decrypt(encrypted) || '<account></account>';
// If the file exists, empty this xml file (only "<account></account>")
if (!err) {
if (TIMER[req.params.id]) clearTimeout(TIMER[req.params.id]);
SMSglue.save('provisions', req.params.id, SMSglue.encrypt('<account></account>'));
log.info('Provision', 'Cleared after request');
}
res.setHeader('Content-Type', 'text/xml');
res.send(xml);
});
});
app.get('/notify/:id', (req, res) => {
log.info('Action', 'notify');
// Deleted the cached history
SMSglue.clear('messages', req.params.id, (err) => {
// Send push notification to device(s)
SMSglue.notify(req.params.id, () => {
// voip.ms expects this reply, otherwise it'll retry every 30 minutes
res.setHeader('Content-Type', 'text/plain');
res.send('ok');
});
});
});
app.get('/report/:id/:device/:app', (req, res) => {
log.info('Action', 'report');
// Read existing devices file
SMSglue.load('devices', req.params.id, (err, encrypted) => {
var devices = SMSglue.decrypt(encrypted) || [];
// Add this push token & app id to the array
if ((req.params.device) && (req.params.app)) {
devices.push({
DeviceToken: req.params.device,
AppId: req.params.app
});
}
// Remove any duplicates
devices = devices.filter((device, index, self) => self.findIndex((d) => {return d.DeviceToken === device.DeviceToken }) === index)
// Save changes to disk
SMSglue.save('devices', req.params.id, SMSglue.encrypt(devices), (err) => {
res.setHeader('Content-Type', 'application/json');
res.send({ response: { error: 0, description: 'Success' }});
});
});
});
// Fetch cached SMS messages, filtered by last SMS ID
app.get(['/fetch/:token/:last_sms','/fetch/:token'], (req, res) => {
log.info('Action', 'fetch');
var glue = new SMSglue(req.params.token);
var last_sms = Number(req.params.last_sms) || 0;
// log.info('last_sms', last_sms);
// Fetch filtered SMS messages back as JSON
var fetchFilteredSMS = function(smss) {
res.setHeader('Content-Type', 'application/json');
res.send({
date: SMSglue.date(),
unread_smss: smss.filter((sms) => (Number(sms.sms_id) > last_sms))
});
}
// First try to read the cached messages
SMSglue.load('messages', glue.id, (err, data) => {
// Decrypt the messages and send them back
var smss = SMSglue.decrypt(data, glue.pass) || [];
if (smss.length) {
// log.info(glue.did, 'Found SMS cache')
fetchFilteredSMS(smss);
// If the array is empty, update the cache from voip.ms and try again
} else {
// log.info(glue.did, 'DID NOT find SMS cache')
glue.get((error) => {
// Read the cached messages one more time
SMSglue.load('messages', glue.id, (err, data) => {
// Decrypt the messages and send them back (last chance)
smss = SMSglue.decrypt(data, glue.pass) || [];
fetchFilteredSMS(smss);
});
});
}
});
});
app.get('/send/:token/:dst/:msg', (req, res) => {
log.info('Action', 'send');
let glue = new SMSglue(req.params.token);
glue.send(req.params.dst, req.params.msg, (err, r, body) => {
body = SMSglue.parseBody(body);
if ((body) && (!err)) {
res.setHeader('Content-Type', 'application/json');
res.send({ response: { error: 0, description: 'Success' }});
} else {
res.setHeader('Content-Type', 'application/json');
res.send({ response: { error: 400, description: 'Invalid parameters' }});
}
});
});
app.get('/', (req, res) => {
log.info('Action', 'index');
fs.readFile(path.resolve(__dirname, 'index.html'), 'utf8', (err, data) => {
data = (process.env.BEFORE_CLOSING_BODY_TAG) ? data.replace("</body>", `${process.env.BEFORE_CLOSING_BODY_TAG}\n</body>`) : data;
res.setHeader('Content-Type', 'text/html');
res.send(data);
});
});
app.get('*', (req, res) => {
log.info('Action', 'redirect');
res.redirect('/');
});
module.exports = app;