Browse Source

refactor: interface of kms check

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

5
cmdRoute.php

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

140
models/kmsCheck.php

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

15
tgInterface.php

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

Loading…
Cancel
Save