// ==================== 固定配置 ==================== const CF_TOKEN = "cfut_dQOlPoYSG2fLy2HydMYU4HOB8UgKZuJU9E5DASUCd6c5533e"; const ZONE_ID = "fb026beabd67d3c68565db24fef60d85"; const ROOT_DOMAIN = "662201.xyz"; // ================================================== // 记忆状态 let STATUS_LOG = { input_prefix: "ccc", current_type: "NONE", current_value: "加载中...", last_status: "等待操作...", status_class: "info" }; // 预设的快捷前缀列表 const PRESET_PREFIXES = ["ccc", "xxx", "vvv", "pip"]; function isValidIPv4(ipStr) { const pattern = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/; return pattern.test(ipStr); } async function getRecordInfo(fullSubdomain) { const url = `https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records?name=${fullSubdomain}`; try { const res = await fetch(url, { headers: { "Authorization": `Bearer ${CF_TOKEN}`, "Content-Type": "application/json" } }); const data = await res.json(); if (data.success && data.result && data.result.length > 0) { for (let record of data.result) { if (record.type === "CNAME" || record.type === "A") { STATUS_LOG.current_type = record.type; STATUS_LOG.current_value = record.content; return { id: record.id, type: record.type }; } } STATUS_LOG.current_type = "NONE"; STATUS_LOG.current_value = "该前缀暂无有效 CNAME/A 记录"; } else { STATUS_LOG.current_type = "NONE"; STATUS_LOG.current_value = "未查到该前缀的解析记录"; } } catch (e) { STATUS_LOG.current_type = "NONE"; STATUS_LOG.current_value = "获取失败(网络异常)"; } return { id: null, type: null }; } async function changeDnsRecord(prefix, targetValue) { const fullSubdomain = `${prefix}.${ROOT_DOMAIN}`; const targetType = isValidIPv4(targetValue) ? "A" : "CNAME"; const { id: recordId, type: currentType } = await getRecordInfo(fullSubdomain); if (recordId && currentType !== targetType) { const delUrl = `https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${recordId}`; try { await fetch(delUrl, { method: "DELETE", headers: { "Authorization": `Bearer ${CF_TOKEN}` } }); } catch (e) {} } let url = `https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records`; let method = "POST"; if (recordId && currentType === targetType) { url = `${url}/${recordId}`; method = "PUT"; } const bodyData = { type: targetType, name: fullSubdomain, content: targetValue, ttl: 60, proxied: false }; try { const res = await fetch(url, { method: method, headers: { "Authorization": `Bearer ${CF_TOKEN}`, "Content-Type": "application/json" }, body: JSON.stringify(bodyData) }); const data = await res.json(); if (data.success) { STATUS_LOG.current_type = targetType; STATUS_LOG.current_value = targetValue; return { success: true, msg: `🎉 成功!【${fullSubdomain}】已成功切换为 【${targetType} 记录】➔ 【${targetValue}】` }; } else { return { success: false, msg: `CF 拒绝修改: ${JSON.stringify(data.errors)}` }; } } catch (e) { return { success: false, msg: `请求失败: ${e.message}` }; } } function generateHtml() { const typeCls = STATUS_LOG.current_type.toLowerCase(); const tagsHtml = PRESET_PREFIXES.map(p => { const activeClass = p === STATUS_LOG.input_prefix ? "active" : ""; return `${p}`; }).join(""); return `