From 90158ca5ad2d5bd9f80d3c04f03ad16c5b2717d1 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 21 Feb 2024 12:32:04 +0300 Subject: [PATCH] add require_ipv4 + require_ipv6 config params --- pddnsc/base.py | 1 - pddnsc/cli.py | 16 +++++++++++++--- pddnsc/outputs/files.py | 1 - pddnsc/sources/fake.py | 1 - settings/config.toml | 3 +++ 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pddnsc/base.py b/pddnsc/base.py index 9dea4a0..cd346a2 100644 --- a/pddnsc/base.py +++ b/pddnsc/base.py @@ -1,4 +1,3 @@ -import httpx import asyncio from abc import ABC, abstractmethod from typing import NamedTuple, Optional diff --git a/pddnsc/cli.py b/pddnsc/cli.py index a97a0f0..6169899 100644 --- a/pddnsc/cli.py +++ b/pddnsc/cli.py @@ -6,7 +6,16 @@ from .plugins import use_plugins from typing import Optional -async def get_ip_addresses() -> Optional[IPAddreses]: +def is_valid_addreses(addrs: IPAddreses, config) -> bool: + result = addrs.ipv4 or addrs.ipv6 + if config.get("require_ipv4"): + result = result and addrs.ipv4 + if config.get("require_ipv6"): + result = result and addrs.ipv6 + return result + + +async def get_ip_addresses(config) -> Optional[IPAddreses]: providers = BaseSourceProvider.registred.values() ip_addresses = None is_done = False @@ -15,9 +24,10 @@ async def get_ip_addresses() -> Optional[IPAddreses]: done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED) for x in done: ip_addresses = x.result() - if ip_addresses.ipv4 or ip_addresses.ipv6: + if is_valid_addreses(ip_addresses, config): is_done = True break + ip_addresses = None if pending: gather = asyncio.gather(*pending) @@ -96,7 +106,7 @@ async def app(config, ipv4t, ipv6t): print_debug_info(config) - ip_addreses = await get_ip_addresses() + ip_addreses = await get_ip_addresses(config) if ip_addreses is None: print("no IP addresses") return diff --git a/pddnsc/outputs/files.py b/pddnsc/outputs/files.py index 80c3bed..5166aed 100644 --- a/pddnsc/outputs/files.py +++ b/pddnsc/outputs/files.py @@ -1,4 +1,3 @@ -import asyncio import aiofiles import json import hashlib diff --git a/pddnsc/sources/fake.py b/pddnsc/sources/fake.py index cbf1284..3d6a2d9 100644 --- a/pddnsc/sources/fake.py +++ b/pddnsc/sources/fake.py @@ -1,4 +1,3 @@ -import httpx import asyncio from pddnsc.base import BaseSourceProvider diff --git a/settings/config.toml b/settings/config.toml index e50a56a..daa4251 100644 --- a/settings/config.toml +++ b/settings/config.toml @@ -1,8 +1,11 @@ debug = true +require_ipv4 = true [sources] [sources.ipfy] provider = "IPIFYSource" + [sources.wtf] + provider = "WTFIsMyIP" [sources.fake] provider = "FakeSource" delay = 10