2021-12-06 14:30:08 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
2021-12-16 15:22:09 +01:00
|
|
|
|
|
|
|
from datetime import date, timedelta
|
2021-12-06 14:30:08 +01:00
|
|
|
from functools import lru_cache
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-12-16 15:22:09 +01:00
|
|
|
import pycountry # type: ignore
|
|
|
|
|
|
|
|
from flask import request, session
|
|
|
|
|
2021-12-06 14:30:08 +01:00
|
|
|
from bgpranking.default import get_homedir
|
|
|
|
|
|
|
|
|
|
|
|
def src_request_ip(request) -> str:
|
|
|
|
# NOTE: X-Real-IP is the IP passed by the reverse proxy in the headers.
|
|
|
|
real_ip = request.headers.get('X-Real-IP')
|
|
|
|
if not real_ip:
|
|
|
|
real_ip = request.remote_addr
|
|
|
|
return real_ip
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache(64)
|
|
|
|
def get_secret_key() -> bytes:
|
|
|
|
secret_file_path: Path = get_homedir() / 'secret_key'
|
|
|
|
if not secret_file_path.exists() or secret_file_path.stat().st_size < 64:
|
|
|
|
if not secret_file_path.exists() or secret_file_path.stat().st_size < 64:
|
|
|
|
with secret_file_path.open('wb') as f:
|
|
|
|
f.write(os.urandom(64))
|
|
|
|
with secret_file_path.open('rb') as f:
|
|
|
|
return f.read()
|
2021-12-16 15:22:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def load_session():
|
|
|
|
if request.method == 'POST':
|
|
|
|
d = request.form
|
|
|
|
elif request.method == 'GET':
|
|
|
|
d = request.args # type: ignore
|
|
|
|
|
|
|
|
for key in d:
|
|
|
|
if '_all' in d.getlist(key):
|
|
|
|
session.pop(key, None)
|
|
|
|
else:
|
|
|
|
values = [v for v in d.getlist(key) if v]
|
|
|
|
if values:
|
|
|
|
if len(values) == 1:
|
|
|
|
session[key] = values[0]
|
|
|
|
else:
|
|
|
|
session[key] = values
|
|
|
|
|
|
|
|
# Edge cases
|
|
|
|
if 'asn' in session:
|
|
|
|
session.pop('country', None)
|
|
|
|
elif 'country' in session:
|
|
|
|
session.pop('asn', None)
|
|
|
|
if 'date' not in session:
|
|
|
|
session['date'] = (date.today() - timedelta(days=1)).isoformat()
|
|
|
|
|
|
|
|
|
|
|
|
def get_country_codes():
|
|
|
|
for c in pycountry.countries:
|
|
|
|
yield c.alpha_2, c.name
|