Add token-system

main
t 1 year ago
parent 83b43cdd5b
commit 6abc4c4691

@ -7,7 +7,7 @@ import time
# Easy debug
from icecream import ic
# ic.disable() # Turn off
#ic.disable() # Turn off
# Limit for requests
LTIME = cur_time = time.monotonic()
@ -23,6 +23,33 @@ def kbv(dict_, value):
from listwork import *
# Work with tokens
import json
global tokens
def tokens_load():
with open('tokens.json', 'r') as openfile:
return json.load(openfile)
# (Re)generate tokens
def tokens_regen():
import hashlib
from random import randint as ri
tokens = tokens_load()
tokens["admin"] = hashlib.sha256(str.encode( str(ri(2443, 6543)) + tokens["secret"] )).hexdigest()
tokens["premium"] = []
for i in range(10):
tokens["premium"].append( hashlib.sha256(str.encode( str(ri(2443, 6543)) + tokens["secret"] )).hexdigest() )
js = json.dumps(tokens, indent=2)
with open("tokens.json", "w") as outfile:
outfile.write(js)
# Uncomment to regen
#tokens_regen()
class RequestHandler(BaseHTTPRequestHandler):
MATRIX_SIZE = (720, 1280)
@ -76,10 +103,23 @@ class RequestHandler(BaseHTTPRequestHandler):
params = parse_qs(body.decode('utf-8'))["main"][0]
ic(params)
# Parse token
ic( parse_qs(body.decode('utf-8')) )
token = parse_qs(body.decode('utf-8'))["token"][0]
#Set limit pixels for 1 response
ic(len(params))
if len(params) > 5685:
return 0
if len(params) > 6700:
# Get tokens
tokens = tokens_load()
# Admin's token
if token[0] == tokens["admin"][0]:
pass
elif token[0] in tokens["premium"] and len(params) < 10000:
pass
else:
return 0
matrix = self.get_matrix()

Loading…
Cancel
Save