Files
CloudSearch/source_clean/src/cloud/ip-lookup.ts

33 lines
1.3 KiB
TypeScript

/**
* IP 归属地查询工具
* 通过系统配置中的 IP 地理接口查询
*/
import { getSystemConfig } from '../admin/system-config.service';
export async function lookupIpLocation(ip: string): Promise<string | null> {
if (!ip || ip === '127.0.0.1' || ip === '::1' || ip.startsWith('192.168.') || ip.startsWith('10.')) {
return null;
}
try {
const apiId = getSystemConfig('ip_geo_api_id');
const apiKey = getSystemConfig('ip_geo_api_key');
if (!apiId || !apiKey) return null;
const url = `https://cn.apihz.cn/api/ip/chaapi.php?id=${encodeURIComponent(apiId)}&key=${encodeURIComponent(apiKey)}&ip=${encodeURIComponent(ip)}&td=0`;
const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
if (!res.ok) return null;
const data = await res.json() as {
code: number; sheng?: string; shi?: string; qu?: string;
isp?: string; msg?: string; guo?: string;
};
if (data.code !== 200) return null;
// Format: "四川 绵阳 江油 中国联通" — strip 省/市/区/州 suffixes for compact display
const stripSuffix = (s: string | undefined) => s?.replace(/[省市州区]$/, '');
const parts = [stripSuffix(data.sheng), stripSuffix(data.shi), stripSuffix(data.qu), data.isp].filter(Boolean);
return parts.join(' ');
} catch {
return null;
}
}