Update to 0.1.1
This commit is contained in:
parent
8fc2a4df78
commit
185d101099
@ -10,11 +10,27 @@
|
|||||||
namespace Workerman\Protocols;
|
namespace Workerman\Protocols;
|
||||||
class Dns
|
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){
|
public static function send($response,$query,$info){
|
||||||
$response=hex2bin($response);
|
$response=hex2bin($response);
|
||||||
$traffic=strlen($response);
|
$traffic=strlen($response);
|
||||||
$info=json_decode($info);
|
$info=json_decode($info);
|
||||||
#var_dump($info);
|
var_dump($info);
|
||||||
#出流量统计
|
#出流量统计
|
||||||
#您也可以在此处保存$response,下一次通过raw类型实现快速缓存相应.
|
#您也可以在此处保存$response,下一次通过raw类型实现快速缓存相应.
|
||||||
return $response;
|
return $response;
|
||||||
@ -426,18 +442,9 @@ class Dns
|
|||||||
$answerRRs=substr($data,12,4);
|
$answerRRs=substr($data,12,4);
|
||||||
$authorityRRs=substr($data,16,4);
|
$authorityRRs=substr($data,16,4);
|
||||||
$additionalRRs=substr($data,20,4);
|
$additionalRRs=substr($data,20,4);
|
||||||
$startbyte=24;
|
$gdomain=Dns::getDomain($data,24);
|
||||||
$dlen=substr($data,$startbyte,2);
|
$realname=$gdomain['name'];
|
||||||
$startbyte=26;
|
$startbyte=$gdomain['startbyte'];
|
||||||
$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);
|
|
||||||
$type=substr($data,$startbyte,4);
|
$type=substr($data,$startbyte,4);
|
||||||
switch($type){
|
switch($type){
|
||||||
case '0001':
|
case '0001':
|
||||||
@ -466,9 +473,54 @@ class Dns
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$query=substr($data,24,$startbyte-16);
|
$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;
|
return $returndata;
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ use dnstools\ipv6; #用于IPv6相关
|
|||||||
|
|
||||||
class DnsController
|
class DnsController
|
||||||
{
|
{
|
||||||
public function DNS($type,$name,$rip,$id,$query,$traffic=0)
|
public function DNS($type,$name,$rip,$ip,$id,$query,$traffic=0)
|
||||||
{
|
{
|
||||||
#输出信息
|
#输出信息
|
||||||
#echo "\n Type:$type \n Domain: $name\n Client IP: $rip \n";
|
#echo "\n Type:$type \n Domain: $name\n Client IP: $rip \n";
|
||||||
@ -44,8 +44,7 @@ class DnsController
|
|||||||
if(!isset($send['ttl'])){
|
if(!isset($send['ttl'])){
|
||||||
$send['ttl']=0;
|
$send['ttl']=0;
|
||||||
}
|
}
|
||||||
$send['info']=json_encode(['domain'=>$name,'querytype'=>$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']]); $return=json_encode($send);
|
||||||
$return=json_encode($send);
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,12 +12,16 @@ class DnsProcess
|
|||||||
$type=$data->type; #查询类型
|
$type=$data->type; #查询类型
|
||||||
$name=$data->name; #查询内容(一般是域名,PTR时为倒序IP)
|
$name=$data->name; #查询内容(一般是域名,PTR时为倒序IP)
|
||||||
$rip=$connection->getRemoteIp(); #客户端IP
|
$rip=$connection->getRemoteIp(); #客户端IP
|
||||||
|
if(isset($data->addR->csubnet->ip)){
|
||||||
|
$ip=$data->addR->csubnet->ip;
|
||||||
|
}else{
|
||||||
|
$ip=$rip;
|
||||||
|
}
|
||||||
#输出信息
|
#输出信息
|
||||||
#echo "\n Type:$type \n Domain: $name\n Client IP: $rip \n";
|
#echo "\n Type:$type \n Domain: $name\n Client IP: $rip \n";
|
||||||
|
|
||||||
$dns=new DnsController;
|
$dns=new DnsController;
|
||||||
$return=$dns->DNS($type,$name,$rip,$data->id,$data->query,$data->traffic);
|
$return=$dns->DNS($type,$name,$rip,$ip,$data->id,$data->query,$data->traffic);
|
||||||
$connection->send($return);
|
$connection->send($return);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user