generated from template/slidev-template
fix: update import paths and enhance error handling in IP fetching
This commit is contained in:
22
agent/ip.ts
22
agent/ip.ts
@@ -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?.('获取地址失败');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user