Added whois response parser & RIPE parser
This commit is contained in:
parent
818365eb34
commit
1b45a5b6c9
5 changed files with 191 additions and 2 deletions
|
@ -5,7 +5,7 @@
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"CodeSpace\\WhoisParser\\": "src/",
|
"CodeSpace\\WhoisParser\\": "src/",
|
||||||
"CodeSpace\\WhoisParser\\Provider\\": "src/provider/"
|
"CodeSpace\\WhoisParser\\Parser\\": "src/parser/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
|
19
src/ParserInterface.php
Normal file
19
src/ParserInterface.php
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?php namespace CodeSpace\WhoisParser;
|
||||||
|
|
||||||
|
interface ParserInterface {
|
||||||
|
|
||||||
|
public function parseAsn(WhoisParser $whois): ?array;
|
||||||
|
|
||||||
|
public function parseAbuse(WhoisParser $whois): ?array;
|
||||||
|
|
||||||
|
public function parseOrg(WhoisParser $whois): ?array;
|
||||||
|
|
||||||
|
public function parseRoute(WhoisParser $whois): ?array;
|
||||||
|
|
||||||
|
public function parseInet(WhoisParser $whois): ?array;
|
||||||
|
|
||||||
|
public function findAsn(WhoisParser $whois): ?string;
|
||||||
|
|
||||||
|
public function findAbuseContact(WhoisParser $whois): ?string;
|
||||||
|
|
||||||
|
}
|
101
src/WhoisParser.php
Normal file
101
src/WhoisParser.php
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
<?php namespace CodeSpace\WhoisParser;
|
||||||
|
|
||||||
|
class WhoisParser {
|
||||||
|
|
||||||
|
public array $lines = [];
|
||||||
|
public string $provider;
|
||||||
|
|
||||||
|
public static function fromArray(array $lines, string $provider) {
|
||||||
|
$parser = new self();
|
||||||
|
$parser->lines = $lines;
|
||||||
|
$parser->provider = $provider;
|
||||||
|
return $parser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromString(string $whois, string $provider) {
|
||||||
|
return self::fromArray(explode("\n", $whois), $provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getOffset(array $arr, int $index): mixed {
|
||||||
|
return count($arr) > $index ? $arr[$index] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function workWithKeys(array $keys, $fn): array {
|
||||||
|
$num = 0;
|
||||||
|
$arr = [];
|
||||||
|
foreach ($this->lines as &$line) {
|
||||||
|
foreach ($keys as &$key) {
|
||||||
|
if (str_starts_with($line, "$key: ")) {
|
||||||
|
array_push($arr, $fn($line, $num));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$num++;
|
||||||
|
}
|
||||||
|
return $arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getKeyValues(string $key): ?array {
|
||||||
|
return $this->getKeysValues([$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getKeysValues(array $keys): ?array {
|
||||||
|
return $this->workWithKeys($keys, function($line, $num) {
|
||||||
|
$start = strpos($line, ": ") + 2;
|
||||||
|
return trim(mb_substr($line, $start));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getKeyValue(string $key, int $offset = 0): ?string {
|
||||||
|
return $this->getKeysValue([$key], $offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getKeysValue(array $keys, int $offset = 0): ?string {
|
||||||
|
$arr = $this->workWithKeys($keys, function($line, $num) {
|
||||||
|
$start = strpos($line, ": ") + 2;
|
||||||
|
return trim(substr($line, $start));
|
||||||
|
});
|
||||||
|
return $this->getOffset($arr, $offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
function findKey(string $key, int $offset = 0): ?int {
|
||||||
|
return $this->findFirstKey([$key], $offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
function findFirstKey(array $keys, int $offset = 0): ?int {
|
||||||
|
$arr = $this->workWithKeys($keys, function($line, $num) {
|
||||||
|
return $num;
|
||||||
|
});
|
||||||
|
return $this->getOffset($arr, $offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSection(int $line) {
|
||||||
|
$start = $end = $line;
|
||||||
|
$line = null;
|
||||||
|
while ($line !== "") {
|
||||||
|
$start--;
|
||||||
|
$line = $this->lines[$start];
|
||||||
|
}
|
||||||
|
$line_count = count($this->lines) - 1;
|
||||||
|
$line = null;
|
||||||
|
while ($line !== "" && $end < $line_count) {
|
||||||
|
$end++;
|
||||||
|
$line = $this->lines[$end];
|
||||||
|
}
|
||||||
|
return $this->getSubset($start, $end);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSubset(int $start, int $end) {
|
||||||
|
return self::fromArray(array_slice($this->lines, $start, $end-$start), $this->provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSectionWithKey(string $key) {
|
||||||
|
return $this->getSectionWithKeys([$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSectionWithKeys(array $keys): ?WhoisParser {
|
||||||
|
$line = $this->findFirstKey($keys);
|
||||||
|
if ($line == null) return null;
|
||||||
|
return $this->getSection($line);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
69
src/parser/RipeParser.php
Normal file
69
src/parser/RipeParser.php
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
<?php namespace CodeSpace\WhoisParser\Parser;
|
||||||
|
|
||||||
|
use CodeSpace\WhoisParser\ParserInterface;
|
||||||
|
use CodeSpace\WhoisParser\WhoisParser;
|
||||||
|
|
||||||
|
class RipeParser implements ParserInterface {
|
||||||
|
|
||||||
|
public function parseAsn(WhoisParser $whois): ?array {
|
||||||
|
$as = $whois->getSectionWithKey("aut-num");
|
||||||
|
return [
|
||||||
|
"asn" => $as->getKeyValue("aut-num"),
|
||||||
|
"name" => $as->getKeyValue("as-name"),
|
||||||
|
"source" => $as->getKeyValue("source")
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parseAbuse(WhoisParser $whois): ?array {
|
||||||
|
$abuse = $whois->getSectionWithKey("abuse-mailbox");
|
||||||
|
return [
|
||||||
|
"role" => $abuse->getKeyValue("role"),
|
||||||
|
"address" => $abuse->getKeyValues("address"),
|
||||||
|
"phone" => $abuse->getKeyValue("phone"),
|
||||||
|
"fax" => $abuse->getKeyValue("fax-no"),
|
||||||
|
"email" => $abuse->getKeyValue("abuse-mailbox")
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parseOrg(WhoisParser $whois): ?array {
|
||||||
|
$org = $whois->getSectionWithKey("organisation");
|
||||||
|
return [
|
||||||
|
"id" => $org->getKeyValue("organisation"),
|
||||||
|
"name" => $org->getKeyValue("org-name"),
|
||||||
|
"address" => $org->getKeyValues("address"),
|
||||||
|
"phone" => $org->getKeyValue("phone"),
|
||||||
|
"fax" => $org->getKeyValue("fax-no"),
|
||||||
|
"email" => $org->getKeyValue("e-mail")
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parseRoute(WhoisParser $whois): ?array {
|
||||||
|
$section = $whois->getSectionWithKeys(["route", "route6"]);
|
||||||
|
$range = $section->getKeysValue(["route", "route6"]);
|
||||||
|
return [
|
||||||
|
"range" => $range,
|
||||||
|
"description" => $section->getKeyValue("descr"),
|
||||||
|
"asn" => $section->getKeyValue("origin")
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parseInet(WhoisParser $whois): ?array {
|
||||||
|
$section = $whois->getSectionWithKeys(["inetnum", "inet6num"]);
|
||||||
|
$range = $section->getKeysValue(["inetnum", "inet6num"]);
|
||||||
|
return [
|
||||||
|
"range" => $range,
|
||||||
|
"name" => $section->getKeyValue("netname"),
|
||||||
|
"description" => $section->getKeyValue("descr"),
|
||||||
|
"country" => $section->getKeyValue("country")
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findAsn(WhoisParser $whois): ?string {
|
||||||
|
return $whois->getKeyValue("origin");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findAbuseContact(WhoisParser $whois): ?string {
|
||||||
|
return $whois->getKeyValue("abuse-c");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue