-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_registry.py
executable file
·386 lines (300 loc) · 11.4 KB
/
server_registry.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import os
import sys
from log import *
import logging
import re
import json
import time
MBOXES_PATH = "mboxes"
RECEIPTS_PATH = "receipts"
DESC_FILENAME = "description"
class UserDescription(dict):
def __init__(self, uid, description=None):
dict.__init__(self, id=uid, description=description)
self.id = uid
self.description = description
class ServerRegistry:
def __init__(self):
self.users = {}
self.clientsInfo = {}
for dirname in [MBOXES_PATH, RECEIPTS_PATH]:
try:
if not os.path.exists(dirname):
logging.debug("Creating " + dirname)
os.mkdir(dirname)
except:
logging.exception("Cannot create directory " + dirname)
sys.exit(1)
for entryname in os.listdir(MBOXES_PATH):
logging.info("Found " + entryname)
if os.path.isdir(os.path.join(MBOXES_PATH, entryname)):
uid = 0
try:
uid = int(entryname)
except:
continue
logging.info("Loading " + entryname)
path = os.path.join(MBOXES_PATH, entryname, DESC_FILENAME)
description = None
try:
with open(path) as f:
description = json.loads(f.read())
except:
logging.exception(
"Cannot load user description from " + path)
sys.exit(1)
self.users[uid] = UserDescription(uid, description)
def saveOnFile(self, path, data):
with open(path, "w+") as f:
f.write(data)
def readFromFile(self, path):
log(logging.DEBUG, "Read from file: " + path)
with open(path, "r") as f:
return f.read()
def messageWasRed(self, uid, msg):
msg = str(msg)
if msg.startswith("_"):
return os.path.exists(os.path.join(self.userMessageBox(uid), msg))
else:
return os.path.exists(os.path.join(self.userMessageBox(uid), "_" + msg))
def messageExists(self, uid, message):
return os.path.exists(os.path.join(self.userMessageBox(uid), message))
def copyExists(self, uid, message):
return os.path.exists(os.path.join(self.userReceiptBox(uid), message))
def userExists(self, uid):
return self.getUser(uid) is not None
def getUser(self, uid):
if isinstance(uid, int):
if uid in self.users.keys():
return self.users[uid]
return None
if isinstance(uid, str):
for user in users:
if user.id == uid:
return user
return None
def addUser(self, description):
uid = 1
while self.userExists(uid):
uid += 1
if 'type' in description.keys():
del description['type']
user = UserDescription(uid, description)
self.users[uid] = user
for path in [self.userMessageBox(uid), self.userReceiptBox(uid)]:
try:
os.mkdir(path)
except:
logging.exception("Cannot create directory " + path)
sys.exit(1)
path = ""
try:
path = os.path.join(MBOXES_PATH, str(uid), DESC_FILENAME)
log(logging.DEBUG, "add user description " + path)
self.saveOnFile(path, json.dumps(description))
except:
logging.exception("Cannot create description file " + path)
sys.exit(1)
return user
def updateUser(self, idd, description):
if 'type' in description.keys():
del description['type']
user = UserDescription(idd, description)
self.users[idd] = user
path = ""
try:
path = os.path.join(MBOXES_PATH, str(idd), DESC_FILENAME)
log(logging.DEBUG, "update user description " + path)
self.saveOnFile(path, json.dumps(description))
return True
except:
logging.exception("Cannot update description file " + path)
return False
return False
def listUsers(self, uid):
if uid != 0:
user = self.getUser(uid)
if user is not None:
return [user]
return None
userList = []
for k in self.users.keys():
userList.append({
'id': k,
'description' : self.users[k].description,
}) # retorna id, not uuid
return userList
def userDirExists(self,uuid):
for key in self.users.keys():
description= self.users[key]['description']
if description['uuid']==uuid:
return True
return False
def getMyUUID(self,id):
uuid = 0
for k in self.user.keys():
if k == id:
uuid = self.users[k].description["uuid"]
return uuid
def getMyId(self, data):
uuid = data['id']
userList = []
for k in self.users.keys():
if self.users[k].description["uuid"] == uuid:
userList.append(self.users[k].id)
return userList
def getId(self, uuid):
id = 0
for k in self.users.keys():
if self.users[k].description["uuid"] == uuid:
id = self.users[k].id
return id
def updatePublicKey(self, uuid, publicKey):
for k in self.users.keys():
if self.users[k].description["uuid"] == uuid:
description = self.users[k].description
# changing
description["Client_pubKey"] = publicKey
userid = k
self.updateUser(k, description)
return True
return False
def checkPassphrase(self, uuid, passphrase):
for k in self.users.keys():
if self.users[k].description["uuid"] == uuid:
stored_pw = self.users[k].description["passphrase"]
if stored_pw == passphrase:
return True
return False
def getUserCertificate(self, uuid, mode):
for k in self.users.keys():
if self.users[k].description['uuid'] == uuid:
if mode == 'AUTHENTICATION':
cert = self.users[k].description['auth_certificate']
return cert
if mode == 'SIGNATURE':
cert = auth_cert = self.users[k].description['sign_certificate']
return cert
return
def userAllMessages(self, uid):
return self.userMessages(self.userMessageBox(uid), "_?[0-9]+_[0-9]+")
def userNewMessages(self, uid):
return self.userMessages(self.userMessageBox(uid), "[0-9]+_[0-9]+")
def userSentMessages(self, uid):
return self.userMessages(self.userReceiptBox(uid), "[0-9]+_[0-9]+")
def userMessages(self, path, pattern):
log(logging.DEBUG, "Look for files at " +
path + " with pattern " + pattern)
messageList = []
if not os.path.exists(path):
return []
try:
for filename in os.listdir(path):
log(logging.DEBUG, "\tFound file " + filename)
if re.match(pattern, filename):
messageList.append(filename)
except:
logging.exception(
"Error while listing messages in directory " + path)
return messageList
def newFile(self, basename):
i = 1
while True:
#path = os.path.join(basename, str(i))
path = basename + str(i)
if not os.path.exists(path):
return str(i)
i += 1
def sendMessage(self, src, dst, msg, receipt):
nr = "0"
src = str(src)
dst = str(dst)
try:
path = os.path.join(self.userMessageBox(dst), src + "_")
path2 = os.path.join(self.userMessageBox(dst), "_" + src + "_")
nr1 = self.newFile(path)
nr2 = self.newFile(path2)
nr = max(nr1, nr2)
self.saveOnFile(path + nr, msg)
result = [src + "_" + nr]
path = os.path.join(self.userReceiptBox(src), dst + "_")
self.saveOnFile(path + nr, receipt)
except:
logging.exception(
"Cannot create message or receipt file " + path + nr)
return ["", ""]
result.append(dst + "_" + nr)
return result
def readMsgFile(self, uid, msg):
path = self.userMessageBox(uid)
if msg.startswith('_'):
path = os.path.join(path, msg)
else:
try:
f = os.path.join(path, msg)
path = os.path.join(path, "_" + msg)
log(logging.DEBUG, "Marking message " + msg + " as read")
os.rename(f, path)
except:
logging.exception("Cannot rename message file to " + path)
path = os.path.join(self.userMessageBox(str(uid)), msg)
return self.readFromFile(path)
def recvMessage(self, uid, msg):
uid = str(uid)
msg = str(msg)
result = []
pattern = "_?([0-9]+)_[0-9]+"
matches = re.match(pattern, msg)
if not matches:
log(logging.ERROR,
"Internal error, wrong message file name format!")
sys.exit(2)
result.extend(matches.group(1))
try:
result.append(self.readMsgFile(uid, msg))
except:
logging.exception("Cannot read message " +
msg + " from user " + uid)
result.append(msg) #numero mensagem
return result
def userMessageBox(self, uid):
return os.path.join(MBOXES_PATH, str(uid))
def userReceiptBox(self, uid):
return os.path.join(RECEIPTS_PATH, str(uid))
def storeReceipt(self, uid, msg, receipt):
pattern = re.compile("_?([0-9]+)_([0-9])")
m = pattern.match(msg)
if not m:
log(logging.ERROR,
"Internal error, wrong message file name (" + msg + ") format!")
sys.exit(2)
path = self.userReceiptBox(os.path.join(m.group(1), "_%s_%s_%d" % (uid, m.group(2), time.time() * 1000)))
try:
self.saveOnFile(path, receipt)
except:
logging.exception("Cannot create receipt file " + path)
def getReceipts(self, uid, msg):
pattern = re.compile("_(([0-9])+_[0-9])_([0-9]+)")
boxdir = self.userReceiptBox(uid)
result = {}
copy = ""
try:
path = os.path.join(self.userReceiptBox(uid), msg)
copy = self.readFromFile(path)
except:
logging.exception("Cannot read a copy file")
copy = ""
result = {"msg": copy, "receipts": []}
for fname in os.listdir(boxdir):
m = pattern.match(fname)
if m and m.group(1) == msg:
path = os.path.join(self.userReceiptBox(uid), fname)
try:
receiptText = self.readFromFile(path)
except:
logging.exception("Cannot read a receipt file")
receiptText = ""
receipt = {
"date": m.group(3), "id": m.group(2), "receipt": receiptText}
result['receipts'].append(receipt)
return result