Browse Source

refactor: query of qqwry.dat and country.db

pull/1/head
Dnomd343 4 years ago
parent
commit
2d17ca60dc
  1. 11
      backend/getCountry.php
  2. 35
      backend/getInfo.php
  3. 217
      backend/qqwry.php

11
backend/getCountry.php

@ -1,16 +1,13 @@
<?php
$db_path = 'country.db';
$db = new SQLiteDB;
class SQLiteDB extends SQLite3 {
class countryDB extends SQLite3 {
function __construct() {
global $db_path;
$this->open($db_path);
$this->open('country.db'); // 国家地区缩写及代号数据库
}
}
function get_country($code) {
global $db;
function getCountry($code) { // 根据两位国家代码获取英文与中文全称
$db = new countryDB;
if ($code == null) {
return null;
}

35
backend/getInfo.php

@ -4,10 +4,10 @@ include("getCountry.php");
include("qqwry.php");
function getIPInfo($ip) {
$qqwry = new IpLocation();
$qqwry = new QQWry();
$detail = $qqwry->getDetail($ip);
$specialIpInfo = getSpecialIpInfo($ip);
if (is_string($specialIpInfo)) {
$specialInfo = getSpecialInfo($ip);
if (is_string($specialInfo)) {
$info['ip'] = $ip;
$info['as'] = null;
$info['city'] = null;
@ -15,25 +15,23 @@ function getIPInfo($ip) {
$info['country'] = null;
$info['timezone'] = null;
$info['loc'] = null;
$info['isp'] = $specialIpInfo;
$info['cidr'] = $detail['cidr'];
$info['detail'] = $detail['addr'];
$info['isp'] = $specialInfo;
} else {
$rawIspInfo = getIspInfo($ip);
$rawIspInfo = getInfo($ip);
$info['ip'] = $ip;
$info['as'] = getAS($rawIspInfo);
$info['city'] = $rawIspInfo['city'];
$info['region'] = $rawIspInfo['region'];
$info['country'] = get_country($rawIspInfo['country'])['en'];
$info['country'] .= "(".get_country($rawIspInfo['country'])['cn'].")";
$info['country'] = getCountry($rawIspInfo['country'])['en'];
$info['country'] .= "(".getCountry($rawIspInfo['country'])['cn'].")";
$info['timezone'] = $rawIspInfo['timezone'];
$info['loc'] = $rawIspInfo['loc'];
$info['isp'] = getIsp($rawIspInfo);
$info['cidr'] = $detail['cidr'];
$info['detail'] = $detail['addr'];
}
$info['cidr'] = $detail['beginIP'] . ' - ' . $detail['endIP'];
$info['detail'] = $detail['dataA'] . $detail['dataB'];
if ($_GET['cli'] == "true") {
if ($_GET['cli'] == "true") { // 使用命令行模式
$cli = "IP: ".$info['ip'].PHP_EOL;
$cli .= "AS: ".$info['as'].PHP_EOL;
$cli .= "City: ".$info['city'].PHP_EOL;
@ -47,11 +45,11 @@ function getIPInfo($ip) {
return $cli;
}
header('Content-Type: application/json; charset=utf-8');
header('Content-Type: application/json; charset=utf-8'); // 以JSON格式发送
return json_encode($info);
}
function getSpecialIpInfo($ip) {
function getSpecialInfo($ip) { // 识别特殊IP地址
if ('::1' === $ip) {
return 'localhost IPv6 access';
}
@ -76,12 +74,11 @@ function getSpecialIpInfo($ip) {
return null;
}
function getIspInfo($ip) {
$json = file_get_contents('https://ipinfo.io/'.$ip.'/json');
function getInfo($ip) { // 获取IP详细信息
$json = file_get_contents('https://ipinfo.io/' . $ip . '/json');
if (!is_string($json)) {
return null;
}
$data = json_decode($json, true);
if (!is_array($data)) {
return null;
@ -89,7 +86,7 @@ function getIspInfo($ip) {
return $data;
}
function getIsp($rawIspInfo) {
function getIsp($rawIspInfo) { // 提取ISP信息
if (
!is_array($rawIspInfo)
|| !array_key_exists('org', $rawIspInfo)
@ -101,7 +98,7 @@ function getIsp($rawIspInfo) {
return preg_replace('/AS\\d+\\s/', '', $rawIspInfo['org']);
}
function getAS($rawIspInfo) {
function getAS($rawIspInfo) { // 提取AS信息
if (
!is_array($rawIspInfo)
|| !array_key_exists('org', $rawIspInfo)

217
backend/qqwry.php

@ -1,17 +1,17 @@
<?php
class IpLocation {
class QQWry {
private $fp; // 文件指针
private $firstip; // 第一条记录的偏移地址
private $lastip; // 最后一条记录的偏移地址
private $totalip; // 总记录条数
private $firstRecord; // 第一条记录的偏移地址
private $lastRecord; // 最后一条记录的偏移地址
private $recordNum; // 总记录条数
public function __construct() { // 构造函数
$this->fp = 0;
if (($this->fp = fopen(__DIR__.'/qqwry.dat', 'rb')) !== false) {
$this->firstip = $this->getlong();
$this->lastip = $this->getlong();
$this->totalip = ($this->lastip - $this->firstip) / 7;
if (($this->fp = fopen(__DIR__ . '/qqwry.dat', 'rb')) !== false) {
$this->firstRecord = $this->read4byte();
$this->lastRecord = $this->read4byte();
$this->recordNum = ($this->lastRecord - $this->firstRecord) / 7; // 每条索引长度为7字节
}
}
@ -22,129 +22,132 @@ class IpLocation {
$this->fp = 0;
}
public function getDetail($ip) { // 获取IP地址区段及所在位置
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { // 判断IP是否有效
return null;
}
$location = $this->getLocation($ip);
if (!$location) {
return null;
}
$detail['cidr'] = $location['beginip'] . ' - ' . $location['endip'];
$detail['addr'] = $location['country'] . $location['area'];
return $detail;
}
private function getlong() { // 将读取的4字节转化为长整型数
private function read4byte() { // 读取4字节并转为long
return unpack('Vlong', fread($this->fp, 4))['long'];
}
private function getlong3() { // 将读取的3字节转化为长整型数
private function read3byte() { // 读取3字节并转为long
return unpack('Vlong', fread($this->fp, 3) . chr(0))['long'];
}
private function ip2long($ip) { // 将IP地址转为数字地址
private function readString() { // 读取字符串
$str = '';
$char = fread($this->fp, 1);
while (ord($char) != 0) { // 读到二进制0结束
$str .= $char;
$char = fread($this->fp, 1);
}
return $str;
}
private function zipIP($ip) { // IP地址转为数字
$ip_arr = explode('.', $ip);
return (16777216 * intval($ip_arr[0])) + (65536 * intval($ip_arr[1])) + (256 * intval($ip_arr[2])) + intval($ip_arr[3]);
$tmp = (16777216 * intval($ip_arr[0])) + (65536 * intval($ip_arr[1])) + (256 * intval($ip_arr[2])) + intval($ip_arr[3]);
return pack('N', intval($tmp)); // 32位无符号大端序长整型
}
private function packip($ip) { // 计算压缩后的IP地址
return pack('N', intval($this->ip2long($ip)));
private function unzipIP($ip) { // 数字转为IP地址
return long2ip($ip);
}
private function getstring($data = "") { // 读取字符串
$char = fread($this->fp, 1);
while (ord($char) > 0) { // 字符串读取到\0结束
$data .= $char;
$char = fread($this->fp, 1);
}
return $data;
public function getVersion() { // 获取当前数据库的版本
fseek($this->fp, $this->lastRecord + 4);
$tmp = $this->getRecord($this->read3byte())['B'];
return substr($tmp, 0, 4) . substr($tmp, 7, 2) . substr($tmp, 12, 2);
}
private function getArea() { // 获取地区信息
$flag = fread($this->fp, 1); // 标志字节
if (ord($flag) == 0) { // 无区域信息
return '';
} else if (ord($flag) == 1 || ord($flag) == 2) { // 区域信息被重定向
fseek($this->fp, $this->getlong3());
return $this->getstring();
} else { // 区域信息未被重定向
return $this->getstring($flag);
public function getDetail($ip) { // 获取IP地址区段及所在位置
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { // 判断是否为IPv4地址
return null;
}
}
fseek($this->fp, $this->searchRecord($ip)); // 跳转到对应IP记录的位置
$detail['beginIP'] = $this->unzipIP($this->read4byte()); // 目标IP所在网段的起始IP
$offset = $this->read3byte(); // 索引后3字节为对应记录的偏移量
fseek($this->fp, $offset);
$detail['endIP'] = $this->unzipIP($this->read4byte()); // 目标IP所在网段的结束IP
private function getLocation($ip) { // 根据IP地址返回地区信息
$ip = $this->packip($ip);
$l = 0; // 搜索下边界
$u = $this->totalip; // 搜索上边界
$findip = $this->lastip; // 若未找到则返回最后一条记录
$tmp = $this->getRecord($offset); // 获取记录的dataA与dataB
$detail['dataA'] = $tmp['A'];
$detail['dataB'] = $tmp['B'];
if ($detail['beginIP'] == '255.255.255.0') { // 去除附加信息
$detail['dataA'] = 'IANA';
$detail['dataB'] = '保留地址';
}
if ($detail['dataA'] == ' CZ88.NET' || $detail['dataA'] == '纯真网络') {
$detail['dataA'] = '';
}
if ($detail['dataB'] == ' CZ88.NET') {
$detail['dataB'] = '';
}
return $detail;
}
while ($l <= $u) { // 发起查找
$i = floor(($l + $u) / 2); // 计算二分点
fseek($this->fp, $this->firstip + $i * 7);
$beginip = strrev(fread($this->fp, 4)); // 获取二分点所在区域的下边界
if ($ip < $beginip) { // 目标IP小于二分区域的下边界
$u = $i - 1; // 搜索的上边界缩小到二分点以下
} else { // 目标IP大于或等于二分区域的下边界
fseek($this->fp, $this->getlong3());
private function searchRecord($ip) { // 根据IP地址获取索引的绝对偏移量
$ip = $this->zipIP($ip); // 转为数字以比较大小
$down = 0;
$up = $this->recordNum;
while ($down <= $up) { // 二分法查找
$mid = floor(($down + $up) / 2); // 计算二分点
fseek($this->fp, $this->firstRecord + $mid * 7);
$beginip = strrev(fread($this->fp, 4)); // 获取二分区域的下边界
if ($ip < $beginip) { // 目标IP在二分区域以下
$up = $mid - 1; // 缩小搜索的上边界
} else {
fseek($this->fp, $this->read3byte());
$endip = strrev(fread($this->fp, 4)); // 获取二分区域的上边界
if ($ip > $endip) { // 目标IP大于二分区域的上边界
$l = $i + 1; // 搜索的下边界缩小到二分点以上
if ($ip > $endip) { // 目标IP在二分区域以上
$down = $mid + 1; // 缩小搜索的下边界
} else { // 目标IP在二分区域内
$findip = $this->firstip + $i * 7;
break;
return $this->firstRecord + $mid * 7; // 返回索引的偏移量
}
}
}
fseek($this->fp, $findip);
$location['beginip'] = long2ip($this->getlong()); // 目标IP所在区域的下边界
$offset = $this->getlong3();
fseek($this->fp, $offset);
$location['endip'] = long2ip($this->getlong()); // 目标IP所在区域的上边界
return $this->lastRecord; // 无法找到对应索引,返回最后一条记录的偏移量
}
//获取目标IP的位置信息
$byte = fread($this->fp, 1); // 标志字节
switch (ord($byte)) {
case 1: // 国家和区域信息均被重定向
$countryOffset = $this->getlong3(); // 重定向地址
fseek($this->fp, $countryOffset);
$byte = fread($this->fp, 1); // 标志字节
switch (ord($byte)) {
case 2: // 国家信息被重定向
fseek($this->fp, $this->getlong3());
$location['country'] = $this->getstring();
fseek($this->fp, $countryOffset + 4);
$location['area'] = $this->getArea();
break;
default: // 国家信息未被重定向
$location['country'] = $this->getstring($byte);
$location['area'] = $this->getArea();
break;
}
break;
case 2: // 国家信息被重定向
fseek($this->fp, $this->getlong3());
$location['country'] = $this->getstring();
fseek($this->fp, $offset + 8);
$location['area'] = $this->getArea();
break;
default: // 国家信息未被重定向
$location['country'] = $this->getstring($byte);
$location['area'] = $this->getArea();
break;
private function getRecord($offset) { // 读取IP记录的数据
fseek($this->fp, $offset + 4);
$flag = ord(fread($this->fp, 1));
if ($flag == 1) { // dataA与dataB均重定向
$offset = $this->read3byte(); // 重定向偏移
fseek($this->fp, $offset);
if (ord(fread($this->fp, 1)) == 2) { // dataA再次重定向
fseek($this->fp, $this->read3byte());
$data['A'] = $this->readString();
fseek($this->fp, $offset + 4);
$data['B'] = $this->getDataB();
} else { // dataA无重定向
fseek($this->fp, -1, SEEK_CUR); // 文件指针回退1字节
$data['A'] = $this->readString();
$data['B'] = $this->getDataB();
}
} else if ($flag == 2) { // dataA重定向
fseek($this->fp, $this->read3byte());
$data['A'] = $this->readString();
fseek($this->fp, $offset + 8); // IP占4字节, 重定向标志占1字节, dataA指针占3字节
$data['B'] = $this->getDataB();
} else { // dataA无重定向
fseek($this->fp, -1, SEEK_CUR); // 文件指针回退1字节
$data['A'] = $this->readString();
$data['B'] = $this->getDataB();
}
$data['A'] = iconv("GBK", "UTF-8", $data['A']); // GBK -> UTF-8
$data['B'] = iconv("GBK", "UTF-8", $data['B']);
return $data;
}
// 转为UTF-8编码
$location['country'] = iconv("GBK", "UTF-8", $location['country']);
$location['area'] = iconv("GBK", "UTF-8", $location['area']);
// 去除附带信息
if ($location['country'] == " CZ88.NET" || $location['country'] == "纯真网络") {
$location['country'] = "Unknow";
}
if ($location['area'] == " CZ88.NET") {
$location['area'] = "";
private function getDataB() { // 从fp指定偏移获取dataB
$flag = ord(fread($this->fp, 1));
if ($flag == 0) { // dataB无信息
return '';
} else if ($flag == 1 || $flag == 2) { // dataB重定向
fseek($this->fp, $this->read3byte());
return $this->readString();
} else { // dataB无重定向
fseek($this->fp, -1, SEEK_CUR); // 文件指针回退1字节
return $this->readString();
}
return $location;
}
}

Loading…
Cancel
Save