feat: Add domain Whois API

This commit is contained in:
overflowerror 2023-11-24 11:04:34 +01:00
parent 7708c7c0f1
commit f5d3f75d2e
2 changed files with 60 additions and 3 deletions

View file

@ -18,22 +18,41 @@ function getContactData(array $data) {
$kind = array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "kind"))[0][3] ?? null; $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"))); $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; $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[] = [ $contacts[] = [
"handle" => $entity["handle"], "handle" => $entity["handle"] ?? null,
"roles" => $entity["roles"], "roles" => $entity["roles"],
"function" => $fn, "function" => $fn,
"kind" => $kind, "kind" => $kind,
"address" => $adr, "address" => $adr,
"email" => $email, "email" => $email,
"remarks" => $remarks,
]; ];
} }
return $contacts; 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) { 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 [ return [
"query" => $ip, "query" => $ip,
"version" => $data["ipVersion"], "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) { return function (array $context) {
$result = null; $result = null;
@ -61,6 +104,10 @@ return function (array $context) {
} else { } else {
$result = whoisIp($ip); $result = whoisIp($ip);
} }
} elseif (key_exists("domain", $_GET)) {
$domain = $_GET["domain"];
$result = whoisDomain($domain);
} else { } else {
setStatusCode(400); setStatusCode(400);
$result = errorResponse("Unknown mode", "Please specify one of the following query parameters: ip"); $result = errorResponse("Unknown mode", "Please specify one of the following query parameters: ip");

View file

@ -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_RETURNTRANSFER, true);
curl_setopt($curlResource, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curlResource, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true); 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) if ($body)
curl_setopt($curlResource, CURLOPT_POSTFIELDS, $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); 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); curl_close($curlResource);