Browse Source

implement resolve feature

main 1.6.0
yggverse 9 months ago
parent
commit
96cd4163be
  1. 31
      README.md
  2. 185
      src/Resolve.php

31
README.md

@ -68,6 +68,37 @@ var_dump( @@ -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.

185
src/Resolve.php

@ -0,0 +1,185 @@ @@ -0,0 +1,185 @@
<?php
declare(strict_types=1);
namespace Yggverse\Net;
class Resolve
{
private array $_providers =
[
'1.1.1.1',
'8.8.8.8'
];
private array $_records =
[
'A',
'AAAA'
];
private int $_timeout = 5;
private bool $_shuffle = false;
public function __construct(
?array $records = null,
?array $providers = null,
?int $timeout = null,
bool $shuffle = false
) {
if ($records)
{
$this->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;
}
}
Loading…
Cancel
Save