UNPKG

absenat

Version:

dedicated messaging service core

137 lines (135 loc) 5.55 kB
const get_profile = (socket) => { socket.on('get_profile', (contact_public_key, cipher) => { const target_public_key = myPrivateKey.decrypt(cipher).toString(); console.log(`requested ${chalk.blue(target_public_key)}`); console.log(target_public_key); db.getContactByPublicKey(target_public_key, target_contact => { socket.emit('contact_found', config.public_key, createPublicKey(contact_public_key).encrypt(JSON.stringify({ first_name: target_contact.first_name, last_name: target_contact.last_name, nickname: target_contact.nickname, bio: target_contact.bio, phone: target_contact.phone, public_key: target_contact.public_key, }))); console.log(chalk.green('sent profile successfully!')); cli.finished(); }, () => { console.log(chalk.red('contact not found.')); socket.emit('contact_not_found', config.public_key, createPublicKey(contact_public_key).encrypt("sorry!")); cli.finished(); }); }); } // keeps chunks of messages being received until they are completed. const tmp_msg_chunks = {}; const indirect_message = (socket) => { socket.on('indirect_message', (contact_public_key, cipher) => { const message = JSON.parse(myPrivateKey.decrypt(cipher)); console.log(`\rindirect packet from ${chalk.blue(contact_public_key.slice(0, 10))} (pi: ${message.i}/${message.c})`); let tmp_message = tmp_msg_chunks[message.id]; if (!tmp_message) tmp_message = { pk: null, data: [], }; tmp_message.data[message.i] = message.data; if (message.pk) tmp_message.pk = message.pk; if (tmp_message.data.length === message.c) { console.log(`\rindirect message from ${chalk.blue(contact_public_key.slice(0, 10))}`); socket.emit('indirect_message_recieved', config.public_key, createPublicKey(contact_public_key).encrypt(JSON.stringify({ id: message.id }))); saveMessage(message.id, contact_public_key, tmp_message.pk, message.data); } socket.emit('indirect_packet_recieved', config.public_key, createPublicKey(contact_public_key).encrypt(uuidv4())); cli.finished(); }); } const send_undelivereds = (socket, contact_public_key) => { db.getUndeliveredMessages(contact_public_key, (messages) => { const message_ids = messages.map(message => message.id); console.log(message_ids); const contactPublicKeyNode = createPublicKey(contact_public_key); socket.emit('undelivered_ids', config.public_key, contactPublicKeyNode.encrypt(JSON.stringify(message_ids))); socket.once('undelivered_responses', (contact_public_key, cipher) => { const message = myPrivateKey.decrypt(cipher); console.log(messages); }); }); socket.on('undelivered_ids', (contact_public_key, cipher) => { const message_ids = JSON.parse(myPrivateKey.decrypt(cipher)); console.log(message_ids); }); } const connection_request = (socket) => { /** * =============================================== * Step 0: sends public key to dst node * =============================================== */ socket.on('connect_0', (from, contactPublicKey) => { console.log(`${socket.id} connected as ${from}`); connections[socket.id] = { nickname: from, public_key: contactPublicKey, socket, }; /** * =============================================== * Step 1: dst node sends public key and a random number (DTN or dst trust number), encrypted by src node public key to src node * =============================================== */ const contactPublicKeyNode = createPublicKey(contactPublicKey); const dtn = parseInt(Math.random() * 99999); socket.emit('connect_1', config.nickname, contactPublicKeyNode.encrypt(JSON.stringify({ public_key: config.public_key, dtn, }))); /** * =============================================== * Step 2: recieves dtn and stn encrypted by dst public key * =============================================== */ socket.on('connect_2', (from, cipher) => { const answer = JSON.parse(myPrivateKey.decrypt(cipher)); if (answer.dtn !== dtn) { console.log(chalk.red('Err: invalid DTN.')); cli.finished(); } else { console.log(chalk.green('DTN is valid.')); /** * =============================================== * Step 3: sends stn to src node to completing the trust level * =============================================== */ socket.emit('connect_3', config.nickname, contactPublicKeyNode.encrypt(answer.stn)); /** * =============================================== * Step 4: sends stn to src node to completing the trust level * =============================================== */ socket.on('connect_4', from => { console.log(chalk.green('STN is valid. finished the trusting level!')); db.addConnection(from, socket, connections[socket.id].public_key); cli.finished(); // hearbeat progress setInterval(() => { socket.emit('hi_im_bridge'); }, config.max_timeout); socket.on('hi_im_normal', () => { db.connectionHeartBeat(contactPublicKey); send_undelivereds(socket, contactPublicKey); }); }); } }); }); handlers.messages(socket); get_profile(socket); indirect_message(socket); }; module.exports = { connection_request, }