amphp/dns

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. amphp/dns provides hostname to IP address resolution and querying specific DNS records.

Installation

This package can be installed as a Composer dependency.

composer require amphp/dns

Usage

Configuration

amphp/dns automatically detects the system configuration and uses it. On Unix-like systems it reads /etc/resolv.conf and respects settings for nameservers, timeouts, and attempts. On Windows it looks up the correct entries in the Windows Registry and takes the listed nameservers. You can pass a custom ConfigLoader instance to Rfc1035StubResolver to load another configuration, such as a static config.

It respects the system's hosts file on Unix and Windows based systems, so it works just fine in environments like Docker with named containers.

The package uses a global default resolver which can be accessed and changed via AmpDnsresolver(). If an argument other than null is given, the resolver is used as global instance.

Usually you don't have to change the resolver. If you want to use a custom configuration for a certain request, you can create a new resolver instance and use that instead of changing the global one.

Hostname to IP Resolution

AmpDnsresolve provides hostname to IP address resolution. It returns an array of IPv4 and IPv6 addresses by default. The type of IP addresses returned can be restricted by passing a second argument with the respective type.

// Example without type restriction. Will return IPv4 and / or IPv6 addresses. // What's returned depends on what's available for the given hostname. /** @var AmpDnsDnsRecord[] $records */ $records = AmpDnsresolve("github.com");

// Example with type restriction. Will throw an exception if there are no A records. /** @var AmpDnsDnsRecord[] $records */ $records = AmpDnsresolve("github.com", AmpDnsDnsRecord::A);

Custom Queries

AmpDnsquery supports the various other DNS record types such as MX, PTR, or TXT. It automatically rewrites passed IP addresses for PTR lookups.

/** @var AmpDnsDnsRecord[] $records */ $records = AmpDnsquery("google.com", AmpDnsDnsRecord::MX);

/** @var AmpDnsDnsRecord[] $records */ $records = AmpDnsquery("8.8.8.8", AmpDnsDnsRecord::PTR);

Caching

The Rfc1035StubResolver caches responses by default in an AmpCacheLocalCache. You can set any other AmpCacheCache implementation by creating a custom instance of Rfc1035StubResolver and setting that via AmpDnsresolver(), but it's usually unnecessary. If you have a lot of very short running scripts, you might want to consider using a local DNS resolver with a cache instead of setting a custom cache implementation, such as dnsmasq.

Reloading Configuration

The Rfc1035StubResolver (which is the default resolver shipping with that package) will cache the configuration of /etc/resolv.conf / the Windows Registry and the read host files by default. If you wish to reload them, you can set a periodic timer that requests a background reload of the configuration.

EventLoop::repeat(600, function () use ($resolver) { AmpDnsdnsResolver()->reloadConfig(); });

Note The above code relies on the resolver not being changed. reloadConfig is specific to Rfc1035StubResolver and is not part of the Resolver interface.

Example

<?php require __DIR__ . '/examples/_bootstrap.php'; $githubIpv4 = AmpDnsresolve("github.com", DnsRecord::A); pretty_print_records("github.com", $githubIpv4); $firstGoogleResult = AmpFutureawaitFirst([ Ampasync(fn() => AmpDnsresolve("google.com", DnsRecord::A)), Ampasync(fn() => AmpDnsresolve("google.com", DnsRecord::AAAA)), ]); pretty_print_records("google.com", $firstGoogleResult); $combinedGoogleResult = AmpDnsresolve("google.com"); pretty_print_records("google.com", $combinedGoogleResult); $googleMx = AmpDnsquery("google.com", AmpDnsDnsRecord::MX); pretty_print_records("google.com", $googleMx);

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。