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
| const domain_mappings = { "github.com": "gh.", "avatars.githubusercontent.com": "avatars-githubusercontent-com.", "github.githubassets.com": "github-githubassets-com.", "collector.github.com": "collector-github-com.", "api.github.com": "api-github-com.", "raw.githubusercontent.com": "raw-githubusercontent-com.", "gist.githubusercontent.com": "gist-githubusercontent-com.", "github.io": "github-io.", "assets-cdn.github.com": "assets-cdn-github-com.", "cdn.jsdelivr.net": "cdn.jsdelivr-net.", "securitylab.github.com": "securitylab-github-com.", "www.githubstatus.com": "www-githubstatus-com.", "npmjs.com": "npmjs-com.", "git-lfs.github.com": "git-lfs-github-com.", "githubusercontent.com": "githubusercontent-com.", "github.global.ssl.fastly.net": "github-global-ssl-fastly-net.", "api.npms.io": "api-npms-io.", "github.community": "github-community.", };
const redirect_paths = ["/", "/login", "/signup", "/copilot"];
addEventListener("fetch", (event) => { event.respondWith(handleRequest(event.request)); });
async function handleRequest(request) { const url = new URL(request.url); const current_host = url.host;
const host_header = request.headers.get("Host"); const effective_host = host_header || current_host;
if (redirect_paths.includes(url.pathname)) { return Response.redirect("https://www.gov.cn", 302); }
if (url.protocol === "http:") { url.protocol = "https:"; return Response.redirect(url.href); }
const host_prefix = getProxyPrefix(effective_host); if (!host_prefix) { return new Response("Domain not configured for proxy", { status: 404 }); }
let target_host = null; for (const [original, prefix] of Object.entries(domain_mappings)) { if (prefix === host_prefix) { target_host = original; break; } }
if (!target_host) { return new Response("Domain not configured for proxy", { status: 404 }); }
let pathname = url.pathname;
pathname = pathname.replace( /(\/[^\/]+\/[^\/]+\/(?:latest-commit|tree-commit-info)\/[^\/]+)\/https%3A\/\/[^\/]+\/.*/, "$1" );
pathname = pathname.replace( /(\/[^\/]+\/[^\/]+\/(?:latest-commit|tree-commit-info)\/[^\/]+)\/https:\/\/[^\/]+\/.*/, "$1" );
const new_url = new URL(url); new_url.host = target_host; new_url.pathname = pathname; new_url.protocol = "https:";
const new_headers = new Headers(request.headers); new_headers.set("Host", target_host); new_headers.set("Referer", new_url.href);
try { const response = await fetch(new_url.href, { method: request.method, headers: new_headers, body: request.method !== "GET" ? request.body : undefined, });
const response_clone = response.clone();
const new_response_headers = new Headers(response.headers); new_response_headers.set("access-control-allow-origin", "*"); new_response_headers.set("access-control-allow-credentials", "true"); new_response_headers.set("cache-control", "public, max-age=14400"); new_response_headers.delete("content-security-policy"); new_response_headers.delete("content-security-policy-report-only"); new_response_headers.delete("clear-site-data");
const modified_body = await modifyResponse( response_clone, host_prefix, effective_host );
return new Response(modified_body, { status: response.status, headers: new_response_headers, }); } catch (err) { return new Response(`Proxy Error: ${err.message}`, { status: 502 }); } }
function getProxyPrefix(host) { if (host.startsWith("gh.")) { return "gh."; }
for (const prefix of Object.values(domain_mappings)) { if (host.startsWith(prefix)) { return prefix; } }
return null; }
async function modifyResponse(response, host_prefix, effective_hostname) { const content_type = response.headers.get("content-type") || ""; if ( !content_type.includes("text/") && !content_type.includes("application/json") && !content_type.includes("application/javascript") && !content_type.includes("application/xml") ) { return response.body; }
let text = await response.text();
const domain_suffix = effective_hostname.substring(host_prefix.length);
for (const [original_domain, proxy_prefix] of Object.entries( domain_mappings )) { const escaped_domain = original_domain.replace(/\./g, "\\."); const full_proxy_domain = `${proxy_prefix}${domain_suffix}`;
text = text.replace( new RegExp(`https?://${escaped_domain}(?=/|"|'|\\s|$)`, "g"), `https://${full_proxy_domain}` );
text = text.replace( new RegExp(`//${escaped_domain}(?=/|"|'|\\s|$)`, "g"), `//${full_proxy_domain}` ); }
if (host_prefix === "gh.") { text = text.replace( /(?<=["'])\/(?!\/|[a-zA-Z]+:)/g, `https://${effective_hostname}/` ); }
return text; }
|