ws01博客 - 证书
type: channel
link: https://boke.ndjp.net/index.php/tag/%E8%AF%81%E4%B9%A6/
items: 在cloudflare上部署IP6.ARPA域名自动添加SSL证书

在cloudflare上部署IP6.ARPA域名自动添加SSL证书
type: item
date: 2025-11-01T12:09:00+00:00
description: 在cloudflare上部署IP6.ARPA域名自动添加SSL证书export default { async fetch(request, env, ctx) { const url = new URL(request.url); // 处理 API 请求：支持 POST (/api/add-ssl) 和 GET (/?...) if ( (url.pathname === '/api/add-ssl' && request.method === 'POST') || (url.pathname === '/' && request.method === 'GET' && url.searchParams.has('zoneId')) ) { return handleApiRequest(request, url.searchParams); } // 返回 HTML 页面 (仅当是根路径的 GET 请求且没有API参数时) return new Response(getHTML(), { headers: { 'Content-Type': 'text/html; charset=utf-8', }, }); }, }; // 统一处理 API 请求（支持 POST Body 和 GET Query Params） async function handleApiRequest(request, queryParams) { let email, zone_id, api_key, enabled, certificate_authority; try { if (request.method === 'POST') { // POST 请求：从请求体中解析 JSON const body = await request.json(); email = body.email; zone_id = body.zoneId; api_key = body.apikey; enabled = body.enabled !== undefined ? body.enabled : true; certificate_authority = body.ca || "ssl_com"; } else if (request.method === 'GET') { // GET 请求：从 URL 查询参数中获取 email = queryParams.get('email'); zone_id = queryParams.get('zoneId'); api_key = queryParams.get('apikey'); enabled = !(queryParams.get('enabled') === 'false'); certificate_authority = queryParams.get('ca') || "ssl_com"; } // 验证必需的输入 if (!email || !zone_id || !api_key) { return new Response(JSON.stringify({ success: false, errors: ['邮箱、区域ID和API密钥都是必需的'] }), { status: 400, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', } }); } // 验证并设置 CA 默认值 const validCAs = ["ssl_com", "lets_encrypt", "google", "sectigo"]; const caToUse = validCAs.includes(certificate_authority) ? certificate_authority : "ssl_com"; // 调用 Cloudflare API const response = await fetch(`https://api.cloudflare.com/client/v4/zones/${zone_id}/ssl/universal/settings`, { method: 'PATCH', headers: { 'X-Auth-Email': email, 'X-Auth-Key': api_key, 'Content-Type': 'application/json', }, body: JSON.stringify({ enabled: enabled, certificate_authority: caToUse }), }); const result…