qqwry.dat 是國內廣泛使用的 IP 地址庫,格式特點:
<?php
class QQWryIpLocator
{
private string $filePath;
private $fp; // 檔案句柄或資源
private int $firstIndexOffset;
private int $lastIndexOffset;
private int $totalRecords;
private bool $useMmap = false;
private string $mmapData = '';
public function __construct(string $filePath, bool $useMmap = false)
{
$this->filePath = $filePath;
$this->useMmap = $useMmap && function_exists('mmap'); // 需擴展支援
if ($this->useMmap) {
$this->mmapData = file_get_contents($filePath); // 簡易版:全讀入記憶體
} else {
$this->fp = fopen($filePath, 'rb');
}
$this->initIndex();
}
private function initIndex(): void
{
$this->firstIndexOffset = $this->readLong4(0);
$this->lastIndexOffset = $this->readLong4(4);
$this->totalRecords = ($this->lastIndexOffset - $this->firstIndexOffset) / 7;
}
/** 讀取 4 字節無符號長整數 */
private function readLong4(int $offset): int
{
$bytes = $this->readBytes($offset, 4);
return unpack('V', $bytes)[1]; // V = little-endian unsigned long
}
/** 讀取 3 字節偏移(補齊 1 字節 0) */
private function readLong3(int $offset): int
{
$bytes = $this->readBytes($offset, 3) . "\x00";
return unpack('V', $bytes)[1];
}
private function readBytes(int $offset, int $length): string
{
if ($this->useMmap) {
return substr($this->mmapData, $offset, $length);
}
fseek($this->fp, $offset);
return fread($this->fp, $length);
}
/** 讀取以 \0 結尾的字串 */
private function readString(int $offset): string
{
$str = '';
while (true) {
$char = $this->readBytes($offset++, 1);
if ($char === "\x00" || $char === '') break;
$str .= $char;
}
return $str;
}
/** 核心查詢:二分查找索引區 */
public function query(string $ip): ?array
{
if (!$this->validateIp($ip)) return null;
$ipLong = ip2long($ip);
if ($ipLong === false) return null;
// ip2long 回傳有號整數,轉無符號比較
$ipLong = sprintf('%u', $ipLong);
$low = 0;
$high = $this->totalRecords - 1;
$foundOffset = 0;
while ($low <= $high) {
$mid = (int)(($low + $high) / 2);
$indexOffset = $this->firstIndexOffset + $mid * 7;
$startIp = $this->readLong4($indexOffset);
$recordOffset = $this->readLong3($indexOffset + 4);
$endIp = $this->readLong4($recordOffset);
if ($ipLong < $startIp) {
$high = $mid - 1;
} elseif ($ipLong > $endIp) {
$low = $mid + 1;
} else {
$foundOffset = $recordOffset + 4; // 跳過結束 IP
break;
}
}
if ($foundOffset === 0) return null;
return $this->parseRecord($foundOffset);
}
private function parseRecord(int $offset): array
{
$byte = $this->readBytes($offset, 1);
$mode = ord($byte);
$country = '';
$area = '';
switch ($mode) {
case 0x01: // 國家和地區都重定向
$countryOffset = $this->readLong3($offset + 1);
$country = $this->parseCountry($countryOffset);
$area = $this->parseArea($countryOffset + strlen($country) + 1);
break;
case 0x02: // 國家重定向,地區不重定向
$countryOffset = $this->readLong3($offset + 1);
$country = $this->parseCountry($countryOffset);
$area = $this->parseArea($offset + 4);
break;
default: // 國家不重定向
$country = $this->parseCountry($offset);
$area = $this->parseArea($offset + strlen($country) + 1);
break;
}
// GBK → UTF-8,移除純真廣告
return [
'country' => $this->clean(iconv('GBK', 'UTF-8//IGNORE', $country)),
'area' => $this->clean(iconv('GBK', 'UTF-8//IGNORE', $area)),
];
}
private function parseCountry(int $offset): string
{
$byte = $this->readBytes($offset, 1);
$mode = ord($byte);
if ($mode === 0x01 || $mode === 0x02) {
$redirectOffset = $this->readLong3($offset + 1);
return $this->readString($redirectOffset);
}
return $this->readString($offset);
}
private function parseArea(int $offset): string
{
$byte = $this->readBytes($offset, 1);
$mode = ord($byte);
if ($mode === 0x01 || $mode === 0x02) {
$redirectOffset = $this->readLong3($offset + 1);
return $this->readString($redirectOffset);
}
if ($mode === 0x00) return '';
return $this->readString($offset);
}
private function clean(string $str): string
{
return str_replace(['CZ88.NET', '纯真网络', 'cz88.net'], '', $str);
}
private function validateIp(string $ip): bool
{
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
}
public function __destruct()
{
if ($this->fp) fclose($this->fp);
}
}
?>
<?php
// 下載最新 qqwry.dat:https://github.com/out0fmemory/qqwry.dat
$locator = new QQWryIpLocator(__DIR__ . '/qqwry.dat');
$testIps = [
'8.8.8.8', // Google DNS
'114.114.114.114', // 114 DNS
'223.5.5.5', // 阿里 DNS
'192.168.1.1', // 內網 IP
'256.256.256.256', // 無效 IP
];
foreach ($testIps as $ip) {
$result = $locator->query($ip);
echo "$ip => " . ($result ? json_encode($result, JSON_UNESCAPED_UNICODE) : '無效/未找到') . "\n";
}
/*
輸出範例:
8.8.8.8 => {"country":"美国","area":"加利福尼亚州圣克拉拉县谷歌公司"}
114.114.114.114 => {"country":"中国","area":"江苏省南京市电信"}
223.5.5.5 => {"country":"中国","area":"浙江省杭州市阿里云"}
192.168.1.1 => {"country":"内网IP","area":"内网IP"}
256.256.256.256 => 無效/未找到
*/
?>
| 策略 | 實現 | 效果 |
|---|---|---|
| 記憶體映射 | file_get_contents 全讀入 / mmap 擴展 | 避免系統調用開銷,查詢速度提升 10-50x |
| 索引緩存 | 啟動時讀取所有索引到陣列 | 二分查找無 IO,極快 |
| 熱點 IP 緩存 | Redis/LRU 緩存最近查詢結果 | 重複查詢 O(1) |
| 批次查詢 | 一次傳入多 IP,排序後單掃描索引 | 大批量查詢效率最大化 |
<?php
class QQWryFastLocator extends QQWryIpLocator
{
private array $indexCache = []; // [offset => [startIp, endIp, recordOffset]]
public function __construct(string $filePath)
{
parent::__construct($filePath, true); // 強制全讀入記憶體
$this->buildIndexCache();
}
private function buildIndexCache(): void
{
for ($i = 0; $i < $this->totalRecords; $i++) {
$offset = $this->firstIndexOffset + $i * 7;
$startIp = $this->readLong4($offset);
$recordOffset = $this->readLong3($offset + 4);
$endIp = $this->readLong4($recordOffset);
$this->indexCache[] = [$startIp, $endIp, $recordOffset + 4];
}
}
public function query(string $ip): ?array
{
if (!$this->validateIp($ip)) return null;
$ipLong = sprintf('%u', ip2long($ip));
// 二分查找緩存陣列(純記憶體,極快)
$low = 0;
$high = count($this->indexCache) - 1;
while ($low <= $high) {
$mid = ($low + $high) >> 1;
[$start, $end, $recOffset] = $this->indexCache[$mid];
if ($ipLong < $start) $high = $mid - 1;
elseif ($ipLong > $end) $low = $mid + 1;
else return $this->parseRecord($recOffset);
}
return null;
}
}
?>
hgxd/qqwry:純 PHP 實現、支援 Composer、含索引緩存jasongrimes/ip2location:支援 MaxMind GeoIP2 格式,功能更豐富geoip2/geoip2:MaxMind 官方庫,精度最高(需付費授權)純真 IP 庫格式雖舊但免費、覆蓋國內 ISP 較全。核心解析邏輯在於:二分查找索引區 → 解析記錄區重定向鏈 → GBK 轉 UTF-8。生產環境建議:啟動時全量載入索引到記憶體、熱點 IP Redis 緩存、定期更新數據庫檔案。高併發場景下,單機 QPS 可達 10 萬+。