-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy path_worker.js
More file actions
153 lines (136 loc) · 4.79 KB
/
_worker.js
File metadata and controls
153 lines (136 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
let token = "";
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname !== '/') {
let githubRawUrl = 'https://raw.githubusercontent.com';
if (new RegExp(githubRawUrl, 'i').test(url.pathname)) {
githubRawUrl += url.pathname.split(githubRawUrl)[1];
} else {
if (env.GH_NAME) {
githubRawUrl += '/' + env.GH_NAME;
if (env.GH_REPO) {
githubRawUrl += '/' + env.GH_REPO;
if (env.GH_BRANCH) githubRawUrl += '/' + env.GH_BRANCH;
}
}
githubRawUrl += url.pathname;
}
//console.log(githubRawUrl);
// 初始化请求头
const headers = new Headers();
let authTokenSet = false; // 标记是否已经设置了认证token
// 检查TOKEN_PATH特殊路径鉴权
if (env.TOKEN_PATH) {
const 需要鉴权的路径配置 = await ADD(env.TOKEN_PATH);
// 将路径转换为小写进行比较,防止大小写绕过
const normalizedPathname = decodeURIComponent(url.pathname.toLowerCase());
//检测访问路径是否需要鉴权
for (const pathConfig of 需要鉴权的路径配置) {
const configParts = pathConfig.split('@');
if (configParts.length !== 2) {
// 如果格式不正确,跳过这个配置
continue;
}
const [requiredToken, pathPart] = configParts;
const normalizedPath = '/' + pathPart.toLowerCase().trim();
// 精确匹配路径段,防止部分匹配绕过
const pathMatches = normalizedPathname === normalizedPath ||
normalizedPathname.startsWith(normalizedPath + '/');
if (pathMatches) {
const providedToken = url.searchParams.get('token');
if (!providedToken) {
return new Response('TOKEN不能为空', { status: 400 });
}
if (providedToken !== requiredToken.trim()) {
return new Response('TOKEN错误', { status: 403 });
}
// token验证成功,使用GH_TOKEN作为GitHub请求的token
if (!env.GH_TOKEN) {
return new Response('服务器GitHub TOKEN配置错误', { status: 500 });
}
headers.append('Authorization', `token ${env.GH_TOKEN}`);
authTokenSet = true;
break; // 找到匹配的路径配置后退出循环
}
}
}
// 如果TOKEN_PATH没有设置认证,使用默认token逻辑
if (!authTokenSet) {
if (env.GH_TOKEN && env.TOKEN) {
if (env.TOKEN == url.searchParams.get('token')) token = env.GH_TOKEN || token;
else token = url.searchParams.get('token') || token;
} else token = url.searchParams.get('token') || env.GH_TOKEN || env.TOKEN || token;
const githubToken = token;
//console.log(githubToken);
if (!githubToken || githubToken == '') {
return new Response('TOKEN不能为空', { status: 400 });
}
headers.append('Authorization', `token ${githubToken}`);
}
// 发起请求
const response = await fetch(githubRawUrl, { headers });
// 检查请求是否成功 (状态码 200 到 299)
if (response.ok) {
return new Response(response.body, {
status: response.status,
headers: response.headers
});
} else {
const errorText = env.ERROR || '无法获取文件,检查路径或TOKEN是否正确。';
// 如果请求不成功,返回适当的错误响应
return new Response(errorText, { status: response.status });
}
} else {
const envKey = env.URL302 ? 'URL302' : (env.URL ? 'URL' : null);
if (envKey) {
const URLs = await ADD(env[envKey]);
const URL = URLs[Math.floor(Math.random() * URLs.length)];
return envKey === 'URL302' ? Response.redirect(URL, 302) : fetch(new Request(URL, request));
}
//首页改成一个nginx伪装页
return new Response(await nginx(), {
headers: {
'Content-Type': 'text/html; charset=UTF-8',
},
});
}
}
};
async function nginx() {
const text = `
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
`
return text;
}
async function ADD(envadd) {
var addtext = envadd.replace(/[ |"'\r\n]+/g, ',').replace(/,+/g, ','); // 将空格、双引号、单引号和换行符替换为逗号
//console.log(addtext);
if (addtext.charAt(0) == ',') addtext = addtext.slice(1);
if (addtext.charAt(addtext.length - 1) == ',') addtext = addtext.slice(0, addtext.length - 1);
const add = addtext.split(',');
//console.log(add);
return add;
}