/
Server.py
197 lines (167 loc) · 5.84 KB
/
Server.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import socket
import threading
import time
from queue import Queue
import queue
import Message
class SockSendThread(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None):
super(SockSendThread, self).__init__(group=group, target=target, name=name)
(self.conn, self.c) = args
self.q = Queue()
def run(self):
while not self.c.quitf:
try:
message = self.q.get(block=False)
except queue.Empty:
time.sleep(0.1)
continue
try:
#print("Sent data on socket: " + str(message))
self.conn.send(message)
self.q.task_done()
except Exception as e:
if '35' in str(e):
pass
else:
print(e)
break
# came out of loop
self.conn.close()
def sock_recv_server(conn, c, recv_queue):
conn.setblocking(True)
while not c.quitf:
try:
# Receive bytes
data = conn.recv(4096)
if data:
recv_queue.put(data)
msg = Message.Message(data, decrypt=True, key=PASSWORD)
msg_author = msg.author.decode(encoding='utf-8')
msg_data = msg.data.decode(encoding='utf-8')
try:
print("Data received on socket:", msg_author, msg_data)
except Exception as e:
print(type(e))
print("you have the wrong password")
else:
continue
time.sleep(0.1)
except Exception as e:
if e.errno == 11:
pass
if e.errno == 35:
# BlockingIOError
print(e)
pass
if e.errno == 54 or e.errno == 9:
# Client has lost connection
print("Client has disconnected.")
break
else:
print("ERROR:", e)
break
# came out of loop
conn.close()
def sock_loop(s, c):
q = Queue()
send_threads = []
while not c.quitf:
# check the Queue for data(s) to send
while True:
# keep looping until the queue is empty
try:
rcv_msg = q.get_nowait()
for thread in send_threads:
thread.q.put(rcv_msg)
except queue.Empty:
# the queue has been emptied
break
# check the Carrier for data to send
if c.send_str:
for thread in send_threads:
thread.q.put(c.send_str)
c.send_str = None
# obtain a new socket connection
# wait to accept a connection - NON blocking call
try:
s.setblocking(False)
conn, addr = s.accept()
s.setblocking(True)
if conn and addr:
print(addr[0] + ':' + str(addr[1]) + " has connected.")
print("Authenticating sock...")
challenge = Message.random_chal()
conn.send(challenge)
print("Sent request...")
while True:
try:
response = conn.recv(32)
if response:
break
except BlockingIOError:
time.sleep(0.1)
if Message.enc_chal(challenge, PASSWORD) == response:
print("Client authenticated successfully.")
sock_recv_thread = threading.Thread(target=sock_recv_server, args=(conn, c, q))
sock_recv_thread.daemon = True
sock_recv_thread.start()
sock_send_thread = SockSendThread(args=(conn, c))
sock_send_thread.daemon = True
sock_send_thread.start()
send_threads.append(sock_send_thread)
conn.send(Message.Message('Welcome to the server.', author='Server').encrypt(PASSWORD))
else:
# Client cannot authenticate. Disconnect it
print("Client has incorrect password. Disconnecting client...")
conn.send(b'\mq')
conn.close()
except Exception as e:
if e.errno == 11:
# we don't got it yet
time.sleep(0.1)
pass
if e.errno == 35:
time.sleep(0.1)
pass
else:
print("ERROR:", e)
class Carrier:
def __init__(self):
self.quitf = False
self.send_str = None
def send(self, ssr):
self.send_str = ssr
# block until watching function toggles back string to None
while self.send_str:
pass
def load_config():
with open("config", mode='r') as config_file:
if config_file.readline().strip() == "[global]":
port = int(config_file.readline().split("=")[1].strip())
host = str(config_file.readline().split("=")[1].strip())
return port, host
HOST = ''
PORT = load_config()[0]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
print("Socket bind complete")
# Allow rebinding
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# Enable blocking
sock.setblocking(True)
sock.listen(10)
print("Socket now listening")
global PASSWORD
PASSWORD = Message.hash_password('chiken')
# Create the message queue
message_queue = Queue()
carr = Carrier()
# Start listening continuously
sock_thread = threading.Thread(target=sock_loop, args=(sock, carr))
sock_thread.daemon = True
sock_thread.start()
while True:
time.sleep(2)