feat: implement the ability to disable processing ipv4 or ipv6

This commit is contained in:
Rekryt
2024-10-25 16:03:35 +03:00
parent 2d842df7a0
commit 18556c7270
6 changed files with 90 additions and 57 deletions

View File

@@ -1,5 +1,7 @@
COMPOSE_PROJECT_NAME="iplist" COMPOSE_PROJECT_NAME="iplist"
STORAGE_SAVE_INTERVAL="120" STORAGE_SAVE_INTERVAL="120"
SYS_DNS_RESOLVE_IP4="true"
SYS_DNS_RESOLVE_IP6="true"
SYS_DNS_RESOLVE_CHUNK_SIZE="10" SYS_DNS_RESOLVE_CHUNK_SIZE="10"
SYS_DNS_RESOLVE_DELAY="100" SYS_DNS_RESOLVE_DELAY="100"
SYS_MEMORY_LIMIT="1024M" SYS_MEMORY_LIMIT="1024M"

View File

@@ -92,9 +92,11 @@ cp .env.example .env
If needed, edit the `.env` file: If needed, edit the `.env` file:
| property | default value | description | | property | default value | description |
|----------------------------|---------------|----------------------------------------------------------------| |----------------------------|---------------|-----------------------------------------------------------------|
| COMPOSE_PROJECT_NAME | iplist | Name of the compose project | | COMPOSE_PROJECT_NAME | iplist | Name of the compose project |
| STORAGE_SAVE_INTERVAL | 120 | Cache save interval for whois (seconds) | | 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_CHUNK_SIZE | 10 | Chunk size for retrieving DNS records |
| SYS_DNS_RESOLVE_DELAY | 100 | Delay between receiving dns records (milliseconds) | | SYS_DNS_RESOLVE_DELAY | 100 | Delay between receiving dns records (milliseconds) |
| SYS_MEMORY_LIMIT | 1024M | Memory limit | | SYS_MEMORY_LIMIT | 1024M | Memory limit |

View File

@@ -97,6 +97,8 @@ cp .env.example .env
|----------------------------|-----------------------|------------------------------------------------------------| |----------------------------|-----------------------|------------------------------------------------------------|
| COMPOSE_PROJECT_NAME | iplist | Имя compose проекта | | COMPOSE_PROJECT_NAME | iplist | Имя compose проекта |
| STORAGE_SAVE_INTERVAL | 120 | Период сохранения кеша whois (секунды) | | 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_CHUNK_SIZE | 10 | Размер чанка для получения dns записей |
| SYS_DNS_RESOLVE_DELAY | 100 | Задержка между получением dns записей (миллисекунды) | | SYS_DNS_RESOLVE_DELAY | 100 | Задержка между получением dns записей (миллисекунды) |
| SYS_MEMORY_LIMIT | 1024M | Предельное кол-во памяти. | | SYS_MEMORY_LIMIT | 1024M | Предельное кол-во памяти. |

View File

@@ -12,9 +12,12 @@ use Revolt\EventLoop;
use stdClass; use stdClass;
use function Amp\async; use function Amp\async;
use function Amp\Future\await; use function Amp\Future\await;
use function OpenCCK\getEnv;
final class Site { final class Site {
private DNSHelper $dnsHelper; private DNSHelper $dnsHelper;
private bool $isUseIpv6;
private bool $isUseIpv4;
/** /**
* @param string $name Name of portal * @param string $name Name of portal
@@ -42,6 +45,8 @@ final class Site {
public object $external = new stdClass() public object $external = new stdClass()
) { ) {
$this->dnsHelper = new DNSHelper($dns); $this->dnsHelper = new DNSHelper($dns);
$this->isUseIpv4 = (getEnv('SYS_DNS_RESOLVE_IP4') ?? 'true') == 'true';
$this->isUseIpv6 = (getEnv('SYS_DNS_RESOLVE_IP6') ?? 'true') == 'true';
} }
/** /**
@@ -51,15 +56,20 @@ final class Site {
$startTime = time(); $startTime = time();
App::getLogger()->notice('Preloading for ' . $this->name, ['started']); App::getLogger()->notice('Preloading for ' . $this->name, ['started']);
if ($this->timeout) { if ($this->timeout) {
if ($this->isUseIpv4) {
$this->cidr4 = SiteFactory::normalize( $this->cidr4 = SiteFactory::normalize(
IP4Helper::processCIDR($this->ip4, SiteFactory::normalize($this->cidr4)), IP4Helper::processCIDR($this->ip4, SiteFactory::normalize($this->cidr4)),
true true
); );
}
if ($this->isUseIpv6) {
$this->cidr6 = SiteFactory::normalize( $this->cidr6 = SiteFactory::normalize(
IP6Helper::processCIDR($this->ip6, SiteFactory::normalize($this->cidr6)), IP6Helper::processCIDR($this->ip6, SiteFactory::normalize($this->cidr6)),
true true
); );
} }
}
App::getLogger()->notice('Preloaded for ' . $this->name, ['finished', time() - $startTime]); App::getLogger()->notice('Preloaded for ' . $this->name, ['finished', time() - $startTime]);
} }
@@ -80,14 +90,19 @@ final class Site {
} }
} }
if ($this->isUseIpv4) {
$newIp4 = SiteFactory::normalize(array_diff($ip4, $this->ip4), true); $newIp4 = SiteFactory::normalize(array_diff($ip4, $this->ip4), true);
$this->cidr4 = SiteFactory::normalize(IP4Helper::processCIDR($newIp4, $this->cidr4), true); $this->cidr4 = SiteFactory::normalize(IP4Helper::processCIDR($newIp4, $this->cidr4), true);
$this->ip4 = SiteFactory::normalize(array_merge($this->ip4, $ip4), true);
}
if ($this->isUseIpv6) {
$newIp6 = SiteFactory::normalize(array_diff($ip6, $this->ip6), true); $newIp6 = SiteFactory::normalize(array_diff($ip6, $this->ip6), true);
$this->cidr6 = SiteFactory::normalize(IP6Helper::processCIDR($newIp6, $this->cidr6), true); $this->cidr6 = SiteFactory::normalize(IP6Helper::processCIDR($newIp6, $this->cidr6), true);
$this->ip4 = SiteFactory::normalize(array_merge($this->ip4, $ip4), true);
$this->ip6 = SiteFactory::normalize(array_merge($this->ip6, $ip6), true); $this->ip6 = SiteFactory::normalize(array_merge($this->ip6, $ip6), true);
}
$this->saveConfig(); $this->saveConfig();
App::getLogger()->notice('Reloaded for ' . $this->name, ['finished', time() - $startTime]); 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) { foreach ($this->external->ip4 as $url) {
$this->ip4 = SiteFactory::normalize( $this->ip4 = SiteFactory::normalize(
array_merge($this->ip4, explode("\n", file_get_contents($url))), 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) { foreach ($this->external->ip6 as $url) {
$this->ip6 = SiteFactory::normalize( $this->ip6 = SiteFactory::normalize(
array_merge($this->ip6, explode("\n", file_get_contents($url))), 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) { foreach ($this->external->cidr4 as $url) {
$this->cidr4 = SiteFactory::normalize( $this->cidr4 = SiteFactory::normalize(
array_merge($this->cidr4, explode("\n", file_get_contents($url))), 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) { foreach ($this->external->cidr6 as $url) {
$this->cidr6 = SiteFactory::normalize( $this->cidr6 = SiteFactory::normalize(
array_merge($this->cidr6, explode("\n", file_get_contents($url))), array_merge($this->cidr6, explode("\n", file_get_contents($url))),

View File

@@ -3,11 +3,11 @@
namespace OpenCCK\Domain\Factory; namespace OpenCCK\Domain\Factory;
use OpenCCK\Domain\Entity\Site; use OpenCCK\Domain\Entity\Site;
use OpenCCK\Domain\Helper\IP4Helper;
use OpenCCK\Domain\Helper\IP6Helper;
use OpenCCK\Infrastructure\API\App; use OpenCCK\Infrastructure\API\App;
use stdClass; use stdClass;
use function \OpenCCK\getEnv;
class SiteFactory { class SiteFactory {
// prettier-ignore // prettier-ignore
const TWO_LEVEL_DOMAIN_ZONES = [ const TWO_LEVEL_DOMAIN_ZONES = [
@@ -35,6 +35,8 @@ class SiteFactory {
$cidr4 = $config->cidr4 ?? []; $cidr4 = $config->cidr4 ?? [];
$cidr6 = $config->cidr6 ?? []; $cidr6 = $config->cidr6 ?? [];
$external = $config->external ?? new stdClass(); $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)) {
if (isset($external->domains)) { if (isset($external->domains)) {
@@ -44,28 +46,28 @@ class SiteFactory {
} }
} }
if (isset($external->ip4)) { if (isset($external->ip4) && $isUseIpv4) {
foreach ($external->ip4 as $url) { foreach ($external->ip4 as $url) {
App::getLogger()->debug('Loading external ip4 from ' . $url); App::getLogger()->debug('Loading external ip4 from ' . $url);
$ip4 = array_merge($ip4, explode("\n", file_get_contents($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) { foreach ($external->ip6 as $url) {
App::getLogger()->debug('Loading external ip6 from ' . $url); App::getLogger()->debug('Loading external ip6 from ' . $url);
$ip6 = array_merge($ip6, explode("\n", file_get_contents($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) { foreach ($external->cidr4 as $url) {
App::getLogger()->debug('Loading external cidr4 from ' . $url); App::getLogger()->debug('Loading external cidr4 from ' . $url);
$cidr4 = array_merge($cidr4, explode("\n", file_get_contents($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) { foreach ($external->cidr6 as $url) {
App::getLogger()->debug('Loading external cidr6 from ' . $url); App::getLogger()->debug('Loading external cidr6 from ' . $url);
$cidr6 = array_merge($cidr6, explode("\n", file_get_contents($url))); $cidr6 = array_merge($cidr6, explode("\n", file_get_contents($url)));

View File

@@ -15,12 +15,17 @@ use Throwable;
use function Amp\delay; use function Amp\delay;
use function Amp\Dns\dnsResolver as dnsResolverFactory; use function Amp\Dns\dnsResolver as dnsResolverFactory;
use function OpenCCK\getEnv;
class DNSHelper { class DNSHelper {
private float $resolveDelay; private float $resolveDelay;
private bool $isUseIpv4;
private bool $isUseIpv6;
public function __construct(private array $dnsServers = []) { public function __construct(private array $dnsServers = []) {
$this->resolveDelay = (\OpenCCK\getEnv('SYS_DNS_RESOLVE_DELAY') ?? 500) / 1000; $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,6 +58,7 @@ class DNSHelper {
foreach ($this->dnsServers as $server) { foreach ($this->dnsServers as $server) {
delay($this->resolveDelay); delay($this->resolveDelay);
$dnsResolver = $this->getResolver([$server]); $dnsResolver = $this->getResolver([$server]);
if ($this->isUseIpv4) {
try { try {
$ipv4 = array_merge( $ipv4 = array_merge(
$ipv4, $ipv4,
@@ -66,8 +72,11 @@ class DNSHelper {
App::getLogger()->error($e->getMessage(), [$server]); App::getLogger()->error($e->getMessage(), [$server]);
} }
} }
}
delay($this->resolveDelay); delay($this->resolveDelay);
if ($this->isUseIpv6) {
try { try {
$ipv6 = array_merge( $ipv6 = array_merge(
$ipv6, $ipv6,
@@ -82,6 +91,7 @@ class DNSHelper {
} }
} }
} }
}
App::getLogger()->debug('resolve: ' . $domain, [count($ipv4), count($ipv6)]); App::getLogger()->debug('resolve: ' . $domain, [count($ipv4), count($ipv6)]);
return [$ipv4, $ipv6]; return [$ipv4, $ipv6];
} }