IP地址库解析


main();

function main(){
	$ip = isset($_GET["ip"]) ? $_GET["ip"] : "";

	if (empty($ip)) {
		IP::respone("101", "Empty IP");
		exit();
	}

	if (!preg_match("/(\d+\.){3}\d+/", $ip)) {
		IP::respone("102", "Wrong IP");
		exit();
	}

	$address = IP::find($ip);

	IP::respone("0", $address);
}

class IP {
	
    private static $ip     = NULL;
    private static $fp     = NULL;
    private static $offset = NULL;
    private static $index  = NULL;

    public static function find($ip)
    {
        if (empty($ip) === TRUE)
        {
            return "N/A";
        }

        $nip   = gethostbyname($ip);
        $ipdot = explode(".", $nip);

        if ($ipdot[0] < 0 || $ipdot[0] > 255 || count($ipdot) !== 4)
        {
            return "N/A";
        }

        if (self::$fp === NULL)
        {
            self::init();
        }

        $nip2 = pack("N", ip2long($nip));

        $tmp_offset = ((int)$ipdot[0] * 256 + (int)$ipdot[1]) * 4;
        $start      = unpack("Vlen", self::$index[$tmp_offset] . self::$index[$tmp_offset + 1] . self::$index[$tmp_offset + 2] . self::$index[$tmp_offset + 3]);

        $index_offset = $index_length = NULL;
        $max_comp_len = self::$offset["len"] - 262144 - 4;
        for ($start = $start["len"] * 9 + 262144; $start < $max_comp_len; $start += 9)
        {
            if (self::$index{$start} . self::$index{$start + 1} . self::$index{$start + 2} . self::$index{$start + 3} >= $nip2)
            {
                $index_offset = unpack("Vlen", self::$index{$start + 4} . self::$index{$start + 5} . self::$index{$start + 6} . "\x0");
                $index_length = unpack("nlen", self::$index{$start + 7} . self::$index{$start + 8});

                break;
            }
        }

        if ($index_offset === NULL)
        {
            return "N/A";
        }

        fseek(self::$fp, self::$offset["len"] + $index_offset["len"] - 262144);

        return explode("\t", fread(self::$fp, $index_length["len"]));
    }

    private static function init()
    {
        if (self::$fp === NULL)
        {
            self::$ip = new self();

            self::$fp = fopen(__DIR__ . "/ipdb.datx", "rb");
            if (self::$fp === FALSE)
            {
                throw new Exception("Invalid ipdb.datx file!");
            }

            self::$offset = unpack("Nlen", fread(self::$fp, 4));
            if (self::$offset["len"] < 4)
            {
                throw new Exception("Invalid ipdb.datx file!");
            }

            self::$index = fread(self::$fp, self::$offset["len"] - 4);
        }
    }

    public function __destruct()
    {
        if (self::$fp !== NULL)
        {
            fclose(self::$fp);

            self::$fp = NULL;
        }
    }

    public static function respone($code, $data){
        echo json_encode(["code" => $code, "data" => $data], JSON_UNESCAPED_UNICODE);
    }

}