Browse Source

feat: ntp check on server

master
Dnomd343 3 years ago
parent
commit
211159ed06
  1. 5
      cmdRoute.php
  2. 8
      models/kmsCheck.php
  3. 119
      models/ntpCheck.php

5
cmdRoute.php

@ -4,11 +4,13 @@ require_once 'models/tgDC.php';
require_once 'models/ipInfo.php';
require_once 'models/cfopPic.php';
require_once 'models/kmsCheck.php';
require_once 'models/ntpCheck.php';
$cmds = array( // 命令列表
'ip',
'dc',
'kms',
'ntp',
'cfop'
);
@ -23,6 +25,9 @@ function route($cmd, $rawParam) { // 命令请求路由
case 'kms':
kmsCheck($rawParam);
break;
case 'ntp':
ntpCheck($rawParam);
break;
case 'cfop':
cfopPic($rawParam);
break;

8
models/kmsCheck.php

@ -6,7 +6,7 @@ class kmsCheck {
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) { // 域名
return true;
if (!is_numeric(substr($host, -1))) { return true; } // 域名最后一位不为数字
}
if (filter_var($host, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { // IPv4地址
return true;
@ -120,7 +120,7 @@ class kmsCheck {
}
function kmsCheck($rawParam) { // KMS测试入口
global $chatId, $messageId;
global $chatId;
if ($rawParam == '' || $rawParam === 'help') { // 显示使用说明
sendMessage($chatId, array(
'parse_mode' => 'Markdown',
@ -148,11 +148,11 @@ function kmsCheck($rawParam) { // KMS测试入口
sendPayload(array(
'method' => 'editMessageText',
'chat_id' => $chatId,
'message_id' => $messageId,
'message_id' => $message['result']['message_id'],
) + (new kmsCheck)->checkKms($check['host'], $check['port'])); // 发起查询并返回结果
}
function kmsCheckCallback($rawParam) {
function kmsCheckCallback($rawParam) { // KMS测试回调入口
global $chatId, $messageId;
$selectMsg = array(
'text' => 'Which one did you need?',

119
models/ntpCheck.php

@ -0,0 +1,119 @@
<?php
class ntpCheck {
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) { // 域名
if (!is_numeric(substr($host, -1))) { return true; } // 域名最后一位不为数字
}
if (filter_var($host, FILTER_VALIDATE_IP)) { // IP地址
return true;
}
return false;
}
private function curlPost($url, $data) { // curl模拟post操作 40s超时
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 40);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$content = curl_exec($curl);
curl_close($curl);
return $content;
}
private function getNtpStatus($host) { // 获取NTP服务器状态
$html = $this->curlPost('https://servertest.online/ntp', array(
'a' => $host,
'c' => 'Query+both'
));
preg_match('/<\/form>[\s\S]+<footer>/', $html, $match); // 切取数据部分
preg_match('/<label>[\s\S]+<footer>/', $match[0], $match); // 去除前部干扰
$match = substr($match[0], 0, strlen($match[0]) - 8); // 去除后部<footer>
$match = strtr($match, array('<h2>IPv6 test results</h2>' => '<br><br>')); // 去除中间干扰
$match = explode('<br>', $match); // 以<br>为界限切割
$match[] = ''; // 添加空记录方便分组
foreach ($match as $row) { // 匹配分组
if ($row == '') {
if (count($temp) === 5) {
$record[] = $temp;
$temp = array();
}
continue;
}
$temp[] = $row;
}
foreach ($record as $group) { // 检查组内容是否正常
$group['ok'] = '';
foreach ($group as $index => $row) { // 遍历组内每一行
if ($index === 'ok') { continue; }
$row = strtr($row, array('<label>' => ''));
$row = strtr($row, array(':</label>' => '|'));
$content = explode('|', $row); // 切取参数
if (count($content) !== 2) { unset($group['ok']); } // 格式不合格
$group[$content[0]] = $content[1];
unset($group[$index]); // 删除原始记录
}
if (!isset($group['ok'])) { continue; } // 剔除不合格组
if ($group['Result'] !== '<b class=com>OK</b>') { continue; }
if (!filter_var($group['Server'], FILTER_VALIDATE_IP)) { continue; }
unset($group['Result']);
unset($group['ok']);
$data[] = $group;
}
return ($data === null) ? array() : $data;
}
public function checkHost($host) { // 检测host合法性
if ($this->isHost($host)) {
return array('status' => 'ok');
} else {
return array('status' => 'error');
}
}
public function checkNtp($host) {
$servers = $this->getNtpStatus($host);
if (count($servers) === 0) {
$msg = '`' . $host . '`' . PHP_EOL;
$msg .= 'NTP Server *Offline*' . PHP_EOL . PHP_EOL;
return array(
'parse_mode' => 'Markdown',
'text' => $msg
);
}
$msg = '`' . $host . '`' . PHP_EOL;
$msg .= 'NTP Server *Normally*' . PHP_EOL . PHP_EOL;
foreach ($servers as $server) {
$msg .= '`' . $server['Server'] . '`' . PHP_EOL;
$msg .= '_Stratum:_ ' . $server['Stratum'] . PHP_EOL;
$msg .= '_Offset:_ ' . $server['Offset'] . PHP_EOL;
$msg .= PHP_EOL;
}
return array(
'parse_mode' => 'Markdown',
'text' => $msg
);
}
}
function ntpCheck($rawParam) { // NTP测试入口
global $chatId;
if ((new ntpCheck)->checkHost($rawParam)['status'] === 'error') {
sendText($chatId, 'Illegal host'); // 输入错误
return;
}
$message = json_decode(sendMessage($chatId, array(
'parse_mode' => 'Markdown',
'text' => '`' . $rawParam . '`' . PHP_EOL . 'NTP Server Checking...'
)), true);
sendPayload(array(
'method' => 'editMessageText',
'chat_id' => $chatId,
'message_id' => $message['result']['message_id'],
) + (new ntpCheck)->checkNtp($rawParam)); // 发起查询并返回结果
}
?>
Loading…
Cancel
Save