dnsclient/start.php

80 lines
2.5 KiB
PHP

<?php
/**
* Workerman DNS Client Protocol
* @author Shanghai Laysense Information Technology Co. Ltd. [https://laysense.cn]<public@laysense.cn>
* @Repo https://git.laysense.com/Laysense/dnsclient
* @AzureDevOps https://dev.azure.com/laysense/HighSpeakerDNS/_git/dnsclient
* @license http://www.apache.org/licenses/ Apache License, Version 2.0
*/
use Workerman\Worker;
use Workerman\Timer;
use Workerman\Connection\AsyncUdpConnection;
require_once __DIR__ . '/vendor/autoload.php';
define('QUERYTIMEOUT',3);
class Client
{
function __construct() {
$this->server='119.29.29.29';
$this->id=str_pad(dechex(rand(0,65535)),4,"0",STR_PAD_LEFT);
}
public function query($type)
{
$id=$this->id;
$this->dnstype=$type;
$con = new Workerman\Connection\AsyncUdpConnection('Dnsc://'.$this->server.':53');
$con->timer_id = Timer::add(QUERYTIMEOUT, function()use($con)
{
$con->close();
Timer::del($con->timer_id);
echo 'timeout';
});
$con->onConnect = function($con) {
$con->send($this);
};
$con->onMessage = function($con, $data)use($id,$type) {
if($data['id']==$id){
Timer::del($con->timer_id);
print_r($data['answer']);
$con->close();
}
};
$con->connect();
#print_r($this);
}
public function domain(string $domain){
$domain=explode('.',$domain);
$hex='';
foreach($domain as $value){
$hex.=str_pad(dechex(strlen($value)),2,"0",STR_PAD_LEFT).bin2hex($value);
}
$this->data=$hex.'00';
return $this;
}
public function ptr(string $ip){
$iparray=array_reverse(explode('.',$ip));
$domain=join('.',$iparray).'.in-addr.arpa';
$domain=explode('.',$domain);
$hex='';
foreach($domain as $value){
$hex.=str_pad(dechex(strlen($value)),2,"0",STR_PAD_LEFT).bin2hex($value);
}
$this->data=$hex.'00';
return $this;
}
public function server($ip){
$this->server=$ip;
return $this;
}
}
$task = new Worker();
$task->onWorkerStart = function($task)
{
$query=new Client;
$query->server('8.8.8.8')->domain('cnnic.laysense.cn')->query('A');
#PTR Use↓
#$query->server('8.8.8.8')->ptr('119.29.29.29')->query('PTR');
};
Worker::runAll();
?>