fix: 使用ctx.waitUntil优化请求日志记录的异步处理

This commit is contained in:
cmliu
2025-11-25 19:52:01 +08:00
parent 2d6a4425c1
commit def4ddc978

View File

@@ -4,7 +4,7 @@ let SOCKS5白名单 = ['*tapecontent.net', '*cloudatacdn.com', '*loadshare.org',
const Pages静态页面 = 'https://edt-pages.github.io';
///////////////////////////////////////////////////////主程序入口///////////////////////////////////////////////
export default {
async fetch(request, env) {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const UA = request.headers.get('User-Agent') || 'null';
const upgradeHeader = request.headers.get('Upgrade');
@@ -93,7 +93,7 @@ export default {
if (访问路径 === 'admin/init') {// 重置配置为默认值
try {
config_JSON = await 读取config_JSON(env, host, userID, true);
await 请求日志记录(env, request, 访问IP, 'Init_Config', config_JSON);
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Init_Config', config_JSON));
config_JSON.init = '配置已重置为默认值';
return new Response(JSON.stringify(config_JSON, null, 2), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
} catch (err) {
@@ -109,7 +109,7 @@ export default {
// 保存到 KV
await env.KV.put('config.json', JSON.stringify(newConfig, null, 2));
await 请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON);
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON));
return new Response(JSON.stringify({ success: true, message: '配置已保存' }), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
} catch (error) {
console.error('保存配置失败:', error);
@@ -137,7 +137,7 @@ export default {
// 保存到 KV
await env.KV.put('cf.json', JSON.stringify(CF_JSON, null, 2));
await 请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON);
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON));
return new Response(JSON.stringify({ success: true, message: '配置已保存' }), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
} catch (error) {
console.error('保存配置失败:', error);
@@ -153,7 +153,7 @@ export default {
if (!newConfig.BotToken || !newConfig.ChatID) return new Response(JSON.stringify({ error: '配置不完整' }), { status: 400, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
await env.KV.put('tg.json', JSON.stringify(newConfig, null, 2));
}
await 请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON);
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON));
return new Response(JSON.stringify({ success: true, message: '配置已保存' }), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
} catch (error) {
console.error('保存配置失败:', error);
@@ -163,7 +163,7 @@ export default {
try {
const customIPs = await request.text();
await env.KV.put('ADD.txt', customIPs);// 保存到 KV
await 请求日志记录(env, request, 访问IP, 'Save_Custom_IPs', config_JSON);
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Save_Custom_IPs', config_JSON));
return new Response(JSON.stringify({ success: true, message: '自定义IP已保存' }), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
} catch (error) {
console.error('保存自定义IP失败:', error);
@@ -180,7 +180,7 @@ export default {
return new Response(JSON.stringify(request.cf, null, 2), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
}
await 请求日志记录(env, request, 访问IP, 'Admin_Login', config_JSON);
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Admin_Login', config_JSON));
return fetch(Pages静态页面 + '/admin');
} else if (访问路径 === 'logout') {//清除cookie并跳转到登录页面
const 响应 = new Response('重定向中...', { status: 302, headers: { 'Location': '/login' } });
@@ -190,7 +190,7 @@ export default {
const 订阅TOKEN = await MD5MD5(host + userID);
if (url.searchParams.get('token') === 订阅TOKEN) {
config_JSON = await 读取config_JSON(env, host, userID);
await 请求日志记录(env, request, 访问IP, 'Get_SUB', config_JSON);
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Get_SUB', config_JSON));
const ua = UA.toLowerCase();
const expire = 4102329600;//2099-12-31 到期时间
const now = Date.now();