-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathworker.js
More file actions
336 lines (289 loc) · 7.75 KB
/
worker.js
File metadata and controls
336 lines (289 loc) · 7.75 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
name: cloudflare安全代理
author: eray
version: v1.0
blog: blog.eray.cc
email: admin#eray.cc
desp: 本项目可以给cf代理设置一个密码,使代理更安全,调用更方便
config set:
需要替换:
密码验证路径(可选):
/your-pwd-verify-path
越权Api代理前缀路径[用于代理openai等api]
(可以直接注释删除,不使用):
/your-safe-api-prefix/
访问密码(推荐设置复杂一些):
your-access-pwd
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url);
// Check if some api direct browse, the path should be hard to guess, u can comment it out if you don't need it
if (url.pathname.includes('/your-safe-api-prefix/')) {
return handleApiRequest(request);
}
// Check if the request is for password verification
if (url.pathname === '/your-pwd-verify-path' && request.method === 'POST') {
const password = await request.text();
if (verifyPassword(password)) {
return new Response('success', {
headers: {
'Content-Type': 'text/plain'
},
});
} else {
return new Response('failure', {
headers: {
'Content-Type': 'text/plain'
},
});
}
}
// Check if password cookie exists
const savedPassword = request.headers.get('Cookie');
if (!savedPassword) {
return createPasswordPage();
}
//if exists cookie but is not pwd
if (savedPassword) {
const parsedCookies = parseCookies(savedPassword);
const passwordValue = parsedCookies['password'];
if (!verifyPassword(passwordValue)) {
return createPasswordPage();
}
}
// Check if accessing path
if (url.pathname === '/' || url.pathname === '/proxy/' || url.pathname === '/proxy') {
return createLandingPage();
}
// If password cookie exists and accessing other paths, handle proxying
return handleProxyRequest(request);
}
async function handleProxyRequest(request) {
const url = new URL(request.url);
// Use fixUrl function to ensure valid URL protocol
const actualUrlStr = fixUrl(url.pathname.replace("/proxy/", "")) + url.search + url.hash;
const actualUrl = new URL(actualUrlStr);
const modifiedRequest = new Request(actualUrl, {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});
const response = await fetch(modifiedRequest);
const modifiedResponse = new Response(response.body, response);
// Add headers for CORS
modifiedResponse.headers.set('Access-Control-Allow-Origin', '*');
return modifiedResponse;
}
async function handleApiRequest(request) {
const url = new URL(request.url);
// Use fixUrl function to ensure valid URL protocol
const actualUrlStr = fixUrl(url.pathname.replace("/your-safe-api-prefix/", "")) + url.search + url.hash;
const actualUrl = new URL(actualUrlStr);
const modifiedRequest = new Request(actualUrl, {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});
const response = await fetch(modifiedRequest);
const modifiedResponse = new Response(response.body, response);
// Add headers for CORS
modifiedResponse.headers.set('Access-Control-Allow-Origin', '*');
return modifiedResponse;
}
function fixUrl(url) {
if (url.includes("://")) {
return url;
} else if (url.includes(':/')) {
return url.replace(':/', '://');
} else {
return "http://" + url;
}
}
// parse cookie
function parseCookies(cookieString) {
return cookieString.split(';')
.reduce((cookies, cookie) => {
const [name, value] = cookie.trim()
.split('=');
cookies[name] = value;
return cookies;
}, {});
}
// Create the password verification page
function createPasswordPage() {
const html = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>密码验证</title>
</head>
<style>
body {
background-color: #fbfbfb;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
color: #444;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
form {
background-color: white;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
padding: 2rem;
border-radius: 8px;
}
input {
display: block;
width: 100%;
font-size: 18px;
padding: 15px;
border: solid 1px #ccc;
border-radius: 4px;
margin: 1rem 0;
}
button {
padding: 15px;
background-color: #0288d1;
color: white;
font-size: 18px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color: #039BE5;
}
</style>
<body>
<h1>密码验证</h1>
<form id="password-form">
<input type="password" id="password" name="password" placeholder="请输入访问密码" required />
<button type="submit">验证密码</button>
</form>
<script>
const form = document.getElementById('password-form');
form.addEventListener('submit', event => {
event.preventDefault();
const input = document.getElementById('password');
const password = input.value;
// Send password for verification
fetch('/your-pwd-verify-path', { method: 'POST', body: password })
.then(response => response.text())
.then(result => {
if (result === 'success') {
// Password verification successful, set cookie, and redirect to the landing page
document.cookie = 'password=' + password + '; expires=' + new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toUTCString();
location.href = '/proxy/';
} else {
alert('密码验证失败!');
}
});
});
</script>
</body>
</html>
`;
return new Response(html, {
headers: {
'Content-Type': 'text/html'
}
});
}
// Create the landing page
function createLandingPage() {
const html = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<style>
body {
background-color: #fbfbfb;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
color: #444;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
form {
background-color: white;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
padding: 2rem;
border-radius: 8px;
}
input {
display: block;
width: 100%;
font-size: 18px;
padding: 15px;
border: solid 1px #ccc;
border-radius: 4px;
margin: 1rem 0;
}
button {
padding: 15px;
background-color: #0288d1;
color: white;
font-size: 18px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color: #039BE5;
}
</style>
<meta charset="UTF-8">
<title>安全访问</title>
</head>
<body>
<h1>请输入安全访问路径</h1>
<form id="proxy-form">
<input type="text" id="url" name="url" placeholder="请输入路径" required />
<button type="submit">安全访问</button>
</form>
<script>
const form = document.getElementById('proxy-form');
form.addEventListener('submit', event => {
event.preventDefault();
const input = document.getElementById('url');
const actualUrl = input.value;
const proxyUrl = '/proxy/' + actualUrl;
location.href = proxyUrl;
});
</script>
</body>
</html>
`;
return new Response(html, {
headers: {
'Content-Type': 'text/html'
}
});
}
// Verify the password function
function verifyPassword(savedPassword) {
// Replace this logic with your actual password verification
return savedPassword === "your-access-pwd";
}