From f06e4f058c935c5c99cb8a88b0959566abe85264 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Mon, 31 May 2021 19:21:57 +0200 Subject: [PATCH] Add hostname to plain.php --- src/plain.php | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/plain.php b/src/plain.php index a26f423..452740e 100644 --- a/src/plain.php +++ b/src/plain.php @@ -7,22 +7,33 @@ if (substr($ip, 0, 9) === "10.23.42.") { $ip = "116.202.163.154"; } -$res = array(); -$res["address"] = $ip; -$countrydb = '/var/lib/GeoIP/GeoLite2-Country.mmdb'; $asndb = '/var/lib/GeoIP/GeoLite2-ASN.mmdb'; - $asnr = (new Reader($asndb))->get($ip); -$res["country"] = (new Reader($countrydb))->get($ip)["country"]["iso_code"]; +$res = array(); +$res["address"] = $ip; +$res["hostname"] = gethost($ip); $res["asn"] = "AS".$asnr["autonomous_system_number"]." ".$asnr["autonomous_system_organization"]; -if ($res["country"] == null) { - $res["country"] = "_unknown"; -} - if ($asnr["autonomous_system_number"] == null) { $res["asn"] = "No GeoIP entry found, are you new?"; } print(json_encode($res, JSON_PRETTY_PRINT) . "\n"); + +function gethost($ip) +{ + $host = `host -s -W 5 $ip`; + $host = ($host ? end( explode(' ', trim(trim($host), '.'))) : $ip); + + if(in_array($host, Array('reached', 'record', '3(NXDOMAIN)'))) + { + return "none"; + } + + else if (in_array( $host, Array('2(SERVFAIL)'))){ + return "servfail"; + } + + return $host; +}