-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
session.py
40 lines (33 loc) · 1.14 KB
/
session.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
import time
import json
import hashlib
class Session:
KEEP_ALIVE_TIME = 75
def __init__(self, data):
self.ip = data['peer']['ip']
self.port = data['peer']['port']
self.user_agent = data['headers']['USER-AGENT']
self.uuid = data['uuid']
self.timestamp = time.time()
self.count = 1
self.paths = [{'path': data['path'], 'timestamp': time.time()}]
def update_session(self, path):
self.timestamp = time.time()
self.count += 1
self.paths.append({'path': path, 'timestamp': time.time()})
def is_expired(self):
exp_time = self.timestamp + self.KEEP_ALIVE_TIME
if (time.time() - exp_time > 0):
return True
def to_json(self):
s = dict(peer=dict(ip=self.ip, port=self.port),
user_agent=self.user_agent,
uuid=self.uuid,
timestamp=self.timestamp,
count=self.count,
paths=self.paths
)
return json.dumps(s)
def get_key(self):
bstr = (self.ip + self.user_agent).encode('utf-8')
return hashlib.md5(bstr).digest()