This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
uaServer.py
63 lines (49 loc) · 1.83 KB
/
uaServer.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
from peewee import fn
from sanic import Sanic
from sanic.log import logger
from sanic.response import json,redirect
from termcolor import colored
from contextlib import contextmanager
import time
@contextmanager
def checkTimes():
startTime = time.time()
yield
logger.info(
colored(f'cost times: [{str(round(round(time.time()-startTime,5)*1000,3)) }]ms', 'green'))
from config import conf
from FakeUAdb import UAS
app = Sanic()
@app.route('/')
def handel_request(request):
return redirect('/fakeua')
@app.route('/fakeua')
async def query_string(request):
with checkTimes():
args = request.args
query = UAS.select()
keywords = args.get('keywords', [''])[0][:16].lower()
if keywords:
query = query.where(UAS.useragent.in_(keywords))
engine = args.get('engine', [''])[0]
engine = engine.lower() if len(engine) < 10 and ''.join(
engine.split()).isalpha() else ''
if engine:
query = query.where(UAS.engine.in_(engine))
types = args.get('types', [''])[0]
types = types.lower() if len(types) < 10 else ''
if types:
query = query.where(UAS.types.in_(types))
software = args.get('software', [''])[0]
software = software.lower() if len(software) < 24 else ''
if software:
query = query.where(UAS.software.in_(software))
limit = args.get('limit', [conf.DEFAULT_LIMIT])[0]
limit = int(limit) if limit.isdigit() else conf.DEFAULT_LIMIT
limit = limit if limit < conf.MAX_LIMIT + 1 else conf.DEFAULT_LIMIT
counts = query.count()
# result = query.order_by(fn.Random()).limit(limit)
# return json({'total': counts, 'results': result})
return json({})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=conf.SERVER_PORT)