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