forked from cryptpad/cryptpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.js
172 lines (136 loc) · 4.46 KB
/
rpc.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
/* Use Nacl for checking signatures of messages */
var Nacl = require("tweetnacl");
var RPC = module.exports;
var pin = function (ctx, cb) { };
var unpin = function (ctx, cb) { };
var getHash = function (ctx, cb) { };
var getTotalSize = function (ctx, cb) { };
var getFileSize = function (ctx, cb) { };
var isValidChannel = function (chan) {
return /^[a-fA-F0-9]/.test(chan);
};
var makeCookie = function (seq) {
return [
Math.floor(new Date() / (1000*60*60*24)),
process.pid, // jshint ignore:line
seq
].join('|');
};
var parseCookie = function (cookie) {
if (!(cookie && cookie.split)) { return null; }
var parts = cookie.split('|');
if (parts.length !== 3) { return null; }
var c = {};
c.time = new Date(parts[0]);
c.pid = parts[1];
c.seq = parts[2];
return c;
};
var isValidCookie = function (ctx, cookie) {
var now = +new Date();
if (now - cookie.time > 300000) { // 5 minutes
return false;
}
// different process. try harder
if (process.pid !== cookie.pid) { // jshint ignore:line
return false;
}
//if (cookie.seq !==
return true;
};
var checkSignature = function (signedMsg, signature, publicKey) {
if (!(signedMsg && publicKey)) { return false; }
var signedBuffer;
var pubBuffer;
var signatureBuffer;
try {
signedBuffer = Nacl.util.decodeUTF8(signedMsg);
} catch (e) {
console.log('invalid signedBuffer');
console.log(signedMsg);
return null;
}
try {
pubBuffer = Nacl.util.decodeBase64(publicKey);
} catch (e) {
return false;
}
try {
signatureBuffer = Nacl.util.decodeBase64(signature);
} catch (e) {
return false;
}
if (pubBuffer.length !== 32) {
console.log('public key length: ' + pubBuffer.length);
console.log(publicKey);
return false;
}
if (signatureBuffer.length !== 64) {
return false;
}
return Nacl.sign.detached.verify(signedBuffer, signatureBuffer, pubBuffer);
};
RPC.create = function (config, cb) {
// load pin-store...
console.log('loading rpc module...');
var Cookies = {};
var rpc = function (ctx, data, respond) {
if (!data.length) {
return void respond("INSUFFICIENT_ARGS");
} else if (data.length !== 1) {
console.log(data.length);
}
var msg = data[0].slice(0);
var signature = msg.shift();
var publicKey = msg.shift();
var cookie = parseCookie(msg.shift());
if (!cookie) {
// no cookie is fine if the RPC is to get a cookie
if (msg[0] !== 'COOKIE') {
return void respond('NO_COOKIE');
}
} else if (!isValidCookie(cookie)) { // is it a valid cookie?
return void respond('INVALID_COOKIE');
}
var serialized = JSON.stringify(msg);
if (!(serialized && publicKey)) {
return void respond('INVALID_MESSAGE_OR_PUBLIC_KEY');
}
if (checkSignature(serialized, signature, publicKey) !== true) {
return void respond("INVALID_SIGNATURE_OR_PUBLIC_KEY");
}
if (!msg.length) {
return void respond("INVALID_SIGNATURE_OR_PUBLIC_KEY");
}
if (typeof(msg) !== 'object') {
return void respond('INVALID_MSG');
}
switch (msg[0]) {
case 'COOKIE':
return void respond(void 0, makeCookie());
case 'ECHO':
return void respond(void 0, msg);
case 'RESET':
return void respond('NOT_IMPLEMENTED', msg);
case 'PIN':
return void respond('NOT_IMPLEMENTED', msg);
case 'UNPIN':
return void respond('NOT_IMPLEMENTED', msg);
case 'GET_HASH':
return void respond('NOT_IMPLEMENTED', msg);
case 'GET_TOTAL_SIZE':
return void respond('NOT_IMPLEMENTED', msg);
case 'GET_FILE_SIZE':
if (!isValidChannel(msg[1])) {
return void respond('INVALID_CHAN');
}
return void ctx.store.getChannelSize(msg[1], function (e, size) {
if (e) { return void respond(e.code); }
respond(void 0, size);
});
default:
return void respond('UNSUPPORTED_RPC_CALL', msg);
}
};
cb(void 0, rpc);
};