From 78ce6014043e3cfb6fb2950fbd9d377bbefa1100 Mon Sep 17 00:00:00 2001 From: cmliu Date: Tue, 6 Jan 2026 19:04:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0APIURL=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=88=B0CF=5FJSON=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96Cloudflare=E4=BD=BF=E7=94=A8=E6=83=85=E5=86=B5?= =?UTF-8?q?=E7=9A=84=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _worker.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/_worker.js b/_worker.js index 2488fcf..a0f34d3 100644 --- a/_worker.js +++ b/_worker.js @@ -122,18 +122,20 @@ export default { } else if (访问路径 === 'admin/cf.json') { // 保存cf.json配置 try { const newConfig = await request.json(); - const CF_JSON = { Email: null, GlobalAPIKey: null, AccountID: null, APIToken: null }; + const CF_JSON = { Email: null, GlobalAPIKey: null, AccountID: null, APIToken: null, APIURL: null }; if (!newConfig.init || newConfig.init !== true) { if (newConfig.Email && newConfig.GlobalAPIKey) { CF_JSON.Email = newConfig.Email; CF_JSON.GlobalAPIKey = newConfig.GlobalAPIKey; CF_JSON.AccountID = null; CF_JSON.APIToken = null; + CF_JSON.APIURL = null; } else if (newConfig.AccountID && newConfig.APIToken) { CF_JSON.Email = null; CF_JSON.GlobalAPIKey = null; CF_JSON.AccountID = newConfig.AccountID; CF_JSON.APIToken = newConfig.APIToken; + CF_JSON.APIURL = null; } else { return new Response(JSON.stringify({ error: '配置不完整' }), { status: 400, headers: { 'Content-Type': 'application/json;charset=utf-8' } }); } @@ -1124,6 +1126,7 @@ async function 读取config_JSON(env, hostname, userID, path, 重置配置 = fal GlobalAPIKey: null, AccountID: null, APIToken: null, + APIURL: null, Usage: { success: false, pages: 0, @@ -1170,7 +1173,7 @@ async function 读取config_JSON(env, hostname, userID, path, 重置配置 = fal console.error(`读取tg.json出错: ${error.message}`); } - const 初始化CF_JSON = { Email: null, GlobalAPIKey: null, AccountID: null, APIToken: null }; + const 初始化CF_JSON = { Email: null, GlobalAPIKey: null, AccountID: null, APIToken: null, APIURL: null }; config_JSON.CF = { ...初始化CF_JSON, Usage: { success: false, pages: 0, workers: 0, total: 0 } }; try { const CF_TXT = await env.KV.get('cf.json'); @@ -1178,12 +1181,23 @@ async function 读取config_JSON(env, hostname, userID, path, 重置配置 = fal await env.KV.put('cf.json', JSON.stringify(初始化CF_JSON, null, 2)); } else { const CF_JSON = JSON.parse(CF_TXT); - config_JSON.CF.Email = CF_JSON.Email ? CF_JSON.Email : null; - config_JSON.CF.GlobalAPIKey = CF_JSON.GlobalAPIKey ? 掩码敏感信息(CF_JSON.GlobalAPIKey) : null; - config_JSON.CF.AccountID = CF_JSON.AccountID ? 掩码敏感信息(CF_JSON.AccountID) : null; - config_JSON.CF.APIToken = CF_JSON.APIToken ? 掩码敏感信息(CF_JSON.APIToken) : null; - const Usage = await getCloudflareUsage(CF_JSON.Email, CF_JSON.GlobalAPIKey, CF_JSON.AccountID, CF_JSON.APIToken); - config_JSON.CF.Usage = Usage; + if (CF_JSON.APIURL) { + try { + const response = await fetch(CF_JSON.APIURL); + const Usage = await response.json(); + config_JSON.CF.Usage = Usage; + } catch (err) { + console.error(`请求 CF_JSON.APIURL 失败: ${err.message}`); + } + } else { + config_JSON.CF.Email = CF_JSON.Email ? CF_JSON.Email : null; + config_JSON.CF.GlobalAPIKey = CF_JSON.GlobalAPIKey ? 掩码敏感信息(CF_JSON.GlobalAPIKey) : null; + config_JSON.CF.AccountID = CF_JSON.AccountID ? 掩码敏感信息(CF_JSON.AccountID) : null; + config_JSON.CF.APIToken = CF_JSON.APIToken ? 掩码敏感信息(CF_JSON.APIToken) : null; + config_JSON.CF.APIURL = null; + const Usage = await getCloudflareUsage(CF_JSON.Email, CF_JSON.GlobalAPIKey, CF_JSON.AccountID, CF_JSON.APIToken); + config_JSON.CF.Usage = Usage; + } } } catch (error) { console.error(`读取cf.json出错: ${error.message}`);