const $ = require('jquery');
const encryption = require('./2_encryption');
const nanoid = require('nanoid');
let connectedPeers = []; // TODO: Save new peers in array
let connectedPeer;
const peerId = nanoid();
// setup encryption
if (encryption.setup() && encryption.check()) {
// TODO: Ask for passphrase
chat();
} else {
console.log('[LOG] No existing keys found! Generating...');
encryption.generate(peerId, 'supersecure').then(() => chat());
}
function chat() {
const peer = new Peer(peerId, {host: '127.0.0.1', port: 4242, path: '/', debug: 0});
// Peer events
peer.on('open', id => console.log('[LOG] Your ID is', id));
peer.on('error', err => console.error(err));
peer.on('connection', conn => {
connectedPeer = conn;
console.log('[LOG] Connected with', connectedPeer.peer);
connectedPeer.on('open', () => transferKey(encryption.getPublic()));
connectedPeer.on('data', message => receivedMessage(message));
});
/**
* Connects to a peer via his id
* @param id
*/
function connect(id) {
const connectionId = nanoid();
console.log('[LOG] Connecting to', id);
console.log('[LOG] Your connection ID is', connectionId);
connectedPeer = peer.connect(id, {label: connectionId, reliable: true});
console.log('[LOG] Connected with', connectedPeer.peer);
connectedPeer.on('open', () => transferKey(encryption.getPublic()));
connectedPeer.on('data', message => receivedMessage(message))
}
/**
* Sends a message to the peer with which you're currently connected
* @param message
*/
function sendMessage(message) {
console.log(`[LOG] Sending message ${message} to ${connectedPeer.peer}`);
encryption.encrypt(message, encryption.get(connectedPeer.peer)).then(encrypted => {
connectedPeer.send({type: 'text', data: encrypted});
receivedMessage(message, true);
})
}
/**
* Transfers the (public) key to the currently connected peer
* @param key
*/
function transferKey(key) {
console.log(`[LOG] Transferring key to ${connectedPeer.peer}`);
connectedPeer.send({type: 'key', data: key});
}
/**
* Renders and processes the incoming messages
* @param message
* @param self
*/
function receivedMessage(message, self = false) {
if (self) {
$('#messages').append(`${message}
`);
} else {
if (message.type === 'text') {
encryption.decrypt(message.data, encryption.get(connectedPeer.peer), encryption.getPrivate(), 'supersecure')
.then(plaintext => $('#messages').append(`${plaintext}
`));
} else if (message.type === 'key') {
encryption.store(connectedPeer.peer, message.data)
}
}
}
/**
* Events after load
*/
$(document).ready(() => {
$('#add_peer_id').on('click', () => connect($('#peer_id').val()));
$('#send_message').on('click', () => sendMessage($('#message').val()));
$('[toggle-contact-modal]').on('click', () => $('#add_contact_modal').toggleClass('is-active'))
});
}
//encryption.test(); // TESTING IF ENCRYPTION WORKS