-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
executable file
·233 lines (184 loc) · 7.48 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python2
# This script implements the server used for CS Games 2014 DCI's Qualification.
# See https://github.com/dciets/qualif_csgames_2014
#
# Undocumented code and the 2 security holes are part of the challenge ;)
import sqlite3
import socket
import threading
import SocketServer
import traceback
import uuid
import logging
import signal
import sys
from simple_websocket_server import WebSocket, WebSocketServer
SERVE_WEBSOCKET = True
TCP_PORT = 31337
WEBSOCKET_PORT = 31338
HOST = '0.0.0.0'
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
def extract_metadata(msg):
metadata = {}
content = msg
if msg[0] == "[" and msg.find("]") != -1:
content = msg[msg.find("]") + 1:].strip()
metadata_str = msg[1:msg.find("]")]
#dict and list comprehension are so sexy
metadata = {s[0].strip():s[1].strip() for s in [i.split("=") for i in metadata_str.split(",")]}
return (metadata, content)
def extract_credential(msg):
return map(str.strip, msg.split(' ')[1:])
class ChatServer(object):
__metaclass__ = Singleton
clients = dict()
def __init__(self):
self.db_lock = threading.Lock()
def register(self, client, creds):
username, pwd = creds
with self.db_lock:
db_conn = sqlite3.connect('user.db')
# Check existing user
c = db_conn.cursor()
c.execute("SELECT * FROM users WHERE username='%s'" % username)
user = c.fetchone()
if user is not None:
self.send_error('User "%s" already exist' % username, client)
db_conn.close()
return
# Insert new user
c.execute("INSERT INTO users VALUES('%s', '%s')" % tuple(creds))
db_conn.commit()
db_conn.close()
self.send("user-registered '%s'" % username, client)
def authenticate(self, client, creds):
# Validate credential
with self.db_lock:
db_conn = sqlite3.connect('user.db')
c = db_conn.cursor()
c.execute("SELECT * FROM users WHERE username='%s' AND pwd='%s'" % tuple(creds))
user = c.fetchone()
db_conn.close()
if user is None:
self.send_error('Invalid credential', client)
return
username, pwd = creds
logging.debug('auth %s %s' % (username, pwd))
if username in self.clients:
self.clients[username].close()
self.send_all('user-disconnect ' + username)
self.clients[username] = client
client.is_auth = True
client.username = username
self.send("user-auth '%s'" % client.username, client)
self.send_all("user-connect '%s'" % client.username)
logging.info("Client '%s' connecting (%d clients connected)" % (client.username, len(self.clients)))
def disconnect(self, client):
if not client.was_closed and client.username in self.clients:
del self.clients[client.username]
client.request.close()
self.send_all('user-disconnect ' + client.username)
logging.info("Client '%s' disconnecting (%d clients connected)" % (client.username, len(self.clients)))
def send_all(self, msg, src=None, metadata={}):
for client in self.clients.values():
if not client is src:
self.send(msg, client, src, metadata)
def send_to(self, msg, dst, src=None, metadata={}):
self.send(msg, self.clients[dst], src, metadata)
def send_error(self, error, dst):
self.send("error %s" % error, dst, None)
def send(self, msg, dst, src=None, metadata={}):
try:
metadata["src"] = src.username if src else "server"
metadata_stamp = "[" + ",".join(map(lambda p: "%s=%s" % p, metadata.iteritems())) + "] "
dst.request.sendall(metadata_stamp + msg)
except:
self.disconnect(dst)
def handle_msg(self, msg, src):
if not msg:
return
logging.debug('msg: %s', msg)
metadata, content = extract_metadata(msg)
if src.is_auth and "dst" not in metadata:
self.send_all(content, src, metadata)
elif "dst" in metadata:
if metadata["dst"] in self.clients:
self.send_to(content, metadata["dst"], src, metadata)
if metadata["dst"] == "server":
self.handle_command(content, src)
elif src.is_auth:
self.send_error("User '%s' is not connected" % metadata["dst"], src)
else:
self.send_error("You must be authenticated to send message", src)
def handle_command(self, cmd, src):
if src.is_auth and cmd == "list":
self.send(" ".join(self.clients.keys()), src, metadata={"type":"user_list"})
elif cmd.startswith(("register", "auth")) and not src.is_auth:
creds = extract_credential(cmd)
if len(creds) != 2:
self.send_error("Invalid parameter count. %d given, expecting 2" % len(creds), src)
return
if cmd.startswith("register"):
self.register(src, creds)
else:
self.authenticate(src, creds)
else:
self.send_error("Unknown command: '%s'" % cmd, src)
class ClientHandler(SocketServer.BaseRequestHandler):
def setup(self):
self.server = ChatServer()
self.was_closed = False
self.username = str(uuid.uuid4())
self.is_auth = False
def handle(self):
try:
while not self.was_closed:
msg = self.request.recv(1048576)
if not msg:
break
logging.debug("Msg received: %s" % msg)
self.server.handle_msg(msg.strip(), self)
except:
pass
self.server.disconnect(self)
def close(self):
logging.info("close %s connection" % self.username)
self.request.shutdown(socket.SHUT_RDWR)
self.request.close()
self.was_closed = True
class WebSocketClientHandler():
def setup(self):
self.server = ChatServer()
self.was_closed = False
self.username = str(uuid.uuid4())
self.is_auth = False
def onmessage(self, msg):
self.server.handle_msg(msg.strip(), self)
def onclose(self):
self.server.disconnect(self)
def close(self):
logging.info("close %s websocket connection" % self.username)
self.request.client.shutdown(socket.SHUT_RDWR)
self.was_closed = True
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
if SERVE_WEBSOCKET:
server = WebSocketServer(HOST, WEBSOCKET_PORT, WebSocketClientHandler)
server_thread = threading.Thread(target=server.listen, args=[5])
server_thread.start()
# Add SIGINT handler for killing the threads
def signal_handler(signal, frame):
logging.info("Caught Ctrl+C, shutting down...")
server.running = False
sys.exit()
signal.signal(signal.SIGINT, signal_handler)
server = ThreadedTCPServer((HOST, TCP_PORT), ClientHandler)
logging.info("Listening TCP on %s" % TCP_PORT)
server.serve_forever()