Browse Source

feat: kms check in shell mode

master
dnomd343 2 years ago
parent
commit
988a7864a1
  1. 19
      src/Basis.php
  2. 34
      src/Check.php
  3. 6
      src/Route.php

19
src/Basis.php

@ -12,20 +12,29 @@ function lenUtf8(string $str): int { // get string length (Chinese -> 2)
return strlen(iconv('utf-8', 'gb2312', $str)); return strlen(iconv('utf-8', 'gb2312', $str));
} }
function isIPv4($ip): bool { function isIPv4(string $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 { function isIPv6(string $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 { function isDomain(string $domain): bool {
$regex = '/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/'; $regex = '/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/';
preg_match($regex, $domain, $match); preg_match($regex, $domain, $match);
return count($match) != 0; return count($match) != 0;
} }
function isHost(string $host): bool { // IPv4 / IPv6 / Domain
return isIPv4($host) or isIPv6($host) or isDomain($host);
}
function isPort(int $port): bool {
$port = intval($port);
return ($port < 65536 and $port > 0);
}
function getKeys(bool $isWinServer = false): array { // get kms keys asset function getKeys(bool $isWinServer = false): array { // get kms keys asset
$keysAsset = json_decode(file_get_contents('../assets/kms-keys.json'), true); $keysAsset = json_decode(file_get_contents('../assets/kms-keys.json'), true);
return $isWinServer ? array_reverse($keysAsset['win-server']) : $keysAsset['win']; return $isWinServer ? array_reverse($keysAsset['win-server']) : $keysAsset['win'];
@ -43,7 +52,7 @@ function getHost(): string {
return 'KMS_HOST'; return 'KMS_HOST';
} }
$host = v6DelBracket($_SERVER['HTTP_HOST']); // try to remove ipv6 bracket $host = v6DelBracket($_SERVER['HTTP_HOST']); // try to remove ipv6 bracket
if (isIPv4($host) or isIPv6($host) or isDomain($host)) { // invalid host if (isHost($host)) { // valid host
return $host; return $host;
} }
preg_match_all('/(\S+):\d+$/', $host, $match); preg_match_all('/(\S+):\d+$/', $host, $match);
@ -51,7 +60,7 @@ function getHost(): string {
return 'KMS_HOST'; return 'KMS_HOST';
} }
$host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket again $host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket again
return (isIPv4($host) or isIPv6($host) or isDomain($host)) ? $host : 'KMS_HOST'; return (isHost($host)) ? $host : 'KMS_HOST';
} }
function officeInfo(): array { // office dir and kms key for different version function officeInfo(): array { // office dir and kms key for different version

34
src/Check.php

@ -11,7 +11,7 @@ function vlmcsCheck(string $host, int $port = 1688): bool {
return count($match[0]) != 0; return count($match[0]) != 0;
} }
function kmsCheck(): array { function kmsCheckApi(): array {
if (!isset($_GET['host'])) { // missing host param if (!isset($_GET['host'])) { // missing host param
return array( return array(
'success' => false, 'success' => false,
@ -35,12 +35,42 @@ function kmsCheck(): array {
if (vlmcsCheck($host, $port)) { // KMS server available if (vlmcsCheck($host, $port)) { // KMS server available
return array( return array(
'success' => true, 'success' => true,
'available' => true,
'host' => $host,
'port' => intval($port),
'message' => 'kms server available' 'message' => 'kms server available'
); );
} else { // KMS server couldn't reach } else { // KMS server couldn't reach
return array( return array(
'success' => false, 'success' => true,
'available' => false,
'host' => $host,
'port' => intval($port),
'message' => 'kms server connect failed' 'message' => 'kms server connect failed'
); );
} }
} }
function kmsCheckCli(string $host): void {
$port = 1688;
$host = v6DelBracket($host); // try to remove ipv6 bracket
if (!isIPv4($host) and !isIPv6($host) and !isDomain($host)) { // invalid host
preg_match_all('/(\S+):(\d+)$/', $host, $match);
if (!count($match[1]) or !count($match[2])) { // ${HOST}:${PORT} format not found
echo "Invalid host\n";
exit;
}
$port = $match[2][0];
$host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket
if (!isIPv4($host) and !isIPv6($host) and !isDomain($host)) { // still invalid host
echo "Invalid host\n";
exit;
}
}
if (!isPort($port)) {
echo "Invalid port\n";
exit;
}
echo "KMS Server: $host ($port) -> ";
echo (vlmcsCheck($host, $port) ? 'available': 'connect failed') . PHP_EOL;
}

6
src/Route.php

@ -34,9 +34,11 @@ if ($url == '/' or $url == '/help') {
} else { } else {
$isCli ? showKeysCli($kmsKeys, $isGbk) : showKeysHtml($kmsKeys, $caption); // kms keys of windows $isCli ? showKeysCli($kmsKeys, $isGbk) : showKeysHtml($kmsKeys, $caption); // kms keys of windows
} }
} else if ($url == '/check') { } else if ($url == '/check' or $url == '/check/') {
mimeJson(); mimeJson();
echo json_encode(kmsCheck()); // check kms server echo json_encode(kmsCheckApi()); // check kms server
} else if (str_starts_with($url, '/check/')) {
kmsCheckCli(substr($url, 7)); // check kms server (split `/check/`)
} else { // unknown request } else { // unknown request
if ($isCli) { if ($isCli) {
echo "Illegal Request\n"; echo "Illegal Request\n";

Loading…
Cancel
Save