diff --git a/LookingGlass.php b/LookingGlass.php index cf32239..252630f 100644 --- a/LookingGlass.php +++ b/LookingGlass.php @@ -428,7 +428,13 @@ class LookingGlass */ private static function getLatencyFromSs(string $ip): array { - $lines = shell_exec('/usr/sbin/ss -Hti state established'); + $ssPath = exec('which ss 2>/dev/null'); + if (empty($ssPath)) { + // RHEL based systems + $ssPath = '/usr/sbin/ss'; + } + $lines = shell_exec("$ssPath -Hnti state established"); + $ss = []; $i = 0; $j = 0; @@ -445,26 +451,22 @@ class LookingGlass $output = []; foreach ($ss as $socket) { $socket = preg_replace('!\s+!', ' ', $socket); - $explodedsocket = explode(' ', $socket); - preg_match('/\d+\.\d+\.\d+\.\d+/', $explodedsocket[2], $temp); - if (!isset($temp[0])) { + preg_match('/^\d+\s+\d+\s+(?:\[::ffff:)?(?[a-f0-9.:]+)]?:(?\d+)\s+(?:\[::ffff:)?(?[a-f0-9.:]+)]?:(?\d+)/', $line, $matches); + if ($matches['remoteIp'] !== $ip) { continue; } - $sock['local'] = $temp[0]; - preg_match('/\d+\.\d+\.\d+\.\d+/', $explodedsocket[3], $temp); - $sock['remote'] = $temp[0]; + $sock['local'] = $matches['localIp']; + $sock['remote'] = $matches['remoteIp']; preg_match('/segs_out:(\d+)/', $socket, $temp); $sock['segs_out'] = $temp[1]; preg_match('/segs_in:(\d+)/', $socket, $temp); $sock['segs_in'] = $temp[1]; - preg_match_all('/rtt:(\d+\.\d+)\/(\d+\.\d+)/', $socket, $temp); - $sock['latency'] = $temp[1][0]; - $sock['jitter'] = $temp[2][0]; - preg_match_all('/retrans:\d+\/(\d+)/', $socket, $temp); - $sock['retransmissions'] = (isset($temp[1][0]) ? $temp[1][0] : 0); - if ($sock['remote'] == $ip) { - $output[] = $sock; - } + preg_match('/rtt:(\d+\.\d+)\/(\d+\.\d+)/', $socket, $temp); + $sock['latency'] = $temp[1]; + $sock['jitter'] = $temp[2]; + preg_match('/retrans:\d+\/(\d+)/', $socket, $temp); + $sock['retransmissions'] = ($temp[1] ?? 0); + $output[] = $sock; } return $output; }