diff --git a/composer.json b/composer.json index 62f6f7b..3c56d51 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "autoload": { "psr-4": { "CodeSpace\\WhoisParser\\": "src/", - "CodeSpace\\WhoisParser\\Parser\\": "src/parser/" + "CodeSpace\\WhoisParser\\Provider\\": "src/provider/" } }, "require": { diff --git a/src/ParserInterface.php b/src/ParserInterface.php deleted file mode 100644 index 2bf6559..0000000 --- a/src/ParserInterface.php +++ /dev/null @@ -1,19 +0,0 @@ -addAsn($list); + $this->addAbuse($list); + return [ + "network" => $list->parseInet(), + "route" => $list->parseRoute(), + "asn" => $list->parseAsn(), + "org" => $list->parseOrg(), + "abuse" => $list->parseAbuse() + ]; + } + + private function addAsn(WhoisList $list) { + $asn = $list->findAsn(); + if (!$asn) return; + $asn_whois = WhoisClient::query($asn); + $list->addResponse($asn_whois); + } + + private function addAbuse(WhoisList $list) { + $abuse_c = $list->findAbuseContact(); + if (!$abuse_c) return; + $abuse_whois = WhoisClient::query($abuse_c); + $list->addResponse($abuse_whois); + } + +} diff --git a/src/WhoisClient.php b/src/WhoisClient.php new file mode 100644 index 0000000..97290d2 --- /dev/null +++ b/src/WhoisClient.php @@ -0,0 +1,26 @@ +makeRequest($q); + return WhoisParser::fromString($response, Provider::tryFrom($server)); + } + + public static function getServer(string $q): ?string { + if (str_ends_with($q, "-RIPE")) return Provider::RIPE_WHOIS; + if (str_ends_with($q, "-AP")) return Provider::APNIC_WHOIS; + $res = self::queryServer($q, "whois.iana.org"); + return $res->getKeysValue(["refer", "whois"]); + } + +} diff --git a/src/WhoisList.php b/src/WhoisList.php new file mode 100644 index 0000000..5aab7fb --- /dev/null +++ b/src/WhoisList.php @@ -0,0 +1,57 @@ +addResponse($response); + } + + public function addResponse(?WhoisParser $response) { + if (!$response) return; + $provider = $response->getProvider(); + if (!$provider) return; + $this->responses[] = $response; + $this->providers[] = $provider; + } + + private function providerIter(string $fn) { + foreach ($this->providers as $provider) { + $res = $provider->$fn(); + if ($res) return $res; + } + return null; + } + + public function parseAsn(): ?array { + return $this->providerIter("parseAsn"); + } + + public function parseAbuse(): ?array { + return $this->providerIter("parseAbuse"); + } + + public function parseOrg(): ?array { + return $this->providerIter("parseOrg"); + } + + public function parseRoute(): ?array { + return $this->providerIter("parseRoute"); + } + + public function parseInet(): ?array { + return $this->providerIter("parseInet"); + } + + public function findAsn(): ?string { + return $this->providerIter("findAsn"); + } + + public function findAbuseContact(): ?string { + return $this->providerIter("findAbuseContact"); + } + +} diff --git a/src/WhoisParser.php b/src/WhoisParser.php index 213156b..43ee9dd 100644 --- a/src/WhoisParser.php +++ b/src/WhoisParser.php @@ -1,21 +1,32 @@ lines = $lines; $parser->provider = $provider; return $parser; } - public static function fromString(string $whois, string $provider) { + public static function fromString(string $whois, ?Provider $provider = null) { return self::fromArray(explode("\n", $whois), $provider); } + public function getProvider(): ?ProviderInterface { + if (!$this->provider) return null; + return match ($this->provider) { + Provider::RIPE => new RipeProvider($this), + Provider::APNIC => new ApNicProvider($this) + }; + } + private function getOffset(array $arr, int $index): mixed { return count($arr) > $index ? $arr[$index] : null; } diff --git a/src/provider/ApNicProvider.php b/src/provider/ApNicProvider.php new file mode 100644 index 0000000..23fc4b7 --- /dev/null +++ b/src/provider/ApNicProvider.php @@ -0,0 +1,3 @@ +getSectionWithKey("aut-num"); + private $whois; + + public function __construct(WhoisParser $whois) { + $this->whois = $whois; + } + + public function parseAsn(): ?array { + $as = $this->whois->getSectionWithKey("aut-num"); + if (!$as) return null; return [ "asn" => $as->getKeyValue("aut-num"), "name" => $as->getKeyValue("as-name"), @@ -14,8 +21,9 @@ class RipeParser implements ParserInterface { ]; } - public function parseAbuse(WhoisParser $whois): ?array { - $abuse = $whois->getSectionWithKey("abuse-mailbox"); + public function parseAbuse(): ?array { + $abuse = $this->whois->getSectionWithKey("abuse-mailbox"); + if (!$abuse) return null; return [ "role" => $abuse->getKeyValue("role"), "address" => $abuse->getKeyValues("address"), @@ -25,8 +33,9 @@ class RipeParser implements ParserInterface { ]; } - public function parseOrg(WhoisParser $whois): ?array { - $org = $whois->getSectionWithKey("organisation"); + public function parseOrg(): ?array { + $org = $this->whois->getSectionWithKey("organisation"); + if (!$org) return null; return [ "id" => $org->getKeyValue("organisation"), "name" => $org->getKeyValue("org-name"), @@ -37,8 +46,9 @@ class RipeParser implements ParserInterface { ]; } - public function parseRoute(WhoisParser $whois): ?array { - $section = $whois->getSectionWithKeys(["route", "route6"]); + public function parseRoute(): ?array { + $section = $this->whois->getSectionWithKeys(["route", "route6"]); + if (!$section) return null; $range = $section->getKeysValue(["route", "route6"]); return [ "range" => $range, @@ -47,8 +57,9 @@ class RipeParser implements ParserInterface { ]; } - public function parseInet(WhoisParser $whois): ?array { - $section = $whois->getSectionWithKeys(["inetnum", "inet6num"]); + public function parseInet(): ?array { + $section = $this->whois->getSectionWithKeys(["inetnum", "inet6num"]); + if (!$section) return null; $range = $section->getKeysValue(["inetnum", "inet6num"]); return [ "range" => $range, @@ -58,12 +69,12 @@ class RipeParser implements ParserInterface { ]; } - public function findAsn(WhoisParser $whois): ?string { - return $whois->getKeyValue("origin"); + public function findAsn(): ?string { + return $this->whois->getKeyValue("origin"); } - public function findAbuseContact(WhoisParser $whois): ?string { - return $whois->getKeyValue("abuse-c"); + public function findAbuseContact(): ?string { + return $this->whois->getKeyValue("abuse-c"); } }