ip6.biz/src/plain.php

40 lines
889 B
PHP

<?php
use MaxMind\Db\Reader;
header('Content-Type: application/json');
$ip = $_SERVER['REMOTE_ADDR'];
if (substr($ip, 0, 9) === "10.23.42.") {
$ip = "88.99.139.92";
}
$asndb = '/var/lib/GeoIP/GeoLite2-ASN.mmdb';
$asnr = (new Reader($asndb))->get($ip);
$res = array();
$res["address"] = $ip;
$res["hostname"] = gethost($ip);
$res["asn"] = "AS".$asnr["autonomous_system_number"]." ".$asnr["autonomous_system_organization"];
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;
}