From 3581d8cf962876403f50542b71a8be2b8ceaaf62 Mon Sep 17 00:00:00 2001 From: xiongxiao Date: Sat, 11 Apr 2026 03:33:02 +0800 Subject: [PATCH] fix: update import paths and enhance error handling in IP fetching --- agent/index.ts | 2 +- agent/ip.ts | 22 ++++++++++++++++++++-- agent/task.ts | 29 +++++++++++++++++------------ tsconfig.json | 7 ++++--- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/agent/index.ts b/agent/index.ts index 0d77e42..b728ecd 100644 --- a/agent/index.ts +++ b/agent/index.ts @@ -1,6 +1,6 @@ import {app} from './app.ts'; -import './ip' +import './ip.ts' import './task.ts'; import './routes/cloudflare.ts'; // app.listen(8080); diff --git a/agent/ip.ts b/agent/ip.ts index a033d3d..6922f8f 100644 --- a/agent/ip.ts +++ b/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?.('获取地址失败'); } diff --git a/agent/task.ts b/agent/task.ts index 7226e64..e1f48de 100644 --- a/agent/task.ts +++ b/agent/task.ts @@ -28,20 +28,23 @@ app.route({ }).define(async (ctx) => { const config = await storage.getItem('cloudflare.json'); if (!config) { - ctx.throw?.('未找到配置'); + return ctx.throw('未找到配置'); } + // console.log('当前配置:', config); const now = Date.now(); const date = new Date(now); const updateIp = async (isV4 = true) => { - const res = await app.call({ path: 'ip', key: isV4 ? 'v4' : 'v6' }); + const res = await app.run({ path: 'ip', key: isV4 ? 'v4' : 'v6' }); if (res.code !== 200) { - ctx.throw(res.message); + console.error(date.toLocaleString() + ` 获取当前${isV4 ? 'IPv4' : 'IPv6'}地址失败:`, res.message); + return ctx.throw(res.message); } - const newIp = res.body.ip as string; + const newIp = res.data.ip as string; const oldIp = isV4 ? config.ipv4 : config.ipv6; + console.log(date.toLocaleString() + ` 当前${isV4 ? 'IPv4' : 'IPv6'}地址: ${newIp}, 上次记录的IP地址: ${oldIp}`); if (newIp !== oldIp) { // IP地址有变化,更新DNS记录 - await app.call({ + const cfUpdateRes = await app.run({ path: 'cf', key: 'update', payload: { zone_id: config.zone_id, record_id: isV4 ? config.record_id4 : config.record_id6, @@ -51,6 +54,7 @@ app.route({ type: isV4 ? 'A' : 'AAAA', } }, {}); + console.log(date.toLocaleString() + ` 更新${isV4 ? 'IPv4' : 'IPv6'}地址结果:`, cfUpdateRes); // 更新配置文件中的IP地址 if (isV4) { config.ipv4 = newIp; @@ -83,7 +87,7 @@ app.route({ description: '初始化配置文件cloudflare.json', }).define(async (ctx) => { // 初始化逻辑 - const config = await storage.getItem('cloudflare.json'); + const config = await storage.getItem('cloudflare.json')!; const isIpv4 = config?.flag === 1 || config?.flag === 3; const isIpv6 = config?.flag === 2 || config?.flag === 3; @@ -92,15 +96,15 @@ app.route({ ctx.throw?.('配置错误:api_token为空'); } const getIp = async (isV4: boolean) => { - const res = await app.call({ path: 'ip', key: isV4 ? 'v4' : 'v6' }); + const res = await app.run({ path: 'ip', key: isV4 ? 'v4' : 'v6' }); if (res.code !== 200) { ctx.throw?.(`获取${isV4 ? 'IPv4' : 'IPv6'}地址失败: ` + res.message); } - return res.body.ip as string; + return res.data.ip as string; } const createCfRecord = async (isV4: boolean) => { const newIp = await getIp(isV4); - const cfRes = await app.call({ + const cfRes = await app.run({ path: 'cf', key: 'create', payload: { @@ -141,7 +145,7 @@ app.route({ }).addTo(app); export const main = async () => { - const res = await app.call({ + const res = await app.run({ path: 'ip', key: 'init', }); @@ -149,11 +153,12 @@ export const main = async () => { console.error('初始化失败:', res.message); return; } - if (res.body.init) { + console.log('初始化结果:', res.data); + if (res.data.init) { console.log('初始化完成,并创建了必要的DNS记录,任务结束。'); return; } - await app.call({ + await app.run({ path: 'ip', key: 'task', }) diff --git a/tsconfig.json b/tsconfig.json index 1e979bb..cc532a6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,8 +7,9 @@ "sourceMap": false, "allowJs": true, "newLine": "LF", - "baseUrl": "./", + "strict": false, "typeRoots": [ + "node", "node_modules/@types", ], "declaration": false, @@ -20,10 +21,10 @@ "esModuleInterop": true, "paths": { "@/*": [ - "src/*" + "./src/*" ], "@agent/*": [ - "agent/*" + "./agent/*" ], } },