访问www.val.town ,我用github a/c: brightmann@gmail.com登录。登录后,来到了https://www.val.town/dashboard这个网址,点击右上角的new val ,给name取个名字,比如abc .然后选择public ,然后点击create val .在跳转到的页面中,输入如下代码:
const TARGET = "https://briteming.hatenablog.com";
export default async function (req: Request): Promise<Response> {
const url = new URL(req.url);
const targetUrl = TARGET + url.pathname + url.search;
// 构建转发请求的 headers,移除 host 相关的头
const headers = new Headers(req.headers);
headers.delete("host");
headers.set("Accept-Encoding", "identity"); // 避免压缩编码问题
const resp = await fetch(targetUrl, {
method: req.method,
headers,
body: req.method !== "GET" && req.method !== "HEAD" ? req.body : undefined,
redirect: "follow",
});
// 复制响应 headers
const respHeaders = new Headers(resp.headers);
respHeaders.delete("content-encoding");
// 移除安全相关头,避免阻止加载
respHeaders.delete("content-security-policy");
respHeaders.delete("x-frame-options");
const contentType = resp.headers.get("content-type") || "";
// 对 HTML 内容进行 URL 重写,将绝对路径指向代理
if (contentType.includes("text/html")) {
let html = await resp.text();
// 替换指向源站的绝对 URL
html = html.replaceAll("https://briteming.hatenablog.com", "");
html = html.replaceAll("//briteming.hatenablog.com", url.origin);
return new Response(html, {
status: resp.status,
headers: respHeaders,
});
}
return new Response(resp.body, {
status: resp.status,
headers: respHeaders,
});
}
然后点击右上角的run按钮, 然后点击run按钮下面的add triggers->点击respond to web requests, 稍等片刻,就会显示一个网址:
https://brightmann--21e673f0ce8b11f1839a1607ee4eb77e.web.val.run ,点击此网址右边的第2个图标(笔形图标),可以定制子域名,我定制的子域名是bm-htn ,然后点击save & copy URL, 就会显示网址https://bm-htn.val.run/,这实际上就是https://briteming.hatenablog.com/的反向代理。
我还另外搞了3个反向代理:
https://bm-hn.val.run, 反向代理了briteming.blogspot.com
https://nlb.val.run, 反向代理了 nlb.luckypoem.workers.dev
https://luckypoem.val.run ,反向代理了luckypoem.github.io
以前只知道nginx 可以提供目标网站的反向代理服务,没想到,现在还可以这样玩。xx
No comments:
Post a Comment