Browse Source

feat: add kms server host

master
dnomd343 2 years ago
parent
commit
8fb6c046e9
  1. 25
      src/Basis.php
  2. 4
      src/KmsWeb.php

25
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

4
src/KmsWeb.php

@ -1,7 +1,7 @@
<?php
function showKeysHtml(array $kmsKeys, string $header): void { // show kms keys in html
echo '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">';
echo '<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8">';
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
echo '<link rel="stylesheet" href="./assets/style.css" />';
echo "<title>$header</title></head><body><div>";
@ -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 '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">';
echo '<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8">';
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
echo '<link rel="stylesheet" href="./assets/style.css" />';
echo "<title>Office KMS Server</title></head>\n<body><div>";

Loading…
Cancel
Save