generated from template/slidev-template
feat: 添加IPv6地址获取功能,优化Cloudflare DNS记录更新逻辑,更新版本号并添加README
This commit is contained in:
@@ -12,6 +12,12 @@ export type CloudflareConfig = {
|
||||
api_token: string;
|
||||
ipv6: string;
|
||||
ipv4: string;
|
||||
ipList?: {
|
||||
type: 'A' | 'AAAA';
|
||||
ip: string;
|
||||
domain: string;
|
||||
record_id: string;
|
||||
}[];
|
||||
flag: number; // 0: 不更新, 1: 仅IPv4, 2: 仅IPv6, 3: IPv4和IPv6
|
||||
time: string; // 上次更新时间戳
|
||||
}
|
||||
@@ -43,7 +49,7 @@ app.route({
|
||||
const oldIp = isV4 ? config.ipv4 : config.ipv6;
|
||||
console.log(date.toLocaleString() + ` 当前${isV4 ? 'IPv4' : 'IPv6'}地址: ${newIp}, 上次记录的IP地址: ${oldIp}`);
|
||||
if (newIp !== oldIp) {
|
||||
// IP地址有变化,更新DNS记录
|
||||
// IP地址有变化,更新主DNS记录
|
||||
const cfUpdateRes = await app.run({
|
||||
path: 'cf', key: 'update', payload: {
|
||||
zone_id: config.zone_id,
|
||||
@@ -55,6 +61,49 @@ app.route({
|
||||
}
|
||||
}, {});
|
||||
console.log(date.toLocaleString() + ` 更新${isV4 ? 'IPv4' : 'IPv6'}地址结果:`, cfUpdateRes);
|
||||
|
||||
// 更新ipList中所有匹配的记录
|
||||
if (config.ipList && config.ipList.length > 0) {
|
||||
const recordType = isV4 ? 'A' : 'AAAA';
|
||||
for (const item of config.ipList) {
|
||||
if (item.type === recordType) {
|
||||
if (item.record_id) {
|
||||
// record_id 存在,更新记录
|
||||
const listUpdateRes = await app.run({
|
||||
path: 'cf', key: 'update', payload: {
|
||||
zone_id: config.zone_id,
|
||||
record_id: item.record_id,
|
||||
domain: item.domain,
|
||||
new_ip: newIp,
|
||||
api_token: config.api_token,
|
||||
type: recordType,
|
||||
}
|
||||
}, {});
|
||||
console.log(date.toLocaleString() + ` 更新ipList[${item.domain}] 结果:`, listUpdateRes);
|
||||
item.ip = newIp;
|
||||
} else if (item.domain) {
|
||||
// record_id 不存在但 domain 存在,创建新记录
|
||||
const listCreateRes = await app.run({
|
||||
path: 'cf', key: 'create', payload: {
|
||||
zone_id: config.zone_id,
|
||||
domain: item.domain,
|
||||
new_ip: newIp,
|
||||
api_token: config.api_token,
|
||||
type: recordType,
|
||||
}
|
||||
}, {});
|
||||
console.log(date.toLocaleString() + ` 创建ipList[${item.domain}] 记录结果:`, listCreateRes);
|
||||
if (listCreateRes.data?.record_id) {
|
||||
item.record_id = listCreateRes.data.record_id as string;
|
||||
item.ip = newIp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 重新赋值 ipList 确保引用变化,触发更新
|
||||
config.ipList = [...config.ipList];
|
||||
}
|
||||
|
||||
// 更新配置文件中的IP地址
|
||||
if (isV4) {
|
||||
config.ipv4 = newIp;
|
||||
@@ -118,8 +167,8 @@ app.route({
|
||||
if (cfRes.code !== 200) {
|
||||
ctx.throw?.(`创建${isV4 ? 'IPv4' : 'IPv6'}记录失败: ` + cfRes.message);
|
||||
}
|
||||
console.log(`创建${isV4 ? 'IPv4' : 'IPv6'}记录结果:`, cfRes.body.record_id);
|
||||
const record_id = cfRes.body.record_id as string;
|
||||
console.log(`创建${isV4 ? 'IPv4' : 'IPv6'}记录结果:`, cfRes.data.record_id);
|
||||
const record_id = cfRes.data.record_id as string;
|
||||
|
||||
if (isV4) {
|
||||
config.record_id4 = record_id;
|
||||
|
||||
Reference in New Issue
Block a user