-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobile.py
executable file
·89 lines (70 loc) · 2.41 KB
/
mobile.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
#!/usr/bin/env python3
import websocket
import _thread
import time
import rel
import json
import logging
import sec_ecdh
import base64
import logging
import sys
logging.basicConfig(level='INFO')
"""
--usage--:
python3 mobile.py session_id_for_browser_client
1. requires connection url to start connection - gotten from qr code
2. connects to server and gets a session_id, also informs server which
session it is communicating with for_session_id
3. encryption happens between session_id and server
4. once encryption is setup, browser client is informed it can now communicate with session_id
5. information is sent to the browser client decrypted - using session_id
6. information is received from the browser in an encrypted way
"""
for_session_id = sys.argv[1]
session_id = None
ec = sec_ecdh.C_ECDH()
def on_message(ws, message):
"""
"""
message = json.loads(message)
if 'public_key' in message:
ec.generate_secret(message['public_key'])
logging.info("+ handshake complete!")
if 'session_id' in message:
session_id = message['session_id']
with open("sample.json", "r") as f:
sample_data = json.load(f)
data = {
"for_session_id":for_session_id,
"data":sample_data
}
ws.send(json.dumps(data))
if 'from_session_id' in message and 'data' in message:
logging.info("+ New message from browser (%s): %s",
message['from_session_id'],
message['data'])
logging.info(message)
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print("### closed ###")
def on_open(ws):
"""
"""
public_key = ec.get_public_key().decode()
data = {
"for_session_id":for_session_id,
"public_key":public_key
}
ws.send(json.dumps(data))
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://staging.smswithoutborders.com:16000",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever(dispatcher=rel, reconnect=5) # Set dispatcher to automatic reconnection, 5 second reconnect delay if connection closed unexpectedly
rel.signal(2, rel.abort) # Keyboard Interrupt
rel.dispatch()