diff --git a/conf_example.json b/conf_example.json new file mode 100644 index 0000000..6cc9e6f --- /dev/null +++ b/conf_example.json @@ -0,0 +1,7 @@ +{ + "^(?Ptata[0-9])(?P[0-9 ]+)$": "https://example.com/url_to_test1?ref={num}&ref2={num2}", + "^token([0-9]+)$": "https://test2.example.com/url_to_test2?ref={}", + "g +([0-9]+)": "https://localhost/url_to_test2?ref={}", + "a +([0-9a-z]+)": "https://192.168.2.3/url_to_test2?ref={}", + "(.*)": "https://www.qwant.com/?q={}" +} diff --git a/parser.py b/parser.py new file mode 100644 index 0000000..b062541 --- /dev/null +++ b/parser.py @@ -0,0 +1,46 @@ +import re +import json +import ipaddress +import readline +import socket +from urllib.parse import quote_plus as qp +from urllib.parse import urlsplit + +with open("conf.json", 'r') as jsonconf: + conf = json.load(jsonconf, + object_pairs_hook=lambda x: {re.compile(i[0], + flags=re.I): i[1] + for i in x}) + +terms = ["TOTO", "TITI", "TATA"] +rigidurl = "https://rigid.example.com/query?kwd={kwd}&val={val}" + +safe_domains = ['example.com'] + + +def get_redirect_url(key: str): + keywords = key.split() + if keywords and keywords[0].upper() in terms: + return rigidurl.format(kwd = qp(keywords[0]), + val = qp(" ".join(keywords[1:]))), True + for regex, substitute in conf.items(): + if (match := regex.fullmatch(key)) is not None: + url = substitute.format(*[qp(g) for g in match.groups()], + **{k:qp(v) for k, v + in match.groupdict().items()}) + break + netloc = urlsplit(url).netloc + try: + safe = (netloc in safe_domains + or any(dom.split('.') == + netloc.split('.')[-len(dom.split('.')):] + for dom in safe_domains) + or all(ipaddress.ip_address(a[4][0]).is_private + for a in socket.getaddrinfo(netloc, 0))) + except socket.gaierror: + safe = False + return url, safe + +if __name__ == "__main__": + while (key:= input("Keywords? ")) != "quit": + print(get_redirect_url(key=key))