mirror of
https://github.com/rekryt/iplist.git
synced 2025-10-12 16:39:35 +03:00
feat: implement the ability to disable processing ipv4 or ipv6
This commit is contained in:
@@ -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))),
|
||||
|
@@ -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)));
|
||||
|
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user