Browse Source

feat: kms check with random file

master
Dnomd343 3 years ago
parent
commit
b238bfe1d5
  1. 45
      backend/kms-check.php
  2. 1
      conf/docker/vlmcsd.sh

45
backend/kms-check.php

@ -1,39 +1,38 @@
<?php
// TODO: site/app
function checkKms($config) { // 检测KMS服务器是否可用
$tempPath = '/var/www/kms-server/backend/';
if (isset($config['host'])) {
$host = $config['host'];
} else {
return json_encode(array(
} else { // host参数必选
return array(
'status' => 'error',
'message' => 'host param not exist'
));
);
}
if (isset($config['port'])) {
$port = $config['port'];
} else {
$port = 1688;
$port = 1688; // 默认KMS端口
}
if (isset($config['version'])) {
$version = $config['version'];
if (isset($config['site']) && $config['site'] !== '') {
$site = $config['site'];
} else {
$version = 6;
$site = null; // site参数可选
}
$cmd = 'vlmcs ';
if (isDomain($host) || isIPv4($host)) {
$cmd .= $host;
} else if (isIPv6($host)) {
$cmd .= '[' . $host . ']';
$cmd .= '[' . $host . ']'; // IPv6地址需用中括号包围
} else {
return array(
return array( // host内容不是 IPv4/IPv6/Domain
'status' => 'error',
'message' => 'illegal host'
);
}
if ($port > 65535 || $port < 0) {
if ($port > 65535 || $port < 0) { // 端口不存在
return array(
'status' => 'error',
'message' => 'illegal port'
@ -41,24 +40,22 @@ function checkKms($config) { // 检测KMS服务器是否可用
} else {
$cmd .= ':' . $port;
}
if ($version != 4 && $version != 5 && $version != 6) {
return array(
'status' => 'error',
'message' => 'illegal version'
);
} else {
$cmd .= ' -' . $version;
if ($site !== null) {
$cmd .= ' -w ' . $site; // 加入site参数
}
$raw = shell_exec($cmd);
preg_match('/successful/', $raw, $match);
$fileName = md5(rand()) . '.txt'; // 生成随机文件名
$cmd .= ' -G ' . $tempPath . $fileName;
shell_exec($cmd); // 执行vlmcs测试
$raw = shell_exec('ls ' . $tempPath);
preg_match('/' . $fileName . '/', $raw, $match); // 判断随机文件是否存在
header('Content-Type: application/json; charset=utf-8');
if (!count($match)) {
if (!count($match)) { // 随机文件不存在 -> KMS连接错误
return array(
'status' => 'error',
'message' => 'connect fail'
);
} else {
} else { // 随机文件存在 -> KMS测试成功
shell_exec('rm -f ' . $tempPath . $fileName); // 删除随机文件
return array(
'status' => 'ok',
'message' => 'success'

1
conf/docker/vlmcsd.sh

@ -3,6 +3,7 @@ git clone --branch master --single-branch https://github.com/Wind4/vlmcsd.git /t
cd /tmp/vlmcsd-build
make
mkdir /tmp/vlmcsd
cp /tmp/vlmcsd-build/bin/vlmcs /tmp/vlmcsd/
cp /tmp/vlmcsd-build/bin/vlmcsd /tmp/vlmcsd/
cp /tmp/vlmcsd-build/etc/vlmcsd.kmd /tmp/vlmcsd/
rm -rf /tmp/vlmcsd-build
Loading…
Cancel
Save