Total Pageviews

Thursday, 22 October 2026

一个神奇的网站www.val.town, 可以提供目标网站的反向代理服务

访问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, 稍等片刻,就会显示一个网址:

No comments:

Post a Comment