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
| const CONFIG = { upstream: 'e-hentai.org', allowedRegion: ['CN'], blockedIpAddresses: ['0.0.0.0', '127.0.0.1'], https: true, disableCache: false, replaceDict: { '$upstream': '$custom_domain', '//e-hentai.org': '' }, customCookies: { 'ipb_member_id': '114514', 'ipb_pass_hash': 'homo114514' }, fixedUserAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.199 Safari/537.36' };
addEventListener('fetch', event => { event.respondWith(fetchAndApply(event.request)); });
async function fetchAndApply(request) { const { upstream, allowedRegion, blockedIpAddresses, https, disableCache, customCookies, replaceDict, fixedUserAgent } = CONFIG;
const region = request.headers.get('cf-ipcountry')?.toUpperCase() || ''; const ipAddress = request.headers.get('cf-connecting-ip');
if (!allowedRegion.includes(region)) { return jsonResponse('ACCESS_DENIED', '镜像站仅在中国提供服务。', 403); }
if (blockedIpAddresses.includes(ipAddress)) { return jsonResponse('ACCESS_DENIED', '您的IP地址已被阻止。', 403); }
let url = new URL(request.url); url.protocol = https ? 'https:' : 'http:'; url.host = upstream;
const newRequestHeaders = new Headers(request.headers); newRequestHeaders.set('Host', url.host); newRequestHeaders.set('Referer', `${url.protocol}//${url.hostname}`); newRequestHeaders.set('Cookie', formatCookies(customCookies)); newRequestHeaders.set('User-Agent', fixedUserAgent);
try { const originalResponse = await fetch(url.href, { method: request.method, headers: newRequestHeaders });
const newResponseHeaders = new Headers(originalResponse.headers); if (disableCache) newResponseHeaders.set('Cache-Control', 'no-store');
newResponseHeaders.set('access-control-allow-origin', '*'); newResponseHeaders.set('access-control-allow-credentials', 'true'); removeSecurityHeaders(newResponseHeaders);
if (newResponseHeaders.get('x-pjax-url')) { newResponseHeaders.set( 'x-pjax-url', newResponseHeaders.get('x-pjax-url').replace(`//${url.host}`, `//${url.hostname}`) ); }
let responseText; if (isHtmlResponse(newResponseHeaders)) { responseText = await replaceResponseText(originalResponse, replaceDict, url.host, url.hostname); } else { responseText = originalResponse.body; }
return new Response(responseText, { status: originalResponse.status, headers: newResponseHeaders });
} catch (error) { return jsonResponse('FETCH_ERROR', '获取上游响应时出错。', 502); } }
function formatCookies(cookies) { return Object.entries(cookies) .map(([key, value]) => `${key}=${value}`) .join('; '); }
function removeSecurityHeaders(headers) { ['content-security-policy', 'content-security-policy-report-only', 'clear-site-data'].forEach(header => { headers.delete(header); }); }
function isHtmlResponse(headers) { const contentType = headers.get('content-type'); return contentType && contentType.includes('text/html'); }
async function replaceResponseText(response, replaceDict, upstreamDomain, hostname) { let text = await response.text(); for (let [key, value] of Object.entries(replaceDict)) { key = key === '$upstream' ? upstreamDomain : key; value = value === '$custom_domain' ? hostname : value; text = text.split(key).join(value); } return text; }
function jsonResponse(code, message, status) { return new Response(JSON.stringify({ code, message }), { status, headers: { 'Content-Type': 'application/json' } }); }
|