紫曦博客

cloudflare workers反代论坛代码

2024-05-21 420

代码存档

1.workers
/** * cloudflare workers */ addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); }); // 准备反代的目的域名 let target_url = "https://论坛.com"; // 要替换内容的正则表达式 let target_url_reg = /(?<=\/\/).*?论坛\.com/g; async function handleRequest(request) { let url = new URL(request.url); url.hostname = new URL(target_url).hostname; // 复制请求对象并更新它的属性 let headers = new Headers(request.headers); headers.set("Referer", target_url); headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"); //处理头像的 302 跳转,处理编辑帖子之后的报错 let redirect = "manual"; if(url.href.indexOf("/uc_server/") > -1){ redirect = "follow"; } const param = { method: request.method, headers: headers, body: request.body, redirect: redirect } let response = await fetch(url, param); // 检查响应头中的内容类型 const contentType = response.headers.get('content-type'); if (contentType && contentType.includes('text')) { // 如果是文本类型,替换响应主体中的 URL let responseBody = await response.text(); responseBody = await handleResBody(request,responseBody); // 复制响应对象并更新它的属性 let headers = await handleResHeader(response); return new Response(responseBody, { status: response.status, statusText: response.statusText, headers: headers }); } else { // 如果不是文本类型,直接返回响应对象 return response; } } async function handleResBody(request, responseBody){ responseBody = responseBody.replace(target_url_reg, new URL(request.url).hostname); responseBody = responseBody.replace("<head>", '<head>\n<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">'); responseBody = responseBody.replace("</head>", '<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/lifespy/[email protected]/css/responsive.css">\n</head>'); responseBody = responseBody.replace("</body>", '<script src="//cdn.jsdelivr.net/gh/lifespy/[email protected]/js/polish.js" type="text/javascript"></script>\n</body>'); return responseBody; } async function handleResHeader(resp){ let headers = new Headers(resp.headers); headers.set('Access-Control-Allow-Origin', '*'); headers.set('Access-Control-Allow-Methods', 'GET'); headers.set('Access-Control-Allow-Headers', 'Content-Type'); return headers; }
如果反代子目录如论坛.com/bbs跳出,需修改
let target_url = "https://www.hostloc.com/bbs"; let target_url_reg = /(?<=\/\/).*?hostloc\.com\/bbs/g;
2.nginx
server { listen 80; listen [::]:80; server_name 7198.eu.org; #SSL Configuration listen 443 ssl; listen [::]:443 ssl; ssl_certificate /ssl/7198.eu.org.crt; ssl_certificate_key /ssl/7198.eu.org.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; if ($scheme = http) { return 301 https://$server_name$request_uri; } # proxy to another site location ~/ { proxy_pass https://论坛.com; proxy_set_header Host 论坛.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header referer https://论坛.com/$request_uri; proxy_set_header Accept-Encoding ""; proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0"; sub_filter_types *; sub_filter '<base href="https://论坛.com/">' '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/lifespy/css-and-js-hub/css/responsive.css" />'; sub_filter '论坛.com' '你的域名'; sub_filter '</body>' '<script src="//cdn.jsdelivr.net/gh/lifespy/css-and-js-hub/js/polish.js"></script></body>'; sub_filter_once off; set $static_fileEJLfi5A0 0; if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) { set $static_fileEJLfi5A0 1; expires 12h; } if ( $static_fileEJLfi5A0 = 0 ) { add_header Cache-Control no-cache; } } }

0