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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
addEventListener('fetch', event => {
|
| 2 |
+
event.respondWith(handleRequest(event.request));
|
| 3 |
+
});
|
| 4 |
+
|
| 5 |
+
async function handleRequest(request) {
|
| 6 |
+
try {
|
| 7 |
+
const url = new URL(request.url);
|
| 8 |
+
|
| 9 |
+
// 如果访问根目录,返回HTML
|
| 10 |
+
if (url.pathname === "/") {
|
| 11 |
+
return new Response(getRootHtml(), {
|
| 12 |
+
headers: {
|
| 13 |
+
'Content-Type': 'text/html; charset=utf-8'
|
| 14 |
+
}
|
| 15 |
+
});
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
// 从请求路径中提取目标 URL
|
| 19 |
+
let actualUrlStr = decodeURIComponent(url.pathname.replace("/", ""));
|
| 20 |
+
|
| 21 |
+
// 判断用户输入的 URL 是否带有协议
|
| 22 |
+
actualUrlStr = ensureProtocol(actualUrlStr, url.protocol);
|
| 23 |
+
|
| 24 |
+
// 保留查询参数
|
| 25 |
+
actualUrlStr += url.search;
|
| 26 |
+
|
| 27 |
+
// 创建新 Headers 对象,排除以 'cf-' 开头的请求头
|
| 28 |
+
const newHeaders = filterHeaders(request.headers, name => !name.startsWith('cf-'));
|
| 29 |
+
|
| 30 |
+
// 创建一个新的请求以访问目标 URL
|
| 31 |
+
const modifiedRequest = new Request(actualUrlStr, {
|
| 32 |
+
headers: newHeaders,
|
| 33 |
+
method: request.method,
|
| 34 |
+
body: request.body,
|
| 35 |
+
redirect: 'manual'
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
// 发起对目标 URL 的请求
|
| 39 |
+
const response = await fetch(modifiedRequest);
|
| 40 |
+
let body = response.body;
|
| 41 |
+
|
| 42 |
+
// 处理重定向
|
| 43 |
+
if ([301, 302, 303, 307, 308].includes(response.status)) {
|
| 44 |
+
body = response.body;
|
| 45 |
+
// 创建新的 Response 对象以修改 Location 头部
|
| 46 |
+
return handleRedirect(response, body);
|
| 47 |
+
} else if (response.headers.get("Content-Type")?.includes("text/html")) {
|
| 48 |
+
body = await handleHtmlContent(response, url.protocol, url.host, actualUrlStr);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// 创建修改后的响应对象
|
| 52 |
+
const modifiedResponse = new Response(body, {
|
| 53 |
+
status: response.status,
|
| 54 |
+
statusText: response.statusText,
|
| 55 |
+
headers: response.headers
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
// 添加禁用缓存的头部
|
| 59 |
+
setNoCacheHeaders(modifiedResponse.headers);
|
| 60 |
+
|
| 61 |
+
// 添加 CORS 头部,允许跨域访问
|
| 62 |
+
setCorsHeaders(modifiedResponse.headers);
|
| 63 |
+
|
| 64 |
+
return modifiedResponse;
|
| 65 |
+
} catch (error) {
|
| 66 |
+
// 如果请求目标地址时出现错误,返回带有错误消息的响应和状态码 500(服务器错误)
|
| 67 |
+
return jsonResponse({
|
| 68 |
+
error: error.message
|
| 69 |
+
}, 500);
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
// 确保 URL 带有协议
|
| 74 |
+
function ensureProtocol(url, defaultProtocol) {
|
| 75 |
+
return url.startsWith("http://") || url.startsWith("https://") ? url : defaultProtocol + "//" + url;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// 处理重定向
|
| 79 |
+
function handleRedirect(response, body) {
|
| 80 |
+
const location = new URL(response.headers.get('location'));
|
| 81 |
+
const modifiedLocation = `/${encodeURIComponent(location.toString())}`;
|
| 82 |
+
return new Response(body, {
|
| 83 |
+
status: response.status,
|
| 84 |
+
statusText: response.statusText,
|
| 85 |
+
headers: {
|
| 86 |
+
...response.headers,
|
| 87 |
+
'Location': modifiedLocation
|
| 88 |
+
}
|
| 89 |
+
});
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// 处理 HTML 内容中的相对路径
|
| 93 |
+
async function handleHtmlContent(response, protocol, host, actualUrlStr) {
|
| 94 |
+
const originalText = await response.text();
|
| 95 |
+
const regex = new RegExp('((href|src|action)=["\'])/(?!/)', 'g');
|
| 96 |
+
let modifiedText = replaceRelativePaths(originalText, protocol, host, new URL(actualUrlStr).origin);
|
| 97 |
+
|
| 98 |
+
return modifiedText;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// 替换 HTML 内容中的相对路径
|
| 102 |
+
function replaceRelativePaths(text, protocol, host, origin) {
|
| 103 |
+
const regex = new RegExp('((href|src|action)=["\'])/(?!/)', 'g');
|
| 104 |
+
return text.replace(regex, `$1${protocol}//${host}/${origin}/`);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// 返回 JSON 格式的响应
|
| 108 |
+
function jsonResponse(data, status) {
|
| 109 |
+
return new Response(JSON.stringify(data), {
|
| 110 |
+
status: status,
|
| 111 |
+
headers: {
|
| 112 |
+
'Content-Type': 'application/json; charset=utf-8'
|
| 113 |
+
}
|
| 114 |
+
});
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// 过滤请求头
|
| 118 |
+
function filterHeaders(headers, filterFunc) {
|
| 119 |
+
return new Headers([...headers].filter(([name]) => filterFunc(name)));
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// 设置禁用缓存的头部
|
| 123 |
+
function setNoCacheHeaders(headers) {
|
| 124 |
+
headers.set('Cache-Control', 'no-store');
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
// 设置 CORS 头部
|
| 128 |
+
function setCorsHeaders(headers) {
|
| 129 |
+
headers.set('Access-Control-Allow-Origin', '*');
|
| 130 |
+
headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
|
| 131 |
+
headers.set('Access-Control-Allow-Headers', '*');
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// 返回根目录的 HTML
|
| 135 |
+
function getRootHtml() {
|
| 136 |
+
return `<!DOCTYPE html>
|
| 137 |
+
<html lang="zh-CN">
|
| 138 |
+
<head>
|
| 139 |
+
<meta charset="UTF-8">
|
| 140 |
+
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet">
|
| 141 |
+
<title>Proxy Everything</title>
|
| 142 |
+
<link rel="icon" type="image/png" href="https://img.icons8.com/color/1000/kawaii-bread-1.png">
|
| 143 |
+
<meta name="Description" content="Proxy Everything with CF Workers.">
|
| 144 |
+
<meta property="og:description" content="Proxy Everything with CF Workers.">
|
| 145 |
+
<meta property="og:image" content="https://img.icons8.com/color/1000/kawaii-bread-1.png">
|
| 146 |
+
<meta name="robots" content="index, follow">
|
| 147 |
+
<meta http-equiv="Content-Language" content="zh-CN">
|
| 148 |
+
<meta name="copyright" content="Copyright © ymyuuu">
|
| 149 |
+
<meta name="author" content="ymyuuu">
|
| 150 |
+
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="https://img.icons8.com/color/1000/kawaii-bread-1.png">
|
| 151 |
+
<meta name="apple-mobile-web-app-capable" content="yes">
|
| 152 |
+
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
|
| 153 |
+
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
|
| 154 |
+
<style>
|
| 155 |
+
body, html {
|
| 156 |
+
height: 100%;
|
| 157 |
+
margin: 0;
|
| 158 |
+
}
|
| 159 |
+
.background {
|
| 160 |
+
background-image: url('https://imgapi.cn/bing.php');
|
| 161 |
+
background-size: cover;
|
| 162 |
+
background-position: center;
|
| 163 |
+
height: 100%;
|
| 164 |
+
display: flex;
|
| 165 |
+
align-items: center;
|
| 166 |
+
justify-content: center;
|
| 167 |
+
}
|
| 168 |
+
.card {
|
| 169 |
+
background-color: rgba(255, 255, 255, 0.8);
|
| 170 |
+
transition: background-color 0.3s ease, box-shadow 0.3s ease;
|
| 171 |
+
}
|
| 172 |
+
.card:hover {
|
| 173 |
+
background-color: rgba(255, 255, 255, 1);
|
| 174 |
+
box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.3);
|
| 175 |
+
}
|
| 176 |
+
.input-field input[type=text] {
|
| 177 |
+
color: #2c3e50;
|
| 178 |
+
}
|
| 179 |
+
.input-field input[type=text]:focus+label {
|
| 180 |
+
color: #2c3e50 !important;
|
| 181 |
+
}
|
| 182 |
+
.input-field input[type=text]:focus {
|
| 183 |
+
border-bottom: 1px solid #2c3e50 !important;
|
| 184 |
+
box-shadow: 0 1px 0 0 #2c3e50 !important;
|
| 185 |
+
}
|
| 186 |
+
</style>
|
| 187 |
+
</head>
|
| 188 |
+
<body>
|
| 189 |
+
<div class="background">
|
| 190 |
+
<div class="container">
|
| 191 |
+
<div class="row">
|
| 192 |
+
<div class="col s12 m8 offset-m2 l6 offset-l3">
|
| 193 |
+
<div class="card">
|
| 194 |
+
<div class="card-content">
|
| 195 |
+
<span class="card-title center-align"><i class="material-icons left">link</i>Proxy Everything</span>
|
| 196 |
+
<form id="urlForm" onsubmit="redirectToProxy(event)">
|
| 197 |
+
<div class="input-field">
|
| 198 |
+
<input type="text" id="targetUrl" placeholder="在此输入目标地址" required>
|
| 199 |
+
<label for="targetUrl">目标地址</label>
|
| 200 |
+
</div>
|
| 201 |
+
<button type="submit" class="btn waves-effect waves-light teal darken-2 full-width">跳转</button>
|
| 202 |
+
</form>
|
| 203 |
+
</div>
|
| 204 |
+
</div>
|
| 205 |
+
</div>
|
| 206 |
+
</div>
|
| 207 |
+
</div>
|
| 208 |
+
</div>
|
| 209 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
|
| 210 |
+
<script>
|
| 211 |
+
function redirectToProxy(event) {
|
| 212 |
+
event.preventDefault();
|
| 213 |
+
const targetUrl = document.getElementById('targetUrl').value.trim();
|
| 214 |
+
const currentOrigin = window.location.origin;
|
| 215 |
+
window.open(currentOrigin + '/' + encodeURIComponent(targetUrl), '_blank');
|
| 216 |
+
}
|
| 217 |
+
</script>
|
| 218 |
+
</body>
|
| 219 |
+
</html>`;
|
| 220 |
+
}
|