From 77bb2b7ceb58dfbee9d712326f23db09d7389b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Wed, 2 Aug 2023 11:23:20 +0200 Subject: [PATCH] templates: add ip_in_networks test --- src/capport/api/app.py | 1 + src/capport/api/template_filters.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/capport/api/template_filters.py diff --git a/src/capport/api/app.py b/src/capport/api/app.py index cca81b7..9b78a1b 100644 --- a/src/capport/api/app.py +++ b/src/capport/api/app.py @@ -7,4 +7,5 @@ app = MyQuartApp(__name__) __import__('capport.api.setup') __import__('capport.api.proxy_fix') __import__('capport.api.lang') +__import__('capport.api.template_filters') __import__('capport.api.views') diff --git a/src/capport/api/template_filters.py b/src/capport/api/template_filters.py new file mode 100644 index 0000000..4acaa0c --- /dev/null +++ b/src/capport/api/template_filters.py @@ -0,0 +1,16 @@ +from __future__ import annotations + +import ipaddress +import typing + +from .app import app + + +@app.add_template_test +def ip_in_networks(ip_s: str, networks: typing.Iterable[str]) -> bool: + ip = ipaddress.ip_address(ip_s) + for network in networks: + net = ipaddress.ip_network(network) + if ip in net: + return True + return False