前言
因为某种原因很多论坛都被国内屏蔽或者墙了,但我们可以用CFworkers或者 NGINX反向代理来访问这些被屏蔽或者墙了的论坛,这里反代仅限于Discuz程序。
教程
这里我们以全球主机论坛为例,给出 cloudflare workers 版和 Nginx 反代版。
CFworkers 版
/**
* cloudflare workers
*/
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/[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;
}
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://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' '7198.eu.org';
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;
}
}
}
CFworkers 演示
结语
使用自己反代的站点既安全又方便,切勿使用来源不明的非官方反代有被窃取数据的风险!
© 本站文章随意转载,但请注明出处!
>> 如果您觉得本站文章对您有所帮助,购买 VPS 时候请走本站AFF链接!
>> 某些文章具有时效性,若内容有错误或已失效,欢迎在下方评论区留言向我们反馈.
>> 所有文章均基于分享的原则,所有言论均个人观点,请注意全部都不是推荐,是分享!分享!分享!
>> 所有文章均基于分享的原则,所有言论均个人观点,请注意全部都不是推荐,是分享!分享!分享!
THE END