-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.js
82 lines (66 loc) · 2.05 KB
/
client.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
(function() {
'use strict';
module.exports = function(address, key) {
if(!address) {
console.log('Error: no IP address provided.');
console.log('cryptochat <address> <encryption_key>');
return;
}
if(!key) {
console.log('Error: encryption key missing.');
console.log('cryptochat <address> <encryption_key>');
return;
}
var raw = require('raw-socket'),
crypto = require('./encryption.js'),
socket = raw.createSocket({
protocol: raw.Protocol.ICMP
});
process.stdin.setEncoding('utf8');
process.stdin.on('data', send);
function send(msg) {
var payloads = msg.match(/.{1,31}/g),
packets = [],
payload,
length;
var salt = crypto.salt(),
iv = crypto.iv(),
derivedKey = crypto.key(key, salt);
packets.push(packet('3e' + salt.toString('hex') + iv.toString('hex')));
for(var i = 0; i < payloads.length; i++) {
payload = payloads[i];
length = ('00' + (payload.length * 2).toString(16)).substr(-2);
payload = length + crypto.encrypt(payload, derivedKey, iv);
packets.push(packet(payload));
}
packets.push(packet('3e'));
function ping() {
if(!packets.length)
return;
var packet = packets.shift();
socket.send(packet, 0, packet.length, address, function(error) {
if(error)
throw error;
ping();
});
}
ping();
}
function packet(message) {
while(message.length < 64)
message += 'f';
var buffer = new Buffer(40);
buffer[0] = 0x08; // type
buffer[1] = 0x00; // code
buffer[2] = 0x00; // checksum placeholder
buffer[3] = 0x00; // checksum placeholder
buffer[4] = 0x00; // identifier
buffer[5] = 0x01; // identifier
buffer[6] = 0x0a; // seqnum
buffer[7] = 0x09; // seqnum
buffer.write(message, 8, 'hex');
raw.writeChecksum(buffer, 2, raw.createChecksum(buffer));
return buffer;
}
};
}());