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', 'port' => 6379, 'passwd' => '', 'prefix' => 'ntp-' ); 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 getRedisData($host) { // 查询Redis缓存,不存在返回NULL $redis = new Redis(); $redis->connect($this->redisSetting['host'], $this->redisSetting['port']); if ($this->redisSetting['passwd'] != '') { $redis->auth($this->redisSetting['passwd']); } $redisKey = $this->redisSetting['prefix'] . $host; $redisValue = $redis->exists($redisKey) ? $redis->get($redisKey) : NULL; return $redisValue; } private function setRedisData($host, $data, $cacheTTL) { // 写入信息到Redis缓存 $redis = new Redis(); $redis->connect($this->redisSetting['host'], $this->redisSetting['port']); if ($this->redisSetting['passwd'] != '') { $redis->auth($this->redisSetting['passwd']); } $redisKey = $this->redisSetting['prefix'] . $host; $redis->set($redisKey, $data); // 写入数据库 $redis->pexpire($redisKey, $cacheTTL); // 设置过期时间 } private function getNtpStatus($host) { // 获取NTP服务器状态 $html = $this->curlPost('https://servertest.online/ntp', array( 'a' => $host, 'c' => 'Query+both' )); preg_match('/<\/form>[\s\S]+