From 3779739a63c7df35d870a3ce87578d5a6b7140be Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Aug 2021 00:55:04 +0800 Subject: [PATCH] feat: kms check on server --- backend/kms-check.php | 69 +++++++++++++++++++++++++++++++++++++++++++ backend/route.php | 13 +++++++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 backend/kms-check.php diff --git a/backend/kms-check.php b/backend/kms-check.php new file mode 100644 index 0000000..cbcad30 --- /dev/null +++ b/backend/kms-check.php @@ -0,0 +1,69 @@ + 'error', + 'message' => 'host param not exist' + )); + } + if (isset($config['port'])) { + $port = $config['port']; + } else { + $port = 1688; + } + if (isset($config['version'])) { + $version = $config['version']; + } else { + $version = 6; + } + + $cmd = 'vlmcs '; + if (isDomain($host) || isIPv4($host)) { + $cmd .= $host; + } else if (isIPv6($host)) { + $cmd .= '[' . $host . ']'; + } else { + return array( + 'status' => 'error', + 'message' => 'illegal host' + ); + } + if ($port > 65535 || $port < 0) { + return array( + 'status' => 'error', + 'message' => 'illegal port' + ); + } else { + $cmd .= ':' . $port; + } + if ($version != 4 && $version != 5 && $version != 6) { + return array( + 'status' => 'error', + 'message' => 'illegal version' + ); + } else { + $cmd .= ' -' . $version; + } + + $raw = shell_exec($cmd); + preg_match('/successful/', $raw, $match); + header('Content-Type: application/json; charset=utf-8'); + if (!count($match)) { + return array( + 'status' => 'error', + 'message' => 'connect fail' + ); + } else { + return array( + 'status' => 'ok', + 'message' => 'success' + ); + } +} + +?> diff --git a/backend/route.php b/backend/route.php index 19a54c2..5a545e3 100644 --- a/backend/route.php +++ b/backend/route.php @@ -4,6 +4,7 @@ include 'kms-cli.php'; include 'kms-web.php'; include 'kms-help.php'; include 'kms-office.php'; +include 'kms-check.php'; function isDomain($domain) { preg_match('/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/', $domain, $match); @@ -14,6 +15,10 @@ function isIPv4($ip) { return filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4); } +function isIPv6($ip) { + return filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6); +} + $kmsHost = "{KMS_HOST}"; $webSite = $kmsHost; if (isset($_SERVER['HTTP_HOST'])) { // 获取服务URL @@ -110,6 +115,12 @@ if ($url == '/win-server/json') { exit; } +if ($url == '/check') { + header('Content-Type: application/json; charset=utf-8'); + echo json_encode(checkKms($_GET)); + exit; +} + if ($_GET['cli'] == 'true') { // 无效请求 echo 'Illegal Request' . PHP_EOL; } else { @@ -117,4 +128,4 @@ if ($_GET['cli'] == 'true') { // 无效请求 echo '{"status":"error","message":"Illegal Request"}'; } -?> \ No newline at end of file +?>