mode = $mode ?? self::configuredMode(); $this->domains = $domains ?? new DomainHandler(); } public static function configuredMode(): int { $configured = Config::read('domain_dns_status_check'); if (!is_int($configured) && !is_string($configured)) { return self::CHECK_DISABLED; } $mode = (int)$configured; return in_array($mode, [self::CHECK_DISABLED, self::CHECK_ZONE, self::CHECK_MX], true) ? $mode : self::CHECK_DISABLED; } /** @param string[] $domains * @return array{active: int, inactive: int} */ public function refresh(array $domains): array { $result = ['active' => 0, 'inactive' => 0]; if ($this->mode === self::CHECK_DISABLED) { return $result; } foreach (array_unique($domains) as $domain) { if (!is_string($domain) || $domain === '' || $domain === 'ALL') { continue; } $active = $this->isActive($domain); $this->domains->updateDnsStatus($domain, $active); $result[$active ? 'active' : 'inactive']++; } return $result; } public function isActive(string $domain): bool { if ($this->mode === self::CHECK_MX) { return $this->hasUsableMx($domain); } if ($this->mode !== self::CHECK_ZONE) { return false; } foreach ($this->nameservers($domain) as $nameserver) { foreach ($this->nameserverAddresses($nameserver) as $address) { if ($this->authoritativeServerResponds($domain, $address)) { return true; } } } return false; } protected function hasUsableMx(string $domain): bool { foreach ($this->mxTargets($domain) as $target) { if ($this->nameserverAddresses($target) !== []) { return true; } } return false; } /** @return string[] */ protected function mxTargets(string $domain): array { if (!function_exists('dns_get_record')) { return []; } $records = @dns_get_record(rtrim($domain, '.'), DNS_MX); if (!is_array($records)) { return []; } $targets = []; foreach ($records as $record) { if (isset($record['target']) && is_string($record['target'])) { $target = strtolower(rtrim($record['target'], '.')); if ($target !== '') { $targets[] = $target; } } } return array_values(array_unique($targets)); } /** @return string[] */ protected function nameservers(string $domain): array { if (!function_exists('dns_get_record')) { return []; } $records = @dns_get_record(rtrim($domain, '.'), DNS_NS); if (!is_array($records)) { return []; } $nameservers = []; foreach ($records as $record) { if (isset($record['target']) && is_string($record['target'])) { $nameservers[] = strtolower(rtrim($record['target'], '.')); } } return array_values(array_unique($nameservers)); } /** @return string[] */ protected function nameserverAddresses(string $nameserver): array { $records = @dns_get_record($nameserver, DNS_A | DNS_AAAA); if (!is_array($records)) { return []; } $addresses = []; foreach ($records as $record) { if (isset($record['ip']) && filter_var($record['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { $addresses[] = $record['ip']; } elseif (isset($record['ipv6']) && filter_var($record['ipv6'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $addresses[] = $record['ipv6']; } } return array_values(array_unique($addresses)); } /** * Query the nameserver directly for the domain's SOA record and require an * authoritative, successful response. A recursive NS lookup alone only * proves that delegation records exist, not that an authoritative server * is reachable and serving the zone. */ protected function authoritativeServerResponds(string $domain, string $address): bool { $id = random_int(0, 65535); $query = pack('nnnnnn', $id, 0, 1, 0, 0, 0) . $this->encodeName($domain) . pack('nn', 6, 1); $endpoint = str_contains($address, ':') ? "udp://[$address]:53" : "udp://$address:53"; $socket = @stream_socket_client($endpoint, $errno, $error, $this->timeout, STREAM_CLIENT_CONNECT); if ($socket === false) { return false; } $seconds = (int)$this->timeout; $microseconds = (int)round(($this->timeout - (float)$seconds) * 1000000.0); stream_set_timeout($socket, $seconds, $microseconds); $written = @fwrite($socket, $query); $response = $written === strlen($query) ? @fread($socket, 4096) : false; fclose($socket); if (!is_string($response) || strlen($response) < 12) { return false; } $header = unpack('nid/nflags', substr($response, 0, 4)); if (!is_array($header)) { return false; } $flags = (int)$header['flags']; return (int)$header['id'] === $id && ($flags & 0x8000) !== 0 && ($flags & 0x0400) !== 0 && ($flags & 0x000f) === 0; } private function encodeName(string $domain): string { $encoded = ''; foreach (explode('.', rtrim($domain, '.')) as $label) { $length = strlen($label); if ($length === 0 || $length > 63) { return "\0"; } $encoded .= chr($length) . $label; } return $encoded . "\0"; } }