-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
92 lines (79 loc) · 2.29 KB
/
app.py
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
from flask import Flask, render_template,request
from flask_socketio import SocketIO, join_room, emit
import random
import json
from flask_cors import CORS
# e2e encryption proof of concept
# Slack Crow(Aru team) 2018
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
CORS(app)
keySet = dict()
connectedClients = []
secure_random = random.SystemRandom()
keyOwner = ""
serverWideKeySet = False
@app.route("/")
def index():
return render_template('index.html')
@socketio.on('send')
def sub(data):
print(data)
emit('sendMsg', json.dumps(data),broadcast=True)
@socketio.on('getKey')
def getKey(data):
print('getKey')
print(data)
global keySet
keySet[request.sid] = data['publicKey']
toSend = dict()
toSend['key'] = data['publicKey']
toSend['cid'] = request.sid
emit('giveRoomKeyNow', json.dumps(toSend), room=keyOwner)
@socketio.on('getRoomKey')
def getRoomKey(data):
print('getRoomKey')
print(data)
emit('setRoomKeyNow', data['encryptedKey'], room=data['sid'])
@socketio.on('keySet')
def setKeyConfirmed(data):
global keyOwner
global serverWideKeySet
keyOwner = request.sid
print('Key set')
print('Key Owner: ' + keyOwner)
serverWideKeySet = True
for client in connectedClients:
if len(connectedClients) == 1:
break
elif client == keyOwner:
continue
else:
print('test')
emit('givePubKeyNow', "", room=client)
@socketio.on('connect')
def handle_connect():
global connectedClients
connectedClients.append(request.sid)
print('Client connected')
if not serverWideKeySet:
emit('setKeyNow', "", room=request.sid)
else:
emit('givePubKeyNow', "", room=request.sid)
@socketio.on('disconnect')
def handle_disconnect():
print('Client disconnected')
global connectedClients
global keyOwner
global serverWideKeySet
connectedClients.remove(request.sid)
if request.sid == keyOwner:
print('(ERR) Key holder disconnected')
if len(connectedClients) != 0:
clientToSetKey = secure_random.choice(connectedClients)
emit('setKeyNow', "", room=clientToSetKey)
else:
serverWideKeySet = False
if __name__ == '__main__':
socketio.run(app)