From d4b1a961b35f2c9558aee09f9ea24d2156331364 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 10 Aug 2021 23:26:28 +0800 Subject: [PATCH] feat: ntp server list --- cmdRoute.php | 3 ++ models/ntpCheck.php | 96 ++++++++++++++++++++++++++++++++++++++++++++ models/ntpServer.db | Bin 0 -> 12288 bytes 3 files changed, 99 insertions(+) create mode 100644 models/ntpServer.db diff --git a/cmdRoute.php b/cmdRoute.php index 6003c28..b104cc7 100644 --- a/cmdRoute.php +++ b/cmdRoute.php @@ -42,6 +42,9 @@ function routeCallback($cmd, $rawParam) { // 回调请求路由 case 'kms': kmsCheckCallback($rawParam); break; + case 'ntp': + ntpCheckCallback($rawParam); + break; case 'cfop': cfopPicCallback($rawParam); break; diff --git a/models/ntpCheck.php b/models/ntpCheck.php index 063ab15..b23a41f 100644 --- a/models/ntpCheck.php +++ b/models/ntpCheck.php @@ -1,5 +1,74 @@ open('./models/ntpServer.db'); // NTP服务器数据库 + } +} + +class ntpList { + private function getListName($list_id) { // 获取对应组的名称 + $db = new ntpDB; + $res = $db->query('SELECT * FROM `ntp_list` WHERE id=' . $list_id . ';'); + return $res->fetchArray(SQLITE3_ASSOC)['name']; + } + + private function getNtpList() { // 获取所有NTP服务器地址 + $db = new ntpDB; + $res = $db->query('SELECT * FROM `ntp_host`;'); + while ($row = $res->fetchArray(SQLITE3_ASSOC)) { + $index = $row['list_id']; + unset($row['list_id']); + $data[$this->getListName($index)][] = $row; + } + return $data; + } + + private function hashGroupName($str) { // 计算组名的哈希值 + return substr(md5($str), 0, 12); + } + + public function showList() { // 列出所有NTP服务器组 + $ntpList = $this->getNtpList(); + foreach ($ntpList as $index => $ntpHosts) { + $buttons[] = array([ // 生成按钮列表 + 'text' => $index, + 'callback_data' => '/ntp ' . $this->hashGroupName($index) + ]); + } + return array( + 'text' => 'Which one did you like?', + 'reply_markup' => json_encode(array( // 列表按钮 + 'inline_keyboard' => $buttons + )) + ); + } + + public function showNtpServer($targetGroup) { // 列出指定组的NTP服务器 + $ntpList = $this->getNtpList(); + foreach ($ntpList as $index => $ntpHosts) { + if ($this->hashGroupName($index) === $targetGroup) { break; } + } + $msg = '*' . $index . '*' . PHP_EOL; + foreach ($ntpHosts as $ntpHost) { + if ($ntpHost['desc'] !== '') { + $msg .= $ntpHost['desc'] . ':'; + } + $msg .= '`' . $ntpHost['host'] . '`' . PHP_EOL; + } + return array( + 'parse_mode' => 'Markdown', + 'text' => $msg, + 'reply_markup' => json_encode(array( + 'inline_keyboard' => array([[ + 'text' => ' << Go back <<', + 'callback_data' => '/ntp servers' + ]]) + )) + ); + } +} + class ntpCheck { private $redisSetting = array( // redis缓存配置 'host' => '127.0.0.1', @@ -141,6 +210,19 @@ class ntpCheck { function ntpCheck($rawParam) { // NTP测试入口 global $chatId; + if ($rawParam == '' || $rawParam === 'help') { // 显示使用说明 + sendMessage($chatId, array( + 'parse_mode' => 'Markdown', + 'text' => '*Usage:* `/ntp IP/Domain`', + 'reply_markup' => json_encode(array( // 获取NTP服务列表 + 'inline_keyboard' => array([[ + 'text' => 'Get NTP Servers', + 'callback_data' => '/ntp servers' + ]]) + )) + )); + return; + } if ((new ntpCheck)->checkHost($rawParam)['status'] === 'error') { sendText($chatId, 'Illegal host'); // 输入错误 return; @@ -156,4 +238,18 @@ function ntpCheck($rawParam) { // NTP测试入口 ) + (new ntpCheck)->checkNtp($rawParam)); // 发起查询并返回结果 } +function ntpCheckCallback($rawParam) { // NTP测试回调入口 + global $chatId, $messageId; + if ($rawParam === 'servers') { + $content = (new ntpList)->showList(); // 显示可选组 + } else { + $content = (new ntpList)->showNtpServer($rawParam); // 显示指定组的服务器列表 + } + sendPayload(array( + 'method' => 'editMessageText', + 'chat_id' => $chatId, + 'message_id' => $messageId + ) + $content); +} + ?> diff --git a/models/ntpServer.db b/models/ntpServer.db new file mode 100644 index 0000000000000000000000000000000000000000..62a708a5f6ef13ae4d29ae4785c2d157017c61c3 GIT binary patch literal 12288 zcmeI0TWs7^9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