From 87bf2d8368170efa5131c9943c241b7638e32aa5 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 4 Aug 2026 02:50:23 +0530 Subject: [PATCH] improve: add 8-method fallback for internet connectivity check - Method 1: ping to Google DNS (8.8.8.8) - Method 2: DNS resolution via nslookup - Method 3: DNS query using dig - Method 4: curl HTTP HEAD to google.com - Method 5: wget --spider - Method 6: fsockopen to Google DNS (8.8.8.8:53) - Method 7: curl to Cloudflare DNS (1.1.1.1) - Method 8: PHP gethostbyname() for DNS resolution Tries multiple DNS providers (Google, Cloudflare, Quad9) Falls back to 'Offline' only if all methods fail Handles firewalls, ICMP blocks, and missing commands --- app/Libraries/SystemOperations.php | 87 ++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/app/Libraries/SystemOperations.php b/app/Libraries/SystemOperations.php index dacbf38..f6cfb50 100644 --- a/app/Libraries/SystemOperations.php +++ b/app/Libraries/SystemOperations.php @@ -553,12 +553,91 @@ class SystemOperations private function readInternetStatus(): string { - $result = $this->safeCommand('ping -c 2 8.8.8.8 2>/dev/null | tail -n 1'); - if ($result === 'Unavailable' || trim($result) === '') { - return 'Unavailable'; + // Method 1: Try ping to Google DNS + $result = $this->safeCommand('ping -c 1 -W 2 8.8.8.8 2>/dev/null'); + if ($result !== 'Unavailable' && trim($result) !== '' && strpos($result, '100% packet loss') === false && strpos($result, '100.0%') === false) { + return 'Online'; } - return strpos($result, '100% packet loss') !== false ? 'Offline' : 'Online'; + // Method 2: Try DNS resolution using nslookup + $result = $this->safeCommand('nslookup google.com 8.8.8.8 2>/dev/null | grep -i "name:"'); + if ($result !== 'Unavailable' && trim($result) !== '') { + return 'Online'; + } + + // Method 3: Try dig (DNS query) + $result = $this->safeCommand('dig @8.8.8.8 google.com +short 2>/dev/null'); + if ($result !== 'Unavailable' && trim($result) !== '') { + return 'Online'; + } + + // Method 4: Try curl/wget to external site with timeout + $result = $this->safeCommand('curl -s -m 3 -o /dev/null -w "%{http_code}" https://www.google.com 2>/dev/null'); + if ($result !== 'Unavailable' && trim($result) !== '' && intval($result) >= 200 && intval($result) < 400) { + return 'Online'; + } + + // Method 5: Try wget HEAD request + $result = $this->safeCommand('wget --spider -q -T 2 https://www.google.com 2>&1'); + if ($result !== 'Unavailable' && strpos($result, 'HTTP request sent, awaiting response') !== false) { + return 'Online'; + } + + // Method 6: PHP fsockopen to Google DNS port 53 + $onlineViaSocket = $this->checkInternetViaSocket(); + if ($onlineViaSocket) { + return 'Online'; + } + + // Method 7: PHP fsockopen to Cloudflare DNS + $result = $this->safeCommand('curl -s -m 3 -o /dev/null -w "%{http_code}" https://1.1.1.1 2>/dev/null'); + if ($result !== 'Unavailable' && trim($result) !== '' && intval($result) >= 200 && intval($result) < 400) { + return 'Online'; + } + + // Method 8: Try to resolve google.com using PHP's gethostbyname + try { + if (function_exists('gethostbyname')) { + $ip = @gethostbyname('google.com'); + if ($ip !== 'google.com' && filter_var($ip, FILTER_VALIDATE_IP)) { + return 'Online'; + } + } + } catch (\Throwable $e) { + // Ignore errors + } + + return 'Offline'; + } + + private function checkInternetViaSocket(): bool + { + try { + // Try connecting to Google DNS (8.8.8.8:53) + $sock = @fsockopen('8.8.8.8', 53, $errno, $errstr, 3); + if ($sock) { + @fclose($sock); + return true; + } + + // Try Cloudflare DNS (1.1.1.1:53) + $sock = @fsockopen('1.1.1.1', 53, $errno, $errstr, 3); + if ($sock) { + @fclose($sock); + return true; + } + + // Try Quad9 DNS (9.9.9.9:53) + $sock = @fsockopen('9.9.9.9', 53, $errno, $errstr, 3); + if ($sock) { + @fclose($sock); + return true; + } + } catch (\Throwable $e) { + // Ignore socket errors + } + + return false; } private function readServiceStatus(): array