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"
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"

View File

@@ -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:
```

View File

@@ -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 | Предельное кол-во памяти. |

View File

@@ -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))),

View File

@@ -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)));

View File

@@ -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]);
}
}
}
}