-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.py
75 lines (60 loc) · 2.06 KB
/
db.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
from time import time
import dataset
import config
db = dataset.connect(f'sqlite:///{config.database_fn}')
claim_table = db.create_table('claims')
ip_table = db.create_table('ip_claims')
def check_ip(request_ip):
# count the claims from this ip within claim_timespan
# fail if ip_claims_per exceeded
# note multiple users may share same ip
now = int(time())
time_limit = now-config.claim_timespan
if ip_table.count() == 0:
print("ip_table empty, ip validation not active")
return True
try:
query_result = db.query(f'SELECT COUNT(ip) FROM ip_claims WHERE claimtime > {time_limit}')
except Exception as e:
if config.debug:
print(e)
else:
print("Error querying ip_claims table")
return True
for c in query_result:
count_result = c
count_claims = count_result['COUNT(ip)']
if config.debug:
print(f'{count_claims} from IP {request_ip} in past {config.claim_timespan/(60*60)} hours')
if count_claims > config.ip_claims_per:
return False
else:
return True
def check_claims(col,key):
# check database for previous claims using address or cookie
if config.debug: print(f"search by {col}")
now = int(time())
if col == "address":
matches = claim_table.find(address=key,order_by='-claimtime')
else:
matches = claim_table.find(cookie=key,order_by='-claimtime')
for match in matches:
if config.debug:
print(match)
if int(match['claimtime']) > now-config.claim_wait:
if config.debug:
print(f"{int(match['claimtime'])} > {now} - {config.claim_wait}")
return False
return True
def update_claimtime(request_ip,address="",cookie=""):
try:
db.begin()
ip_table.insert(dict(ip=request_ip, claimtime=int(time())),ensure=True)
claim_table.upsert(dict(address=address, cookie=cookie, claimtime=int(time())), ['address','cookie'],ensure=True)
db.commit()
return True
except Exception as e:
if config.debug:
print(e)
db.rollback()
return False