worker.js
· 5.8 KiB · JavaScript
Brut
const upstream = 'cdn.jsdelivr.net'
const upstream_mobile = 'cdn.jsdelivr.net'
const blocked_region = ['KP', 'RU']
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']
const replace_dict = {
'$upstream': '$custom_domain',
'//cdn.jsdelivr.net': ''
}
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request));
})
async function fetchAndApply(request) {
const region = request.headers.get('cf-ipcountry').toUpperCase();
const ip_address = request.headers.get('cf-connecting-ip');
const user_agent = request.headers.get('user-agent');
let response = null;
let url = new URL(request.url);
let url_host = url.host;
// 检查路径是否不是 /gh/ 或 /npm/ 或 /wp/
if (!url.pathname.startsWith('/gh/') && !url.pathname.startsWith('/npm/') && !url.pathname.startsWith('/wp/')) {
// 返回简单的 HTML 页面并在 3 秒后重定向到 https://jsd-page.qyliu.top/
const htmlContent = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>正在跳转...</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f3f4f6;
font-family: Arial, sans-serif;
color: #333;
}
.container {
text-align: center;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.container h1 {
font-size: 24px;
margin-bottom: 10px;
}
.container p {
font-size: 16px;
color: #666;
}
</style>
<script type="text/javascript">
setTimeout(function() {
window.location.href = "https://jsd-page.qyliu.top/";
}, 3000);
</script>
</head>
<body>
<div class="container">
<h1>...正在跳转...</h1>
<p>该路径禁止访问</p>
<p>您将被重定向,请稍候...</p>
</div>
</body>
</html>
`;
return new Response(htmlContent, {
headers: { 'Content-Type': 'text/html' },
});
}
if (url.protocol == 'http:') {
url.protocol = 'https:'
response = Response.redirect(url.href);
return response;
}
let upstream_domain;
if (await device_status(user_agent)) {
upstream_domain = upstream
} else {
upstream_domain = upstream_mobile
}
url.host = upstream_domain;
if (blocked_region.includes(region)) {
response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
status: 403
});
} else if(blocked_ip_address.includes(ip_address)){
response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
status: 403
});
} else{
let method = request.method;
let request_headers = request.headers;
let new_request_headers = new Headers(request_headers);
new_request_headers.set('Host', upstream_domain);
new_request_headers.set('Referer', url.href);
let original_response = await fetch(url.href, {
method: method,
headers: new_request_headers
})
let original_response_clone = original_response.clone();
let original_text = null;
let response_headers = original_response.headers;
let new_response_headers = new Headers(response_headers);
let status = original_response.status;
new_response_headers.set('access-control-allow-origin', '*');
new_response_headers.set('access-control-allow-credentials', 'true'); // 将 true 转换为字符串
new_response_headers.delete('content-security-policy');
new_response_headers.delete('content-security-policy-report-only');
new_response_headers.delete('clear-site-data');
const content_type = new_response_headers.get('content-type');
if (content_type && content_type.includes('text/html') && content_type.includes('UTF-8')) {
original_text = await replace_response_text(original_response_clone, upstream_domain, url_host);
} else {
original_text = original_response_clone.body;
}
response = new Response(original_text, {
status,
headers: new_response_headers
})
}
return response;
}
async function replace_response_text(response, upstream_domain, host_name) {
let text = await response.text()
var i, j;
for (i in replace_dict) {
j = replace_dict[i]
if (i == '$upstream') {
i = upstream_domain
} else if (i == '$custom_domain') {
i = host_name
}
if (j == '$upstream') {
j = upstream_domain
} else if (j == '$custom_domain') {
j = host_name
}
let re = new RegExp(i, 'g')
text = text.replace(re, j);
}
return text;
}
async function device_status (user_agent_info) {
var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var flag = true;
for (var v = 0; v < agents.length; v++) {
if (user_agent_info.indexOf(agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
1 | const upstream = 'cdn.jsdelivr.net' |
2 | const upstream_mobile = 'cdn.jsdelivr.net' |
3 | |
4 | const blocked_region = ['KP', 'RU'] |
5 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] |
6 | |
7 | const replace_dict = { |
8 | '$upstream': '$custom_domain', |
9 | '//cdn.jsdelivr.net': '' |
10 | } |
11 | |
12 | addEventListener('fetch', event => { |
13 | event.respondWith(fetchAndApply(event.request)); |
14 | }) |
15 | |
16 | async function fetchAndApply(request) { |
17 | const region = request.headers.get('cf-ipcountry').toUpperCase(); |
18 | const ip_address = request.headers.get('cf-connecting-ip'); |
19 | const user_agent = request.headers.get('user-agent'); |
20 | |
21 | let response = null; |
22 | let url = new URL(request.url); |
23 | let url_host = url.host; |
24 | |
25 | // 检查路径是否不是 /gh/ 或 /npm/ 或 /wp/ |
26 | if (!url.pathname.startsWith('/gh/') && !url.pathname.startsWith('/npm/') && !url.pathname.startsWith('/wp/')) { |
27 | // 返回简单的 HTML 页面并在 3 秒后重定向到 https://jsd-page.qyliu.top/ |
28 | const htmlContent = ` |
29 | <!DOCTYPE html> |
30 | <html lang="zh-CN"> |
31 | <head> |
32 | <meta charset="UTF-8"> |
33 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
34 | <title>正在跳转...</title> |
35 | <style> |
36 | body { |
37 | margin: 0; |
38 | padding: 0; |
39 | display: flex; |
40 | justify-content: center; |
41 | align-items: center; |
42 | height: 100vh; |
43 | background-color: #f3f4f6; |
44 | font-family: Arial, sans-serif; |
45 | color: #333; |
46 | } |
47 | .container { |
48 | text-align: center; |
49 | background-color: #fff; |
50 | padding: 20px; |
51 | border-radius: 8px; |
52 | box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
53 | } |
54 | .container h1 { |
55 | font-size: 24px; |
56 | margin-bottom: 10px; |
57 | } |
58 | .container p { |
59 | font-size: 16px; |
60 | color: #666; |
61 | } |
62 | </style> |
63 | <script type="text/javascript"> |
64 | setTimeout(function() { |
65 | window.location.href = "https://jsd-page.qyliu.top/"; |
66 | }, 3000); |
67 | </script> |
68 | </head> |
69 | <body> |
70 | <div class="container"> |
71 | <h1>...正在跳转...</h1> |
72 | <p>该路径禁止访问</p> |
73 | <p>您将被重定向,请稍候...</p> |
74 | </div> |
75 | </body> |
76 | </html> |
77 | `; |
78 | return new Response(htmlContent, { |
79 | headers: { 'Content-Type': 'text/html' }, |
80 | }); |
81 | } |
82 | |
83 | if (url.protocol == 'http:') { |
84 | url.protocol = 'https:' |
85 | response = Response.redirect(url.href); |
86 | return response; |
87 | } |
88 | |
89 | let upstream_domain; |
90 | |
91 | if (await device_status(user_agent)) { |
92 | upstream_domain = upstream |
93 | } else { |
94 | upstream_domain = upstream_mobile |
95 | } |
96 | |
97 | url.host = upstream_domain; |
98 | |
99 | if (blocked_region.includes(region)) { |
100 | response = new Response('Access denied: WorkersProxy is not available in your region yet.', { |
101 | status: 403 |
102 | }); |
103 | } else if(blocked_ip_address.includes(ip_address)){ |
104 | response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', { |
105 | status: 403 |
106 | }); |
107 | } else{ |
108 | let method = request.method; |
109 | let request_headers = request.headers; |
110 | let new_request_headers = new Headers(request_headers); |
111 | |
112 | new_request_headers.set('Host', upstream_domain); |
113 | new_request_headers.set('Referer', url.href); |
114 | |
115 | let original_response = await fetch(url.href, { |
116 | method: method, |
117 | headers: new_request_headers |
118 | }) |
119 | |
120 | let original_response_clone = original_response.clone(); |
121 | let original_text = null; |
122 | let response_headers = original_response.headers; |
123 | let new_response_headers = new Headers(response_headers); |
124 | |
125 | let status = original_response.status; |
126 | |
127 | new_response_headers.set('access-control-allow-origin', '*'); |
128 | new_response_headers.set('access-control-allow-credentials', 'true'); // 将 true 转换为字符串 |
129 | new_response_headers.delete('content-security-policy'); |
130 | new_response_headers.delete('content-security-policy-report-only'); |
131 | new_response_headers.delete('clear-site-data'); |
132 | |
133 | const content_type = new_response_headers.get('content-type'); |
134 | if (content_type && content_type.includes('text/html') && content_type.includes('UTF-8')) { |
135 | original_text = await replace_response_text(original_response_clone, upstream_domain, url_host); |
136 | } else { |
137 | original_text = original_response_clone.body; |
138 | } |
139 | |
140 | response = new Response(original_text, { |
141 | status, |
142 | headers: new_response_headers |
143 | }) |
144 | } |
145 | return response; |
146 | } |
147 | |
148 | async function replace_response_text(response, upstream_domain, host_name) { |
149 | let text = await response.text() |
150 | |
151 | var i, j; |
152 | for (i in replace_dict) { |
153 | j = replace_dict[i] |
154 | if (i == '$upstream') { |
155 | i = upstream_domain |
156 | } else if (i == '$custom_domain') { |
157 | i = host_name |
158 | } |
159 | |
160 | if (j == '$upstream') { |
161 | j = upstream_domain |
162 | } else if (j == '$custom_domain') { |
163 | j = host_name |
164 | } |
165 | |
166 | let re = new RegExp(i, 'g') |
167 | text = text.replace(re, j); |
168 | } |
169 | return text; |
170 | } |
171 | |
172 | async function device_status (user_agent_info) { |
173 | var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; |
174 | var flag = true; |
175 | for (var v = 0; v < agents.length; v++) { |
176 | if (user_agent_info.indexOf(agents[v]) > 0) { |
177 | flag = false; |
178 | break; |
179 | } |
180 | } |
181 | return flag; |
182 | } |
183 |