又来一个版本。
直接在CF的worker里添加代码。添加路由的时候,记得网址后面有/*
又弄了一个演示:https://loc.513513.xyz
2023-5-25 16:49
1.修复头像
2.修复交易帖显示缺失
直接上代码:
/**
* write by 西柚论坛 https://www.xiubbs.com
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
// 准备反代的目的域名
let target_url = "https://hostloc.com";
// 要替换内容的正则表达式
let target_url_reg = /(?<=\/\/).*?hostloc\.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/css-and-js-hub@0.1/css/responsive.css">\n</head>');
responseBody = responseBody.replace("</body>", '<script src="//cdn.jsdelivr.net/gh/lifespy/css-and-js-hub@0.1/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;
}
还有一个个nginx版本
server {
listen 80;
listen [::]:80;
server_name loc.xiu.ee;
#SSL Configuration
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /ssl/loc.xiu.ee.crt;
ssl_certificate_key /ssl/loc.xiu.ee.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://hostloc.com;
proxy_set_header Host hostloc.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://hostloc.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://hostloc.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 'hostloc.com' 'loc.xiu.ee';
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;
}
}
}
搞了一个星期的反代discuz都不能发帖,看到你的帖子马上就解决了。
不过还有点地方可以完善。
如果反代的论坛使用了cloudflare需要加上下面3行
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
如果反代的网站都是用了ssl需要加上下面两行
proxy_ssl_name $host;
proxy_ssl_server_name on;