mirror of
https://github.com/YGGverse/net-php.git
synced 2025-08-26 13:42:01 +00:00
initial commit
This commit is contained in:
parent
646ed21a84
commit
df3342fc4b
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/vendor/
|
34
README.md
34
README.md
@ -1,2 +1,34 @@
|
|||||||
# dns-php
|
# dns-php
|
||||||
DNS Library for PHP
|
|
||||||
|
DNS Library for PHP with native Yggdrasil support
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
`composer require yggverse/dns`
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Resolve records
|
||||||
|
```
|
||||||
|
var_dump(
|
||||||
|
\Yggverse\Dns\Dig::records('yo.index', ['A', 'AAAA'])
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Check hostname valid
|
||||||
|
|
||||||
|
var_dump(
|
||||||
|
\Yggverse\Dns\Dig::isHostName('yo.index')
|
||||||
|
);
|
||||||
|
|
||||||
|
## Check record valid
|
||||||
|
|
||||||
|
var_dump(
|
||||||
|
\Yggverse\Dns\Dig::isRecord('A')
|
||||||
|
);
|
||||||
|
|
||||||
|
## Check record value valid
|
||||||
|
|
||||||
|
var_dump(
|
||||||
|
\Yggverse\Dns\Dig::isRecordValue('A', '127.0.0.1')
|
||||||
|
);
|
||||||
|
11
composer.json
Normal file
11
composer.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"name": "yggverse/dns",
|
||||||
|
"type": "library",
|
||||||
|
"license": "MIT",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Yggverse\\Dns\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"require": {}
|
||||||
|
}
|
58
src/Dig.php
Normal file
58
src/Dig.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Yggverse\Dns;
|
||||||
|
|
||||||
|
class Dig
|
||||||
|
{
|
||||||
|
private static function _records(): array
|
||||||
|
{
|
||||||
|
return
|
||||||
|
[
|
||||||
|
'A' => function(string $value): bool {return false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);},
|
||||||
|
'AAAA' => function(string $value): bool {return false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);},
|
||||||
|
// ...
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isHostName(string $hostname): bool
|
||||||
|
{
|
||||||
|
return false !== filter_var($hostname, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isRecord(string $record): bool
|
||||||
|
{
|
||||||
|
return isset(self::_records()[$record]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isRecordValue(string $record, string $value): bool
|
||||||
|
{
|
||||||
|
return isset(self::_records()[$record]) && self::_records()[$record]($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function records(string $hostname, array $records, array &$result = [], array &$error = []): array
|
||||||
|
{
|
||||||
|
if (self::isHostName($hostname))
|
||||||
|
{
|
||||||
|
foreach ($records as $record)
|
||||||
|
{
|
||||||
|
if (self::isRecord($record))
|
||||||
|
{
|
||||||
|
if ($values = exec(sprintf('dig %s %s +short', $record, $hostname)))
|
||||||
|
{
|
||||||
|
foreach (explode(PHP_EOL, $values) as $value)
|
||||||
|
{
|
||||||
|
if (self::isRecordValue($record, $value))
|
||||||
|
{
|
||||||
|
$result[$record][] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user