-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.js
More file actions
187 lines (165 loc) · 4.5 KB
/
proxy.js
File metadata and controls
187 lines (165 loc) · 4.5 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Cloudflare DNS Proxy Service
// 用于解决客户端无法直接调用Cloudflare API的问题
// 验证客户端密钥
function validateClient(request, env) {
const url = new URL(request.url);
const clientId = url.searchParams.get("client_id");
const clientKey = url.searchParams.get("client_key");
// 从环境变量获取客户端配置
const clients = env.CLIENT_KEYS ? env.CLIENT_KEYS.split(",") : [];
// 验证client_id
const idInt = parseInt(clientId, 10);
if (isNaN(idInt) || idInt < 0 || idInt >= clients.length) {
return { valid: false, error: "Invalid client_id" };
}
// 验证client_key
if (clients[idInt] !== clientKey) {
return { valid: false, error: "Invalid client_key" };
}
return { valid: true };
}
// 调用Cloudflare API更新DNS记录
async function updateCloudflareDNS(dnsData) {
const url = `https://api.cloudflare.com/client/v4/zones/${dnsData.zone_id}/dns_records/${dnsData.record_id}`;
const requestBody = {
type: dnsData.type,
name: dnsData.name,
content: dnsData.content,
ttl: dnsData.ttl,
proxied: dnsData.proxied,
};
const response = await fetch(url, {
method: "PATCH",
headers: {
Authorization: `Bearer ${dnsData.api_token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
});
const result = await response.json();
return {
status: response.status,
success: response.ok,
data: result,
};
}
// 处理DNS更新请求
async function handleDNSUpdate(request, env) {
try {
// 验证客户端
const validation = validateClient(request, env);
if (!validation.valid) {
return new Response(validation.error, { status: 400 });
}
// 解析请求体
const dnsData = await request.json();
// 验证必需字段
const requiredFields = [
"api_token",
"zone_id",
"record_id",
"type",
"name",
"content",
];
for (const field of requiredFields) {
if (!dnsData[field]) {
return new Response(`Missing required field: ${field}`, {
status: 400,
});
}
}
// 设置默认值
if (dnsData.ttl === undefined) dnsData.ttl = 1;
if (dnsData.proxied === undefined) dnsData.proxied = false;
// 调用Cloudflare API
const result = await updateCloudflareDNS(dnsData);
if (result.success) {
return new Response(
JSON.stringify({
success: true,
message: `DNS record updated successfully for ${dnsData.name}`,
data: result.data,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
} else {
return new Response(
JSON.stringify({
success: false,
message: `Failed to update DNS record for ${dnsData.name}`,
error: result.data,
}),
{
status: result.status,
headers: { "Content-Type": "application/json" },
}
);
}
} catch (error) {
return new Response(
JSON.stringify({
success: false,
message: "Internal server error",
error: error.message,
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
);
}
}
// 健康检查端点
function handleHealthCheck() {
return new Response(
JSON.stringify({
status: "ok",
service: "cloudflare-dns-proxy",
timestamp: new Date().toISOString(),
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
}
// 主处理函数
export default {
async fetch(request, env) {
const url = new URL(request.url);
// 健康检查
if (url.pathname === "/health") {
return handleHealthCheck();
}
// DNS更新端点
if (url.pathname === "/update-dns") {
if (request.method !== "POST") {
return new Response("Method not allowed", { status: 405 });
}
return handleDNSUpdate(request, env);
}
// 默认端点(兼容性)
if (url.pathname === "/") {
return new Response(
JSON.stringify({
service: "cloudflare-dns-proxy",
endpoints: {
health: "/health",
update_dns: "/update-dns",
},
usage:
"Send POST request to /update-dns with DNS data and client authentication",
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
}
return new Response("Not Found", { status: 404 });
},
};