更新ICP备案查询与whois查询工具

This commit is contained in:
net909
2026-01-26 12:33:15 +08:00
parent 23bb5aea41
commit 4826b8c758
6 changed files with 596 additions and 93 deletions
+130 -51
View File
@@ -21,8 +21,9 @@ class App extends Plugin
public function query(){
$domain = input('post.domain', null, 'trim');
$type = input('post.type', 'web', 'trim');
if(!$domain) return msg('error','no domain');
if(!checkdomain($domain)){
if(strpos($domain,'.') && !checkdomain($domain)){
return msg('error', '域名格式不正确!');
}
@@ -31,70 +32,148 @@ class App extends Plugin
return msg('error', '验证失败,请重新验证');
}
$cache = Db::name('querycache')->where('type', 'icp')->where('key', $domain)->find();
$cache = Db::name('querycache')->where('type', $type.'list')->where('key|subkey', $domain)->find();
if($cache && time() - strtotime($cache['uptime']) <= self::CACHE_TIME){
$array = json_decode($cache['content'], true);
return msg('ok','success',$array);
$data = Db::name('querycache')->where('type', $type.'item')->whereIn('id', implode(',',$array['list']))->select();
$list = [];
foreach($data as $row){
$list[] = json_decode($row['content'], true);
}
return msg('ok','success',['total'=>$array['total'], 'list'=>$list]);
}
$cache = Db::name('querycache')->where('type', $type.'item')->where('key|subkey', $domain)->find();
if($cache && time() - strtotime($cache['uptime']) <= self::CACHE_TIME){
$array = json_decode($cache['content'], true);
return msg('ok','success',['total'=>1, 'list'=>[$array]]);
}
try{
$result = $this->queryapi($domain);
if(!$result){
return msg('ok','success',null);
}
$result = $this->execapi($type, $domain);
}catch(Exception $e){
return msg('error', $e->getMessage());
}
Db::name('querycache')->duplicate([
'type' => 'icp',
'key' => $result['Domain'],
'content' => json_encode($result),
'uptime' => date('Y-m-d H:i:s')
])->insert([
'type' => 'icp',
'key' => $result['Domain'],
'content' => json_encode($result),
'uptime' => date('Y-m-d H:i:s')
]);
if($result['total'] > 1 && count($result['data']) > 1){
$i = 0;
foreach($result['data'] as $row){
$id = Db::name('querycache')->duplicate([
'content' => json_encode($row),
'uptime' => date('Y-m-d H:i:s')
])->insertGetId([
'type' => $type.'item',
'key' => $row['domain'],
'subkey' => $row['webLicence'],
'content' => json_encode($row),
'uptime' => date('Y-m-d H:i:s')
]);
$result['data'][$i++]['id'] = $id;
$ids[] = $id;
}
Db::name('querycache')->duplicate([
'content' => json_encode(['total'=>$result['total'], 'list'=>$ids]),
'uptime' => date('Y-m-d H:i:s')
])->insert([
'type' => $type.'list',
'key' => $domain == $result['data'][0]['domain'] ? $result['data'][0]['domain'] : $result['data'][0]['unitName'],
'subkey' => $domain == $result['data'][0]['domain'] ? $result['data'][0]['webLicence'] : $result['data'][0]['mainLicence'],
'content' => json_encode(['total'=>$result['total'], 'list'=>$ids]),
'uptime' => date('Y-m-d H:i:s')
]);
}elseif($result['total'] == 1 && count($result['data']) > 0){
$id = Db::name('querycache')->duplicate([
'content' => json_encode($result['data'][0]),
'uptime' => date('Y-m-d H:i:s')
])->insertGetId([
'type' => $type.'item',
'key' => $result['data'][0]['domain'],
'subkey' => $result['data'][0]['webLicence'],
'content' => json_encode($result['data'][0]),
'uptime' => date('Y-m-d H:i:s')
]);
$result['data'][0]['id'] = $id;
}
return msg('ok','success',$result);
return msg('ok','success',['total'=>$result['total'], 'list'=>$result['data']]);
}
private function queryapi($domain){
if(config_get('qqapi_url')){
$url = config_get('qqapi_url').'api.php?act=icpquery';
$post = 'key='.config_get('qqapi_key').'&domain='.$domain;
$data = get_curl($url, $post);
$arr = json_decode($data, true);
if(isset($arr['code']) && $arr['code']==0){
return $arr['data'];
}elseif(isset($arr['msg'])){
throw new Exception($arr['msg']);
}else{
throw new Exception('接口请求失败');
}
}elseif(config_get('yapi_token')){
$url = 'https://api.makuo.cc/api/get.other.icp?url='.urlencode($domain);
$header = ['Authorization: '.config_get('yapi_token')];
$data = get_curl($url, 0, 0, 0, 0, 0, 0, $header);
$arr = json_decode($data, true);
if(isset($arr['code']) && $arr['code']==200){
return [
'Domain' => $arr['data']['domain'],
'DomainIcpNum' => $arr['data']['icp'],
'CompanyName' => $arr['data']['unitName'],
'CompanyType' => $arr['data']['natureName'],
'AuditTime' => $arr['data']['updateRecordTime'],
];
}elseif(isset($arr['msg'])){
throw new Exception('接口请求失败,'.$arr['msg']);
}else{
throw new Exception('接口请求失败');
}
public function item(){
$id = input('post.id');
if(!$id) return msg('error','no id');
$cache = Db::name('querycache')->where('id', $id)->find();
if($cache){
$array = json_decode($cache['content'], true);
return msg('ok','success',['total'=>1, 'list'=>[$array]]);
}else{
throw new Exception('请先配置API接口参数');
return msg('ok','success',['total'=>0, 'list'=>[]]);
}
}
/**
* https://github.com/HG-ha/ICP_Query
*/
private function execapi($type, $domain){
$url = 'http://172.17.0.1:16181/query/'.$type.'?search='.urlencode($domain);
$response = get_curl($url);
$arr = json_decode($response, true);
if(isset($arr['code']) && $arr['code']==200){
$list = [];
if(isset($arr['params']['list'])){
foreach($arr['params']['list'] as $row){
$list[] = ['domain'=>isset($row['domain'])?$row['domain']:$row['serviceName'], 'mainLicence'=>$row['mainLicence'], 'webLicence'=>$row['serviceLicence'], 'unitName'=>$row['unitName'], 'unitType'=>$row['natureName'], 'updateTime'=>$row['updateRecordTime'], 'contentTypeName'=>$row['contentTypeName']];
}
}
return ['code'=>0, 'total'=>isset($arr['params']['total']) ? $arr['params']['total'] : 0, 'data'=>$list];
}elseif(isset($arr['msg'])){
throw new Exception($arr['msg']);
}else{
throw new Exception('查询接口请求失败');
}
}
private function execapi2($domain){
$timeStamp = time();
$authKey = md5("testtest" . $timeStamp);
$referer = 'https://beian.miit.gov.cn/';
$headers = ['Origin: https://beian.miit.gov.cn'];
$url = 'https://hlwicpfwc.miit.gov.cn/icpproject_query/api/auth';
$post = 'authKey='.$authKey.'&timeStamp='.$timeStamp;
$response = get_curl($url, $post, $referer, 0, 1, 0, 0, $headers);
$body = substr($response, strpos($response, '{"'));
$arr = json_decode($body, true);
if(isset($arr['code']) && $arr['code']==200){
$cookie = '';
preg_match_all('/set-cookie: (.*?);/i', $response, $matchs);
foreach ($matchs[1] as $val) {
if(substr($val,-1)=='=')continue;
$cookie.=$val.'; ';
}
$token = $arr['params']['bussiness'];
$url = 'https://hlwicpfwc.miit.gov.cn/icpproject_query/api/icpAbbreviateInfo/queryByCondition';
$post = json_encode(['pageNum'=>'','pageSize'=>'','unitName'=>$domain,'serviceType'=>1]);
$headers[] = 'Content-Type: application/json; charset=UTF-8';
$headers[] = 'token: '.$token;
$response = get_curl($url, $post, $referer, $cookie, 0, 0, 0, $headers);
$arr = json_decode($response, true);
if(isset($arr['code']) && $arr['code']==200){
$list = [];
foreach($arr['params']['list'] as $row){
$list[] = ['domain'=>$row['domain'], 'mainLicence'=>$row['mainLicence'], 'webLicence'=>$row['serviceLicence'], 'unitName'=>$row['unitName'], 'unitType'=>$row['natureName'], 'updateTime'=>$row['updateRecordTime'], 'limitAccess'=>$row['limitAccess'], 'contentTypeName'=>$row['contentTypeName'], 'dataId'=>$row['dataId']];
}
return ['code'=>0, 'total'=>$arr['params']['total'], 'data'=>$list];
}elseif(isset($arr['msg'])){
throw new Exception($arr['msg']);
}else{
throw new Exception('查询接口(query)请求失败');
}
}elseif(isset($arr['msg'])){
throw new Exception($arr['msg']);
}else{
throw new Exception('查询接口(auth)请求失败');
}
}
}