Update latency-api.php

This commit is contained in:
Blackwhitebear8 2025-08-19 13:55:18 +02:00
parent b600917d46
commit 975c377fff
2 changed files with 4 additions and 38 deletions

4
latency-api.php Normal file
View file

@ -0,0 +1,4 @@
<?php
header('Content-Type: application/json');
echo json_encode(['status' => 'ok']);
?>

View file

@ -1,38 +0,0 @@
<?php
function testLatency($host) {
$start_time = microtime(true);
$output = shell_exec("ping -c 10 $host");
$end_time = microtime(true);
$latency = $end_time - $start_time;
if ($output !== null) {
if (preg_match('/avg\/min\/max = (\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)/', $output, $matches)) {
$avg_ping = $matches[1];
} else {
$avg_ping = 'Unknown';
}
$latency_rounded = round($latency, 2);
$latency_rounded = number_format($latency_rounded, 2, '.', '');
$response = [
'host' => $host,
'latency_ms' => $latency_rounded,
];
return json_encode($response, JSON_PRETTY_PRINT);
} else {
return json_encode([
'error' => 'Ping failed',
], JSON_PRETTY_PRINT);
}
}
$client_ip = $_SERVER['REMOTE_ADDR'];
echo testLatency($client_ip);
?>