/
util.py
63 lines (46 loc) · 1.45 KB
/
util.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 email.utils import parseaddr
from urllib.parse import urlparse as _parse
from re import compile as _compile
from time import time as _time
# Should I verify with the MX records?
def validate_email_address(email_id: str) -> str:
if "@" in parseaddr(email_id)[1]:
return email_id
raise Exception("Invalid Email")
def add_cookie(data: dict):
return {"cookies": data}
def get_host(request):
get = request.headers.get
form = request.form or {}
orig = form.get("x-halocrypt-origin")
return (
orig
or get("Origin")
or get("x-halocrypt-origin")
or get("Host")
or request.args.get("crs", "")
)
def is_prod(request) -> bool:
t = get_host(request)
return "localhost" not in t
def map_to_list(*args) -> list:
return list(map(*args))
def safe_int(idx) -> int:
return int(idx) if not isinstance(idx, int) else idx
# maybe only strip whitespace?
_sub = _compile(r"([^\w]|_)").sub
sanitize = lambda x: _sub("", x).strip().lower()
js_time = lambda: _time() * 1e3
def get_client_ip(headers: dict, remote_addr: str) -> str:
try:
xfwd = headers.get("x-forwarded-for", remote_addr)
if not xfwd:
return ""
arr = list(filter(bool, map(lambda x: x.strip(), xfwd.split(","))))
if len(arr) == 1:
return arr[0]
num_proxies = 1
real_ip = (arr)[-(1 + num_proxies)]
return real_ip
except:
return ""