75 lines
1.8 KiB
Python
75 lines
1.8 KiB
Python
"""
|
|
this file is part of secmsg, and is licensed under the AGPL.
|
|
please see the terms of the license in the file LICENSE.
|
|
|
|
copyright 2023 randomuser
|
|
"""
|
|
import os
|
|
import hashlib
|
|
|
|
from flask import Flask
|
|
from flask import send_from_directory
|
|
from flask import redirect
|
|
from flask import request
|
|
from flask import render_template
|
|
from flask import escape
|
|
from flask_sock import Sock
|
|
import simple_websocket
|
|
import time
|
|
|
|
app = Flask(__name__, instance_relative_config=True)
|
|
sock = Sock(app)
|
|
clients = []
|
|
messages = {}
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return redirect("/static/app")
|
|
|
|
@app.route('/messages')
|
|
def message_index():
|
|
argument = request.args.get('id')
|
|
if not argument:
|
|
return redirect("/static/messages.html")
|
|
else:
|
|
try:
|
|
text = messages[argument]
|
|
messages.pop(argument, None)
|
|
return text
|
|
except KeyError:
|
|
return redirect("/static/messages.html")
|
|
|
|
@app.route('/newmessage', methods = ['POST'])
|
|
def make_message():
|
|
if request.method == 'POST':
|
|
message = request.form['message']
|
|
hashobj = hashlib.sha256()
|
|
hashobj.update(bytes(message + str(time.time_ns()), 'utf-8'))
|
|
hashed = hashobj.hexdigest()
|
|
messages[hashed] = escape(message)
|
|
|
|
return render_template('messages.html', proto="http", host="127.0.0.1:5000", hashed=hashed)
|
|
|
|
@app.route('/static/app')
|
|
def app_redirect():
|
|
return redirect("/static/app.html")
|
|
|
|
@app.route('/static/<path:path>')
|
|
def static_handler(path):
|
|
return send_from_directory('static', path)
|
|
|
|
@sock.route('/client')
|
|
def sock_handler(ws):
|
|
clients.append(ws)
|
|
|
|
try:
|
|
while True:
|
|
data = ws.receive()
|
|
for client in clients:
|
|
if not client is ws:
|
|
client.send(data)
|
|
|
|
except simple_websocket.ws.ConnectionClosed:
|
|
clients.remove(ws)
|
|
|