Browse Source

update: kms check api

master
dnomd343 2 years ago
parent
commit
2d627193e1
  1. 14
      src/Basis.php
  2. 43
      src/Check.php
  3. 3
      src/Route.php

14
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'];

43
src/Check.php

@ -1,15 +1,46 @@
<?php
require_once 'Basis.php';
require_once 'Process.php';
function kmsCheck(string $host, int $port = 1688): bool {
# TODO: ipv6 host add bracket
$vlmcs = new Process(['vlmcs', $host . ':' . $port], $capture = true);
function vlmcsCheck(string $host, int $port = 1688): bool {
$host = str_contains($host, ':') ? "[$host]" : $host; // ipv6 add bracket
$vlmcs = new Process(['vlmcs', $host . ':' . $port], true);
while($vlmcs->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'
);
}
}

3
src/Route.php

@ -1,6 +1,7 @@
<?php
require_once 'Basis.php';
require_once 'Check.php';
require_once 'KmsCli.php';
require_once 'KmsWeb.php';
@ -35,7 +36,7 @@ if ($url == '/' or $url == '/help') {
}
} else if ($url == '/check') {
mimeJson();
echo "WIP..."; // TODO: kms check
echo json_encode(kmsCheck()); // check kms server
} else { // unknown request
if ($isCli) {
echo "Illegal Request\n";

Loading…
Cancel
Save