From 8fb6c046e9c3fc2b713b05542791104d306fdec6 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 10 Aug 2022 22:30:32 +0800 Subject: [PATCH] feat: add kms server host --- src/Basis.php | 25 ++++++++++++++++++++++--- src/KmsWeb.php | 4 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Basis.php b/src/Basis.php index 77de02f..a8ba2cb 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -13,11 +13,11 @@ function lenUtf8(string $str): int { // get string length (Chinese -> 2) } function isIPv4($ip): bool { - return filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4); + return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); } function isIPv6($ip): bool { - return filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6); + return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); } function isDomain($domain): bool { @@ -31,8 +31,27 @@ function getKeys(bool $isWinServer = false): array { // get kms keys asset return $isWinServer ? array_reverse($keysAsset['win-server']) : $keysAsset['win']; } +function v6DelBracket(string $host): string { + if (str_starts_with($host, '[') and str_ends_with($host, ']')) { + return substr($host, 1, strlen($host) - 2); // remove bracket of ipv6 + } + return $host; // no change +} + function getHost(): string { - return $_SERVER['HTTP_HOST'] ?? 'KMS_HOST'; // TODO: remove port content + if (!isset($_SERVER['HTTP_HOST'])) { // missing http host + return 'KMS_HOST'; + } + $host = v6DelBracket($_SERVER['HTTP_HOST']); // try to remove ipv6 bracket + if (isIPv4($host) or isIPv6($host) or isDomain($host)) { // invalid host + return $host; + } + preg_match_all('/(\S+):\d+$/', $host, $match); + if (count($match[1]) == 0) { // ${HOST}:${PORT} format not found + return 'KMS_HOST'; + } + $host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket again + return (isIPv4($host) or isIPv6($host) or isDomain($host)) ? $host : 'KMS_HOST'; } function officeInfo(): array { // office dir and kms key for different version diff --git a/src/KmsWeb.php b/src/KmsWeb.php index 0dfdffe..dcc07f1 100644 --- a/src/KmsWeb.php +++ b/src/KmsWeb.php @@ -1,7 +1,7 @@ '; + echo ''; echo ''; echo ''; echo "$header
"; @@ -29,7 +29,7 @@ function showHelpHtml(string $host): void { // show help message in html } function showOfficeHtml(string $host): void { // show office commands in html - echo ''; + echo ''; echo ''; echo ''; echo "Office KMS Server\n
";