@@ -122,18 +122,20 @@ export default {
122122 } else if ( 访问路径 === 'admin/cf.json' ) { // 保存cf.json配置
123123 try {
124124 const newConfig = await request . json ( ) ;
125- const CF_JSON = { Email : null , GlobalAPIKey : null , AccountID : null , APIToken : null } ;
125+ const CF_JSON = { Email : null , GlobalAPIKey : null , AccountID : null , APIToken : null , UsageAPI : null } ;
126126 if ( ! newConfig . init || newConfig . init !== true ) {
127127 if ( newConfig . Email && newConfig . GlobalAPIKey ) {
128128 CF_JSON . Email = newConfig . Email ;
129129 CF_JSON . GlobalAPIKey = newConfig . GlobalAPIKey ;
130130 CF_JSON . AccountID = null ;
131131 CF_JSON . APIToken = null ;
132+ CF_JSON . UsageAPI = null ;
132133 } else if ( newConfig . AccountID && newConfig . APIToken ) {
133134 CF_JSON . Email = null ;
134135 CF_JSON . GlobalAPIKey = null ;
135136 CF_JSON . AccountID = newConfig . AccountID ;
136137 CF_JSON . APIToken = newConfig . APIToken ;
138+ CF_JSON . UsageAPI = null ;
137139 } else {
138140 return new Response ( JSON . stringify ( { error : '配置不完整' } ) , { status : 400 , headers : { 'Content-Type' : 'application/json;charset=utf-8' } } ) ;
139141 }
@@ -205,7 +207,7 @@ export default {
205207 if ( config_JSON . CF . Usage . success ) {
206208 pagesSum = config_JSON . CF . Usage . pages ;
207209 workersSum = config_JSON . CF . Usage . workers ;
208- total = 1024 * 100 ;
210+ total = Number . isFinite ( config_JSON . CF . Usage . max ) ? ( config_JSON . CF . Usage . max / 1000 ) * 1024 : 1024 * 100 ;
209211 }
210212 const responseHeaders = {
211213 "content-type" : "text/plain; charset=utf-8" ,
@@ -330,7 +332,6 @@ export default {
330332 }
331333 return new Response ( 订阅内容 , { status : 200 , headers : responseHeaders } ) ;
332334 }
333- return new Response ( '无效的订阅TOKEN' , { status : 403 } ) ;
334335 } else if ( 访问路径 === 'locations' ) { //反代locations列表
335336 const cookies = request . headers . get ( 'Cookie' ) || '' ;
336337 const authCookie = cookies . split ( ';' ) . find ( c => c . trim ( ) . startsWith ( 'auth=' ) ) ?. split ( '=' ) [ 1 ] ;
@@ -1125,11 +1126,13 @@ async function 读取config_JSON(env, hostname, userID, path, 重置配置 = fal
11251126 GlobalAPIKey : null ,
11261127 AccountID : null ,
11271128 APIToken : null ,
1129+ UsageAPI : null ,
11281130 Usage : {
11291131 success : false ,
11301132 pages : 0 ,
11311133 workers : 0 ,
11321134 total : 0 ,
1135+ max : 100000 ,
11331136 } ,
11341137 }
11351138 } ;
@@ -1171,20 +1174,31 @@ async function 读取config_JSON(env, hostname, userID, path, 重置配置 = fal
11711174 console . error ( `读取tg.json出错: ${ error . message } ` ) ;
11721175 }
11731176
1174- const 初始化CF_JSON = { Email : null , GlobalAPIKey : null , AccountID : null , APIToken : null } ;
1175- config_JSON . CF = { ...初始化CF_JSON , Usage : { success : false , pages : 0 , workers : 0 , total : 0 } } ;
1177+ const 初始化CF_JSON = { Email : null , GlobalAPIKey : null , AccountID : null , APIToken : null , UsageAPI : null } ;
1178+ config_JSON . CF = { ...初始化CF_JSON , Usage : { success : false , pages : 0 , workers : 0 , total : 0 , max : 100000 } } ;
11761179 try {
11771180 const CF_TXT = await env . KV . get ( 'cf.json' ) ;
11781181 if ( ! CF_TXT ) {
11791182 await env . KV . put ( 'cf.json' , JSON . stringify ( 初始化CF_JSON , null , 2 ) ) ;
11801183 } else {
11811184 const CF_JSON = JSON . parse ( CF_TXT ) ;
1182- config_JSON . CF . Email = CF_JSON . Email ? CF_JSON . Email : null ;
1183- config_JSON . CF . GlobalAPIKey = CF_JSON . GlobalAPIKey ? 掩码敏感信息 ( CF_JSON . GlobalAPIKey ) : null ;
1184- config_JSON . CF . AccountID = CF_JSON . AccountID ? 掩码敏感信息 ( CF_JSON . AccountID ) : null ;
1185- config_JSON . CF . APIToken = CF_JSON . APIToken ? 掩码敏感信息 ( CF_JSON . APIToken ) : null ;
1186- const Usage = await getCloudflareUsage ( CF_JSON . Email , CF_JSON . GlobalAPIKey , CF_JSON . AccountID , CF_JSON . APIToken ) ;
1187- config_JSON . CF . Usage = Usage ;
1185+ if ( CF_JSON . UsageAPI ) {
1186+ try {
1187+ const response = await fetch ( CF_JSON . UsageAPI ) ;
1188+ const Usage = await response . json ( ) ;
1189+ config_JSON . CF . Usage = Usage ;
1190+ } catch ( err ) {
1191+ console . error ( `请求 CF_JSON.UsageAPI 失败: ${ err . message } ` ) ;
1192+ }
1193+ } else {
1194+ config_JSON . CF . Email = CF_JSON . Email ? CF_JSON . Email : null ;
1195+ config_JSON . CF . GlobalAPIKey = CF_JSON . GlobalAPIKey ? 掩码敏感信息 ( CF_JSON . GlobalAPIKey ) : null ;
1196+ config_JSON . CF . AccountID = CF_JSON . AccountID ? 掩码敏感信息 ( CF_JSON . AccountID ) : null ;
1197+ config_JSON . CF . APIToken = CF_JSON . APIToken ? 掩码敏感信息 ( CF_JSON . APIToken ) : null ;
1198+ config_JSON . CF . UsageAPI = null ;
1199+ const Usage = await getCloudflareUsage ( CF_JSON . Email , CF_JSON . GlobalAPIKey , CF_JSON . AccountID , CF_JSON . APIToken ) ;
1200+ config_JSON . CF . Usage = Usage ;
1201+ }
11881202 }
11891203 } catch ( error ) {
11901204 console . error ( `读取cf.json出错: ${ error . message } ` ) ;
@@ -1466,7 +1480,7 @@ async function getCloudflareUsage(Email, GlobalAPIKey, AccountID, APIToken) {
14661480 const cfg = { "Content-Type" : "application/json" } ;
14671481
14681482 try {
1469- if ( ! AccountID && ( ! Email || ! GlobalAPIKey ) ) return { success : false , pages : 0 , workers : 0 , total : 0 } ;
1483+ if ( ! AccountID && ( ! Email || ! GlobalAPIKey ) ) return { success : false , pages : 0 , workers : 0 , total : 0 , max : 100000 } ;
14701484
14711485 if ( ! AccountID ) {
14721486 const r = await fetch ( `${ API } /accounts` , {
@@ -1508,12 +1522,13 @@ async function getCloudflareUsage(Email, GlobalAPIKey, AccountID, APIToken) {
15081522 const pages = sum ( acc . pagesFunctionsInvocationsAdaptiveGroups ) ;
15091523 const workers = sum ( acc . workersInvocationsAdaptive ) ;
15101524 const total = pages + workers ;
1511- console . log ( `统计结果 - Pages: ${ pages } , Workers: ${ workers } , 总计: ${ total } ` ) ;
1512- return { success : true , pages, workers, total } ;
1525+ const max = 100000 ;
1526+ console . log ( `统计结果 - Pages: ${ pages } , Workers: ${ workers } , 总计: ${ total } , 上限: 100000` ) ;
1527+ return { success : true , pages, workers, total, max } ;
15131528
15141529 } catch ( error ) {
15151530 console . error ( '获取使用量错误:' , error . message ) ;
1516- return { success : false , pages : 0 , workers : 0 , total : 0 } ;
1531+ return { success : false , pages : 0 , workers : 0 , total : 0 , max : 100000 } ;
15171532 }
15181533}
15191534
0 commit comments