-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathupdate-dns.js
More file actions
171 lines (149 loc) · 6.25 KB
/
Copy pathupdate-dns.js
File metadata and controls
171 lines (149 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import fs from 'fs';
import path from 'path';
import axios from 'axios';
async function processRecord(filePath) {
let recordConfig;
let subdomain;
try {
console.log(`[Message] Processing file: ${filePath}`);
subdomain = path.basename(filePath, '.json');
const fileContent = fs.readFileSync(filePath, 'utf8');
recordConfig = JSON.parse(fileContent);
console.log(recordConfig)
} catch (error) {
console.error(`[Error] Failed to read ${filePath}`);
process.exit(1);
}
const CF_API_TOKEN = process.env.CF_API_TOKEN;
const CF_ZONE_ID = process.env.CF_ZONE_ID;
const rootDomain = 'moe.page';
if (!CF_API_TOKEN || !CF_ZONE_ID) {
console.error('[Error] Global Variable CF_API_TOKEN and/or CF_ZONE_ID are missing!');
process.exit(1);
}
const apiUrl = `https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records`;
const headers = {
'Authorization': `Bearer ${CF_API_TOKEN}`,
'Content-Type': 'application/json',
};
for (const [recordType, recordValues] of Object.entries(recordConfig.records)) {
const name = `${recordConfig.domain}.${rootDomain}`;
const proxiedTypes = ['A', 'AAAA', 'CNAME'];
const isProxiedCapable = proxiedTypes.includes(recordType);
// 1) 规范 & 去重;CNAME 只取一个
let values = Array.isArray(recordValues) ? recordValues : [recordValues];
if (recordType === 'CNAME') values = [values[0]];
values = [...new Set(values.filter(v => v != null && v !== ''))];
// 2) 目标记录(desired)一次性算好
const desired = values.map(content => ({
type: recordType,
name,
content,
proxied: isProxiedCapable ? (recordConfig.proxied || false) : false,
ttl: (isProxiedCapable && (recordConfig.proxied || false)) ? 1 : (recordConfig.ttl || 120),
}));
console.log(`[Plan] ${recordType} ${name} 目标 ${desired.map(d => d.content).join(', ') || '(空)'}`);
// 3) 只发一次 GET(按 name+type)
let existing = [];
try {
const getResponse = await axios.get(apiUrl, {
headers,
params: { name, type: recordType }
});
existing = getResponse.data?.result || [];
} catch (error) {
console.error(`[Error] 获取现有记录失败:`, error.response?.data || error.message);
process.exit(1);
}
// 4) CNAME:单条 upsert(最多1次PUT或1次POST)
if (recordType === 'CNAME') {
const wanted = desired[0];
const ex = existing[0];
if (!wanted) {
console.log(`[Skip] 无 CNAME 目标,跳过`);
continue;
}
if (!ex) {
console.log(`[Message] CNAME 不存在,Create...`);
try {
await axios.post(apiUrl, wanted, { headers });
console.log(`[Success] Record ${wanted.name} created`);
} catch (error) {
console.error(`[Error] 创建失败:`, error.response?.data || error.message);
process.exit(1);
}
} else {
const needUpdate =
ex.content !== wanted.content ||
(ex.proxied ?? false) !== wanted.proxied ||
(ex.ttl !== wanted.ttl);
if (needUpdate) {
console.log(`[Message] CNAME 已存在 (ID: ${ex.id}),Updating...`);
try {
await axios.put(`${apiUrl}/${ex.id}`, wanted, { headers });
console.log(`[Success] Record ${wanted.name} successfully updated`);
} catch (error) {
console.error(`[Error] 更新失败:`, error.response?.data || error.message);
process.exit(1);
}
} else {
console.log(`[Skip] CNAME ${name} 已是最新 -> ${ex.content}`);
}
}
continue;
}
const existByContent = new Map(existing.map(r => [r.content, r]));
// 创建或更新
for (const r of desired) {
const hit = existByContent.get(r.content);
if (!hit) {
console.log(`[Message] 缺少 ${recordType} ${name} -> ${r.content},Create...`);
try {
await axios.post(apiUrl, r, { headers });
console.log(`[Success] Record ${name} created (${r.content})`);
} catch (error) {
console.error(`[Error] 创建失败:`, error.response?.data || error.message);
process.exit(1);
}
} else {
const needUpdate =
(hit.proxied ?? false) !== r.proxied ||
(hit.ttl !== r.ttl);
if (needUpdate) {
console.log(`[Message] 属性变化 (ID: ${hit.id}),Updating...`);
try {
await axios.put(`${apiUrl}/${hit.id}`, r, { headers });
console.log(`[Success] Record ${name} updated (${r.content})`);
} catch (error) {
console.error(`[Error] 更新失败:`, error.response?.data || error.message);
process.exit(1);
}
} else {
console.log(`[Skip] ${recordType} ${name} -> ${r.content} 已是最新`);
}
}
}
// 删除多余(严格同步开关)
if (process.env.STRICT_SYNC === '1') {
const desiredSet = new Set(values);
for (const ex of existing) {
if (!desiredSet.has(ex.content)) {
console.log(`[Message] 多余 ${recordType} ${name} -> ${ex.content},Delete...`);
try {
await axios.delete(`${apiUrl}/${ex.id}`, { headers });
console.log(`[Success] Deleted ${recordType} ${name} -> ${ex.content}`);
} catch (error) {
console.error(`[Error] 删除失败:`, error.response?.data || error.message);
process.exit(1);
}
}
}
}
}
}
const filePath = process.argv[2];
if (!filePath) {
console.error('[Error] Please provide a file path as a parameter!');
process.exit(1);
}
processRecord(filePath);