Browse Source

refactor: query of qqwry.dat and country.db

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

35
backend/getInfo.php

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

217
backend/qqwry.php

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

Loading…
Cancel
Save