PHP DNS 工具

维护者

详细信息

github.com/anquanssl/dns

主页

源代码

安装: 21

依赖: 0

建议: 0

安全: 0

星标: 2

关注者: 0

分支: 6

v0.0.4 2024-03-12 14:34 UTC

This package is auto-updated.

Last update: 2024-09-12 15:51:18 UTC


README

PHP-7.4 PHP-8.0 PHP-8.1 PHP-8.2 PHPUnit codecov

使用特定的 DNS 处理器进行 DNS 询问

仅限 PHP >= 7.4

对于旧版 PHP 版本,我们强烈建议使用 bluelibraries/php5-dns

演示

示例

$records = DNS::getRecords('bluelibraries.com', RecordTypes::ANY);
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\NS Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 21600
                    [class] => IN
                    [type] => NS
                    [target] => ns3.instradns.com
                )
        )
    [1] => BlueLibraries\Dns\Records\Types\A Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 21600
                    [class] => IN
                    [type] => A
                    [ip] => 198.50.252.64
                )
        )
)

通过 composer 安装

composer require bluelibraries/dns

本包包含 4 种类型,可用于 DNS 询问

  1. DnsGetRecord 基于 PHP 函数 dns_get_record
  2. Dig 基于 shell 命令 dig(比 dns_get_record 更好,且依然安全)
  3. UDP 基于 raw DNS 调用使用 UDP/socket - 对于短回答查询很有用,因为 UDP 答案可能限制为 512 字节
  4. TCP 基于 raw DNS 调用使用 TCP/socket - 这是最优选择,并设置为 默认 处理器

DNS 处理器比较

DNS 处理器自定义设置

// Let's customize the DNS request handler - TCP
$dnsHandler = (new TCP())
    ->setPort(53)
    ->setNameserver('8.8.8.8')
    ->setTimeout(3) // limit execution to 3 seconds
    ->setRetries(5); // allows 5 retries if response fails

// Let's initialize the DNS records service
$dnsRecordsService = new DnsRecords($dnsHandler);

// let's get some TXT records from `bluelibraries.com`
$records = $dnsRecordsService->get('bluelibraries.com', RecordTypes::TXT);

// let's display them
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\TXT Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => TXT
                    [txt] => google-site-verification=kWtestq0tP8Ae_WJhRwUcZoqpdEkvuXJk
                )
        )
    [1] => BlueLibraries\Dns\Records\Types\TXT Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => TXT
                    [txt] => 55d34914-636b-4x-b349-fdb9f2c1eaca
                )
        )
)

UDP 和 DIG 类似

$dnsHandler = (new UDP())
    ->setPort(53)
    ->setNameserver('8.8.8.8')
    ->setTimeout(3) // limit execution to 3 seconds
    ->setRetries(5); // allows 5 retries if response fails

$dnsHandler = (new DIG())
    ->setPort(53)
    ->setNameserver('8.8.8.8')
    ->setTimeout(3) // limit execution to 3 seconds
    ->setRetries(5); // allows 5 retries if response fails

DnsGetRecord - 此处理器设置有限

// DnsGetRecord allows only Timeout and Retries, but there is no control over timeout
// so the timeout may be much longer than the limit we set!
$dnsHandler = (new DnsGetRecord())
    ->setTimeout(3) // limit execution to 3 seconds
    ->setRetries(5); // allows 5 retries if response fails

获取记录示例,等等...

使用 dns_get_record 获取记录

$records = DNS::getRecords('bluelibraries.com', RecordTypes::TXT, DnsHandlerTypes::DNS_GET_RECORD);
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\Txt\DomainVerification Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [class] => IN
                    [ttl] => 0
                    [type] => TXT
                    [txt] => google-site-verification=test-636b-4a56-b349-test
                )
        )
)

使用 dig 获取记录

$records = DNS::getRecords('bluelibraries.com', RecordTypes::TXT, DnsHandlerTypes::DIG);
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\Txt\DomainVerification Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [class] => IN
                    [ttl] => 0
                    [type] => TXT
                    [txt] => google-site-verification=test-636b-4a56-b349-test
                )
        )
)

使用 UDP 获取记录

$records = DNS::getRecords('bluelibraries.com', RecordTypes::TXT, DnsHandlerTypes::UDP);
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\Txt\DomainVerification Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [class] => IN
                    [ttl] => 0
                    [type] => TXT
                    [txt] => google-site-verification=test-636b-4a56-b349-test
                )
        )
)

使用 TCP 获取记录

// TCP is the default DNS handler, so if you are using it then you can skip it
$records = DNS::getRecords('bluelibraries.com', RecordTypes::TXT);
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\Txt\DomainVerification Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [class] => IN
                    [ttl] => 0
                    [type] => TXT
                    [txt] => google-site-verification=test-636b-4a56-b349-test
                )
        )
)

获取 TXT 记录

$records = DNS::getRecords('bluelibraries.com', RecordTypes::TXT);
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\Txt\DomainVerification Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3454
                    [class] => IN
                    [type] => TXT
                    [txt] => google-site-verification=kW9t2V_S7WjOX57zq0tP8Ae_WJhRwUcZoqpdEkvuXJk
                )
        )
    [1] => BlueLibraries\Dns\Records\Types\TXT Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3454
                    [class] => IN
                    [type] => TXT
                    [txt] => 55d14914-636b-4a56-b349-fdb9f2c1eaca
                )
        )
)

获取 A(地址)记录

$records = DNS::getRecords('bluelibraries.com', RecordTypes::A);
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\A Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => A
                    [ip] => 67.225.146.248
                )
        )
)

获取所有记录

$records = DNS::getRecords('bluelibraries.com', RecordTypes::ALL, DnsHandlerTypes::DIG);
print_r($records);
Array
(
    [0] => BlueLibraries\Dns\Records\Types\NS Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => NS
                    [target] => ns2.teestbluelibraries.com
                )
        )
    [1] => BlueLibraries\Dns\Records\Types\Txt\DomainVerification Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => TXT
                    [txt] => google-site-verification=errre
                )
        )
    [2] => BlueLibraries\Dns\Records\Types\NS Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => NS
                    [target] => tst3.bluelibraries.com
                )
        )
    [3] => BlueLibraries\Dns\Records\Types\TXT Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => TXT
                    [txt] => 55d34914-636b-4tes-b349-fdb9f2c1eaca
                )
        )
    [4] => BlueLibraries\Dns\Records\Types\A Object
        (
            [data:protected] => Array
                (
                    [host] => bluelibraries.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => A
                    [ip] => 67.225.146.248
                )
        )
)