You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/common/sframe-chainpad-netflux-out...

237 lines
9.0 KiB
JavaScript

7 years ago
/*
* Copyright 2014 XWiki SAS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
7 years ago
define([], function () {
7 years ago
var USE_HISTORY = true;
var verbose = function (x) { console.log(x); };
verbose = function () {}; // comment out to enable verbose logging
var unBencode = function (str) { return str.replace(/^\d+:/, ''); };
7 years ago
var start = function (conf) {
7 years ago
var channel = conf.channel;
var Crypto = conf.crypto;
var validateKey = conf.validateKey;
var readOnly = conf.readOnly || false;
var network = conf.network;
7 years ago
var sframeChan = conf.sframeChan;
7 years ago
conf = undefined;
7 years ago
var initializing = true;
var lastKnownHash;
7 years ago
var queue = [];
var messageFromInner = function (m, cb) { queue.push([ m, cb ]); };
7 years ago
sframeChan.on('Q_RT_MESSAGE', function (message, cb) {
7 years ago
messageFromInner(message, cb);
});
7 years ago
var onReady = function () {
7 years ago
// Trigger onReady only if not ready yet. This is important because the history keeper sends a direct
// message through "network" when it is synced, and it triggers onReady for each channel joined.
if (!initializing) { return; }
7 years ago
sframeChan.event('EV_RT_READY', null);
7 years ago
// we're fully synced
initializing = false;
7 years ago
};
7 years ago
7 years ago
// shim between chainpad and netflux
var msgIn = function (peerId, msg) {
msg = msg.replace(/^cp\|/, '');
try {
var decryptedMsg = Crypto.decrypt(msg, validateKey);
return decryptedMsg;
} catch (err) {
console.error(err);
return msg;
}
};
var msgOut = function (msg) {
if (readOnly) { return; }
try {
var cmsg = Crypto.encrypt(msg);
if (msg.indexOf('[4') === 0) { cmsg = 'cp|' + cmsg; }
return cmsg;
} catch (err) {
console.log(msg);
throw err;
7 years ago
}
};
var onMessage = function(peer, msg, wc, network, direct) {
// unpack the history keeper from the webchannel
var hk = network.historyKeeper;
if (direct && peer !== hk) {
return;
}
if (direct) {
var parsed = JSON.parse(msg);
if (parsed.validateKey && parsed.channel) {
if (parsed.channel === wc.id && !validateKey) {
validateKey = parsed.validateKey;
}
// We have to return even if it is not the current channel:
// we don't want to continue with other channels messages here
return;
}
if (parsed.state && parsed.state === 1 && parsed.channel) {
if (parsed.channel === wc.id) {
7 years ago
onReady(wc);
7 years ago
}
// We have to return even if it is not the current channel:
// we don't want to continue with other channels messages here
return;
}
}
// The history keeper is different for each channel :
// no need to check if the message is related to the current channel
7 years ago
if (peer === hk) {
7 years ago
// if the peer is the 'history keeper', extract their message
var parsed1 = JSON.parse(msg);
msg = parsed1[4];
// Check that this is a message for us
if (parsed1[3] !== wc.id) { return; }
}
lastKnownHash = msg.slice(0,64);
7 years ago
var message = msgIn(peer, msg);
7 years ago
verbose(message);
// slice off the bencoded header
// Why are we getting bencoded stuff to begin with?
// FIXME this shouldn't be necessary
message = unBencode(message);//.slice(message.indexOf(':[') + 1);
// pass the message into Chainpad
7 years ago
sframeChan.query('Q_RT_MESSAGE', message, function () { });
7 years ago
};
// We use an object to store the webchannel so that we don't have to push new handlers to chainpad
// and remove the old ones when reconnecting and keeping the same 'realtime' object
// See realtime.onMessage below: we call wc.bcast(...) but wc may change
var wcObject = {};
7 years ago
var onOpen = function(wc, network, firstConnection) {
7 years ago
wcObject.wc = wc;
channel = wc.id;
// Add the existing peers in the userList
7 years ago
sframeChan.event('EV_RT_CONNECT', { myID: wc.myID, members: wc.members, readOnly: readOnly });
7 years ago
// Add the handlers to the WebChannel
wc.on('message', function (msg, sender) { //Channel msg
onMessage(sender, msg, wc, network);
});
7 years ago
wc.on('join', function (m) { sframeChan.event('EV_RT_JOIN', m); });
wc.on('leave', function (m) { sframeChan.event('EV_RT_LEAVE', m); });
7 years ago
7 years ago
if (firstConnection) {
7 years ago
// Sending a message...
7 years ago
messageFromInner = function(message, cb) {
7 years ago
// Filter messages sent by Chainpad to make it compatible with Netflux
7 years ago
message = msgOut(message);
if (message) {
7 years ago
// Do not remove wcObject, it allows us to use a new 'wc' without changing the handler if we
// want to keep the same chainpad (realtime) object
wcObject.wc.bcast(message).then(function() {
7 years ago
cb('OK');
7 years ago
}, function(err) {
// The message has not been sent, display the error.
console.error(err);
});
}
7 years ago
};
queue.forEach(function (arr) { messageFromInner(arr[0], arr[1]); });
7 years ago
}
// Get the channel history
7 years ago
if (USE_HISTORY) {
7 years ago
var hk;
wc.members.forEach(function (p) {
if (p.length === 16) { hk = p; }
});
network.historyKeeper = hk;
var msg = ['GET_HISTORY', wc.id];
// Add the validateKey if we are the channel creator and we have a validateKey
msg.push(validateKey);
msg.push(lastKnownHash);
if (hk) { network.sendto(hk, JSON.stringify(msg)); }
7 years ago
} else {
onReady(wc);
7 years ago
}
};
var isIntentionallyLeaving = false;
window.addEventListener("beforeunload", function () {
isIntentionallyLeaving = true;
});
7 years ago
var findChannelById = function (webChannels, channelId) {
7 years ago
var webChannel;
// Array.some terminates once a truthy value is returned
// best case is faster than forEach, though webchannel arrays seem
// to consistently have a length of 1
webChannels.some(function(chan) {
if(chan.id === channelId) { webChannel = chan; return true;}
});
return webChannel;
};
7 years ago
var connectTo = function (network, firstConnection) {
7 years ago
// join the netflux network, promise to handle opening of the channel
network.join(channel || null).then(function(wc) {
onOpen(wc, network, firstConnection);
}, function(error) {
console.error(error);
});
};
7 years ago
network.on('disconnect', function (reason) {
if (isIntentionallyLeaving) { return; }
if (reason === "network.disconnect() called") { return; }
7 years ago
sframeChan.event('EV_RT_DISCONNECT');
7 years ago
});
7 years ago
7 years ago
network.on('reconnect', function (uid) {
initializing = true;
connectTo(network, false);
});
7 years ago
7 years ago
network.on('message', function (msg, sender) { // Direct message
var wchan = findChannelById(network.webChannels, channel);
if (wchan) {
onMessage(sender, msg, wchan, network, true);
7 years ago
}
7 years ago
});
7 years ago
7 years ago
connectTo(network, true);
};
7 years ago
7 years ago
return {
start: function (config) {
config.sframeChan.whenReg('EV_RT_READY', function () {
start(config);
});
7 years ago
}
7 years ago
};
});