增加CNAME+A和CNAME+AAAA记录,增加域名不存在应答

This commit is contained in:
Enoch 2022-12-18 18:01:07 +08:00
parent af2df369e6
commit 03b27f8cb1
5 changed files with 236 additions and 8 deletions

View File

@ -12,9 +12,9 @@
* MX * MX
* TXT * TXT
> 本仓库内vendor文件夹为[Workerman](https://www.workerman.net/) > 本仓库内vendor文件夹为[Workerman](https://www.workerman.net/)您可以删除.
> >
> 您可以删除,直接将本仓库根目录下的 Dns.php 放置到您的Workerman项目中的 /vendor/workerman/workerman/Protocols 目录下 > 直接将本仓库根目录下的 Dns.php 放置到您的Workerman项目中的 /vendor/workerman/workerman/Protocols 目录下即可使用
--- ---

View File

@ -33,7 +33,7 @@ if($type=='A'){
}; };
#此处进行一个CNAME+A记录返回的实例 #此处进行一个CNAME+A记录返回的实例
if($type=='A' && $name='cn.bing.com'){ if($type=='A' && $name=='cn.bing.com'){
$send['type']='CNAME+A'; $send['type']='CNAME+A';
#CNAME+A和CNAME+AAAA的情况下均只会返回一条CNAME如多条CNAME的均衡负载请通过您的代码在此处服务端实现 #CNAME+A和CNAME+AAAA的情况下均只会返回一条CNAME如多条CNAME的均衡负载请通过您的代码在此处服务端实现
$send['detail']='china.bing123.com'; $send['detail']='china.bing123.com';
@ -83,6 +83,13 @@ if($type=='AAAA'){
$send['detail'][2]=bin2hex($ipv6->ip2bin("2001:0:2851:b9d0:2c5f:f0d9:21be:4b96")); $send['detail'][2]=bin2hex($ipv6->ip2bin("2001:0:2851:b9d0:2c5f:f0d9:21be:4b96"));
$send['ttl']=600; $send['ttl']=600;
} }
#此处进行一个CNAME+AAAA记录返回的实例
if($type=='AAAA' && $name=='cname6.xx.com'){
$send['type']='CNAME+AAAA';
#CNAME+A和CNAME+AAAA的情况下均只会返回一条CNAME如多条CNAME的均衡负载请通过您的代码在此处服务端实现
$send['detail']='ipv6.xx.com';
$send['ttl']=30;
}
if($type=='TEXT'){ if($type=='TEXT'){
$send['type']='TEXT'; $send['type']='TEXT';
@ -102,8 +109,7 @@ if($type=='MX'){
$send['ttl']=600; $send['ttl']=600;
} }
#无记录情况下返回SOA记录或域名不存在记录防止报错 if($type=='SOA'){
if( (!isset($send['type'])) || (!isset($send['detail'])) || (!isset($send['ttl'])) || $type=='SOA'){
$send['type']='SOA'; $send['type']='SOA';
$send['detail']= array(); $send['detail']= array();
@ -129,7 +135,16 @@ if( (!isset($send['type'])) || (!isset($send['detail'])) || (!isset($send['ttl']
**/ **/
}; }
#返回域名不存在(自动获取SOA)
if($name=='phpisthebestlanguage.com'){
$send['type']='none';
$send['detail']="$name";
}
#id和query一般情况下直接返回输出即可 #id和query一般情况下直接返回输出即可
$send['id']=$data->id; $send['id']=$data->id;

View File

@ -1,3 +1,20 @@
<?php <?php
print_r(dns_get_record('cname.n2n.icu',DNS_A)); #print_r(dns_get_record('icu',DNS_SOA));
$url='al.c919.n3n.com.cn';
while(true){
preg_match("#\.(.*)#i",$url,$match);//获取根域名
$domin = $match[1];
print_r($domin);
$soa=dns_get_record($domin,DNS_SOA);
if(array_key_exists('0',$soa)){
if(array_key_exists('mname',$soa[0])){
print_r($soa[0]);
break;
}else{
$url=$domin;
}
}else{
$url=$domin;
}
}
?> ?>

View File

@ -685,3 +685,84 @@ Stack trace:
2022-12-18 04:11:46 pid:952 Workerman[start.php] start in DEBUG mode 2022-12-18 04:11:46 pid:952 Workerman[start.php] start in DEBUG mode
2022-12-18 04:13:27 pid:952 Workerman[start.php] stopping ... 2022-12-18 04:13:27 pid:952 Workerman[start.php] stopping ...
2022-12-18 04:13:27 pid:952 Workerman[start.php] has been stopped 2022-12-18 04:13:27 pid:952 Workerman[start.php] has been stopped
2022-12-18 08:42:28 pid:1472 Workerman[start.php] start in DEBUG mode
2022-12-18 08:43:12 pid:1518 Workerman[start.php] start in DEBUG mode
2022-12-18 08:43:36 pid:1519 Worker[1519] process terminated with ERROR: E_ERROR "Cannot redeclare Workerman\Protocols\ip2bin() (previously declared in /home/enoch/phpdns/vendor/workerman/workerman/Protocols/Dns.php:33) in /home/enoch/phpdns/vendor/workerman/workerman/Protocols/Dns.php on line 33"
2022-12-18 08:43:36 pid:1518 worker[none:1519] exit with status 65280
2022-12-18 08:43:38 pid:1518 Workerman[start.php] stopping ...
2022-12-18 08:43:38 pid:1518 Workerman[start.php] has been stopped
2022-12-18 08:44:06 pid:1634 Workerman[start.php] start in DEBUG mode
2022-12-18 08:44:08 pid:1635 Worker[1635] process terminated with ERROR: E_ERROR "Cannot redeclare Workerman\Protocols\ipbin() (previously declared in /home/enoch/phpdns/vendor/workerman/workerman/Protocols/Dns.php:33) in /home/enoch/phpdns/vendor/workerman/workerman/Protocols/Dns.php on line 33"
2022-12-18 08:44:08 pid:1634 worker[none:1635] exit with status 65280
2022-12-18 08:45:25 pid:1634 Workerman[start.php] stopping ...
2022-12-18 08:45:25 pid:1634 Workerman[start.php] has been stopped
2022-12-18 08:45:26 pid:1719 Workerman[start.php] start in DEBUG mode
2022-12-18 08:46:43 pid:1719 Workerman[start.php] stopping ...
2022-12-18 08:46:43 pid:1719 Workerman[start.php] has been stopped
2022-12-18 08:52:34 pid:2096 Workerman[start.php] start in DEBUG mode
2022-12-18 08:52:35 pid:2097 Error: Call to undefined function Workerman\Protocols\base_path() in /home/enoch/phpdns/vendor/workerman/workerman/Protocols/Dns.php:157
Stack trace:
#0 /home/enoch/phpdns/vendor/workerman/workerman/Connection/UdpConnection.php(73): Workerman\Protocols\Dns::encode()
#1 /home/enoch/phpdns/start.php(148): Workerman\Connection\UdpConnection->send()
#2 /home/enoch/phpdns/vendor/workerman/workerman/Worker.php(2569): {closure}()
#3 [internal function]: Workerman\Worker->acceptUdpConnection()
#4 /home/enoch/phpdns/vendor/workerman/workerman/Events/Event.php(193): EventBase->loop()
#5 /home/enoch/phpdns/vendor/workerman/workerman/Worker.php(2463): Workerman\Events\Event->loop()
#6 /home/enoch/phpdns/vendor/workerman/workerman/Worker.php(1574): Workerman\Worker->run()
#7 /home/enoch/phpdns/vendor/workerman/workerman/Worker.php(1416): Workerman\Worker::forkOneWorkerForLinux()
#8 /home/enoch/phpdns/vendor/workerman/workerman/Worker.php(1390): Workerman\Worker::forkWorkersForLinux()
#9 /home/enoch/phpdns/vendor/workerman/workerman/Worker.php(557): Workerman\Worker::forkWorkers()
#10 /home/enoch/phpdns/start.php(152): Workerman\Worker::runAll()
#11 {main}
2022-12-18 08:52:35 pid:2096 worker[none:2097] exit with status 64000
2022-12-18 08:52:37 pid:2096 Workerman[start.php] stopping ...
2022-12-18 08:52:37 pid:2096 Workerman[start.php] has been stopped
2022-12-18 09:00:43 pid:2352 Workerman[start.php] start in DEBUG mode
2022-12-18 09:01:19 pid:2352 Workerman[start.php] stopping ...
2022-12-18 09:01:19 pid:2352 Workerman[start.php] has been stopped
2022-12-18 09:01:19 pid:2425 Workerman[start.php] start in DEBUG mode
2022-12-18 09:02:34 pid:2425 Workerman[start.php] stopping ...
2022-12-18 09:02:34 pid:2425 Workerman[start.php] has been stopped
2022-12-18 09:03:21 pid:2700 Workerman[start.php] start in DEBUG mode
2022-12-18 09:10:26 pid:2700 Workerman[start.php] stopping ...
2022-12-18 09:10:26 pid:2700 Workerman[start.php] has been stopped
2022-12-18 09:10:27 pid:2859 Workerman[start.php] start in DEBUG mode
2022-12-18 09:10:38 pid:2859 Workerman[start.php] stopping ...
2022-12-18 09:10:38 pid:2859 Workerman[start.php] has been stopped
2022-12-18 09:41:48 pid:3697 Workerman[start.php] start in DEBUG mode
2022-12-18 09:41:56 pid:3743 Workerman[start.php] start in DEBUG mode
2022-12-18 09:42:08 pid:3743 Workerman[start.php] stopping ...
2022-12-18 09:42:08 pid:3743 Workerman[start.php] has been stopped
2022-12-18 09:42:09 pid:3795 Workerman[start.php] start in DEBUG mode
2022-12-18 09:45:18 pid:3795 Workerman[start.php] stopping ...
2022-12-18 09:45:18 pid:3795 Workerman[start.php] has been stopped
2022-12-18 09:45:18 pid:3964 Workerman[start.php] start in DEBUG mode
2022-12-18 09:47:40 pid:3964 Workerman[start.php] stopping ...
2022-12-18 09:47:40 pid:3964 Workerman[start.php] has been stopped
2022-12-18 09:47:41 pid:4041 Workerman[start.php] start in DEBUG mode
2022-12-18 09:48:02 pid:4041 Workerman[start.php] stopping ...
2022-12-18 09:48:02 pid:4041 Workerman[start.php] has been stopped
2022-12-18 09:48:03 pid:4089 Workerman[start.php] start in DEBUG mode
2022-12-18 09:49:00 pid:4089 Workerman[start.php] stopping ...
2022-12-18 09:49:00 pid:4089 Workerman[start.php] has been stopped
2022-12-18 09:49:01 pid:4189 Workerman[start.php] start in DEBUG mode
2022-12-18 09:49:06 pid:4189 Workerman[start.php] stopping ...
2022-12-18 09:49:06 pid:4189 Workerman[start.php] has been stopped
2022-12-18 09:49:33 pid:4275 Workerman[start.php] start in DEBUG mode
2022-12-18 09:49:38 pid:4275 Workerman[start.php] stopping ...
2022-12-18 09:49:38 pid:4275 Workerman[start.php] has been stopped
2022-12-18 09:50:02 pid:4340 Workerman[start.php] start in DEBUG mode
2022-12-18 09:51:44 pid:4340 Workerman[start.php] stopping ...
2022-12-18 09:51:44 pid:4340 Workerman[start.php] has been stopped
2022-12-18 09:51:44 pid:4421 Workerman[start.php] start in DEBUG mode
2022-12-18 09:52:29 pid:4421 Workerman[start.php] stopping ...
2022-12-18 09:52:29 pid:4421 Workerman[start.php] has been stopped
2022-12-18 09:52:30 pid:4469 Workerman[start.php] start in DEBUG mode
2022-12-18 09:59:07 pid:4469 Workerman[start.php] stopping ...
2022-12-18 09:59:07 pid:4469 Workerman[start.php] has been stopped
2022-12-18 09:59:08 pid:4676 Workerman[start.php] start in DEBUG mode
2022-12-18 10:00:05 pid:4676 Workerman[start.php] stopping ...
2022-12-18 10:00:06 pid:4676 Workerman[start.php] has been stopped
2022-12-18 10:00:06 pid:4770 Workerman[start.php] start in DEBUG mode
2022-12-18 10:00:16 pid:4770 Workerman[start.php] stopping ...
2022-12-18 10:00:16 pid:4770 Workerman[start.php] has been stopped

View File

@ -30,6 +30,7 @@ class Dns
*/ */
public static function encode($buffer) public static function encode($buffer)
{ {
$buffer=json_decode($buffer); $buffer=json_decode($buffer);
$type=$buffer->type; $type=$buffer->type;
switch($type){ switch($type){
@ -151,6 +152,57 @@ class Dns
$response=$buffer->id.$status.$questions.$AnswerRRs.$AuthorityRRs.$AdditionalRRs.$buffer->query.$answer; $response=$buffer->id.$status.$questions.$AnswerRRs.$AuthorityRRs.$AdditionalRRs.$buffer->query.$answer;
return hex2bin($response); return hex2bin($response);
break;
case 'CNAME+AAAA':
$type='0005';
$ns=$buffer->detail;
$nss=explode('.',$ns);
$detail[0]='';
foreach($nss as $part){
$len=str_pad(dechex(strlen($part)),2,"0",STR_PAD_LEFT);
$tpart=bin2hex($part);
$detail[0]=$detail[0].$len.$tpart;
};
$detail[0]=$detail[0].'00';
$lenth[0]=str_pad(dechex((strlen($detail[0])/2)),4,"0",STR_PAD_LEFT);
$ttl=str_pad(dechex($buffer->ttl),8,"0",STR_PAD_LEFT);
$answer='';
$answer=$answer.'C00C'.$type.'0001'.$ttl.$lenth[0].$detail[0];
$ip=dns_get_record($ns,DNS_AAAA);
$type='001C';
$n=0;
foreach($ip as $i){
$ipv6=$i['ipv6'];
$hexstr = unpack("H*hex", inet_pton($ipv6));
$ipv6=substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hexstr['hex']), 0, -1);
$ipv6=str_replace(':','',$ipv6);
#$ipv6= bin2hex($ipv6);
$detail[$n]="$ipv6";
$lenth[$n]="0010";
$n=$n+1;
};
$n=0;
foreach($detail as $c){
$rlenth='';
$rlenth=$lenth[$n];
$n=$n+1;
$answer=$answer.'C02C'.$type.'0001'.$ttl.$rlenth.$c;
}
$status='8180';
$questions='0001';
$AuthorityRRs='0000';
$AdditionalRRs='0000';
$AnswerRRs=str_pad((count((array)$ip)+1),4,"0",STR_PAD_LEFT);
$response=$buffer->id.$status.$questions.$AnswerRRs.$AuthorityRRs.$AdditionalRRs.$buffer->query.$answer;
return hex2bin($response);
break; break;
case 'SOA': case 'SOA':
$type='0006'; $type='0006';
@ -227,6 +279,69 @@ class Dns
$n=$n+1; $n=$n+1;
}; };
break; break;
case 'none':
$type='0006';
$ns=$buffer->detail;
$url=$ns;
while(true){
preg_match("#\.(.*)#i",$url,$match);//获取根域名
$domin = $match[1];
$soa=dns_get_record($domin,DNS_SOA);
if(array_key_exists('0',$soa)){
if(array_key_exists('mname',$soa[0])){
$qname=$domin;
$ns=$soa[0];
break;
}else{
$url=$domin;
}
}else{
$url=$domin;
}
}
$nss=explode('.',$ns['mname']);
$detail='';
foreach($nss as $part){
$len=str_pad(dechex(strlen($part)),2,"0",STR_PAD_LEFT);
$tpart=bin2hex($part);
$detail=$detail.$len.$tpart;
};
$detail=$detail.'00';
unset($nss,$len,$tpart);
$nss=explode('.',$ns['rname']);
foreach($nss as $part){
$len=str_pad(dechex(strlen($part)),2,"0",STR_PAD_LEFT);
$tpart=bin2hex($part);
$detail=$detail.$len.$tpart;
};
$detail=$detail.'00'.str_pad(dechex($ns['serial']),8,"0",STR_PAD_LEFT).str_pad(dechex($ns['refresh']),8,"0",STR_PAD_LEFT).str_pad(dechex($ns['retry']),8,"0",STR_PAD_LEFT).str_pad(dechex($ns['expire']),8,"0",STR_PAD_LEFT).str_pad(dechex($ns['minimum-ttl']),8,"0",STR_PAD_LEFT);
$lenth=str_pad(dechex((strlen($detail)/2)),4,"0",STR_PAD_LEFT);
$ttl=str_pad(dechex($buffer->ttl),8,"0",STR_PAD_LEFT);
$status='8183';
$questions='0001';
$AnswerRRs='0000';
$AuthorityRRs='0001';
$AdditionalRRs='0000';
#$qname
$nss=explode('.',$qname);
$qname='';
foreach($nss as $part){
#$len=strlen($part);
$len=str_pad(dechex(strlen($part)),2,"0",STR_PAD_LEFT);
$tpart=bin2hex($part);
$qname=$qname.$len.$tpart;
};
$qname=$qname.'00';
$answer='';
$answer=$answer.$qname.$type.'0001'.$ttl.$lenth.$detail;
$response=$buffer->id.$status.$questions.$AnswerRRs.$AuthorityRRs.$AdditionalRRs.$buffer->query.$answer;
return hex2bin($response);
break;
} }
$ttl=str_pad(dechex($buffer->ttl),8,"0",STR_PAD_LEFT); $ttl=str_pad(dechex($buffer->ttl),8,"0",STR_PAD_LEFT);
$status='8180'; $status='8180';