Browse Source

feat: kms check on server

master
Dnomd343 3 years ago
parent
commit
3779739a63
  1. 69
      backend/kms-check.php
  2. 13
      backend/route.php

69
backend/kms-check.php

@ -0,0 +1,69 @@
<?php
// TODO: site/app
function checkKms($config) { // 检测KMS服务器是否可用
if (isset($config['host'])) {
$host = $config['host'];
} else {
return json_encode(array(
'status' => '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'
);
}
}
?>

13
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"}';
}
?>
?>

Loading…
Cancel
Save