From 03b27f8cb14b2ce052634db63b93fd3f8155695f Mon Sep 17 00:00:00 2001 From: Enoch Date: Sun, 18 Dec 2022 18:01:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0CNAME+A=E5=92=8CCNAME+AAAA?= =?UTF-8?q?=E8=AE=B0=E5=BD=95,=E5=A2=9E=E5=8A=A0=E5=9F=9F=E5=90=8D?= =?UTF-8?q?=E4=B8=8D=E5=AD=98=E5=9C=A8=E5=BA=94=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 4 +- start.php | 23 +++- test.php | 21 +++- vendor/workerman/workerman.log | 81 +++++++++++++ vendor/workerman/workerman/Protocols/Dns.php | 115 +++++++++++++++++++ 5 files changed, 236 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index c1876ff..4241030 100644 --- a/readme.md +++ b/readme.md @@ -12,9 +12,9 @@ * MX * 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 目录下即可使用 --- diff --git a/start.php b/start.php index 33417e1..2930fc7 100644 --- a/start.php +++ b/start.php @@ -33,7 +33,7 @@ if($type=='A'){ }; #此处进行一个CNAME+A记录返回的实例 -if($type=='A' && $name='cn.bing.com'){ +if($type=='A' && $name=='cn.bing.com'){ $send['type']='CNAME+A'; #CNAME+A和CNAME+AAAA的情况下,均只会返回一条CNAME,如多条CNAME的均衡负载请通过您的代码在此处服务端实现 $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['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'){ $send['type']='TEXT'; @@ -102,8 +109,7 @@ if($type=='MX'){ $send['ttl']=600; } -#无记录情况下返回SOA记录或域名不存在记录,防止报错 -if( (!isset($send['type'])) || (!isset($send['detail'])) || (!isset($send['ttl'])) || $type=='SOA'){ +if($type=='SOA'){ $send['type']='SOA'; $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一般情况下直接返回输出即可 $send['id']=$data->id; diff --git a/test.php b/test.php index c027469..bf40a25 100644 --- a/test.php +++ b/test.php @@ -1,3 +1,20 @@ \ No newline at end of file +#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; +} +} +?> diff --git a/vendor/workerman/workerman.log b/vendor/workerman/workerman.log index f3d289a..52e74ea 100644 --- a/vendor/workerman/workerman.log +++ b/vendor/workerman/workerman.log @@ -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: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 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 diff --git a/vendor/workerman/workerman/Protocols/Dns.php b/vendor/workerman/workerman/Protocols/Dns.php index 82b3212..f44ee5d 100644 --- a/vendor/workerman/workerman/Protocols/Dns.php +++ b/vendor/workerman/workerman/Protocols/Dns.php @@ -30,6 +30,7 @@ class Dns */ public static function encode($buffer) { + $buffer=json_decode($buffer); $type=$buffer->type; switch($type){ @@ -151,6 +152,57 @@ class Dns $response=$buffer->id.$status.$questions.$AnswerRRs.$AuthorityRRs.$AdditionalRRs.$buffer->query.$answer; 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; case 'SOA': $type='0006'; @@ -227,6 +279,69 @@ class Dns $n=$n+1; }; 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); $status='8180';