\App\Http\HelpersIpRegion

IP 归属地解析

职责:纯真 IP 数据库(qqwry.dat)的离线解析器——二分索引定位 IP 段、 解析国家/地区字符串(含 0x01/0x02 重定向模式)、GBK 转 UTF-8。 每次查询独立打开文件做二分(约 22 次定长读取),无内存映射依赖; 文件缺失/格式异常一律返回空串,绝不向日志链路抛异常。

Summary

Methods
Properties
Constants
find()
No public properties found
CACHE_MAX
No protected methods found
No protected properties found
N/A
findUncached()
resolveLocation()
areaAt()
bytes()
uint32()
uint24()
byte()
stringAt()
$cache
N/A

Constants

CACHE_MAX

CACHE_MAX = 1024

缓存上限:满则整体清空(日志 IP 集合可能很大,简单防无限增长)

Properties

$cache

$cache : array

进程级结果缓存(IP → 归属地):日志链路同 IP 高频重复,避免每次 fopen + 二分

Type

array

Methods

find()

find(string  $ip) : string

查询 IP 归属地(进程级结果缓存)。

流程:① 命中缓存直接返回(含空串结果——无效 IP/库缺失也缓存,防重复 fopen) ② 未命中走解析并写入缓存。 缓存键绑定库文件路径:换库/库缺失场景自动失效(防旧库结果残留)。

Parameters

string $ip

Returns

string —

findUncached()

findUncached(string  $ip) : string

查询 IP 归属地(无缓存路径)。

流程:① 校验库文件与 IP 格式 ② 索引头二分定位 IP 段 ③ 按模式解析国家/地区 ④ 清洗广告后缀并转 UTF-8

Parameters

string $ip

Returns

string —

resolveLocation()

resolveLocation(mixed  $handle, int  $recordOffset) : string

解析命中记录的国家 + 地区(0x01 双偏移重定向 / 0x02 国家后跟地区)

Parameters

mixed $handle
int $recordOffset

Returns

string —

areaAt()

areaAt(mixed  $handle, int  $offset) : string

地区段解析:重定向模式(0x01/0x02)经 3 字节偏移跳转,否则就地读串

Parameters

mixed $handle
int $offset

Returns

string —

bytes()

bytes(mixed  $handle, int  $offset, int  $length) : string

指定偏移读取定长字节(失败返回空串)

Parameters

mixed $handle
int $offset
int $length

Returns

string —

uint32()

uint32(mixed  $handle, int  $offset) : int

小端 32 位无符号整数

Parameters

mixed $handle
int $offset

Returns

int —

uint24()

uint24(mixed  $handle, int  $offset) : int

小端 24 位无符号整数(索引记录尾 3 字节)

Parameters

mixed $handle
int $offset

Returns

int —

byte()

byte(mixed  $handle, int  $offset) : int

单字节无符号整数

Parameters

mixed $handle
int $offset

Returns

int —

stringAt()

stringAt(mixed  $handle, int  $offset) : string

自偏移读 NUL 结尾字符串(上限 255 字节防坏数据越读)

Parameters

mixed $handle
int $offset

Returns

string —