refactor: IP归属地改为apihz.cn id+key配置,后端测试修复验证条件
This commit is contained in:
@@ -10,9 +10,10 @@ export async function lookupIpLocation(ip: string): Promise<string | null> {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const apiUrlTemplate = getSystemConfig('ip_geo_api_url');
|
||||
if (!apiUrlTemplate) return null;
|
||||
const url = apiUrlTemplate.replace('{ip}', encodeURIComponent(ip));
|
||||
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;
|
||||
|
||||
@@ -535,16 +535,17 @@ router.post('/admin/test-external-service', async (req: Request, res: Response)
|
||||
break;
|
||||
}
|
||||
case 'ip_geo': {
|
||||
const geoUrl = url || getSystemConfig('ip_geo_api_url') || '';
|
||||
if (!geoUrl) {
|
||||
res.json({ ok: false, info: '请先输入 IP 归属地查询 API 地址' });
|
||||
const apiId = url || getSystemConfig('ip_geo_api_id') || '';
|
||||
const apiKey = getSystemConfig('ip_geo_api_key') || '';
|
||||
if (!apiId || !apiKey) {
|
||||
res.json({ ok: false, info: '请先配置 IP 归属地 API ID 和 Key' });
|
||||
return;
|
||||
}
|
||||
const testUrl = geoUrl.replace('{ip}', '8.8.8.8');
|
||||
const testUrl = `https://cn.apihz.cn/api/ip/chaapi.php?id=${encodeURIComponent(apiId)}&key=${encodeURIComponent(apiKey)}&ip=8.8.8.8&td=0`;
|
||||
const response = await fetch(testUrl, { signal: AbortSignal.timeout(8000) });
|
||||
const data: any = await response.json();
|
||||
const latency = Date.now() - start;
|
||||
const valid = !!(data?.country || data?.region || data?.city || data?.countryCode);
|
||||
const valid = data?.code === 200;
|
||||
res.json({ ok: valid, latency, info: valid ? '连接成功' : '响应格式不符' });
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user