diff --git a/README.md b/README.md index d22fa2a..34b9e0c 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Network Library for PHP with native Yggdrasil support #### Check socket is open -``` +``` php var_dump( \Yggverse\Net\Socket::isOpen('yo.index', 80) ); @@ -20,7 +20,7 @@ var_dump( #### Check host valid -``` +``` php var_dump( \Yggverse\Net\Socket::isHost('yo.index') ); @@ -28,7 +28,7 @@ var_dump( #### Check port valid -``` +``` php var_dump( \Yggverse\Net\Socket::isPort(80) ); @@ -38,7 +38,7 @@ var_dump( #### Resolve records -``` +``` php var_dump( \Yggverse\Net\Dig::records('yo.index', ['A', 'AAAA'], &$result = [], &$error = [], $provider = null, $timeout = 5) ); @@ -46,7 +46,7 @@ var_dump( #### Check hostname valid -``` +``` php var_dump( \Yggverse\Net\Dig::isHostName('yo.index') ); @@ -54,7 +54,7 @@ var_dump( #### Check record valid -``` +``` php var_dump( \Yggverse\Net\Dig::isRecord('A') ); @@ -62,12 +62,67 @@ var_dump( #### Check record value valid -``` +``` php var_dump( \Yggverse\Net\Dig::isRecordValue('A', '127.0.0.1') ); ``` +### Resolve + +#### Init resolver + +``` php +$resolve = new \Yggverse\Net\Resolve( + [ + 'A', + 'AAAA' + ], + [ + '1.1.1.1', + '8.8.8.8' + ], + // .. +); +``` + +#### Get resolved URL string + +``` php +$resolved = $resolve->url( + 'https://en.wikipedia.org/wiki/Domain_Name_System' + // next arguments contain debug variables and new address object +); + +if ($resolved) +{ + var_dump( + $resolved // https://185.15.59.224/wiki/Domain_Name_System + ); +} +``` + +#### Resolve Address object + +``` php +$resolved = $resolve->address( + new \Yggverse\Net\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. @@ -89,7 +144,7 @@ Different operations with address parts: **Document root** -``` +``` php $base = new \Yggverse\Net\Address( 'http://yo.ygg/a1/b1/c1' ); @@ -107,7 +162,7 @@ var_dump( **Current folder** -``` +``` php $base = new \Yggverse\Net\Address( 'http://yo.ygg/a1/b1/c1' ); @@ -125,7 +180,7 @@ var_dump( **Ending slash** -``` +``` php $base = new \Yggverse\Net\Address( 'http://yo.ygg/a1/b1/c1/' ); @@ -143,7 +198,7 @@ var_dump( **All options** -``` +``` php $base = new \Yggverse\Net\Address( 'http://user:password@yo.ygg/a1/b1/c1?attribute=value#anchor' ); @@ -159,7 +214,20 @@ var_dump( ); ``` +### Valid + +Network entities validation + +**Supported methods** + +* `Valid::ip` +* `Valid::ip4` +* `Valid::ip6` +* `Valid::domainHostName` + ## Integrations -* [Network API with native Yggdrasil/IPv6 support](https://github.com/YGGverse/web-api) -* [Yo! Crawler for different networks](https://github.com/YGGverse/Yo) +* [gemini-dl](https://github.com/YGGverse/gemini-dl) - CLI batch downloader for Gemini protocol +* [web-api](https://github.com/YGGverse/web-api) - Network API with native Yggdrasil/IPv6 support +* [Yo!](https://github.com/YGGverse/Yo) - Crawler and search engine for different networks +* [Yoda](https://github.com/YGGverse/Yoda) - Experimental PHP-GTK browser for Gemini protocol diff --git a/src/Address.php b/src/Address.php index f8b7e71..41d19c6 100644 --- a/src/Address.php +++ b/src/Address.php @@ -83,7 +83,9 @@ class Address public function isAbsolute(): bool { - return ($this->_scheme && $this->_host); + return boolval( + $this->_scheme || $this->_host + ); } public function isRelative(): bool @@ -198,28 +200,37 @@ class Address $this->_separator = $value; } - public function get(): string + public function get( + bool $scheme = true, + bool $user = true, + bool $pass = true, + bool $host = true, + bool $port = true, + bool $path = true, + bool $query = true, + bool $fragment = true, + ): string { $address = ''; - if ($scheme = $this->getScheme()) + if ($scheme && $this->getScheme()) { $address .= sprintf( '%s:%s%s', - $scheme, + $this->getScheme(), $this->getSeparator(), $this->getSeparator() ); } - if ($user = $this->getUser()) + if ($user && $this->getUser()) { - if ($pass = $this->getPass()) + if ($pass && $this->getPass()) { $address .= sprintf( '%s:%s@', - $user, - $pass + $this->getUser(), + $this->getPass() ); } @@ -227,65 +238,65 @@ class Address { $address .= sprintf( '%s@', - $user + $this->getUser() ); } } - if ($host = $this->getHost()) + if ($host && $this->getHost()) { - $address .= $host; + $address .= $this->getHost(); } - if ($port = $this->getPort()) + if ($port && $this->getPort()) { $address .= sprintf( ':%d', - $port + $this->getPort() ); } - if ($path = $this->getPath()) + if ($path && $this->getPath()) { - if (!str_starts_with($path, $this->getSeparator())) + if (!str_starts_with($this->getPath(), $this->getSeparator())) { $address .= $this->getSeparator(); } - $address .= $path; + $address .= $this->getPath(); } - if ($query = $this->getQuery()) + if ($query && $this->getQuery()) { $address .= sprintf( '?%s', - $query + $this->getQuery() ); } - if ($fragment = $this->getFragment()) + if ($fragment && $this->getFragment()) { $address .= sprintf( '#%s', - $fragment + $this->getFragment() ); } return $address; } - public function getAbsolute( + public function toAbsolute( \Yggverse\Net\Address $base - ): ?string + ): bool { if ($this->isAbsolute()) { - return $this->get(); + return true; } if ($base->isRelative()) { - return null; + return false; } $this->setScheme( @@ -314,7 +325,7 @@ class Address if (str_starts_with((string) $this->getPath(), $this->getSeparator())) { - return $this->get(); + return true; } if ($path = $this->getPath()) @@ -342,7 +353,7 @@ class Address { if (empty($prefix[$index])) { - return null; + return false; } unset( @@ -368,8 +379,23 @@ class Address ) ) ); + + return true; } - return $this->get(); + return false; + } + + // @TODO deprecated, legacy needs only + public function getAbsolute( + \Yggverse\Net\Address $base + ): ?string + { + if ($this->toAbsolute($base)) + { + return $this->get(); + } + + return null; } } \ No newline at end of file diff --git a/src/Resolve.php b/src/Resolve.php new file mode 100644 index 0000000..16f6d7e --- /dev/null +++ b/src/Resolve.php @@ -0,0 +1,204 @@ +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 url( + string $url, + array &$result = [], + array &$error = [], + ?\Yggverse\Net\Address &$resolved = null + ): ?string + { + $resolved = $this->address( + new \Yggverse\Net\Address( + $url + ), + $result, + $error + ); + + if ($resolved) + { + return $resolved->get(); + } + + return null; + } + + public function address( + \Yggverse\Net\Address $address, + array &$result = [], + array &$error = [] + ): ?\Yggverse\Net\Address + { + 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 (\Yggverse\Net\Valid::ip6($host)) + { + $address->setHost( + sprintf( + '[%s]', + $host + ) + ); + } + + else + { + $address->setHost( + $host + ); + } + + return $address; + } + } + } + + return null; + } +} \ No newline at end of file diff --git a/src/Valid.php b/src/Valid.php new file mode 100644 index 0000000..3d80ecc --- /dev/null +++ b/src/Valid.php @@ -0,0 +1,40 @@ +