From 3e248b0adf75b4617a13f975523dcc924483e2f5 Mon Sep 17 00:00:00 2001
From: Dmitry <b4tm4n@mail.ru>
Date: Sun, 25 Feb 2024 15:42:21 +0300
Subject: [PATCH] add GenericHttpRegexSource provider

---
 pddnsc/sources/httpregex.py | 33 +++++++++++++++++++++++++++++++++
 settings/config.toml        |  6 +++++-
 2 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 pddnsc/sources/httpregex.py

diff --git a/pddnsc/sources/httpregex.py b/pddnsc/sources/httpregex.py
new file mode 100644
index 0000000..99a93e1
--- /dev/null
+++ b/pddnsc/sources/httpregex.py
@@ -0,0 +1,33 @@
+import re
+
+from pddnsc.sources.http import GenericHttpSource
+
+
+class GenericHttpRegexSource(GenericHttpSource):
+    """Базовый провайдер получения IP адресов по http/https ссылкам в виде текста c помощью регулярных выражений
+
+    Конфигурация:
+
+      - url_v4: *URL* для получения адреса *IPv4*
+      - url_v6: *URL* для получения адреса *IPv6*
+      - regex_v4: регулярное выражение для *IPv4*, значение по умолчанию см. в коде
+      - regex_v6: регулярное выражение для *IPv6*, значение по умолчанию см. в коде
+      - headers (`dict`): словарь дополнительных заголовков (*необязательно*)
+    """
+
+    def post_init(self):
+        super().post_init()
+        rx_v4 = r"\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
+        rx_v6 = r"\b(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\b"
+        self.regex_v4 = re.compile(self.config.get("regex_v4", rx_v4))
+        self.regex_v6 = re.compile(self.config.get("regex_v6", rx_v6))
+
+    def filter_ipv4(self, ipv4):
+        match = self.regex_v4.search(ipv4 or "")
+        ipv4 = match and match[0]
+        return super().filter_ipv4(ipv4)
+
+    def filter_ipv6(self, ipv6):
+        match = self.regex_v6.search(ipv6 or "")
+        ipv6 = match and match[0]
+        return super().filter_ipv6(ipv6)
diff --git a/settings/config.toml b/settings/config.toml
index 5dc081f..40cb8d0 100644
--- a/settings/config.toml
+++ b/settings/config.toml
@@ -1,4 +1,4 @@
-debug = true
+debug = false
 require_ipv4 = true
 
 [sources]
@@ -8,6 +8,10 @@ require_ipv4 = true
     provider = "WTFIsMyIP"
   [sources.ipsb]
     provider = "IPSB"
+  [sources.checkip_dyndns]
+    provider = "GenericHttpRegexSource"
+    url_v4 = "http://checkip.dyndns.org/"
+    #regex_v4 = "\\b\\d{1,3}(\\.\\d{1,3}){3}\\b"
   [sources.ifconfig]
     provider = "GenericHttpSource"
     url_v4 = "https://ifconfig.me/ip"