forked from ryan-shaw/votifier-send
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (55 loc) · 1.68 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
var ursa = require('ursa-purejs');
var net = require('net');
module.exports = {
send: sendData
};
function sendData(settings, callback){
settings.key = settings.key.replace(/ /g, '+');
settings.key = wordwrap(settings.key, 65, true);
var timestampdata = new Date().getTime();
if(settings.data.timestamp)
timestampdata = new Date(settings.data.timestamp);
var pubKey = new Buffer('-----BEGIN PUBLIC KEY-----\n' + settings.key + '\n-----END PUBLIC KEY-----\n');
var build = 'VOTE\n' + settings.data.site + '\n'+ settings.data.user + '\n' + settings.data.addr + '\n'+timestampdata+'\n';
var buf = new Buffer(build, 'binary');
key = ursa.createPublicKey(pubKey);
var data = key.encrypt(build, 'binary', 'binary', ursa.RSA_PKCS1_PADDING);
var called = false;
var callbackWrapper = function(e){
if (!called){
called = true;
callback(e);
}
};
var connection = net.createConnection({
host: settings.host,
port: settings.port
}, function(){
connection.write(data, 'binary', function(){
connection.end();
callbackWrapper(null);
});
});
connection.setTimeout(settings.timeout || 2000, function(){
connection.end();
return callbackWrapper(new Error("Socket timeout"));
});
connection.once('error', function(e){
return callbackWrapper(e);
});
}
function wordwrap(str, maxWidth) {
var newLineStr = "\n"; done = false; res = '';
do {
found = false;
if (!found) {
res += [str.slice(0, maxWidth), newLineStr].join('');
str = str.slice(maxWidth);
}
if (str.length < maxWidth){
res += str;
done = true;
}
} while (!done);
return res;
}