From 3746df6214549a7dd753396ad7fd9ef6d82f742b Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 12 Aug 2021 21:58:51 -0400 Subject: [PATCH] initial commit. messages are unencrypted and sent messages get echoed back as well. decryption routines are ready but need to add ui to get public keys by name --- achat | 33 ++++++++ static/client.js | 116 ++++++++++++++++++++++++++++ static/index.html | 48 ++++++++++++ static/jsencrypt.min.js | 2 + static/jsencrypt.min.js.LICENSE.txt | 8 ++ static/keys/index.html | 11 +++ static/msgs/index.html | 11 +++ static/style.css | 93 ++++++++++++++++++++++ 8 files changed, 322 insertions(+) create mode 100755 achat create mode 100644 static/client.js create mode 100644 static/index.html create mode 100644 static/jsencrypt.min.js create mode 100644 static/jsencrypt.min.js.LICENSE.txt create mode 100644 static/keys/index.html create mode 100644 static/msgs/index.html create mode 100644 static/style.css diff --git a/achat b/achat new file mode 100755 index 0000000..8f86808 --- /dev/null +++ b/achat @@ -0,0 +1,33 @@ +#!/bin/sh + +watch_msgs(){ + inotifywait -m -e create static/msgs/ --format "%f" +} + +start_client(){ + watch_msgs & + bkg=$! + while read line; do + file="static/msgs/$(date +"%s%N")" + printf "%s" "$line" > "$file" + done + kill "$bkg" +} + +start_server(){ + websocketd --port=8080 --staticdir=static/ ./achat client +} + +make_keys(){ + openssl genrsa -out rsa_1024_priv.pem 1024 + openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem + cat rsa_1024_priv.pem rsa_1024_pub.pem +} + +cmd="$1" +shift +case "$cmd" in + client) start_client "$@" ;; + server) start_server "$@" ;; + genkeys) make_keys "$@" ;; +esac \ No newline at end of file diff --git a/static/client.js b/static/client.js new file mode 100644 index 0000000..1e11f08 --- /dev/null +++ b/static/client.js @@ -0,0 +1,116 @@ +/* + Chat Client Interface +*/ +const Client = ((state = {keys: {}})=>({ + init: ()=>{ + /* connect using keys from storage */ + if (localStorage.privKey && localStorage.pubKey) { + Client.privKey(localStorage.privKey); + Client.pubKey(localStorage.pubKey); + Client.connect(); + } + /* load chat logs and contacts */ + // TODO + }, + + log: (msg)=>{ + const element = document.createElement('DIV'); + element.textContent += msg + '\n'; + const messages = document.getElementById('messages') + messages.appendChild(element); + messages.scrollTop = messages.scrollHeight; + }, + + send: ()=>{ + if (message.value.length > 0) { + Client.log("me: " + message.value); + state.conn.send(message.value); + message.value = ""; + } + }, + + loadKey: (name, key)=>{ + localStorage[name] = key; + const crypt = new JSEncrypt(); + crypt.setKey(key); + return crypt; + }, + + privKey: (key)=>{ + state.keys.private = Client.loadKey('privKey', key); + }, + + pubKey: (key)=>{ + state.keys.public = Client.loadKey('pubKey', key); + }, + + keysValid: ()=> state.keys.private.decrypt(state.keys.public.encrypt("test")), + + getFile: (file) => fetch(file).then((resp) => resp.text()), + + addContact: (name)=>{ + Client.getFile('./keys/' + name) + .then((key) => state.keys[name] = Client.loadKey(name, key)); + }, + + getMessage: (id)=>{ + Client.getFile('./msgs/' + id).then((msg)=>{ + Client.log('them(raw): ' + msg); + const text = state.keys.private.decrypt(msg); + if (text) { Client.log(text); } + }); + }, + + connect: ()=>{ + state.conn = new WebSocket('ws://localhost:8080/'); + + state.conn.onopen = ()=>{ + Client.log('CONNECT'); + }; + + state.conn.onclose = ()=>{ + Client.log('DISCONNECT'); + dialog.classList.add("visible"); + }; + + state.conn.onmessage = (event)=>{ + Client.getMessage(event.data); + }; + } +}))(); + +/* + UI Event Handlers +*/ + +submit.onclick = Client.send; + +message.onkeyup = (ev)=>{ + ev.preventDefault(); + if (ev.keyCode === 13) { + Client.send(); + } +}; + +connect.onclick = ()=>{ + public_key.files[0].text() + .then(key =>{ + Client.pubKey(key); + return private_key.files[0].text() + }) + .then(key => { + Client.privKey(key); + if (Client.keysValid()) + { + Client.connect(); + dialog.classList.remove("visible"); + } + }) +} + +// Try connecting based on local storage +(()=>{ + Client.init(); +// Client.addContact("clientA"); +// Client.getMessage("0"); +})(); diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..3c8c541 --- /dev/null +++ b/static/index.html @@ -0,0 +1,48 @@ + + + + + + Chat + + + + +
+
+
+
+
+ +
+
+ +
+ + +
+
+ +