fix: prioritize real server IP, skip loopback (127.x, ::1)
- Changed primary method to: ip -4 addr show scope global (gets public/private IPs only) - Added isLoopbackIp() helper to filter out loopback addresses (127.* and ::1) - Fallback: hostname -I with loopback filtering - Last resort: gethostbyname() with loopback check - On remote server with eth0: 159.89.175.190, will now show public IP instead of 127.0.0.1
This commit is contained in:
parent
a4bccc765c
commit
1ade027b00
|
|
@ -430,31 +430,40 @@ class SystemOperations
|
|||
|
||||
private function readNetworkStatus(): string
|
||||
{
|
||||
// Try PHP built-in first — works on Linux and Windows without shell
|
||||
// Primary: get first non-loopback IPv4 from Linux ip command (scope global)
|
||||
$result = $this->safeCommand("ip -4 addr show scope global 2>/dev/null | grep -oP 'inet \K[0-9.]+' | head -1");
|
||||
if ($result !== 'Unavailable' && trim($result) !== '' && filter_var(trim($result), FILTER_VALIDATE_IP)) {
|
||||
return trim($result);
|
||||
}
|
||||
|
||||
// Fallback: hostname -I and take first non-loopback IP
|
||||
$result = $this->safeCommand('hostname -I 2>/dev/null');
|
||||
if ($result !== 'Unavailable' && trim($result) !== '') {
|
||||
$ips = preg_split('/\s+/', trim($result));
|
||||
foreach ($ips as $ip) {
|
||||
if (filter_var($ip, FILTER_VALIDATE_IP) && !$this->isLoopbackIp($ip)) {
|
||||
return $ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: try gethostbyname but filter out loopback
|
||||
$hostname = gethostname();
|
||||
if ($hostname !== false) {
|
||||
$ip = gethostbyname($hostname);
|
||||
// gethostbyname returns the hostname unchanged if resolution fails
|
||||
if ($ip !== $hostname && filter_var($ip, FILTER_VALIDATE_IP)) {
|
||||
if ($ip !== $hostname && filter_var($ip, FILTER_VALIDATE_IP) && !$this->isLoopbackIp($ip)) {
|
||||
return $ip;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: parse first non-loopback IPv4 from ip addr output
|
||||
$result = $this->safeCommand("ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d/ -f1 | head -1 2>/dev/null");
|
||||
if ($result !== 'Unavailable' && trim($result) !== '' && filter_var(trim($result), FILTER_VALIDATE_IP)) {
|
||||
return trim($result);
|
||||
}
|
||||
|
||||
// Fallback: hostname -I
|
||||
$result = $this->safeCommand('hostname -I 2>/dev/null | awk \'{print $1}\'');
|
||||
if ($result !== 'Unavailable' && trim($result) !== '' && filter_var(trim($result), FILTER_VALIDATE_IP)) {
|
||||
return trim($result);
|
||||
}
|
||||
|
||||
return 'Unavailable';
|
||||
}
|
||||
|
||||
private function isLoopbackIp(string $ip): bool
|
||||
{
|
||||
return strpos($ip, '127.') === 0 || $ip === '::1';
|
||||
}
|
||||
|
||||
private function readInternetStatus(): string
|
||||
{
|
||||
$result = $this->safeCommand('ping -c 2 8.8.8.8 2>/dev/null | tail -n 1');
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user