const http = require("http");
const https = require("https");
const { URL } = require("url");

const TARGET = "https://tiger.bookapi.cc";
const PORT = 18801;

http.createServer((req, res) => {
  const url = new URL(req.url, TARGET);
  const headers = { ...req.headers, host: url.host, "user-agent": "curl/8.0" };

  // Strip identifying headers
  for (const k of Object.keys(headers)) {
    if (k.startsWith("x-stainless") || k === "anthropic-dangerous-direct-browser-access" || k === "sec-fetch-mode") {
      delete headers[k];
    }
  }

  const proxy = https.request(url.href, { method: req.method, headers }, (pRes) => {
    res.writeHead(pRes.statusCode, pRes.headers);
    pRes.pipe(res);
  });
  proxy.on("error", (e) => { res.writeHead(502); res.end(e.message); });
  req.pipe(proxy);
}).listen(PORT, "127.0.0.1", () => console.log(`Proxy on 127.0.0.1:${PORT}`));
