Browse Source

refactor: interface of kms check

master
Dnomd343 3 years ago
parent
commit
a0a6451972
  1. 5
      cmdRoute.php
  2. 242
      models/kmsCheck.php
  3. 15
      tgInterface.php

5
cmdRoute.php

@ -25,7 +25,7 @@ function route($cmd, $rawParam) { // 命令请求路由
$entry = new tgDCEntry;
break;
case 'kms':
kmsCheck($rawParam);
$entry = new kmsCheckEntry;
break;
case 'ntp':
ntpCheck($rawParam);
@ -38,7 +38,8 @@ function route($cmd, $rawParam) { // 命令请求路由
break;
}
if ($entry) {
if (!$isCallback) {
global $tgEnv;
if (!$tgEnv['isCallback']) {
$entry->query($rawParam);
} else {
$entry->callback($rawParam);

242
models/kmsCheck.php

@ -1,8 +1,50 @@
<?php
class kmsCheck {
private $api = 'https://kms.343.re';
private $api = 'https://kms.343.re/';
public function getKmsKeys($type) {
switch ($type) {
case '':
$url = $this->api . 'json';
break;
case 'win':
case 'win-server':
$url = $this->api . $type . '/json';
break;
default:
return array();
}
return json_decode(file_get_contents($url), true);
}
public function kmsStatus($host, $port) {
$server = $host . ':' . $port;
$url = $this->api . 'check?host=' . $host . '&port=' . $port;
$content = json_decode(file_get_contents($url), true); // 请求上游接口
switch ($content['status']) {
case 'ok':
return array(
'status' => 'ok',
'online' => true,
'server' => $server
);
case 'error':
return array(
'status' => 'ok',
'online' => false,
'server' => $server
);
default:
return array(
'status' => 'error',
'message' => 'Server error'
);
}
}
}
class kmsCheckEntry {
private function isHost($host) { // 判断host是否合法
preg_match('/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/', $host, $match);
if (count($match) !== 0) { // 域名
@ -14,10 +56,9 @@ class kmsCheck {
return false;
}
public function check($server) {
private function formatCheck($server) {
$temp = explode(':', $server);
if (count($temp) === 1) { // 不带:的请求
if ($this->isHost($temp[0])) {
$host = $server;
$port = 1688;
@ -61,8 +102,8 @@ class kmsCheck {
return implode('', $match[0]);
}
public function getKmsVersions($type) {
$kmsKeys = json_decode(file_get_contents($this->api . '/' . $type . '/json'), true);
private function getKmsVersions($type) { // 获取win或win-server的版本列表
$kmsKeys = (new kmsCheck)->getKmsKeys($type);
foreach ($kmsKeys as $version => $kmsKey) {
$buttons[] = array([ // 生成按钮列表
'text' => $version,
@ -81,8 +122,8 @@ class kmsCheck {
);
}
public function getKmsKeys($targetVersion) { // 显示指定版本的KMS密钥列表
$kmsKeys = json_decode(file_get_contents($this->api . '/json'), true);
private function getKmsKeys($targetVersion) { // 显示指定版本的KMS密钥列表
$kmsKeys = (new kmsCheck)->getKmsKeys('');
foreach ($kmsKeys as $version => $kmsKey) { // 比对压缩以后的名称
if ($this->simpStr($version) === $targetVersion) { break; } // 匹配成功
}
@ -96,117 +137,110 @@ class kmsCheck {
);
}
public function checkKms($host, $port) {
$server = $host . ':' . $port;
$url = $this->api . '/check?host=' . $host . '&port=' . $port;
$content = json_decode(file_get_contents($url), true); // 请求上游接口
switch ($content['status']) {
case 'ok':
private function checkKms($host, $port) { // 检查KMS服务器状态
$content = (new kmsCheck)->kmsStatus($host, $port);
if ($content['status'] === 'ok') {
if ($content['online'] === true) {
return array(
'parse_mode' => 'Markdown',
'text' => '`' . $server . '`' . PHP_EOL . 'KMS服务*正常运行*'
'text' => '`' . $content['server'] . '`' . PHP_EOL . 'KMS服务*正常运行*'
);
case 'error':
} else {
return array(
'parse_mode' => 'Markdown',
'text' => '`' . $server . '`' . PHP_EOL . 'KMS服务*无法使用*'
);
default:
return array(
'text' => 'Server error'
'text' => '`' . $content['server'] . '`' . PHP_EOL . 'KMS服务*无法使用*'
);
}
} else {
return array(
'text' => $content['message']
);
}
}
}
function kmsCheck($rawParam) { // KMS测试入口
global $chatId;
if ($rawParam == '' || $rawParam === 'help') { // 显示使用说明
sendMessage($chatId, array(
'parse_mode' => 'Markdown',
'text' => '*Usage:* `/kms IP/Domain[:port]`',
'reply_markup' => json_encode(array( // 获取KMS密钥按钮
'inline_keyboard' => array([[
'text' => 'Get KMS Keys',
'callback_data' => '/kms keys'
]])
))
));
return;
}
$check = (new kmsCheck)->check($rawParam);
if ($check['status'] === 'error') { // 输入格式有误
sendMessage($chatId, array(
'text' => $check['message']
));
return;
}
$message = json_decode(sendMessage($chatId, array(
'parse_mode' => 'Markdown',
'text' => '`' . $rawParam . '`' . PHP_EOL . 'KMS服务检测中...'
)), true);
sendPayload(array(
'method' => 'editMessageText',
'chat_id' => $chatId,
'message_id' => $message['result']['message_id'],
) + (new kmsCheck)->checkKms($check['host'], $check['port'])); // 发起查询并返回结果
}
function kmsCheckCallback($rawParam) { // KMS测试回调入口
global $chatId, $messageId;
$selectMsg = array(
'text' => 'Which one did you need?',
'reply_markup' => json_encode(array(
'inline_keyboard' => array(
array([
'text' => 'Windows',
'callback_data' => '/kms win'
]),
array([
'text' => 'Windows Server',
'callback_data' => '/kms win-server'
]),
array([
'text' => 'Activation Command',
'callback_data' => '/kms cmd'
])
)
))
);
$actiCmd = '```' . PHP_EOL . 'slmgr /upk' . PHP_EOL . 'slmgr /ipk {KMS_KEY}' . PHP_EOL;
$actiCmd .= 'slmgr /skms {KMS_HOST}' . PHP_EOL . 'slmgr /ato' . PHP_EOL . 'slmgr /dlv';
$actiCmd .= PHP_EOL . '```';
switch ($rawParam) {
case 'keys':
sendPayload(array(
'method' => 'editMessageText',
'chat_id' => $chatId,
'message_id' => $messageId,
) + $selectMsg);
return;
case 'cmd':
sendPayload(array(
'method' => 'editMessageText',
'chat_id' => $chatId,
'message_id' => $messageId,
public function query($rawParam) { // kmsCheck查询入口
if ($rawParam == '' || $rawParam === 'help') { // 显示使用说明
tgApi::sendMessage(array(
'parse_mode' => 'Markdown',
'text' => $actiCmd
'text' => '*Usage:* `/kms IP/Domain[:port]`',
'reply_markup' => json_encode(array( // 获取KMS密钥按钮
'inline_keyboard' => array([[
'text' => 'Get KMS Keys',
'callback_data' => '/kms keys'
]])
))
));
return;
case 'win':
case 'win-server':
sendPayload(array(
'method' => 'editMessageText',
'chat_id' => $chatId,
'message_id' => $messageId,
) + (new kmsCheck)->getKmsVersions($rawParam));
}
$check = $this->formatCheck($rawParam);
if ($check['status'] === 'error') { // 输入格式有误
tgApi::sendText($check['message']);
return;
}
$message = json_decode(tgApi::sendMessage(array(
'parse_mode' => 'Markdown',
'text' => '`' . $rawParam . '`' . PHP_EOL . 'KMS服务检测中...'
)), true);
fastcgi_finish_request(); // 断开连接
tgApi::editMessage(array(
'message_id' => $message['result']['message_id'],
) + $this->checkKms($check['host'], $check['port'])); // 发起查询并返回结果
}
public function callback($rawParam) { // kmsCheck回调入口
global $tgEnv;
$selectMsg = array(
'text' => 'Which one did you need?',
'reply_markup' => json_encode(array(
'inline_keyboard' => array(
array([
'text' => 'Windows',
'callback_data' => '/kms win'
]),
array([
'text' => 'Windows Server',
'callback_data' => '/kms win-server'
]),
array([
'text' => 'Activation Command',
'callback_data' => '/kms cmd'
])
)
))
);
$actiCmd = '```' . PHP_EOL . 'slmgr /upk' . PHP_EOL . 'slmgr /ipk {KMS_KEY}' . PHP_EOL;
$actiCmd .= 'slmgr /skms {KMS_HOST}' . PHP_EOL . 'slmgr /ato' . PHP_EOL . 'slmgr /dlv';
$actiCmd .= PHP_EOL . '```';
switch ($rawParam) {
case 'keys':
tgApi::editMessage(array(
'message_id' => $tgEnv['messageId'],
) + $selectMsg);
return;
case 'cmd':
tgApi::editMessage(array(
'message_id' => $tgEnv['messageId'],
'parse_mode' => 'Markdown',
'text' => $actiCmd,
'reply_markup' => json_encode(array(
'inline_keyboard' => array([[
'text' => '<< Go back <<',
'callback_data' => '/kms keys'
]])
))
));
return;
case 'win':
case 'win-server':
tgApi::editMessage(array(
'message_id' => $tgEnv['messageId'],
) + $this->getKmsVersions($rawParam));
return;
}
tgApi::editMessage(array(
'message_id' => $tgEnv['messageId']
) + $this->getKmsKeys($rawParam));
}
sendPayload(array(
'method' => 'editMessageText',
'chat_id' => $chatId,
'message_id' => $messageId
) + (new kmsCheck)->getKmsKeys($rawParam));
}
?>

15
tgInterface.php

@ -7,7 +7,7 @@ class tgApi {
), $chatId);
}
public function sendMessage($params, $chatId = 0) { // 发送文字消息
public function sendMessage($params, $chatId = 0) { // 发送消息
if ($chatId === 0) { // 未指定chatId
global $tgEnv;
$chatId = $tgEnv['chatId'];
@ -19,6 +19,18 @@ class tgApi {
return tgApi::sendPayload($params);
}
public function editMessage($params, $chatId = 0) { // 修改消息
if ($chatId === 0) { // 未指定chatId
global $tgEnv;
$chatId = $tgEnv['chatId'];
}
$params += array (
'method' => 'editMessageText',
'chat_id' => $chatId
);
return tgApi::sendPayload($params);
}
public function deleteMessage($params, $chatId = 0) { // 删除消息
if ($chatId === 0) { // 未指定chatId
global $tgEnv;
@ -30,6 +42,7 @@ class tgApi {
);
return tgApi::sendPayload($params);
}
public function sendPayload($payload) { // 发送原始数据
global $tgEnv;
$url = $tgEnv['apiPath'] . '/' . $payload['method'] . '?';

Loading…
Cancel
Save