mirror of
https://git.hexahost.dev/smueller/HexaHost-Frontend.git
synced 2026-06-02 12:18:43 +00:00
180 lines
5.0 KiB
PHP
180 lines
5.0 KiB
PHP
<?php
|
|
/**
|
|
* HexaDNS - Ping/Verfügbarkeitstest API
|
|
*
|
|
* Prüft die Erreichbarkeit einer Domain
|
|
*
|
|
* Verwendung: GET /api/ping-check.php?domain=example.com
|
|
*/
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit;
|
|
}
|
|
|
|
$domain = isset($_GET['domain']) ? trim($_GET['domain']) : '';
|
|
|
|
if (empty($domain)) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Domain-Parameter fehlt']);
|
|
exit;
|
|
}
|
|
|
|
// Protokoll und Pfad entfernen
|
|
$domain = preg_replace('/^(https?:\/\/)?/', '', $domain);
|
|
$domain = explode('/', $domain)[0];
|
|
|
|
if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\-\.]*\.[a-zA-Z]{2,}$/', $domain)) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Ungültiges Domain-Format']);
|
|
exit;
|
|
}
|
|
|
|
$startTime = microtime(true);
|
|
$results = performConnectivityCheck($domain);
|
|
$totalTime = round((microtime(true) - $startTime) * 1000, 2);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'domain' => $domain,
|
|
'total_time_ms' => $totalTime,
|
|
'timestamp' => date('Y-m-d H:i:s'),
|
|
'results' => $results
|
|
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
|
|
/**
|
|
* Führt verschiedene Erreichbarkeitstests durch
|
|
*/
|
|
function performConnectivityCheck(string $domain): array {
|
|
$results = [
|
|
'dns_resolution' => checkDnsResolution($domain),
|
|
'icmp_ping' => checkIcmpPing($domain),
|
|
'http' => checkHttpConnection($domain, false),
|
|
'https' => checkHttpConnection($domain, true),
|
|
'overall_status' => 'offline'
|
|
];
|
|
|
|
// Overall-Status bestimmen
|
|
if ($results['https']['reachable'] || $results['http']['reachable']) {
|
|
$results['overall_status'] = 'online';
|
|
} elseif ($results['icmp_ping']['reachable']) {
|
|
$results['overall_status'] = 'partial';
|
|
} elseif ($results['dns_resolution']['resolved']) {
|
|
$results['overall_status'] = 'dns_only';
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* DNS-Auflösung prüfen
|
|
*/
|
|
function checkDnsResolution(string $domain): array {
|
|
$start = microtime(true);
|
|
$ip = gethostbyname($domain);
|
|
$time = round((microtime(true) - $start) * 1000, 2);
|
|
|
|
$resolved = $ip !== $domain;
|
|
|
|
return [
|
|
'resolved' => $resolved,
|
|
'ip' => $resolved ? $ip : null,
|
|
'response_time_ms' => $time
|
|
];
|
|
}
|
|
|
|
/**
|
|
* ICMP Ping (falls verfügbar)
|
|
*/
|
|
function checkIcmpPing(string $domain): array {
|
|
$result = [
|
|
'reachable' => false,
|
|
'response_time_ms' => null,
|
|
'packet_loss' => null
|
|
];
|
|
|
|
// Versuche ping-Kommando
|
|
$pingResult = @shell_exec("ping -c 3 -W 2 {$domain} 2>/dev/null");
|
|
|
|
if ($pingResult) {
|
|
// Prüfe auf erfolgreiche Antworten
|
|
if (preg_match('/(\d+)% packet loss/', $pingResult, $lossMatch)) {
|
|
$result['packet_loss'] = (int)$lossMatch[1];
|
|
$result['reachable'] = $result['packet_loss'] < 100;
|
|
}
|
|
|
|
// Durchschnittliche Zeit extrahieren
|
|
if (preg_match('/avg.*?=.*?[\d.]+\/([\d.]+)\//', $pingResult, $timeMatch)) {
|
|
$result['response_time_ms'] = (float)$timeMatch[1];
|
|
} elseif (preg_match('/time[=<]([\d.]+)\s*ms/', $pingResult, $timeMatch)) {
|
|
$result['response_time_ms'] = (float)$timeMatch[1];
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* HTTP(S)-Verbindung prüfen
|
|
*/
|
|
function checkHttpConnection(string $domain, bool $https = false): array {
|
|
$protocol = $https ? 'https' : 'http';
|
|
$port = $https ? 443 : 80;
|
|
$url = "{$protocol}://{$domain}";
|
|
|
|
$result = [
|
|
'reachable' => false,
|
|
'status_code' => null,
|
|
'response_time_ms' => null,
|
|
'redirect_url' => null,
|
|
'server' => null
|
|
];
|
|
|
|
$start = microtime(true);
|
|
|
|
// cURL verwenden
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_HEADER => true,
|
|
CURLOPT_NOBODY => true,
|
|
CURLOPT_TIMEOUT => 10,
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
|
CURLOPT_FOLLOWLOCATION => false,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_SSL_VERIFYHOST => 0,
|
|
CURLOPT_USERAGENT => 'HexaDNS Ping Check/1.0'
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$result['response_time_ms'] = round((microtime(true) - $start) * 1000, 2);
|
|
|
|
if ($response !== false) {
|
|
$result['status_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$result['reachable'] = $result['status_code'] > 0;
|
|
|
|
// Redirect-URL
|
|
$redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
|
|
if (!empty($redirectUrl)) {
|
|
$result['redirect_url'] = $redirectUrl;
|
|
}
|
|
|
|
// Server-Header
|
|
if (preg_match('/Server:\s*([^\r\n]+)/i', $response, $serverMatch)) {
|
|
$result['server'] = trim($serverMatch[1]);
|
|
}
|
|
} else {
|
|
$result['error'] = curl_error($ch);
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|