-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
298 lines (242 loc) · 8.02 KB
/
index.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
var net = require('net'),
express = require('express'),
app = express(),
path = require('path'),
fs = require('fs'),
server = require('http').Server(app),
//tls = require('https').Server({ key: fs.readFileSync('public/certs/client-key.pem'), cert: fs.readFileSync('public/certs/client-cert.pem') }, app),
crypto = require('crypto'),
aesjs = require('aes-js'),
io = require('socket.io')(server),//tls),
axios = require('axios'),
querystring = require('querystring');
bodyParser =require('body-parser');
qr = require('qrcode');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/login', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/web', function(req, res) {
res.sendFile(path.join(__dirname, 'messenger.html'));
});
app.get('/setup', function(req, res) {
res.sendFile(path.join(__dirname, 'setup.html'));
});
app.get('/privacy', function(req, res) {
res.sendFile(path.join(__dirname, 'privacy.html'));
});
app.get('/qr', function(req, res) { // TODO Add branding
var q = req.query;
if(q['k'] && q['a'] && q['p']) {
qr.toDataURL('{"a":"' + q['a'] + '","k":"' + q['k'] + '","p":"' + q['p'] + '"}', {width:112, margin:0, color: { light: '#00000000'} }, function(err, url) {
var img = Buffer.from(url.replace(/^data:image\/png;base64,/, ''), 'base64');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
res.end(img);
});
} else {
res.status(400).send('Bad Request');
}
});
server.listen(8737, function() {
console.log('Listening on *:8737');
});
/*tls.listen(443, function() {
console.log('Using SSL');
});*/
app.use('/public', express.static(path.join(__dirname, 'public')));
io.on('connection', function(socket) {
var uuid = crypto.randomBytes(8).toString('hex');
var key = crypto.randomBytes(16).toString('hex');
socket.emit('setup', uuid, key);
socket.on('stored', function(config) {
socket.emit('start');
setupServer(socket, JSON.parse(config));
});
socket.on('ready', function(addr) {
var client = new net.Socket();
var to = addr.replace('http://', '').replace('https://', '');
if(to.endsWith('/')) {
to = to.substring(0, to.length - 1);
}
client.connect(8736, to);
client.on('data', function(data) {
var message = data.toString().replace("\n", "").replace("\r", "");
switch(message) {
case "ACK":
socket.emit('valid');
client.write('jMessage Web');
break;
case "DAT":
client.write(uuid);
break;
default:
var decryptedText = decrypt(message, key, uuid);
var config = JSON.parse(decryptedText);
socket.emit('start', config);
setupServer(socket, config);
break;
}
});
socket.on('disconnect', function() {
client.end();
client.destroy();
});
client.on('error', function(err) {
socket.emit('err', err);
});
});
});
////
function setupServer(client, json) {
if(json === null) {
return;
}
var server = new net.Socket();
var addr = json['a'];
var pass = json['p'];
var key = json['k'];
addr = addr.replace('http://', '').replace('https://', '');
if(addr.endsWith('/')) {
addr = addr.substring(0, to.length - 1);
}
server.connect(8736, addr);
var response = '';
client.on('disconnecting', function() {
server.destroy();
client.emit('logout');
});
server.on('data', function(data) {
response = handleIncoming(data.toString(), server, client, addr, pass, key, response);
});
server.on('close', function() {
client.emit('logout');
});
client.on('getContacts', function() {
getContacts(client, addr, pass, key);
});
client.on('sendMessage', function(display, id, partners, custom, guid, msg) {
sendMessage(client, addr, pass, key, display, id, partners, custom, guid, msg);
});
client.on('getMessages', function(id, offset) {
loadMessages(client, addr, pass, key, id, offset);
});
client.on('getAttachment', function(id, uti) {
loadAttachment(client, addr, pass, key, id, uti);
});
}
function handleIncoming(msg, server, client, addr, pass, key, response) {
if(msg.indexOf("\n") == -1) {
return response + msg;
} else {
var indx = msg.indexOf("\n");
handleResponse(server, client, addr, pass, key, response + msg.substring(0, indx));
return handleIncoming(msg.substring(indx + 1), server, client, addr, pass, key, '');
}
}
function handleResponse(server, client, addr, pass, key, response) {
try {
switch(response) {
case 'ACK':
server.write(encrypt(pass, key));
break;
case 'READY':
loadConversations(client, addr, pass, key)
break;
case 'FAIL':
console.log('Bad password!');
// TODO
break;
case 'A':
break;
default:
client.emit('message', JSON.parse(decrypt(response, key)));
break;
}
} catch(err) {
console.log(err.message);
}
}
function sendMessage(client, addr, pass, key, display, id, partners, custom, guid, msg) {
var iv = crypto.randomBytes(8).toString('hex');
var data = {
iv: iv,
password: encrypt(pass, key, iv),
chat_id: encrypt(id, key, iv),
message: encrypt(msg, key, iv),
hasCustomName: encrypt(custom, key, iv),
participants: encrypt(custom ? partners : display, key, iv),
track: encrypt(1, key, iv)
}
if(guid && guid !== 'null' && guid !== 'undefined') {
data.guid = encrypt(guid, key, iv);
}
axios.post('http://' + addr + ':8735/send', querystring.stringify(data), {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}).then(function(response) {
}).catch(function(error) {
});
}
// TODO Handle decryption on client side, not server side.
function loadAttachment(client, addr, pass, key, id, uti) {
var iv = crypto.randomBytes(8).toString('hex');
client.emit('attachmentGet', id,
'http://' + addr + ':8735/attachment?id=' + encrypt(id, key, iv) + '&password=' + encrypt(pass, key, iv) + '&iv=' + iv, uti);
}
function getContacts(client, addr, pass, key) {
var iv = crypto.randomBytes(8).toString('hex');
axios.get('http://' + addr + ':8735/contacts?iv=' + iv + '&password=' + encrypt(pass, key, iv) + '&web=true').then(function(response) {
client.emit('contacts', JSON.parse(decrypt(response.data, key)));
}).catch(function(error) {
});
}
function loadMessages(client, addr, pass, key, id, offset) {
var iv = crypto.randomBytes(8).toString('hex');
axios.get('http://' + addr + ':8735/messages?iv=' + iv + '&password=' + encrypt(pass, key, iv) +
'&limit=' + encrypt(25, key, iv) + '&chat_id=' + encrypt(id, key, iv) + '&offset=' + encrypt(offset, key, iv) + '&web=true').then(function(response) {
client.emit('messages', JSON.parse(decrypt(response.data, key)), offset);
}).catch(function(error) {
});
}
function loadConversations(client, addr, pass, key) {
var iv = crypto.randomBytes(8).toString('hex');
axios.get('http://' + addr + ':8735/conversations?iv=' + iv + '&password=' + encrypt(pass, key, iv) + '&web=true').then(function(response) {
client.emit('conversations', JSON.parse(decrypt(response.data, key)));
}).catch(function(error) {
});
}
function decrypt(msg, key, vec) {
if(!vec || typeof vec === 'undefined') {
vec = msg.substring(0, 16);
msg = msg.substring(16);
}
var secure = aesjs.utils.utf8.toBytes(key);
var iv = aesjs.utils.utf8.toBytes(vec);
var aesCbc = new aesjs.ModeOfOperation.cbc(secure, iv);
var decryptedBytes = aesCbc.decrypt(aesjs.utils.hex.toBytes(msg));
var decryptedString = aesjs.utils.utf8.fromBytes(decryptedBytes);
return clean(decryptedString).trim();
}
function clean(str) {
var i;
for(i = str.length - 1; i > 0; i--) {
if(str.charCodeAt(i) > 16) {
break;
}
}
return str.substring(0, i + 1);
}
function encrypt(msg, key, withIV) {
var iv = withIV === undefined ? crypto.randomBytes(8).toString('hex') : withIV;
var aesCbc = new aesjs.ModeOfOperation.cbc(aesjs.utils.utf8.toBytes(key), aesjs.utils.utf8.toBytes(iv));
var encryptedBytes = aesCbc.encrypt(aesjs.padding.pkcs7.pad(aesjs.utils.utf8.toBytes(msg)));
return (withIV === undefined ? iv : '') + aesjs.utils.hex.fromBytes(encryptedBytes);
}