fix: update import paths and enhance error handling in IP fetching

This commit is contained in:
2026-04-11 03:33:02 +08:00
parent 02b29014d8
commit 3581d8cf96
4 changed files with 42 additions and 18 deletions

View File

@@ -1,6 +1,12 @@
const baseURLv4 = 'https://4.ipw.cn/';
const baseURLv6 = 'https://6.ipw.cn/';
// ipv6
// curl -6 ifconfig.co
// curl -6 icanhazip.com
// ipv4
// https://api.ipify.org/?format=text
// https://ipinfo.io/ip
import { app } from './app.ts';
app.route({
@@ -21,8 +27,20 @@ app.route({
key: 'v6',
description: '获取当前公网IPv6地址',
}).define(async (ctx) => {
const response = await fetch(baseURLv6);
const ip = (await response.text()).trim();
let ip = ''
try {
const response = await fetch(baseURLv6);
ip = (await response.text()).trim();
} catch (error) {
}
if (!isIpv6(ip)) {
try {
const response2 = await fetch('https://ifconfig.co/ip');
ip = (await response2.text()).trim();
console.log('尝试第二个接口获取IPv6地址:', ip);
} catch (error) { }
}
if (!isIpv6(ip)) {
ctx.throw?.('获取地址失败');
}