diff --git a/.env.example b/.env.example index 1580d8a..3af9b8b 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,7 @@ COMPOSE_PROJECT_NAME="iplist" STORAGE_SAVE_INTERVAL="120" +SYS_DNS_RESOLVE_IP4="true" +SYS_DNS_RESOLVE_IP6="true" SYS_DNS_RESOLVE_CHUNK_SIZE="10" SYS_DNS_RESOLVE_DELAY="100" SYS_MEMORY_LIMIT="1024M" diff --git a/README.en.md b/README.en.md index 16adbe8..74faa4d 100644 --- a/README.en.md +++ b/README.en.md @@ -91,17 +91,19 @@ cp .env.example .env If needed, edit the `.env` file: -| property | default value | description | -|----------------------------|---------------|----------------------------------------------------------------| -| COMPOSE_PROJECT_NAME | iplist | Name of the compose project | -| STORAGE_SAVE_INTERVAL | 120 | Cache save interval for whois (seconds) | -| SYS_DNS_RESOLVE_CHUNK_SIZE | 10 | Chunk size for retrieving DNS records | -| SYS_DNS_RESOLVE_DELAY | 100 | Delay between receiving dns records (milliseconds) | -| SYS_MEMORY_LIMIT | 1024M | Memory limit | -| SYS_TIMEZONE | Europe/Moscow | List of URLs to obtain initial CIDRv4 zones for IPv4 addresses | -| HTTP_HOST | 0.0.0.0 | IP of network interface (default is all interfaces) | -| HTTP_PORT | 8080 | Server network port (default 8080) | -| DEBUG | true | Determines the logging level | +| property | default value | description | +|----------------------------|---------------|-----------------------------------------------------------------| +| COMPOSE_PROJECT_NAME | iplist | Name of the compose project | +| STORAGE_SAVE_INTERVAL | 120 | Cache save interval for whois (seconds) | +| SYS_DNS_RESOLVE_IP4 | true | Resolve IPv4 addresses | +| SYS_DNS_RESOLVE_IP6 | true | Resolve IPv6 addresses | +| SYS_DNS_RESOLVE_CHUNK_SIZE | 10 | Chunk size for retrieving DNS records | +| SYS_DNS_RESOLVE_DELAY | 100 | Delay between receiving dns records (milliseconds) | +| SYS_MEMORY_LIMIT | 1024M | Memory limit | +| SYS_TIMEZONE | Europe/Moscow | List of URLs to obtain initial CIDRv4 zones for IPv4 addresses | +| HTTP_HOST | 0.0.0.0 | IP of network interface (default is all interfaces) | +| HTTP_PORT | 8080 | Server network port (default 8080) | +| DEBUG | true | Determines the logging level | You can access the service in your browser via the HTTP protocol on port 8080: ``` diff --git a/README.md b/README.md index a32b3b4..00d03cd 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,8 @@ cp .env.example .env |----------------------------|-----------------------|------------------------------------------------------------| | COMPOSE_PROJECT_NAME | iplist | Имя compose проекта | | STORAGE_SAVE_INTERVAL | 120 | Период сохранения кеша whois (секунды) | +| SYS_DNS_RESOLVE_IP4 | true | Получать ipv4 адреса | +| SYS_DNS_RESOLVE_IP6 | true | Получать ipv6 адреса | | SYS_DNS_RESOLVE_CHUNK_SIZE | 10 | Размер чанка для получения dns записей | | SYS_DNS_RESOLVE_DELAY | 100 | Задержка между получением dns записей (миллисекунды) | | SYS_MEMORY_LIMIT | 1024M | Предельное кол-во памяти. | diff --git a/src/Domain/Entity/Site.php b/src/Domain/Entity/Site.php index 2d0e3a0..bdbec89 100644 --- a/src/Domain/Entity/Site.php +++ b/src/Domain/Entity/Site.php @@ -12,9 +12,12 @@ use Revolt\EventLoop; use stdClass; use function Amp\async; use function Amp\Future\await; +use function OpenCCK\getEnv; final class Site { private DNSHelper $dnsHelper; + private bool $isUseIpv6; + private bool $isUseIpv4; /** * @param string $name Name of portal @@ -42,6 +45,8 @@ final class Site { public object $external = new stdClass() ) { $this->dnsHelper = new DNSHelper($dns); + $this->isUseIpv4 = (getEnv('SYS_DNS_RESOLVE_IP4') ?? 'true') == 'true'; + $this->isUseIpv6 = (getEnv('SYS_DNS_RESOLVE_IP6') ?? 'true') == 'true'; } /** @@ -51,14 +56,19 @@ final class Site { $startTime = time(); App::getLogger()->notice('Preloading for ' . $this->name, ['started']); if ($this->timeout) { - $this->cidr4 = SiteFactory::normalize( - IP4Helper::processCIDR($this->ip4, SiteFactory::normalize($this->cidr4)), - true - ); - $this->cidr6 = SiteFactory::normalize( - IP6Helper::processCIDR($this->ip6, SiteFactory::normalize($this->cidr6)), - true - ); + if ($this->isUseIpv4) { + $this->cidr4 = SiteFactory::normalize( + IP4Helper::processCIDR($this->ip4, SiteFactory::normalize($this->cidr4)), + true + ); + } + + if ($this->isUseIpv6) { + $this->cidr6 = SiteFactory::normalize( + IP6Helper::processCIDR($this->ip6, SiteFactory::normalize($this->cidr6)), + true + ); + } } App::getLogger()->notice('Preloaded for ' . $this->name, ['finished', time() - $startTime]); } @@ -80,14 +90,19 @@ final class Site { } } - $newIp4 = SiteFactory::normalize(array_diff($ip4, $this->ip4), true); - $this->cidr4 = SiteFactory::normalize(IP4Helper::processCIDR($newIp4, $this->cidr4), true); + if ($this->isUseIpv4) { + $newIp4 = SiteFactory::normalize(array_diff($ip4, $this->ip4), true); + $this->cidr4 = SiteFactory::normalize(IP4Helper::processCIDR($newIp4, $this->cidr4), true); - $newIp6 = SiteFactory::normalize(array_diff($ip6, $this->ip6), true); - $this->cidr6 = SiteFactory::normalize(IP6Helper::processCIDR($newIp6, $this->cidr6), true); + $this->ip4 = SiteFactory::normalize(array_merge($this->ip4, $ip4), true); + } - $this->ip4 = SiteFactory::normalize(array_merge($this->ip4, $ip4), true); - $this->ip6 = SiteFactory::normalize(array_merge($this->ip6, $ip6), true); + if ($this->isUseIpv6) { + $newIp6 = SiteFactory::normalize(array_diff($ip6, $this->ip6), true); + $this->cidr6 = SiteFactory::normalize(IP6Helper::processCIDR($newIp6, $this->cidr6), true); + + $this->ip6 = SiteFactory::normalize(array_merge($this->ip6, $ip6), true); + } $this->saveConfig(); App::getLogger()->notice('Reloaded for ' . $this->name, ['finished', time() - $startTime]); @@ -110,7 +125,7 @@ final class Site { } } - if (isset($this->external->ip4)) { + if (isset($this->external->ip4) && $this->isUseIpv4) { foreach ($this->external->ip4 as $url) { $this->ip4 = SiteFactory::normalize( array_merge($this->ip4, explode("\n", file_get_contents($url))), @@ -119,7 +134,7 @@ final class Site { } } - if (isset($this->external->ip6)) { + if (isset($this->external->ip6) && $this->isUseIpv6) { foreach ($this->external->ip6 as $url) { $this->ip6 = SiteFactory::normalize( array_merge($this->ip6, explode("\n", file_get_contents($url))), @@ -128,7 +143,7 @@ final class Site { } } - if (isset($this->external->cidr4)) { + if (isset($this->external->cidr4) && $this->isUseIpv4) { foreach ($this->external->cidr4 as $url) { $this->cidr4 = SiteFactory::normalize( array_merge($this->cidr4, explode("\n", file_get_contents($url))), @@ -137,7 +152,7 @@ final class Site { } } - if (isset($this->external->cidr6)) { + if (isset($this->external->cidr6) && $this->isUseIpv6) { foreach ($this->external->cidr6 as $url) { $this->cidr6 = SiteFactory::normalize( array_merge($this->cidr6, explode("\n", file_get_contents($url))), diff --git a/src/Domain/Factory/SiteFactory.php b/src/Domain/Factory/SiteFactory.php index 30f9627..d5e8f03 100644 --- a/src/Domain/Factory/SiteFactory.php +++ b/src/Domain/Factory/SiteFactory.php @@ -3,11 +3,11 @@ namespace OpenCCK\Domain\Factory; use OpenCCK\Domain\Entity\Site; -use OpenCCK\Domain\Helper\IP4Helper; -use OpenCCK\Domain\Helper\IP6Helper; use OpenCCK\Infrastructure\API\App; use stdClass; +use function \OpenCCK\getEnv; + class SiteFactory { // prettier-ignore const TWO_LEVEL_DOMAIN_ZONES = [ @@ -35,6 +35,8 @@ class SiteFactory { $cidr4 = $config->cidr4 ?? []; $cidr6 = $config->cidr6 ?? []; $external = $config->external ?? new stdClass(); + $isUseIpv4 = (getEnv('SYS_DNS_RESOLVE_IP4') ?? 'true') == 'true'; + $isUseIpv6 = (getEnv('SYS_DNS_RESOLVE_IP6') ?? 'true') == 'true'; if (isset($external)) { if (isset($external->domains)) { @@ -44,28 +46,28 @@ class SiteFactory { } } - if (isset($external->ip4)) { + if (isset($external->ip4) && $isUseIpv4) { foreach ($external->ip4 as $url) { App::getLogger()->debug('Loading external ip4 from ' . $url); $ip4 = array_merge($ip4, explode("\n", file_get_contents($url))); } } - if (isset($external->ip6)) { + if (isset($external->ip6) && $isUseIpv6) { foreach ($external->ip6 as $url) { App::getLogger()->debug('Loading external ip6 from ' . $url); $ip6 = array_merge($ip6, explode("\n", file_get_contents($url))); } } - if (isset($external->cidr4)) { + if (isset($external->cidr4) && $isUseIpv4) { foreach ($external->cidr4 as $url) { App::getLogger()->debug('Loading external cidr4 from ' . $url); $cidr4 = array_merge($cidr4, explode("\n", file_get_contents($url))); } } - if (isset($external->cidr6)) { + if (isset($external->cidr6) && $isUseIpv6) { foreach ($external->cidr6 as $url) { App::getLogger()->debug('Loading external cidr6 from ' . $url); $cidr6 = array_merge($cidr6, explode("\n", file_get_contents($url))); diff --git a/src/Domain/Helper/DNSHelper.php b/src/Domain/Helper/DNSHelper.php index b4ded02..b97d91c 100644 --- a/src/Domain/Helper/DNSHelper.php +++ b/src/Domain/Helper/DNSHelper.php @@ -15,12 +15,17 @@ use Throwable; use function Amp\delay; use function Amp\Dns\dnsResolver as dnsResolverFactory; +use function OpenCCK\getEnv; class DNSHelper { private float $resolveDelay; + private bool $isUseIpv4; + private bool $isUseIpv6; public function __construct(private array $dnsServers = []) { $this->resolveDelay = (\OpenCCK\getEnv('SYS_DNS_RESOLVE_DELAY') ?? 500) / 1000; + $this->isUseIpv4 = (getEnv('SYS_DNS_RESOLVE_IP4') ?? 'true') == 'true'; + $this->isUseIpv6 = (getEnv('SYS_DNS_RESOLVE_IP6') ?? 'true') == 'true'; } /** @@ -53,32 +58,37 @@ class DNSHelper { foreach ($this->dnsServers as $server) { delay($this->resolveDelay); $dnsResolver = $this->getResolver([$server]); - try { - $ipv4 = array_merge( - $ipv4, - array_map( - fn(DnsRecord $record) => $record->getValue(), - $dnsResolver->resolve($domain, DnsRecord::A) - ) - ); - } catch (Throwable $e) { - if (!str_starts_with($e->getMessage(), 'Giving up resolution')) { - App::getLogger()->error($e->getMessage(), [$server]); + if ($this->isUseIpv4) { + try { + $ipv4 = array_merge( + $ipv4, + array_map( + fn(DnsRecord $record) => $record->getValue(), + $dnsResolver->resolve($domain, DnsRecord::A) + ) + ); + } catch (Throwable $e) { + if (!str_starts_with($e->getMessage(), 'Giving up resolution')) { + App::getLogger()->error($e->getMessage(), [$server]); + } } } delay($this->resolveDelay); - try { - $ipv6 = array_merge( - $ipv6, - array_map( - fn(DnsRecord $record) => $record->getValue(), - $dnsResolver->resolve($domain, DnsRecord::AAAA) - ) - ); - } catch (Throwable $e) { - if (!str_starts_with($e->getMessage(), 'Giving up resolution')) { - App::getLogger()->error($e->getMessage(), [$server]); + + if ($this->isUseIpv6) { + try { + $ipv6 = array_merge( + $ipv6, + array_map( + fn(DnsRecord $record) => $record->getValue(), + $dnsResolver->resolve($domain, DnsRecord::AAAA) + ) + ); + } catch (Throwable $e) { + if (!str_starts_with($e->getMessage(), 'Giving up resolution')) { + App::getLogger()->error($e->getMessage(), [$server]); + } } } }