-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
105 lines (80 loc) · 2.88 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
import socketserver
import threading
import config
import buffer
import controller
from user import PersonUser
from log import logger
class OriginServer(object):
pass
class TCPServer(
socketserver.ThreadingMixIn,
socketserver.TCPServer
):
pass
class UDPServer(
socketserver.ThreadingMixIn,
socketserver.UDPServer
):
pass
class Handler(socketserver.BaseRequestHandler):
user = None
buffer = None
def handle(self):
self.buffer = buffer.Buffer(self.request)
ip = self.client_address[0]
self.user = PersonUser.get(ip, ip)
#print('TCP Connection:', self.user)
logger.debug(type='tcp', action='connection',
user=[self.user.ip, self.user.name],
content=None)
while True:
try:
content = self.read_command()
except buffer.SocketClosedError:
return
self.handle_content(content)
def read_command(self):
startbyte = None
while startbyte != controller.command_header:
startbyte = self.buffer.read(1, True)
size = self.buffer.read(1, True)
return self.buffer.read(size[0], True)
def handle_content(self, content):
orig_content = content
content = controller.find_command(content)
if content is None:
logger.debug(type='tcp', action='invalid_command',
user=[self.user.ip, self.user.name],
content=orig_content)
return
logger.debug(type='tcp', action='command',
user=[self.user.ip, self.user.name],
content=content, bytes=orig_content)
cmd, st = content
if st == controller.CommandType.text:
self.user.name = cmd # TODO change user name on interface too?
return
controller.controller_instance.send_command(content, user=self.user.name)
class UDPHandler(Handler):
def handle(self):
data, sock = self.request
if data == b'MAX-REMOTE':
logger.log(type='udp', action='handshake',
content=self.client_address)
else:
logger.log(type='udp', action='data',
content=repr(data))
sock.sendto(config.server_name, self.client_address)
def load(daemon=True):
host = config.parser.gete('server', 'addr')
port = config.parser.gete('server', 'port')
server_tcp = TCPServer((host, port), Handler)
server_udp = UDPServer((host, port), UDPHandler)
#print(host, ':', port, sep='')
logger.info(type='meta', action='server_listen',
content='{}:{}'.format(host, port))
threading.Thread(target=server_tcp.serve_forever, daemon=daemon).start()
threading.Thread(target=server_udp.serve_forever, daemon=daemon).start()
if __name__ == '__main__':
load(False)