Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
Latest commit
215 lines (169 loc) · 6.04 KB
/
Copy pathindex.py
File metadata and controls
215 lines (169 loc) · 6.04 KB
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
importasyncio
importwebsockets
importjson
importos
importsignal
importlogging
importtime
logfmt='%(asctime)s - %(levelname)s - %(message)s'
loghdlrs= [logging.FileHandler('index.log'), logging.StreamHandler()]
logging.basicConfig(level=logging.INFO, format=logfmt, handlers=loghdlrs)
logging.basicConfig(level=logging.ERROR, format=logfmt, handlers=loghdlrs)
logging.basicConfig(level=logging.CRITICAL, format=logfmt, handlers=loghdlrs)
ifos.getenv('IDEBUG') ==True:
logging.basicConfig(level=logging.DEBUG)
classWSS:
def__init__(self):
self.STATE=0
self.WSS_PORT=2112
self.UID_SOCK=dict()
self.ADDR_UID=dict()
asyncdefstart(self, stop):
asyncwithwebsockets.serve(self.ws, '0.0.0.0', self.WSS_PORT):
logging.info(f'Starting server on {self.WSS_PORT} port')
awaitstop
asyncdefcheckuser(self, user, asker, sock):
ifuserinself.ADDR_UID:
msg= {
'm': 'get',
'q': 'you',
'u': asker
}
jmsg=json.dumps(msg)
logging.info(f'> {jmsg}')
awaitself.UID_SOCK[self.ADDR_UID[user]].send(jmsg)
else:
res= {
'm': 'info',
'q': 'checkuser',
'u': user,
'c': 404,
}
jres=json.dumps(res)
logging.info(f'> {jres}')
awaitsock.send(jres)
asyncdefsenduser(self, asker, name, status):
ifaskerinself.UID_SOCK:
res= {
'm': 'info',
'q': 'checkuser',
'c': 200,
'res': {
'name': name,
'status': status
}
}
jres=json.dumps(res)
logging.info(f'> {jres}')
awaitself.UID_SOCK[asker].send(jres)
asyncdefsendmsg(self, addr, sender, ts, pack):
ifaddrinself.ADDR_UID:
msg= {
'm': 'put',
'q': 'msg',
'user': sender,
'pack': pack
}
jmsg=json.dumps(msg)
logging.info(f'> {jmsg}')
awaitself.UID_SOCK[self.ADDR_UID[addr]].send(jmsg)
c=200
#await asyncio.wait([self.USER_SOCK[_user].send(jmsg) for _user in self.PUB_USER[pub]])
else:
c=404
res= {
'm': 'info',
'q': 'sendmsg',
'd': ts,
'c': c,
'pack': pack
}
returnjson.dumps(res)
asyncdefcheckin(self, uid, sock, addr):
ifaddrinself.ADDR_UID:
c=409
else:
self.ADDR_UID.update({addr: uid})
c=201
res=f'{{"m": "info", "q": "checkin", "c": {c}}}'
logging.info(f'CHECKIN {addr}{uid}')
returnres
asyncdefcheckout(self, addr):
ifaddrinself.ADDR_UID:
self.ADDR_UID.pop(addr, None)
logging.info(f'CHECKOUT {addr}')
else:
pass
asyncdefws(self, SOCK, path=None):
UID=int(time.time() *1000)
self.UID_SOCK.update({UID: SOCK})
logging.info(f'OPEN CONNECTION FROM {UID}')
ADDR=str()
res= {
'm': "info",
'q': "srv",
'c': 100,
'res' : {
'pub': "123123123",
'uid': UID,
'online': len(self.UID_SOCK)
}
}
awaitSOCK.send(json.dumps(res))
try:
asyncforsockinSOCK:
#print(websocket.closed)
#print(websocket.open)
req=json.loads(sock)
logging.info(f'< {req}')
method=req['m']
query=req['q']
#print (locals())
ifmethod=='put':
ifquery=='user':
ADDR=req['pack']['addr']
res=awaitself.checkin(UID, SOCK, ADDR)
logging.info(f'> {res}')
awaitSOCK.send(res)
ifquery=='msg':
if'n'notinreq:
req['n'] ='Unknown'
if'd'notinreq:
req['d'] =int(time.time() *1000)
if'u'inreqand'pack'inreq:
res=awaitself.sendmsg(req['u'], req['n'], req['d'], req['pack'])
else:
res= {
'm': 'info',
'q': 'sendmsg',
'd': req['d'],
'c': 400
}
logging.info(f'> {res}')
awaitSOCK.send(res)
ifmethod=='get':
ifquery=='user':
res=awaitself.checkuser(req['u'], UID, SOCK)
ifmethod=='post':
ifquery=='user':
if'name'notinreq:
req['name'] ='Unknown'
if'status'notinreq:
req['status']='~'
res=awaitself.senduser(req['u'], req['name'], req['status'])
exceptExceptionase:
logging.error(f'{type(e).__name__}: {e}')
#cf01b7a83315db38b082e83cf7dbc1fb7fa73d9606f93211a13efba1ed510e07
finally:
self.UID_SOCK.pop(UID, None)
awaitself.checkout(ADDR)
logging.info(f'CLOSE CONNECTION FROM {UID}')
if__name__=='__main__':
try:
ws=WSS()
loop=asyncio.get_event_loop()
stop=asyncio.Future()
loop.add_signal_handler(signal.SIGTERM, stop.set_result, None)
server=loop.run_until_complete(ws.start(stop))
except (KeyboardInterrupt, SystemExit):
pass