diff --git a/src/Basis.php b/src/Basis.php index 13461c4..77de02f 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -12,6 +12,20 @@ function lenUtf8(string $str): int { // get string length (Chinese -> 2) return strlen(iconv('utf-8', 'gb2312', $str)); } +function isIPv4($ip): bool { + return filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4); +} + +function isIPv6($ip): bool { + return filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6); +} + +function isDomain($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})+$/'; + preg_match($regex, $domain, $match); + return count($match) != 0; +} + function getKeys(bool $isWinServer = false): array { // get kms keys asset $keysAsset = json_decode(file_get_contents('../assets/kms-keys.json'), true); return $isWinServer ? array_reverse($keysAsset['win-server']) : $keysAsset['win']; diff --git a/src/Check.php b/src/Check.php index 4842b95..c3079e1 100644 --- a/src/Check.php +++ b/src/Check.php @@ -1,15 +1,46 @@ isAlive()); // wait vlmcs exit preg_match_all('/Sending activation request \(KMS V6\)/', $vlmcs->getStdout(), $match); return count($match[0]) != 0; } -$host = 'kms.343.re'; -echo "check $host -> "; -echo (kmsCheck($host) ? 'yes' : 'no') . PHP_EOL; +function kmsCheck(): array { + if (!isset($_GET['host'])) { // missing host param + return array( + 'success' => false, + 'message' => 'missing host param' + ); + } + $host = $_GET['host']; + if (!isIPv4($host) and !isIPv6($host) and !isDomain($host)) { // invalid host + return array( + 'success' => false, + 'message' => 'invalid host' + ); + } + $port = $_GET['port'] ?? 1688; + if ($port > 65535 or $port < 0) { // invalid port + return array( + 'success' => false, + 'message' => 'invalid port' + ); + } + if (vlmcsCheck($host, $port)) { // KMS server available + return array( + 'success' => true, + 'message' => 'kms server available' + ); + } else { // KMS server couldn't reach + return array( + 'success' => false, + 'message' => 'kms server connect failed' + ); + } +} diff --git a/src/Route.php b/src/Route.php index 6738798..6504a40 100644 --- a/src/Route.php +++ b/src/Route.php @@ -1,6 +1,7 @@