72def is_suspicious(network: IPv4Network | IPv6Network, request: flask.Request, renew: bool =
False):
73 """Checks whether a valid ping is exists for this (client) network, if not
74 this request is rated as *suspicious*. If a valid ping exists and argument
75 ``renew`` is ``True`` the expire time of this ping is reset to
76 :py:obj:`PING_LIVE_TIME`.
79 redis_client = redisdb.client()
84 if not redis_client.get(ping_key):
85 logger.info(
"missing ping (IP: %s) / request: %s", network.compressed, ping_key)
89 redis_client.set(ping_key, 1, ex=PING_LIVE_TIME)
91 logger.debug(
"found ping for (client) network %s -> %s", network.compressed, ping_key)
95def ping(request: flask.Request, token: str):
96 """This function is called by a request to URL ``/client<token>.css``. If
97 ``token`` is valid a :py:obj:`PING_KEY` for the client is stored in the DB.
98 The expire time of this ping-key is :py:obj:`PING_LIVE_TIME`.
101 from .
import redis_client, cfg
108 real_ip = ip_address(get_real_ip(request))
109 network = get_network(real_ip, cfg)
112 logger.debug(
"store ping_key for (client) network %s (IP %s) -> %s", network.compressed, real_ip, ping_key)
113 redis_client.set(ping_key, 1, ex=PING_LIVE_TIME)
136 """Returns current token. If there is no currently active token a new token
137 is generated randomly and stored in the redis DB.
139 - :py:obj:`TOKEN_LIVE_TIME`
140 - :py:obj:`TOKEN_KEY`
143 redis_client = redisdb.client()
148 token = redis_client.get(TOKEN_KEY)
150 token = token.decode(
'UTF-8')
152 token =
''.join(random.choice(string.ascii_lowercase + string.digits)
for _
in range(16))
153 redis_client.set(TOKEN_KEY, token, ex=TOKEN_LIVE_TIME)