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
| addEventListener("fetch", (event) => { event.respondWith(handleRequest(event.request)); });
async function handleRequest(request) { try { const url = new URL(request.url);
if (url.pathname === "/") { return new Response(getRootHtml(), { headers: { "Content-Type": "text/html; charset=utf-8", }, }); }
let actualUrlStr = decodeURIComponent(url.pathname.replace("/", ""));
actualUrlStr = ensureProtocol(actualUrlStr, url.protocol);
actualUrlStr += url.search;
const newHeaders = filterHeaders( request.headers, (name) => !name.startsWith("cf-") );
const modifiedRequest = new Request(actualUrlStr, { headers: newHeaders, method: request.method, body: request.body, redirect: "manual", });
const response = await fetch(modifiedRequest); let body = response.body;
if ([301, 302, 303, 307, 308].includes(response.status)) { body = response.body; return handleRedirect(response, body); } else if (response.headers.get("Content-Type")?.includes("text/html")) { body = await handleHtmlContent( response, url.protocol, url.host, actualUrlStr ); }
const modifiedResponse = new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers, });
setNoCacheHeaders(modifiedResponse.headers);
setCorsHeaders(modifiedResponse.headers);
return modifiedResponse; } catch (error) { return jsonResponse( { error: error.message, }, 500 ); } }
function ensureProtocol(url, defaultProtocol) { return url.startsWith("http://") || url.startsWith("https://") ? url : defaultProtocol + "//" + url; }
function handleRedirect(response, body) { const location = new URL(response.headers.get("location")); const modifiedLocation = `/${encodeURIComponent(location.toString())}`; return new Response(body, { status: response.status, statusText: response.statusText, headers: { ...response.headers, Location: modifiedLocation, }, }); }
async function handleHtmlContent(response, protocol, host, actualUrlStr) { const originalText = await response.text(); const regex = new RegExp("((href|src|action)=[\"'])/(?!/)", "g"); let modifiedText = replaceRelativePaths( originalText, protocol, host, new URL(actualUrlStr).origin );
return modifiedText; }
function replaceRelativePaths(text, protocol, host, origin) { const regex = new RegExp("((href|src|action)=[\"'])/(?!/)", "g"); return text.replace(regex, `$1${protocol}//${host}/${origin}/`); }
function jsonResponse(data, status) { return new Response(JSON.stringify(data), { status: status, headers: { "Content-Type": "application/json; charset=utf-8", }, }); }
function filterHeaders(headers, filterFunc) { return new Headers([...headers].filter(([name]) => filterFunc(name))); }
function setNoCacheHeaders(headers) { headers.set("Cache-Control", "no-store"); }
function setCorsHeaders(headers) { headers.set("Access-Control-Allow-Origin", "*"); headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); headers.set("Access-Control-Allow-Headers", "*"); }
function getRootHtml() { return `<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet"> <title>Proxy Everything</title> <link rel="icon" type="image/png" href="https://img.icons8.com/color/1000/kawaii-bread-1.png"> <meta name="Description" content="Proxy Everything with CF Workers."> <meta property="og:description" content="Proxy Everything with CF Workers."> <meta property="og:image" content="https://img.icons8.com/color/1000/kawaii-bread-1.png"> <meta name="robots" content="index, follow"> <meta http-equiv="Content-Language" content="zh-CN"> <meta name="copyright" content="Copyright © ymyuuu"> <meta name="author" content="ymyuuu"> <link rel="apple-touch-icon-precomposed" sizes="120x120" href="https://img.icons8.com/color/1000/kawaii-bread-1.png"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> <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"> <style> body, html { height: 100%; margin: 0; } .background { background-image: url('https://imgapi.cn/bing.php'); background-size: cover; background-position: center; height: 100%; display: flex; align-items: center; justify-content: center; } .card { background-color: rgba(255, 255, 255, 0.8); transition: background-color 0.3s ease, box-shadow 0.3s ease; } .card:hover { background-color: rgba(255, 255, 255, 1); box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.3); } .input-field input[type=text] { color: #2c3e50; } .input-field input[type=text]:focus+label { color: #2c3e50 !important; } .input-field input[type=text]:focus { border-bottom: 1px solid #2c3e50 !important; box-shadow: 0 1px 0 0 #2c3e50 !important; } </style> </head> <body> <div class="background"> <div class="container"> <div class="row"> <div class="col s12 m8 offset-m2 l6 offset-l3"> <div class="card"> <div class="card-content"> <span class="card-title center-align"><i class="material-icons left">link</i>Proxy Everything</span> <form id="urlForm" onsubmit="redirectToProxy(event)"> <div class="input-field"> <input type="text" id="targetUrl" placeholder="在此输入目标地址" required> <label for="targetUrl">目标地址</label> </div> <button type="submit" class="btn waves-effect waves-light teal darken-2 full-width">跳转</button> </form> </div> </div> </div> </div> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script> <script> function redirectToProxy(event) { event.preventDefault(); const targetUrl = document.getElementById('targetUrl').value.trim(); const currentOrigin = window.location.origin; window.open(currentOrigin + '/' + encodeURIComponent(targetUrl), '_blank'); } </script> </body> </html>`; }
|