From f5d3f75d2eb82ae54e60e732381dc232802e8ed3 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Fri, 24 Nov 2023 11:04:34 +0100 Subject: [PATCH] feat: Add domain Whois API --- controllers/whois/GET.php | 51 +++++++++++++++++++++++++++++++++++++-- utils/http.php | 12 ++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/controllers/whois/GET.php b/controllers/whois/GET.php index 0157c4d..c5ef313 100644 --- a/controllers/whois/GET.php +++ b/controllers/whois/GET.php @@ -18,22 +18,41 @@ function getContactData(array $data) { $kind = array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "kind"))[0][3] ?? null; $email = array_map(fn(array $a) => $a[3], array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "email"))); $adr = array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "adr"))[0]["label"] ?? null; + $remarks = array_map(fn($r) => $r["title"], $entity["remarks"] ?? []); $contacts[] = [ - "handle" => $entity["handle"], + "handle" => $entity["handle"] ?? null, "roles" => $entity["roles"], "function" => $fn, "kind" => $kind, "address" => $adr, "email" => $email, + "remarks" => $remarks, ]; } return $contacts; } +function fetchError(array $response) { + if ($response["status"] == 404) { + setStatusCode(404); + $description = "The requested object does not exist."; + } else { + setStatusCode(500); + $description = "Remote error: " . $response["error"]; + } + + return errorResponse("Unable to fetch results", $description); +} + function whoisIp(string $ip) { - $data = json_decode(getRequest(RDAP_URL . "/ip/" . $ip), true); + $response = getRequest(RDAP_URL . "/ip/" . $ip); + if ($response["isError"]) { + return fetchError($response); + } + + $data = json_decode($response["body"], true); return [ "query" => $ip, "version" => $data["ipVersion"], @@ -50,6 +69,30 @@ function whoisIp(string $ip) { ]; } +function whoisDomain(string $domain) { + $response = getRequest(RDAP_URL . "/domain/" . $domain); + if ($response["isError"]) { + return fetchError($response); + } + + $data = json_decode($response["body"], true); + + return [ + "query" => $domain, + "name" => $data["ldhName"], + "unicodeName" => $data["unicodeName"] ?? $data["ldhName"], + "nameservers" => array_map(fn($ns) => $ns["ldhName"], $data["nameservers"]), + "registered" => array_values( + array_filter( + $data["events"], + fn($e) => $e["eventAction"] == "registration" + ) + )[0]["eventDate"] ?? null, + + "contacts" => getContactData($data), + ]; +} + return function (array $context) { $result = null; @@ -61,6 +104,10 @@ return function (array $context) { } else { $result = whoisIp($ip); } + } elseif (key_exists("domain", $_GET)) { + $domain = $_GET["domain"]; + + $result = whoisDomain($domain); } else { setStatusCode(400); $result = errorResponse("Unknown mode", "Please specify one of the following query parameters: ip"); diff --git a/utils/http.php b/utils/http.php index 38822c7..4e822f7 100644 --- a/utils/http.php +++ b/utils/http.php @@ -7,6 +7,10 @@ function request(string $method, string $url, string $body = null, array $header curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlResource, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curlResource, CURLOPT_TIMEOUT, 10); + curl_setopt($curlResource, CURLOPT_FAILONERROR, true); + + curl_setopt($curlResource, CURLOPT_VERBOSE, true); if ($body) curl_setopt($curlResource, CURLOPT_POSTFIELDS, $body); @@ -19,7 +23,13 @@ function request(string $method, string $url, string $body = null, array $header curl_setopt($curlResource, CURLOPT_HTTPHEADER, $headers); } - $result = curl_exec($curlResource); + $body = curl_exec($curlResource); + $result = [ + "body" => $body, + "isError" => curl_errno($curlResource), + "error" => curl_error($curlResource), + "status" => curl_getinfo($curlResource, CURLINFO_RESPONSE_CODE), + ]; curl_close($curlResource);