This commit is contained in:
Enoch 2024-02-06 17:49:08 +08:00
parent a3a8476bc4
commit 9130d8c2b0
6 changed files with 1557 additions and 79 deletions

80
Dns.php
View File

@ -10,11 +10,27 @@
namespace Workerman\Protocols;
class Dns
{
public static function getDomain($data,$startbyte){
$dlen=substr($data,$startbyte,2);
$startbyte=$startbyte+2;
$domain[0]='';
$i=0;
while($dlen!='00'){
$domain[$i]=hex2bin(substr($data,$startbyte,hexdec($dlen)*2));
$startbyte=$startbyte+(hexdec($dlen)*2);
$dlen=substr($data,$startbyte,2);
$startbyte=$startbyte+2;
$i++;
}
$realname=join(".",$domain);
$return=['name'=>$realname,'startbyte'=>$startbyte];
return $return;
}
public static function send($response,$query,$info){
$response=hex2bin($response);
$traffic=strlen($response);
$info=json_decode($info);
#var_dump($info);
var_dump($info);
#出流量统计
#您也可以在此处保存$response,下一次通过raw类型实现快速缓存相应.
return $response;
@ -426,18 +442,9 @@ class Dns
$answerRRs=substr($data,12,4);
$authorityRRs=substr($data,16,4);
$additionalRRs=substr($data,20,4);
$startbyte=24;
$dlen=substr($data,$startbyte,2);
$startbyte=26;
$i=1;
while($dlen!='00'){
$domain[$i]=hex2bin(substr($data,$startbyte,hexdec($dlen)*2));
$startbyte=$startbyte+(hexdec($dlen)*2);
$dlen=substr($data,$startbyte,2);
$startbyte=$startbyte+2;
$i++;
}
$realname=join(".",$domain);
$gdomain=Dns::getDomain($data,24);
$realname=$gdomain['name'];
$startbyte=$gdomain['startbyte'];
$type=substr($data,$startbyte,4);
switch($type){
case '0001':
@ -466,9 +473,54 @@ class Dns
break;
}
$query=substr($data,24,$startbyte-16);
#additionalRRs
if($authorityRRs=='0000'&&$additionalRRs=='0001'){
$addR=new \StdClass();
$startbyte=$startbyte+8;
$addR_name=Dns::getDomain($data,$startbyte);
$addR_rname=$addR_name['name'];
$startbyte=$addR_name['startbyte'];
if($addR_rname==''){
$addR_rname=$realname;
}
$addR_type=substr($data,$startbyte,4);
$startbyte=$startbyte+4;
$addR->realname=$addR_rname;
$addR->type=$addR_type;
#OPT
if($addR_type=='0029'){
#dns.rr.udp_playload_size,请求定义该值后响应将可突破512byte默认限制
$addR->playloadSize=hexdec(substr($data,$startbyte,4));
$addR->rcode=substr($data,$startbyte+4,2);#dns.resp.ext_rcode
$addR->edns0v=substr($data,$startbyte+6,2);#Edns0 拓展协议版本
$addR->Z=substr($data,$startbyte+8,4);
$addR->optLen=hexdec(substr($data,$startbyte+12,4))*2;
if($addR->optLen!=0){
$startbyte=$startbyte+16;
$opt=substr($data,$startbyte,$addR->optLen);
$opt_type=substr($opt,0,4);
if($opt_type=='0008'){
$addR->opt_type='CSUBNET';
$csubnet_len=hexdec(substr($opt,4,4))*2;
$csubnet_data=substr($opt,8,$csubnet_len);
$csubnet_family=substr($csubnet_data,0,4);
#IPv4
if($csubnet_family=='0001'){
$csubnet_source=substr($csubnet_data,4,2);
$csubnet_scope=substr($csubnet_data,6,2);
$csubnet_ip=long2ip(hexdec(substr(substr($csubnet_data,8,$csubnet_len-8).'00000000',0,8)));
$addR->csubnet=['family'=>$csubnet_family,'source'=>$csubnet_source,'scope'=>$csubnet_scope,'ip'=>$csubnet_ip];
}
}
}
}
}else{
$addR=null;
}
$returndata= json_encode(array('type' => $type, 'name' => "$realname", 'id'=>"$id", 'query'=>"$query",'traffic'=>$traffic));
$returndata= json_encode(array('type' => $type, 'name' => "$realname", 'id'=>"$id", 'query'=>"$query",'traffic'=>$traffic,'addR'=>$addR));
return $returndata;
}

View File

@ -17,6 +17,7 @@
- 8182SERVFAIL≈HTTP503 :服务器错误
- 8183NXDOMAIN≈HTTP404 :记录不存在
- 8185REFUSE≈HTTP403拒绝请求
- EDNS SubnetIP支持(当通过递归服务器发送请求时可获取到真实请求服务器的IP段)
# 安装
@ -129,7 +130,11 @@ $data=json_decode($data);
$type=$data->type; #查询类型
$name=$data->name; #查询内容(一般是域名PTR时为倒序IP)
$rip=$connection->getRemoteIp(); #客户端IP
if(isset($data->addR->csubnet->ip)){
$ip=$data->addR->csubnet->ip;
}else{
$ip=$rip;
}
# 请在下方编写您的DNS响应
#——————————————————————
@ -140,7 +145,7 @@ $send['query']=$data->query;
if(!isset($send['ttl'])){
$send['ttl']=0;
}
$send['info']=json_encode(['domain'=>$data->name,'querytype'=>$data->type,'answertype'=>$send['type'],'ip'=>$rip,'ttl'=>$send['ttl'],'detail'=>$send['detail']]);
$send['info']=json_encode(['domain'=>$data->name,'querytype'=>$data->type,'answertype'=>$send['type'],'ip'=>$ip,'rip'=>$rip,'ttl'=>$send['ttl'],'detail'=>$send['detail']]);
$send=json_encode($send);
$connection->send($send);
};
@ -159,6 +164,19 @@ Worker::runAll();
# 编写响应:
### 响应参数
您最终需要回复一个$send的数组。定义如下
| 名称 | 意义 | 类型 | 示例 | 说明 |
| --- | --- | --- | --- | --- |
| $send['type'] | 响应类型 | 字符串 | $send['type']=A | 指定响应的类型 |
| $send['detail'] | 响应内容 | 字符串、数组、多维数组 | 119.29.29.29或$send['detail'][1]='119.29.29.29'或$send['detail']= array(); | 根据不同的响应类型,返回不同的响应。具请往下阅读各种记录的响应方式 |
| $send['ttl'] | TTL缓存时间 | 数字 | $send['ttl']=600 | 并非所有记录都需要TTL未传入时默认0 |
| $send['flag'] | Flag类型 | 字符串 | $send['flag']=REFUSE | 使用flag类型时需指定的flag |
| $send['id'] | ID | 数字 | 0001 | 无需更改无需编写。 |
| $send['query'] | 请求体 | 16进制 | | 无需更改无需编写。 |
| $send['info'] | 请求和响应信息 | json | | 无需编写,基础框架内已经完成。 |
您应当根据$name变量进行响应。
@ -465,6 +483,26 @@ object(stdClass)#18 (6) {
其中对于流量将传递$data->traffic变量其为请求包体的大小(Byte)
# EDNS Subnet IP
这是一个DNS协议的扩展。支持的递归DNS服务器会向权威DNS发送请求客户端的IP地址。
WorkermanDNS支持对该协议提供的IPv4进行解析。
通过我们在上述示例框架start.php中的代码
```php
if(isset($data->addR->csubnet->ip)){
$ip=$data->addR->csubnet->ip;
}else{
$ip=$rip;
}
```
$ip参数将在支持EDNS SubnetIP的情况下优先使用SubnetIP不存在时则使用连接到的对端IP。
对于写入后端记录时rip与ip 参数都会始终提供。当不支持EDNS SubnetIP时rip与ip相同。否则ip是EDNS SubnetIPrip是udp对端IP。
# 运行
使用
@ -481,7 +519,11 @@ object(stdClass)#18 (6) {
DNS服务使用53端口为特权端口绝大多数情况下必须使用root权限运行。
### 版本
### 版本记录
[Current]0.1.1@2024/02/06
添加Edns SubnetIP支持
0.1.0@2024/01/31

View File

@ -20,6 +20,12 @@ $data=json_decode($data);
$type=$data->type; #查询类型
$name=$data->name; #查询内容(一般是域名PTR时为倒序IP)
$rip=$connection->getRemoteIp(); #客户端IP
if(isset($data->addR->csubnet->ip)){
$ip=$data->addR->csubnet->ip;
}else{
$ip=$rip;
}
if($type=='A'){
$send['type']='A';
@ -198,7 +204,7 @@ $send['query']=$data->query;
if(!isset($send['ttl'])){
$send['ttl']=0;
}
$send['info']=json_encode(['domain'=>$data->name,'querytype'=>$data->type,'answertype'=>$send['type'],'ip'=>$rip,'ttl'=>$send['ttl'],'detail'=>$send['detail']]);
$send['info']=json_encode(['domain'=>$data->name,'querytype'=>$data->type,'answertype'=>$send['type'],'ip'=>$ip,'rip'=>$rip,'ttl'=>$send['ttl'],'detail'=>$send['detail']]);
$send=json_encode($send);

View File

@ -1,48 +1,7 @@
<?php
$data='a7df00100001000000000001013103646e730457434c4d0264650000010001000029057800008000000b00080007000118007541b3';
$id=substr($data,0,4);
$flag=substr($data,4,4);
$questions=substr($data,8,4);
$answerRRs=substr($data,12,4);
$authorityRRs=substr($data,16,4);
$additionalRRs=substr($data,20,4);
$startbyte=24;
$dlen=substr($data,$startbyte,2);
$startbyte=26;
$i=1;
while($dlen!='00'){
$domain[$i]=hex2bin(substr($data,$startbyte,hexdec($dlen)*2));
$startbyte=$startbyte+(hexdec($dlen)*2);
$dlen=substr($data,$startbyte,2);
$startbyte=$startbyte+2;
$i++;
}
$name=join(".",$domain);
$type=substr($data,$startbyte,4);
switch($type){
case '0001':
$type='A';
break;
case '0002':
$type='NS';
break;
case '000c':
$type='PTR';
break;
case '0006':
$type='SOA';
break;
case '001c':
$type='AAAA';
break;
case '0005':
$type='CNAME';
break;
case '0010':
$type='TEXT';
break;
case '000f':
$type='MX';
break;
}
$query=substr($data,24,$startbyte-20);
use Workerman\Worker;
use Workerman\Protocols\Dns;
require_once __DIR__ . '/php-ipv6.php'; #IPv6支持
require_once __DIR__ . '/vendor/autoload.php';
echo long2ip(hexdec(substr('7541b3'.'00000000',0,8)));

File diff suppressed because it is too large Load Diff

View File

@ -10,11 +10,29 @@
namespace Workerman\Protocols;
class Dns
{
public static function getDomain($data,$startbyte){
$dlen=substr($data,$startbyte,2);
$startbyte=$startbyte+2;
$domain[0]='';
$i=0;
if($dlen!='00'){
while($dlen!='00'){
$domain[$i]=hex2bin(substr($data,$startbyte,hexdec($dlen)*2));
$startbyte=$startbyte+(hexdec($dlen)*2);
$dlen=substr($data,$startbyte,2);
$startbyte=$startbyte+2;
$i++;
}
}
$realname=join(".",$domain);
$return=['name'=>$realname,'startbyte'=>$startbyte];
return $return;
}
public static function send($response,$query,$info){
$response=hex2bin($response);
$traffic=strlen($response);
$info=json_decode($info);
#var_dump($info);
var_dump($info);
#出流量统计
#您也可以在此处保存$response,下一次通过raw类型实现快速缓存相应.
return $response;
@ -426,18 +444,9 @@ class Dns
$answerRRs=substr($data,12,4);
$authorityRRs=substr($data,16,4);
$additionalRRs=substr($data,20,4);
$startbyte=24;
$dlen=substr($data,$startbyte,2);
$startbyte=26;
$i=1;
while($dlen!='00'){
$domain[$i]=hex2bin(substr($data,$startbyte,hexdec($dlen)*2));
$startbyte=$startbyte+(hexdec($dlen)*2);
$dlen=substr($data,$startbyte,2);
$startbyte=$startbyte+2;
$i++;
}
$realname=join(".",$domain);
$gdomain=Dns::getDomain($data,24);
$realname=$gdomain['name'];
$startbyte=$gdomain['startbyte'];
$type=substr($data,$startbyte,4);
switch($type){
case '0001':
@ -466,9 +475,54 @@ class Dns
break;
}
$query=substr($data,24,$startbyte-16);
#additionalRRs
if($authorityRRs=='0000'&&$additionalRRs=='0001'){
$addR=new \StdClass();
$startbyte=$startbyte+8;
$addR_name=Dns::getDomain($data,$startbyte);
$addR_rname=$addR_name['name'];
$startbyte=$addR_name['startbyte'];
if($addR_rname==''){
$addR_rname=$realname;
}
$addR_type=substr($data,$startbyte,4);
$startbyte=$startbyte+4;
$addR->realname=$addR_rname;
$addR->type=$addR_type;
#OPT
if($addR_type=='0029'){
#dns.rr.udp_playload_size,请求定义该值后响应将可突破512byte默认限制
$addR->playloadSize=hexdec(substr($data,$startbyte,4));
$addR->rcode=substr($data,$startbyte+4,2);#dns.resp.ext_rcode
$addR->edns0v=substr($data,$startbyte+6,2);#Edns0 拓展协议版本
$addR->Z=substr($data,$startbyte+8,4);
$addR->optLen=hexdec(substr($data,$startbyte+12,4))*2;
if($addR->optLen!=0){
$startbyte=$startbyte+16;
$opt=substr($data,$startbyte,$addR->optLen);
$opt_type=substr($opt,0,4);
if($opt_type=='0008'){
$addR->opt_type='CSUBNET';
$csubnet_len=hexdec(substr($opt,4,4))*2;
$csubnet_data=substr($opt,8,$csubnet_len);
$csubnet_family=substr($csubnet_data,0,4);
#IPv4
if($csubnet_family=='0001'){
$csubnet_source=substr($csubnet_data,4,2);
$csubnet_scope=substr($csubnet_data,6,2);
$csubnet_ip=long2ip(hexdec(substr(substr($csubnet_data,8,$csubnet_len-8).'00000000',0,8)));
$addR->csubnet=['family'=>$csubnet_family,'source'=>$csubnet_source,'scope'=>$csubnet_scope,'ip'=>$csubnet_ip];
}
}
}
}
}else{
$addR=null;
}
$returndata= json_encode(array('type' => $type, 'name' => "$realname", 'id'=>"$id", 'query'=>"$query",'traffic'=>$traffic));
$returndata= json_encode(array('type' => $type, 'name' => "$realname", 'id'=>"$id", 'query'=>"$query",'traffic'=>$traffic,'addR'=>$addR));
return $returndata;
}