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,6 @@
import {app} from './app.ts';
import './ip'
import './ip.ts'
import './task.ts';
import './routes/cloudflare.ts';
// app.listen(8080);

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?.('获取地址失败');
}

View File

@@ -28,20 +28,23 @@ app.route({
}).define(async (ctx) => {
const config = await storage.getItem<CloudflareConfig>('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<CloudflareConfig>('cloudflare.json');
const config = await storage.getItem<CloudflareConfig>('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',
})

View File

@@ -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/*"
],
}
},