implement serverside RPC infrastructure

pull/1/head
ansuz 8 years ago
parent 81027b4200
commit 612a00b484

@ -209,6 +209,21 @@ const handleMessage = function (ctx, user, msg) {
let parsedMsg = {state: 1, channel: parsed[1]};
sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(parsedMsg)]);
});
} else if (ctx.rpc) {
/* RPC Calls... */
var rpc_call = parsed.slice(1);
// slice off the sequence number and pass in the rest of the message
ctx.rpc(rpc_call, function (err, output) {
if (err) {
console.error('[' + err + ']', output);
sendMsg(ctx, user, [seq, 'ACK']);
sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify([parsed[0], 'ERROR', err])]);
return
}
sendMsg(ctx, user, [seq, 'ACK']);
sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify([parsed[0]].concat(output))]);
});
}
return;
}
@ -251,7 +266,7 @@ const handleMessage = function (ctx, user, msg) {
}
};
let run = module.exports.run = function (storage, socketServer, config) {
let run = module.exports.run = function (storage, socketServer, config, rpc) {
/* Channel removal timeout defaults to 60000ms (one minute) */
config.channelRemovalTimeout =
typeof(config.channelRemovalTimeout) === 'number'?
@ -263,7 +278,8 @@ let run = module.exports.run = function (storage, socketServer, config) {
channels: {},
timeouts: {},
store: storage,
config: config
config: config,
rpc: rpc,
};
setInterval(function () {
Object.keys(ctx.users).forEach(function (userId) {

@ -130,6 +130,13 @@ module.exports = {
*/
openFileLimit: 2048,
/* Cryptpad's socket server can be extended to respond to RPC calls
* you can configure it to respond to custom RPC calls if you like.
* provide the path to your RPC module here, or `false` if you would
* like to disable the RPC interface completely
*/
rpc: './rpc.js',
/* it is recommended that you serve cryptpad over https
* the filepaths below are used to configure your certificates
*/

@ -0,0 +1,37 @@
/* 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) { };
RPC.create = function (config, cb) {
// load pin-store...
console.log('loading rpc module...');
rpc = function (msg, respond) {
switch (msg[0]) {
case 'ECHO':
respond(void 0, msg);
break;
case 'PIN':
case 'UNPIN':
case 'GET_HASH':
case 'GET_TOTAL_SIZE':
case 'GET_FILE_SIZE':
default:
respond('UNSUPPORTED_RPC_CALL', msg);
break;
}
};
cb(void 0, rpc);
};

@ -117,6 +117,7 @@ httpServer.listen(config.httpPort,config.httpAddress,function(){
var wsConfig = { server: httpServer };
var createSocketServer = function (err, rpc) {
if(!config.useExternalWebsocket) {
if (websocketPort !== config.httpPort) {
console.log("setting up a new websocket server");
@ -124,6 +125,24 @@ if(!config.useExternalWebsocket) {
}
var wsSrv = new WebSocketServer(wsConfig);
Storage.create(config, function (store) {
NetfluxSrv.run(store, wsSrv, config);
NetfluxSrv.run(store, wsSrv, config, rpc);
});
}
};
var loadRPC = function (cb) {
config.rpc = typeof(config.rpc) === 'undefined'? './rpc.js' : config.rpc;
if (typeof(config.rpc) === 'string') {
// load pin store...
var Rpc = require(config.rpc);
Rpc.create(config, function (e, rpc) {
if (e) { throw e; }
cb(void 0, rpc);
});
} else {
cb();
}
};
loadRPC(createSocketServer);

Loading…
Cancel
Save