-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
128 lines (106 loc) · 3.94 KB
/
Copy pathclient.py
File metadata and controls
128 lines (106 loc) · 3.94 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import socket
import threading
import json
import os
import dh
def enc_json(data) -> bytes:
return json.dumps(data).encode("utf-8")
def dec_json(data: bytes):
print(f"data: {data.decode('utf-8')}")
return json.loads(data.decode("utf-8"))
def recv_all(sock: socket.socket, block_size: int = 1024) -> bytes:
data = b""
while True:
new = sock.recv(block_size)
data += new
if len(new) < block_size:
break
return data
def client(ip: str, port: int, username: str):
DATA_FILE = f"{username}_keys.json"
try:
with open(DATA_FILE, "r") as f:
key_data = json.load(f)
print("Loaded existing key.")
except (OSError, json.JSONDecodeError) as e:
print(e)
me = dh.DiffieHellman()
key_data = {
"mine": me.save(),
"negotiations": []
}
me = dh.DiffieHellman.load(key_data["mine"])
with open(DATA_FILE, "w") as f:
json.dump(key_data, f, indent=2)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((ip, port))
sock.send(enc_json({
"type": "client_init",
"username": username,
"public_key": me.gen_public_key()
}))
print("Connected.")
stop_event = threading.Event()
def listener():
try:
while not stop_event.is_set():
data = dec_json(recv_all(sock))
if data["type"] == "message_forward":
print(f"<{data['name']}> {data['message']}\n")
elif data["type"] == "user_connect":
print(f"[+] {data['name']} has connected.")
elif data["type"] == "user_disconnect":
print(f"[-] {data['name']} has disconnected.")
elif data["type"] == "server_close":
print(f"Server closed.")
elif data["type"] == "user_list":
print(f"Users connected: {data['users']}")
elif data["type"] == "disconnect":
print(f"Disconnected from server, reason: {data['reason']}")
elif data["type"] == "conversation_init":
print(f"conversation {data}")
other_name = data["name"]
other_public_key = data["public_key"]
shared_key = me.gen_shared_key(other_public_key)
print(f"Shared key: {shared_key}")
else:
print(f"Unimplemented message type {data['type']}")
except Exception as e:
print("err", repr(e))
if stop_event.is_set():
print("Disconnected, closing...")
else:
print("Lost connection, closing...")
stop_event.set()
t = threading.Thread(target=listener)
t.daemon = True
t.start()
target = input("Who to message?")
sock.sendall(enc_json({
"type": "conversation_request",
"name": target
}))
try:
while not stop_event.is_set():
message = input("")
sock.sendall(enc_json({
"type": "message",
"message": message
}))
except KeyboardInterrupt:
stop_event.set()
sock.close()
except:
pass
my_server = True
if my_server:
ip = "192.168.48.1" #"172.16.44.141"
port = 6777
else:
ip = "172.16.55.127"
port = 55555
# my server: 172.16.44.141
# riley's server: 172.16.55.127
#ip = "172.16.44.141" #socket.gethostbyname(socket.gethostname()) #"127.0.0.1"
#port = 55555 #6777
client(ip, port, input("Username: "))