generated from template/slidev-template
fix: update import paths and enhance error handling in IP fetching
This commit is contained in:
@@ -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',
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user