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
|
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 password
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', conn.peer);
conn.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});
// setup listener
connectedPeer.on('open', () => {
// TODO: Activate chat or sth
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}`);
connectedPeer.send({type: 'text', data: message});
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(`<span style="color: green">${message}</span><br>`);
} else {
if (message.type === 'text')
$('#messages').append(`${message.data}<br>`);
else if (message.type === 'key') {
console.log(connectedPeer.peer);
console.log(peer.connections);
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
|