aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--package-lock.json7
-rw-r--r--package.json1
-rw-r--r--public/scripts/chat.js7
-rw-r--r--public/scripts/encryption.js40
4 files changed, 52 insertions, 3 deletions
diff --git a/package-lock.json b/package-lock.json
index c160c64..3770562 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,5 +1,5 @@
{
- "name": "decentralized-chat",
+ "name": "texx",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
@@ -7020,6 +7020,11 @@
"xtend": "^4.0.0"
}
},
+ "moment": {
+ "version": "2.24.0",
+ "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
+ "integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
+ },
"morgan": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz",
diff --git a/package.json b/package.json
index 66f63c0..24bfa92 100644
--- a/package.json
+++ b/package.json
@@ -44,6 +44,7 @@
"express": "^4.16.4",
"jquery": "^3.3.1",
"minify": "^4.1.0",
+ "moment": "^2.24.0",
"morgan": "^1.9.0",
"onchange": "^5.2.0",
"openpgp": "^4.4.5",
diff --git a/public/scripts/chat.js b/public/scripts/chat.js
index 35e75db..36cfdf1 100644
--- a/public/scripts/chat.js
+++ b/public/scripts/chat.js
@@ -90,9 +90,11 @@ function chat() {
// Peer events
peer.on('open', id => console.log('[LOG] Your ID is', id));
peer.on('error', err => console.error(err));
- peer.on('connection', conn => {
+ peer.on('connection', async conn => {
connectedPeer = conn;
console.log('[LOG] Connected with', connectedPeer.peer);
+ encryption.getMsgs(connectedPeer.peer, await encryption.get(connectedPeer.peer), await encryption.getPrivate(), passphrase).then(messages =>
+ messages.forEach(data => receivedMessage(`${data.message} - ${data.time}`, true)));
connectedPeer.on('open', async () => transferKey(await encryption.getPublic()));
connectedPeer.on('data', async message => {
console.log('[LOG] Received new message!');
@@ -111,6 +113,8 @@ function chat() {
console.log('[LOG] Your connection ID is', connectionId);
connectedPeer = peer.connect(id, {label: connectionId, reliable: true});
console.log('[LOG] Connected with', connectedPeer.peer);
+ encryption.getMsgs(connectedPeer.peer, await encryption.get(connectedPeer.peer), await encryption.getPrivate(), passphrase).then(messages =>
+ messages.forEach(data => receivedMessage(`${data.message} - ${data.time}`, true)));
connectedPeer.on('open', async () => transferKey(await encryption.getPublic()));
connectedPeer.on('data', async message => {
console.log('[LOG] Received new message!');
@@ -151,6 +155,7 @@ function chat() {
$('#messages').append(`<span style="color: green">${message}</span><br>`);
} else {
if (message.type === 'text') {
+ await encryption.storeMsg(peerId, message.data);
await encryption.decrypt(message.data, await encryption.get(connectedPeer.peer), await encryption.getPrivate(), passphrase)
.then(plaintext => $('#messages').append(`${plaintext}<br>`));
} else if (message.type === 'key') {
diff --git a/public/scripts/encryption.js b/public/scripts/encryption.js
index 6406906..d1d4038 100644
--- a/public/scripts/encryption.js
+++ b/public/scripts/encryption.js
@@ -6,6 +6,7 @@
*/
const Dexie = require('dexie');
+const moment = require('moment');
const openpgp = require('openpgp');
let db;
@@ -19,7 +20,7 @@ function setupDatabase() {
db.version(2).stores({
own_keys: '&key_type, key_data',
peer_keys: 'peer_id, key_data',
- messages: 'peer_id, message'
+ messages: '++id, peer_id, message, time'
});
localStorage.setItem('database', 'success');
@@ -150,6 +151,41 @@ async function isEncrypted() {
}
/**
+ * Stores a message
+ * @param peerId
+ * @param message
+ */
+async function storeMessage(peerId, message) {
+ await db.messages.put({peer_id: peerId, message: message, time: new Date()}).then(() =>
+ console.log('[LOG] Stored message of ' + peerId)
+ );
+}
+
+/**
+ * Gets a message
+ * @param peerId
+ * @param publicKey
+ * @param privateKey
+ * @param passphrase
+ */
+async function getMessages(peerId, publicKey, privateKey, passphrase) {
+ console.log('[LOG] Getting messages');
+ try {
+ return await db.messages.where('peer_id').equals(peerId).sortBy('id').toArray().then(messages => {
+ let messageArray = [];
+ messages.forEach(async messageObj => messageArray.push({
+ message: await decrypt(messages['message'], publicKey, privateKey, passphrase),
+ time: moment(messageObj['time']).fromNow()
+ }));
+ return messageArray;
+ })
+ } catch (e) {
+ console.log('[LOG] No messages found!');
+ return [];
+ }
+}
+
+/**
* Stores the public key of a peer
* @param peerId
* @param key
@@ -209,6 +245,8 @@ exports.encrypt = encrypt;
exports.decrypt = decrypt;
exports.decryptPrivate = decryptPrivateKey;
exports.check = isEncrypted;
+exports.storeMsg = storeMessage;
+exports.getMsgs = getMessages;
exports.store = storePeerPublicKey;
exports.get = getPeerPublicKey;
exports.getId = getPublicKeyUserId;