-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
165 lines (145 loc) · 6.34 KB
/
Client.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
import socket
import sys
import time
import threading
import dAmn
# client instance
class Client(threading.Thread):
# constructor, create a new client instance.
def __init__(self, parent, sock, clientAddr):
'''(self, Server, Socket, ClientAddress) -> None
Create a new client instance and run it.'''
threading.Thread.__init__(self)
self.username = ''
self.realname = 'dAmnCaek Supporter'
self.typename = 'dAmnCaek Client'
self.gpc = 'guest'
self.symbol = '='
self.channels = []
self.server = parent #we will need this ref later
self.sock = sock
self.addr = clientAddr
self.loggedIn = False
self.running = True
self.lastPing = time.time()
self.lastPong = time.time()
self.bufferSize = 1 #it's easier lol
print('Client incoming: {0}'.format(self.addr))
self.run()
# checks if logged in
def loginCheck(self):
'''(self) -> None
Checks if user has logged in yet, if not, disconnect.'''
if False == self.loggedIn:
try:
self.disconnect('no login')
self.running = False
except:
self.running = False
# perform ping/pong activities
def pingPong(self):
'''(self) -> None
Checks how long it's been since last pong. If > 1m, disconnect. Else, ping.'''
if time.time() - self.lastPong > 60:
self.disconnect('timed out')
else:
self.send('ping\n\0')
self.lastPing = time.time()
threading.Timer(60, self.pingPong, ()).start()
# run client instance
def run(self):
'''(self) -> None
Main loop for the client instance. Handles data IO.'''
if self.server.clientCount > self.server.maxConnections:
self.disconnect('server busy')
return
data = ''
threading.Timer(15, self.loginCheck, ()).start()
while self.running:
try:
buffer = self.sock.recv(self.bufferSize)
if buffer:
try:
data += buffer.decode("utf-8")
if data.endswith('\0'):
self.process(data)
data = ''
except:
print('Something went wrong while trying to read data.')
self.disconnect('bad data')
self.sock.close()
self.running = False
else:
self.sock.close()
self.running = False
except:
print('Client error, killing thread.')
self.running = False
print('Client disconnected: {0}'.format(self.addr))
self.server.clientCount -= 1
if self.loggedIn and self.username.lower() in self.server.clients: #bloody well should be
del self.server.clients[self.username.lower()]
# send data to the remote client
def send(self, data):
'''(self, str) -> None
Sends the specified string packet to the remote client.'''
try:
self.sock.sendall(bytes(data, 'utf-8'))
except:
self.running = False
# disconnect client
def disconnect(self, reason):
'''(self, str) -> None
Disconnects the client with the specified reason.'''
self.send('disconnect\ne={0}\n\0'.format(reason))
try:
self.sock.close()
self.running = False
except:
self.running = False
# process a packet
def process(self, data):
'''(self, str) -> None
Processes the specified string packet and performs actions accordingly.'''
packet = dAmn.Packet()
packet.parse(data)
if packet.command == 'dAmnClient':
if packet.parameter == '0.3':
self.send('dAmnServer 0.3\n\0')
else:
self.disconnect('unsupported dAmnClient version')
elif packet.command == 'login':
if len(packet.parameter) > 0:
self.username = packet.parameter
#TODO: Check authtoken!
#something like if packet.arguments['pk'] == sometokenfromthedb:
if self.username.lower() == 'core':
self.disconnect('invalid username')
elif self.username.lower() in self.server.clients:
self.disconnect('too many connections')
else:
print('Client {0} has logged in as {1}'.format(self.addr, self.username))
self.server.clients[self.username.lower()] = self
self.loggedIn = True
self.send('login {0}\ne=ok\n\nsymbol={1}\nrealname={2}\ntypename={3}\ngpc={4}\n\0'.format(self.username, self.symbol, self.realname, self.typename, self.gpc))
threading.Timer(10, self.pingPong, ()).start()
else:
self.disconnect('no login')
elif packet.command == 'pong':
self.lastPong = time.time()
elif packet.command == 'join':
if packet.parameter.startswith('chat:'):
chan = packet.parameter[5:].lower()
if chan not in self.server.channels:
self.send('join {0}\ne=chatroom doesn\'t exist\n\0'.format(packet.parameter))
else:
self.channels.append(chan)
self.send('join {0}\ne=ok\n\0'.format(packet.parameter))
self.send('property {0}\np=title\nby=core\nts=0\n\nthis is a title\n\0'.format(packet.parameter))
self.send('property {0}\np=topic\nby=core\nts=0\n\nthis is a topic\n\0'.format(packet.parameter))
self.send('property {0}\np=privclasses\n\n1=Guests\n\0'.format(packet.parameter))
self.send('property {0}\np=members\n\nlol\n\0'.format(packet.parameter))
else:
self.send('join {0}\ne=chatroom doesn\'t exist\n\0'.format(packet.parameter))
else:
print('Unknown packet type: {0}'.format(packet.command))