Skip to content

Commit cda1f41

Browse files
authored
Merge pull request cmliu#851 from cmliu/beta2.0
Beta2.0
2 parents bc1c855 + 5c60d0d commit cda1f41

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

_worker.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let SOCKS5白名单 = ['*tapecontent.net', '*cloudatacdn.com', '*loadshare.org',
44
const Pages静态页面 = 'https://edt-pages.github.io';
55
///////////////////////////////////////////////////////主程序入口///////////////////////////////////////////////
66
export default {
7-
async fetch(request, env) {
7+
async fetch(request, env, ctx) {
88
const url = new URL(request.url);
99
const UA = request.headers.get('User-Agent') || 'null';
1010
const upgradeHeader = request.headers.get('Upgrade');
@@ -93,7 +93,7 @@ export default {
9393
if (访问路径 === 'admin/init') {// 重置配置为默认值
9494
try {
9595
config_JSON = await 读取config_JSON(env, host, userID, true);
96-
await 请求日志记录(env, request, 访问IP, 'Init_Config', config_JSON);
96+
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Init_Config', config_JSON));
9797
config_JSON.init = '配置已重置为默认值';
9898
return new Response(JSON.stringify(config_JSON, null, 2), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
9999
} catch (err) {
@@ -109,7 +109,7 @@ export default {
109109

110110
// 保存到 KV
111111
await env.KV.put('config.json', JSON.stringify(newConfig, null, 2));
112-
await 请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON);
112+
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON));
113113
return new Response(JSON.stringify({ success: true, message: '配置已保存' }), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
114114
} catch (error) {
115115
console.error('保存配置失败:', error);
@@ -137,7 +137,7 @@ export default {
137137

138138
// 保存到 KV
139139
await env.KV.put('cf.json', JSON.stringify(CF_JSON, null, 2));
140-
await 请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON);
140+
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON));
141141
return new Response(JSON.stringify({ success: true, message: '配置已保存' }), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
142142
} catch (error) {
143143
console.error('保存配置失败:', error);
@@ -153,7 +153,7 @@ export default {
153153
if (!newConfig.BotToken || !newConfig.ChatID) return new Response(JSON.stringify({ error: '配置不完整' }), { status: 400, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
154154
await env.KV.put('tg.json', JSON.stringify(newConfig, null, 2));
155155
}
156-
await 请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON);
156+
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Save_Config', config_JSON));
157157
return new Response(JSON.stringify({ success: true, message: '配置已保存' }), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
158158
} catch (error) {
159159
console.error('保存配置失败:', error);
@@ -163,7 +163,7 @@ export default {
163163
try {
164164
const customIPs = await request.text();
165165
await env.KV.put('ADD.txt', customIPs);// 保存到 KV
166-
await 请求日志记录(env, request, 访问IP, 'Save_Custom_IPs', config_JSON);
166+
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Save_Custom_IPs', config_JSON));
167167
return new Response(JSON.stringify({ success: true, message: '自定义IP已保存' }), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
168168
} catch (error) {
169169
console.error('保存自定义IP失败:', error);
@@ -180,7 +180,7 @@ export default {
180180
return new Response(JSON.stringify(request.cf, null, 2), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
181181
}
182182

183-
await 请求日志记录(env, request, 访问IP, 'Admin_Login', config_JSON);
183+
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Admin_Login', config_JSON));
184184
return fetch(Pages静态页面 + '/admin');
185185
} else if (访问路径 === 'logout') {//清除cookie并跳转到登录页面
186186
const 响应 = new Response('重定向中...', { status: 302, headers: { 'Location': '/login' } });
@@ -190,7 +190,7 @@ export default {
190190
const 订阅TOKEN = await MD5MD5(host + userID);
191191
if (url.searchParams.get('token') === 订阅TOKEN) {
192192
config_JSON = await 读取config_JSON(env, host, userID);
193-
await 请求日志记录(env, request, 访问IP, 'Get_SUB', config_JSON);
193+
ctx.waitUntil(请求日志记录(env, request, 访问IP, 'Get_SUB', config_JSON));
194194
const ua = UA.toLowerCase();
195195
const expire = 4102329600;//2099-12-31 到期时间
196196
const now = Date.now();
@@ -708,12 +708,7 @@ async function httpConnect(targetHost, targetPort, initialData) {
708708
}
709709
//////////////////////////////////////////////////功能性函数///////////////////////////////////////////////
710710
function surge(content, url, config_JSON) {
711-
let 每行内容;
712-
if (content.includes('\r\n')) {
713-
每行内容 = content.split('\r\n');
714-
} else {
715-
每行内容 = content.split('\n');
716-
}
711+
const 每行内容 = content.includes('\r\n') ? content.split('\r\n') : content.split('\n');
717712

718713
let 输出内容 = "";
719714
for (let x of 每行内容) {
@@ -730,6 +725,7 @@ function surge(content, url, config_JSON) {
730725
输出内容 = `#!MANAGED-CONFIG ${url} interval=${config_JSON.优选订阅生成.SUBUpdateTime * 60 * 60} strict=false` + 输出内容.substring(输出内容.indexOf('\n'));
731726
return 输出内容;
732727
}
728+
733729
async function 请求日志记录(env, request, 访问IP, 请求类型 = "Get_SUB", config_JSON) {
734730
const KV容量限制 = 4;//MB
735731
try {
@@ -1068,8 +1064,10 @@ async function 请求优选API(urls, 默认端口 = '443', 超时时间 = 3000)
10681064
const ipIdx = headers.indexOf('IP地址'), portIdx = headers.indexOf('端口');
10691065
const remarkIdx = headers.indexOf('国家') > -1 ? headers.indexOf('国家') :
10701066
headers.indexOf('城市') > -1 ? headers.indexOf('城市') : headers.indexOf('数据中心');
1067+
const tlsIdx = headers.indexOf('TLS');
10711068
dataLines.forEach(line => {
10721069
const cols = line.split(',').map(c => c.trim());
1070+
if (tlsIdx !== -1 && cols[tlsIdx]?.toLowerCase() !== 'true') return;
10731071
const wrappedIP = IPV6_PATTERN.test(cols[ipIdx]) ? `[${cols[ipIdx]}]` : cols[ipIdx];
10741072
results.add(`${wrappedIP}:${cols[portIdx]}#${cols[remarkIdx]}`);
10751073
});

0 commit comments

Comments
 (0)