diff --git a/README.md b/README.md index d22fa2a..1e54a7c 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,37 @@ var_dump( ); ``` +### Resolve + +``` +$resolve = new \Yggverse\Net\Resolve( + [ + 'A', + 'AAAA' + ], + [ + '1.1.1.1', + '8.8.8.8' + ], + // .. +); + +$resolved = $resolve->address( + 'https://en.wikipedia.org/wiki/Domain_Name_System' +); + +if ($resolved) +{ + var_dump( + $resolved->get() // https://185.15.59.224/wiki/Domain_Name_System + ); + + var_dump( + $resolved->getHost() // 185.15.59.224 + ); +} +``` + ### Address Includes methods to work with network addresses. diff --git a/src/Resolve.php b/src/Resolve.php new file mode 100644 index 0000000..be90e33 --- /dev/null +++ b/src/Resolve.php @@ -0,0 +1,185 @@ +setRecords( + $records + ); + } + + if ($providers) + { + $this->setProviders( + $providers + ); + } + + if ($timeout) + { + $this->setTimeout( + $timeout + ); + } + + $this->setShuffle( + $shuffle + ); + } + + public function getRecords(): array + { + return $this->_records; + } + + public function setRecords(array $records): void + { + foreach ($records as $record) + { + $this->addRecord( + $record + ); + } + } + + public function addRecord(string $record): void + { + $this->_records[] = $record; + + $this->_records = array_unique( + $this->_records + ); + } + + public function getProviders(): array + { + return $this->_providers; + } + + public function setProviders(array $providers): void + { + foreach ($providers as $provider) + { + $this->addProvider( + $provider + ); + } + } + + public function addProvider(string $provider): void + { + $this->_providers[] = $provider; + + $this->_providers = array_unique( + $this->_providers + ); + } + + public function setTimeout( + int $timeout + ): void + { + $this->_timeout = $timeout; + } + + public function getTimeout(): int + { + return $this->_timeout; + } + + public function setShuffle( + bool $shuffle + ): void + { + $this->_shuffle = $shuffle; + } + + public function isShuffle(): bool + { + return $this->_shuffle; + } + + public function address( + string $url, + array &$result = [], + array &$error = [] + ): ?\Yggverse\Net\Address + { + $address = new \Yggverse\Net\Address( + $url + ); + + foreach ($this->_providers as $provider) + { + foreach ( + \Yggverse\Net\Dig::records( + $address->getHost(), + $this->_records, + $result, + $error, + $provider, + $this->_timeout + ) as $record => $data + ) { + + if ($this->_shuffle) + { + shuffle( + $data + ); + } + + foreach ($data as $host) + { + if (false !== filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) + { + $resolved->setHost( + sprintf( + '[%s]', + $host + ) + ); + } + + else + { + $address->setHost( + $host + ); + } + + return $address; + } + } + } + + return null; + } +} \ No newline at end of file