-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewTCPserver_Test.py
312 lines (253 loc) · 12.6 KB
/
NewTCPserver_Test.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import logging, socketserver, os, pickle, socket, threading, select, time, traceback
os.system("title Server")
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
# logging system
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s %(name)-9s %(levelname)-7s %(message)s",
datefmt="%I:%M:%S %p",
filename='server.log',
filemode="w"
)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)-9s %(levelname)-8s %(message)s", datefmt="%H:%M:%S")
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
from BinaryEncodings import BE
from Player import Player
from Connecting import Connecting
from AI import Clock
class RequestHandler(socketserver.BaseRequestHandler):
# "Users" contains all users playing by using a dictionary system where the
# key is the User's address and the value is their player object.
USERS = {}
ADMINS = {}
# "Connecting" is a dictionary of all users who are in the process of
# connecting to the server where the key is their address and the value is
# the Connecting object.
CONNECTING = {}
# This makes it possible to send all the clients a broadcast
REQUESTS = []
# This lets one IP address run multiple clients if True
ALLOW_MULTIPLE_CONNECTIONS = True
def __init__(self, request, client_address, server):
self.logger = logging.getLogger('RHandler')
self.logger.debug('__init__')
self.address = client_address
self.request = request
RequestHandler.REQUESTS.append(self.request)
socketserver.BaseRequestHandler.__init__(self, request, client_address, server)
return
def setup(self):
self.logger.debug('setup')
return socketserver.BaseRequestHandler.setup(self)
def handle(self):
self.logger.debug('handle')
# Respond
while True:
try:
data = self.request.recv(1024)
except:
return
# unpack the message
dataType, data = BE.unpack(data)
self.logger.debug('Server received a %s type message.', dataType)
self.logger.debug('Server received: "%s"', data)
# RESPONSE will contain the string to be sent to the address.
RESPONSE = ""
if dataType == "DIS":
return
elif self.address in RequestHandler.USERS:
RESPONSE = RequestHandler.USERS[self.address].interpret(data)
self.logger.debug("Sender: %s at %s", RequestHandler.USERS[self.address], self.address)
self.logger.debug("Received: %s", data)
self.logger.debug("Replying with: %s", RESPONSE)
elif self.address in RequestHandler.CONNECTING:
if RequestHandler.CONNECTING[self.address].get_status() == "UNKNOWN":
if data.lower() == "load":
RequestHandler.CONNECTING[self.address].set_status("LOAD")
RESPONSE = ("Okay.\nWhat is your username?")
elif data.lower() == "new":
RequestHandler.CONNECTING[self.address].set_status("NEW")
RESPONSE = ("Okay.\nWhat would you like your username to be?")
else:
RESPONSE = ("I didn't catch that.\nType 'load' or 'new' to proceed.")
elif RequestHandler.CONNECTING[self.address].get_status() == "LOAD":
RESPONSE = RequestHandler.CONNECTING[self.address].load(data)
elif RequestHandler.CONNECTING[self.address].get_status() == "NEW":
RESPONSE = RequestHandler.CONNECTING[self.address].new(data)
if RequestHandler.CONNECTING[self.address].get_status() == "CONNECTED":
RequestHandler.USERS[self.address] = RequestHandler.CONNECTING[self.address].generate(self.request)
self.logger.info('[+] User "%s" has joined at %s', str(RequestHandler.USERS[self.address]), self.address)
del RequestHandler.CONNECTING[self.address]
elif dataType == "CON":
if ((any([key[0] == self.address[0] for key in RequestHandler.USERS.keys()]) or
any([key[0] == self.address[0] for key in RequestHandler.CONNECTING.keys()])) and
not RequestHandler.ALLOW_MULTIPLE_CONNECTIONS):
self.logger.warn("[!] Client at %s is attempting to connect multiple times.", self.address)
RESPONSE = ("You are already connected.")
return
else:
self.logger.info("[+] New client connecting at: %s", self.address)
RequestHandler.CONNECTING[self.address] = Connecting()
RESPONSE = ("Welcome to the server!\nType 'load' or 'new' to proceed.")
# Admin cases below which probably won't be used much longer.
elif data == "ADMIN LOGIN 42":
self.logger.info("[^] Admin connecting at: %s", self.address)
RequestHandler.ADMINS[self.address] = Player("Admin " + str(len(RequestHandler.ADMINS)+1))
RESPONSE = ("Welcome, admin!")
elif self.address in RequestHandler.ADMINS:
if data == "SHUTDOWN":
RESPONSE = ("Shutting down server!")
self.logger.warn("[^] Shutting down!")
SHUTDOWN_RESTART = "SHUTDOWN"
elif data == "RESTART":
RESPONSE = ("Restarting server!")
self.logger.warn("[^] Restarting!")
SHUTDOWN_RESTART = "RESTART"
self.logger.info("[^] Executing adminitrator command: %s", data)
try:
exec(data)
RESPONSE = ("It has been done.")
self.logger.info("[^] SUCCESS")
except Exception as errorMSG:
RESPONSE =("Erm... That didn't go as planned:\n"+str(errorMSG))
self.logger.warn("[^] FAILURE")
else:
self.logger.warn("[!] Something fishy coming from %s.", self.address)
self.logger.warn(' They sent "%s"', data)
RESPONSE = ("Modded Client? Want the ban hammer?")
# RESPOND
if RESPONSE is not None: # If nothing needs to be sent, the response can always just be None and the loop will iterate.
self.request.send(BE.MESSAGE + bytes(RESPONSE, "utf-8"))
def finish(self):
if self.address in RequestHandler.USERS:
self.logger.info("[-] User %s:%s disconnected.", RequestHandler.USERS[self.address], self.address)
RequestHandler.USERS[self.address].destruct()
del RequestHandler.USERS[self.address]
elif self.address in RequestHandler.ADMINS:
self.logger.info("[-] Admin %s:%s disconnected.", RequestHandler.ADMINS[self.address], self.address)
RequestHandler.USERS[self.address].destruct()
del RequestHandler.ADMINS[self.address]
elif self.address in RequestHandler.CONNECTING:
self.logger.info("[-] Client %s disconnected.", self.address)
del RequestHandler.CONNECTING[self.address]
RequestHandler.REQUESTS.remove(self.request)
return socketserver.BaseRequestHandler.finish(self)
class Server(socketserver.ThreadingTCPServer):
def __init__(self, server_address, handler_class=RequestHandler):
self.logger = logging.getLogger('Server')
self.logger.debug('__init__')
socketserver.ThreadingTCPServer.allow_reuse_address = False
socketserver.ThreadingTCPServer.__init__(self, server_address, handler_class)
self.__is_shut_down = threading.Event()
self.__shutdown_request = False
return
def server_activate(self):
self.ip, self.port = self.server_address
self.logger.info('Started server on %s:%s', self.ip, self.port)
socketserver.ThreadingTCPServer.server_activate(self)
# Start game clock
self.GAMECLOCK = Clock()
self.GAMECLOCK.start()
return
def serve_forever(self, poll_interval=0.5):
"""Handle one request at a time until shutdown.
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.
"""
self.logger.info('Online and waiting for requests.')
self.__is_shut_down.clear()
try:
while not self.__shutdown_request:
r, w, e = select.select([self], [], [], poll_interval)
if self in r:
self.handle_request()
finally:
self.__shutdown_request = False
self.__is_shut_down.set()
"""
The below functions are not necessary for server function and only serve
as placeholders in case I ever need to override any of them.
"""
# def handle_request(self):
# self.logger.debug('Waiting for requests.')
# return socketserver.ThreadingTCPServer.handle_request(self)
# def verify_request(self, request, client_address):
# self.logger.debug('verify_request(%s, %s)', request, client_address)
# return socketserver.ThreadingTCPServer.verify_request(self, request, client_address)
# def process_request(self, request, client_address):
# self.logger.debug('process_request(%s, %s)', request, client_address)
# return socketserver.ThreadingTCPServer.process_request(self, request, client_address)
# def finish_request(self, request, client_address):
# self.logger.debug('finish_request(%s, %s)', request, client_address)
# return socketserver.ThreadingTCPServer.finish_request(self, request, client_address)
# def close_request(self, request):
# self.logger.debug('close_request(%s)', request)
# return socketserver.ThreadingTCPServer.close_request(self, request)
def shutdown(self, t):
"""Stops the serve_forever loop.
Blocks until the loop has finished. This must be called while
serve_forever() is running in another thread, or it will
deadlock.
"""
if t != 0:
self.logger.warn('[!] Server shutdown in %s seconds!', t)
time.sleep(t)
self.__shutdown_request = True
self.__is_shut_down.wait()
self.logger.warn('[!] Server going down!')
self.GAMECLOCK.stop()
def start_server():
address = ('0.0.0.0', 65053) # let the kernel give us a port
server = Server(address, RequestHandler)
ip, port = server.server_address # find out what port we were given
t = threading.Thread(target=server.serve_forever)
t.setDaemon(True) # don't hang on exit
t.start()
return server, t
def broadcast(message):
"""
Sends a message to all connected clients.
"""
for request in RequestHandler.REQUESTS:
request.send(bytes(message, "utf-8"))
# Build the interface so commands can be run from the server window.
class Injection(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.logger = logging.getLogger("Injector")
def launch(self):
self.serverObj, self.serverThread = start_server()
def shutdown(self, t=0):
self.serverObj.shutdown(t)
def restart(self):
self.shutdown()
self.launch()
def run(self):
self.logger.info("Injector: Online")
while True:
try:
command = input()
if command.lower() =="":
continue
elif command.lower() in ["launch","start"]:
self.launch()
elif command.lower() == "restart":
self.restart()
elif command.lower().split()[0] in ["shutdown","quit","stop"]:
try:
self.shutdown(int(command.lower().split()[1]))
except IndexError:
self.shutdown()
else:
exec(command)
except KeyboardInterrupt:
exit(0)
except Exception:
print(traceback.format_exc())
Injector = Injection()
Injector.setDaemon(False)
Injector.start()