Working more on web server

This commit is contained in:
Martin Asprusten
2025-04-19 16:42:46 +02:00
parent 8652ab2eec
commit 14cdefea69
25 changed files with 306 additions and 30 deletions
View File
+34
View File
@@ -0,0 +1,34 @@
import json
from collections import defaultdict
from flask import Flask, render_template
from flask_socketio import SocketIO, join_room, leave_room, send
app = Flask(__name__)
socketio = SocketIO(app)
if __name__ == 'main':
socketio.run(app)
@app.route('/')
def return_index_page():
return render_template('index.html')
@app.route('/<exchange>')
def return_exchange_page(exchange):
return render_template('exchange.html')
@socketio.on('join')
def on_join(data):
room = data['room']
join_room(room)
@socketio.on('leave')
def on_leave(data):
room = data['room']
leave_room(room)
@socketio.on('message')
def on_message(data):
room = data['room']
send(data, to=room)
+28
View File
@@ -0,0 +1,28 @@
from Crypto.PublicKey.ECC import EccKey
from src.SantaExchange.Message import Message
from src.SantaExchange.MessageHandler import MessageHandler
from src.SantaExchange.SantasBrain import Brain
from src.SantaExchange.UserInterface import UserInterface
class ExchangeClient(MessageHandler, UserInterface):
def __init__(self, send_message_function, receive_user_function, announce_recipient_function):
MessageHandler.__init__(self)
UserInterface.__init__(self)
self.send_message_function = send_message_function
self.receive_user_function = receive_user_function
self.announce_recipient_function = announce_recipient_function
self.brain = Brain(self, self)
def send_message(self, message: Message, signing_key: EccKey):
message_string = message.generate_and_sign(signing_key)
self.send_message_function(message_string)
def receive_user(self, name: str):
self.receive_user_function(name)
def announce_recipient(self, name: str, other_info: str):
self.announce_recipient_function(name, other_info)
+23
View File
@@ -0,0 +1,23 @@
import "./pyodide.js";
async function prepare() {
let pyodide = await loadPyodide();
await pyodide.loadPackage("pycryptodome");
await pyodide.loadPackage("./santaexchange-0.1-py3-none-any.whl")
pyodide.runPython(`
class Test:
def calculate(self):
return 15
a = Test()
`);
console.log('Running python function');
console.log(pyodide.globals.get('a').calculate());
}
prepare();
addEventListener('message', e => {
self.postMessage('Loading pyodide');
self.postMessage('Loaded pyodide');
});
+38
View File
@@ -0,0 +1,38 @@
body {
background-color: #fff7f7;
color: #000000;
text-align: center;
font-family: 'Courier New';
font-size: 20px;
}
h1 {
color: #d46a64;
-webkit-text-stroke-width: 2px;
-webkit-text-stroke-color: var(--header-stroke-color);
font-size: 80px;
font-family: 'Helvetica';
margin-bottom: 20px;
}
.bodyDiv {
width: 80vw;
margin-left: auto;
margin-right: auto;
}
button {
font-family: 'Courier New';
font-size: 20px;
margin-top: 10px;
margin-bottom: 5px;
}
input {
font-size: 20px;
}
textarea {
width: 30vw;
height: 20vh;
}
+49
View File
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exchange</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
<script src="{{ url_for('static', filename='socket.io.min.js') }}" integrity="sha384-mkQ3/7FUtcGyoppY6bz/PORYoGqOl7/aSUMn2ymDOJcapfS6PHqxhRTMh1RR0Q6+" crossorigin="anonymous"></script>
</head>
<body>
<h1>Santa exchange</h1>
<label for="own_name">Your name: </label><br /><input type="text" id="own_name" /><br />
<br />
<label for="info_for_santa">Info for your santa (e.g. shipping address or similar):</label><br />
<textarea id="info_for_santa"></textarea><br />
<button id="submit_info_button">Submit your info</button>
<br /><br /><br />
<p id="statusParagraph"></p>
<button>Start with current participants</button>
<p>Current participants:</p>
<div id='participantList'></div>
<!--<script type="text/javascript">
const socket = io();
const client_id = Math.floor(Math.random() * 2**64)
socket.emit('join', {room: window.location.pathname, 'client_id': client_id})
socket.on('message', data => {
// All messages are broadcast to everyone, so discard messages that were actually sent from ourselves
data = JSON.parse(data);
if (data['client_id'] != client_id) {
console.log(data);
console.log(data['message']);
window.pass_message_to_python(JSON.stringify(data['message']));
}
});
</script>-->
<script type="text/javascript">
const exchangeWorker = new Worker("{{ url_for('static', filename='exchange_worker.js') }}", { type: 'module' });
exchangeWorker.onmessage = (e) => {
console.log('Received message:');
console.log(e);
document.getElementById('statusParagraph').innerHTML = e.data;
};
exchangeWorker.postMessage('Nothing');
</script>
</body>
</html>
+52
View File
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DecentraSanta</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"/>
<script type="text/javascript" src="{{ url_for('static', filename='pyodide.js') }}"></script>
</head>
<body>
<div class="bodyDiv">
<h1>DecentraSanta</h1>
<p>Welcome to the decentralised secret santa exchange. This website uses cryptography to distribute secret santas
in such a way that no one but you, not even the server the communication goes through, can know who you're
giving a
gift to.</p>
<p>You can either start a new secret santa exchange, or join an existing one.</p>
<br/>
<button id="start_new" onclick="onNewExchange();">Start new exchange</button>
<br/>
<br/>
<input type="text" id="existing_address" />
<br/>
<button id="join_existing" onclick="onJoinExchange();">Join existing exchange</button>
</div>
<script type="text/javascript">
function onNewExchange() {
const byteArray = new Uint8Array(8);
self.crypto.getRandomValues(byteArray);
let randomHexString = Array.from(byteArray).map(x => x.toString(16)).reduce((a, c) => a + c);
var urlString = window.location.href;
if (!urlString.endsWith('/')) {
urlString = urlString + '/';
}
urlString = urlString + randomHexString;
window.location.href = urlString;
}
function onJoinExchange() {
const existingExchange = document.getElementById('existing_address').value;
var urlString = window.location.href;
if (!urlString.endsWith('/')) {
urlString = urlString + '/';
}
urlString = urlString + existingExchange;
window.location.href = urlString;
}
</script>
</body>
</html>