Elffuss BraidHub

kikocis/continue public

main · 1.8 KiBRawPermalink
1import cors from "cors";
2import express from "express";
3import { http, https } from "follow-redirects";
4
5const PROXY_PORT = 65433;
6const app = express();
7app.use(cors());
8
9app.use((req, res, next) => {
10 // Proxy the request
11 const { origin, host, ...headers } = req.headers;
12 const url = req.headers["x-continue-url"] as string;
13 const parsedUrl = new URL(url);
14 const protocolString = url.split("://")[0];
15 const protocol = protocolString === "https" ? https : http;
16 const proxy = protocol.request(url, {
17 method: req.method,
18 headers: {
19 ...headers,
20 host: parsedUrl.host,
21 },
22 });
23
24 proxy.on("response", (response) => {
25 res.status(response.statusCode || 500);
26 for (let i = 1; i < response.rawHeaders.length; i += 2) {
27 if (
28 response.rawHeaders[i - 1].toLowerCase() ===
29 "access-control-allow-origin"
30 ) {
31 continue;
32 }
33 res.setHeader(response.rawHeaders[i - 1], response.rawHeaders[i]);
34 }
35 response.pipe(res);
36 });
37
38 proxy.on("error", (error) => {
39 console.error(error);
40 res.sendStatus(500);
41 });
42
43 req.pipe(proxy);
44});
45
46// http-middleware-proxy
47// app.use("/", (req, res, next) => {
48// // Extract the target from the request URL
49// const target = req.headers["x-continue-url"] as string;
50// const { origin, ...headers } = req.headers;
51
52// // Create a new proxy middleware for this request
53// const proxy = createProxyMiddleware({
54// target,
55// ws: true,
56// headers: {
57// origin: "",
58// },
59// });
60
61// // Call the middleware
62// proxy(req, res, next);
63// });
64
65export function startProxy() {
66 const server = app.listen(PROXY_PORT, () => {
67 console.log(`Proxy server is running on port ${PROXY_PORT}`);
68 });
69 server.on("error", (e) => {
70 // console.log("Proxy server already running on port 65433");
71 });
72}