Browse Source

feat: icp query support

master
Dnomd343 3 years ago
parent
commit
5c4cf1abc7
  1. 3
      .env.example
  2. 5
      cmdRoute.php
  3. 123
      models/icpQuery.php

3
.env.example

@ -7,3 +7,6 @@ REDIS_HOST=127.0.0.1
REDIS_PORT=6379 REDIS_PORT=6379
REDIS_PASSWD= REDIS_PASSWD=
REDIS_PREFIX=tgbot REDIS_PREFIX=tgbot
# ChinaZ API
ICP_KEY=

5
cmdRoute.php

@ -5,6 +5,7 @@ require_once 'models/ipInfo.php';
require_once 'models/cfopPic.php'; require_once 'models/cfopPic.php';
require_once 'models/kmsCheck.php'; require_once 'models/kmsCheck.php';
require_once 'models/ntpCheck.php'; require_once 'models/ntpCheck.php';
require_once 'models/icpQuery.php';
require_once 'models/punycode.php'; require_once 'models/punycode.php';
require_once 'models/whoisQuery.php'; require_once 'models/whoisQuery.php';
@ -13,6 +14,7 @@ $cmds = array( // 命令列表
'dc', 'dc',
'kms', 'kms',
'ntp', 'ntp',
'icp',
'cfop', 'cfop',
'whois', 'whois',
'punycode' 'punycode'
@ -32,6 +34,9 @@ function route($cmd, $rawParam) { // 命令请求路由
case 'ntp': case 'ntp':
$entry = new ntpCheckEntry; $entry = new ntpCheckEntry;
break; break;
case 'icp':
$entry = new icpQueryEntry;
break;
case 'cfop': case 'cfop':
$entry = new cfopPicEntry; $entry = new cfopPicEntry;
break; break;

123
models/icpQuery.php

@ -0,0 +1,123 @@
<?php
class icpQuery {
private $apiPath = 'https://apidatav2.chinaz.com/single/icp';
public function __construct() {
global $env;
$this->apiPath .= '?key=' . $env['ICP_KEY'] . '&domain=';
}
private function getIcpInfo($domain) { // ICP备案查询
$info = json_decode(file_get_contents($this->apiPath . $domain), true);
if ($info['StateCode'] === 1) { // 存在ICP备案
$info = array(
'status' => 'ok',
'hasIcp' => true
) + $info['Result'];
} else if ($info['StateCode'] === 0) { // 无ICP备案
$info = array(
'status' => 'ok',
'hasIcp' => false,
'icpMsg' => $info['Reason']
);
} else {
$info = array(
'status' => 'error', // 服务错误
'message' => 'Server error'
);
}
return $info;
}
public function icpInfo($domain) { // ICP查询入口 带缓存
$redis = new redisCache('icp');
$info = $redis->getData($domain); // 查询缓存数据
if (!$info) { // 缓存未命中
$info = $this->getIcpInfo($domain); // 执行查询
if ($info['status'] !== 'ok') { // 查询错误
return $info;
}
unset($info['status']);
$redis->setData($domain, json_encode($info), 30 * 86400); // 缓存30day
} else { // 缓存命中
$info = json_decode($info, true); // 使用缓存数据
}
return array(
'status' => 'ok'
) + $info;
}
}
class icpQueryEntry {
private function check($str) { // 检查输入参数
$content = (new extractDomain)->analyse($str);
if (!isset($content['domain'])) { // 格式错误
return array(
'status' => 'error',
'message' => 'Illegal Request'
);
}
if (!isset($content['tld'])) { // 未知TLD
return array(
'status' => 'error',
'message' => 'Unknow TLD'
);
}
return array(
'status' => 'ok',
'domain' => $content['site'] // 返回主域名
);
}
public function query($rawParam) { // ICP备案查询入口
if ($rawParam == '' || $rawParam === 'help') { // 显示使用说明
tgApi::sendMessage(array(
'parse_mode' => 'Markdown',
'text' => '*Usage:* `/icp domain`',
));
return;
}
$content = $this->check($rawParam);
if ($content['status'] !== 'ok') { // 请求参数错误
tgApi::sendText($content['message']);
return;
}
$domain = $content['domain'];
$info = (new icpQuery)->icpInfo($domain); // 发起查询
if ($info['status'] !== 'ok') {
tgApi::sendText($info['message']); // 查询出错
return;
}
if (!$info['hasIcp']) { // 没有ICP备案
tgApi::sendMessage(array(
'parse_mode' => 'Markdown',
'text' => '`' . $domain . '`' . PHP_EOL . $info['icpMsg']
));
return;
}
$msg = '`' . $domain . '`' . PHP_EOL;
$msg .= '*类型:*' . $info['CompanyType'] . PHP_EOL;
if ($info['Owner'] != '') { // 负责人不为空
$msg .= '*负责人:*' . $info['Owner'] . PHP_EOL;
}
if ($info['Owner'] != $info['CompanyName']) { // 名称与负责人不重复
$msg .= '*名称:*' . $info['CompanyName'] . PHP_EOL;
}
$msg .= '*主页:*';
$pages = explode(';', $info['MainPage']);
foreach ($pages as $page) { // 可能存在多个主页 逐个输出
$msg .= '`' . $page . '` ';
}
$msg .= PHP_EOL;
$msg .= '*网站名:*' . $info['SiteName'] . PHP_EOL;
$msg .= '*审核时间:*' . $info['VerifyTime'] . PHP_EOL;
$msg .= '*许可证号:*' . $info['SiteLicense'] . PHP_EOL;
tgApi::sendMessage(array( // 返回查询数据
'parse_mode' => 'Markdown',
'text' => $msg
));
}
}
?>
Loading…
Cancel
Save