generated from template/slidev-template
fix: update import paths and enhance error handling in IP fetching
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import {app} from './app.ts';
|
import {app} from './app.ts';
|
||||||
|
|
||||||
import './ip'
|
import './ip.ts'
|
||||||
import './task.ts';
|
import './task.ts';
|
||||||
import './routes/cloudflare.ts';
|
import './routes/cloudflare.ts';
|
||||||
// app.listen(8080);
|
// app.listen(8080);
|
||||||
|
|||||||
20
agent/ip.ts
20
agent/ip.ts
@@ -1,6 +1,12 @@
|
|||||||
const baseURLv4 = 'https://4.ipw.cn/';
|
const baseURLv4 = 'https://4.ipw.cn/';
|
||||||
const baseURLv6 = 'https://6.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';
|
import { app } from './app.ts';
|
||||||
|
|
||||||
app.route({
|
app.route({
|
||||||
@@ -21,8 +27,20 @@ app.route({
|
|||||||
key: 'v6',
|
key: 'v6',
|
||||||
description: '获取当前公网IPv6地址',
|
description: '获取当前公网IPv6地址',
|
||||||
}).define(async (ctx) => {
|
}).define(async (ctx) => {
|
||||||
|
let ip = ''
|
||||||
|
try {
|
||||||
const response = await fetch(baseURLv6);
|
const response = await fetch(baseURLv6);
|
||||||
const ip = (await response.text()).trim();
|
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)) {
|
if (!isIpv6(ip)) {
|
||||||
ctx.throw?.('获取地址失败');
|
ctx.throw?.('获取地址失败');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,20 +28,23 @@ app.route({
|
|||||||
}).define(async (ctx) => {
|
}).define(async (ctx) => {
|
||||||
const config = await storage.getItem<CloudflareConfig>('cloudflare.json');
|
const config = await storage.getItem<CloudflareConfig>('cloudflare.json');
|
||||||
if (!config) {
|
if (!config) {
|
||||||
ctx.throw?.('未找到配置');
|
return ctx.throw('未找到配置');
|
||||||
}
|
}
|
||||||
|
// console.log('当前配置:', config);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const date = new Date(now);
|
const date = new Date(now);
|
||||||
const updateIp = async (isV4 = true) => {
|
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) {
|
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;
|
const oldIp = isV4 ? config.ipv4 : config.ipv6;
|
||||||
|
console.log(date.toLocaleString() + ` 当前${isV4 ? 'IPv4' : 'IPv6'}地址: ${newIp}, 上次记录的IP地址: ${oldIp}`);
|
||||||
if (newIp !== oldIp) {
|
if (newIp !== oldIp) {
|
||||||
// IP地址有变化,更新DNS记录
|
// IP地址有变化,更新DNS记录
|
||||||
await app.call({
|
const cfUpdateRes = await app.run({
|
||||||
path: 'cf', key: 'update', payload: {
|
path: 'cf', key: 'update', payload: {
|
||||||
zone_id: config.zone_id,
|
zone_id: config.zone_id,
|
||||||
record_id: isV4 ? config.record_id4 : config.record_id6,
|
record_id: isV4 ? config.record_id4 : config.record_id6,
|
||||||
@@ -51,6 +54,7 @@ app.route({
|
|||||||
type: isV4 ? 'A' : 'AAAA',
|
type: isV4 ? 'A' : 'AAAA',
|
||||||
}
|
}
|
||||||
}, {});
|
}, {});
|
||||||
|
console.log(date.toLocaleString() + ` 更新${isV4 ? 'IPv4' : 'IPv6'}地址结果:`, cfUpdateRes);
|
||||||
// 更新配置文件中的IP地址
|
// 更新配置文件中的IP地址
|
||||||
if (isV4) {
|
if (isV4) {
|
||||||
config.ipv4 = newIp;
|
config.ipv4 = newIp;
|
||||||
@@ -83,7 +87,7 @@ app.route({
|
|||||||
description: '初始化配置文件cloudflare.json',
|
description: '初始化配置文件cloudflare.json',
|
||||||
}).define(async (ctx) => {
|
}).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 isIpv4 = config?.flag === 1 || config?.flag === 3;
|
||||||
const isIpv6 = config?.flag === 2 || config?.flag === 3;
|
const isIpv6 = config?.flag === 2 || config?.flag === 3;
|
||||||
@@ -92,15 +96,15 @@ app.route({
|
|||||||
ctx.throw?.('配置错误:api_token为空');
|
ctx.throw?.('配置错误:api_token为空');
|
||||||
}
|
}
|
||||||
const getIp = async (isV4: boolean) => {
|
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) {
|
if (res.code !== 200) {
|
||||||
ctx.throw?.(`获取${isV4 ? 'IPv4' : 'IPv6'}地址失败: ` + res.message);
|
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 createCfRecord = async (isV4: boolean) => {
|
||||||
const newIp = await getIp(isV4);
|
const newIp = await getIp(isV4);
|
||||||
const cfRes = await app.call({
|
const cfRes = await app.run({
|
||||||
path: 'cf',
|
path: 'cf',
|
||||||
key: 'create',
|
key: 'create',
|
||||||
payload: {
|
payload: {
|
||||||
@@ -141,7 +145,7 @@ app.route({
|
|||||||
}).addTo(app);
|
}).addTo(app);
|
||||||
|
|
||||||
export const main = async () => {
|
export const main = async () => {
|
||||||
const res = await app.call({
|
const res = await app.run({
|
||||||
path: 'ip',
|
path: 'ip',
|
||||||
key: 'init',
|
key: 'init',
|
||||||
});
|
});
|
||||||
@@ -149,11 +153,12 @@ export const main = async () => {
|
|||||||
console.error('初始化失败:', res.message);
|
console.error('初始化失败:', res.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (res.body.init) {
|
console.log('初始化结果:', res.data);
|
||||||
|
if (res.data.init) {
|
||||||
console.log('初始化完成,并创建了必要的DNS记录,任务结束。');
|
console.log('初始化完成,并创建了必要的DNS记录,任务结束。');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await app.call({
|
await app.run({
|
||||||
path: 'ip',
|
path: 'ip',
|
||||||
key: 'task',
|
key: 'task',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,8 +7,9 @@
|
|||||||
"sourceMap": false,
|
"sourceMap": false,
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"newLine": "LF",
|
"newLine": "LF",
|
||||||
"baseUrl": "./",
|
"strict": false,
|
||||||
"typeRoots": [
|
"typeRoots": [
|
||||||
|
"node",
|
||||||
"node_modules/@types",
|
"node_modules/@types",
|
||||||
],
|
],
|
||||||
"declaration": false,
|
"declaration": false,
|
||||||
@@ -20,10 +21,10 @@
|
|||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": [
|
"@/*": [
|
||||||
"src/*"
|
"./src/*"
|
||||||
],
|
],
|
||||||
"@agent/*": [
|
"@agent/*": [
|
||||||
"agent/*"
|
"./agent/*"
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user