Skip to content

Commit

Permalink
Proxy replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Feb 10, 2024
1 parent 810c446 commit ea2342b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 11 deletions.
37 changes: 34 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
"http-proxy": "^1.18.1",
"mime": "^4.0.1",
"node-forge": "^1.3.1",
"portfinder": "^1.0.32"
"portfinder": "^1.0.32",
"ws": "^8.16.0"
},
"devDependencies": {
"@types/http-proxy": "^1.17.14",
"@types/node": "^20.11.17"
"@types/node": "^20.11.17",
"@types/ws": "^8.5.10"
}
}
16 changes: 10 additions & 6 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import staticRoute from "./routes/staticRoute.js";
import { repoPath } from "./core/repoPath.js";
import modulesRoute from "./routes/modulesRoute.js";

import { WebSocketServer } from 'ws';
import { wsProxy } from "./wsProxy.js";

const proxyHost = process.argv.find((s) => /^(http|https)\:\/\//.test(s)) ?? "https://localhost";

console.log(`Starting Proxy to ${proxyHost}`);
Expand All @@ -16,13 +19,15 @@ const errorHandler = (error) => {

const proxyHostURL = new URL(proxyHost);

console.log(proxyHostURL.protocol);

export default function router(server: Server, secure = false) {

const proxy = httpProxy.createProxyServer({
target: proxyHost,
changeOrigin: true,
ws: true,
secure: true,
ws: true,
hostRewrite: proxyHostURL.host,
cookieDomainRewrite: {
"*": ""
Expand Down Expand Up @@ -55,18 +60,17 @@ export default function router(server: Server, secure = false) {

server.on("upgrade", (req, socket, head) => {
console.log(`Forwarding WS ${req.url}`);
socket.on("error", errorHandler);
const url = new URL( req.url, proxyHostURL.protocol === "https:" ? `wss://${proxyHostURL.host}` : `ws://${proxyHostURL.host}`);
// socket.on("error", errorHandler);
// wsProxy(url, req, socket, head);
proxy.ws(req, socket, head);
});

proxy.on("proxyRes", (pRes, res) => {
let cookie = pRes.headers["set-cookie"];
if (cookie) {
cookie = cookie.map((s) => s.split(";").filter((c) => !/^secure$/i.test(c.trim())).join(";") + ";SameSite=false;");
console.log(cookie.join(","));
// pRes.setHeader("set-cookie",cookie);
cookie = cookie.map((s) => s.split(";").filter((c) => !/^secure$/i.test(c.trim())).join(";"));
pRes.headers["set-cookie"] = cookie;
console.log(JSON.stringify(pRes.headers, void 0, 2));
}

if (pRes.statusCode >= 400) {
Expand Down
79 changes: 79 additions & 0 deletions src/wsProxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { IncomingMessage } from "http";
import { Socket } from "net";
import internal from "stream";
import { WebSocket, WebSocketServer } from "ws";

const pipeWebSocket = (src: WebSocket, dest: WebSocket) => {
try {
src.on("message", (data, binary) => {
try {
dest.send(data, { binary });
} catch (error) {
console.error(error);
}
});

const safeClose = (e) => {
console.error(e);
try {
if (dest.readyState === 1) {
dest.close();
}
} catch (e1) {
console.error(e1);
}
};

src.on("error", safeClose);
src.on("close", safeClose);
} catch (error) {
console.error(error);
}
};

const wss = new WebSocketServer({ noServer: true });


export function wsProxy(url: URL, req: IncomingMessage, socket: internal.Duplex, head) {

wss.handleUpgrade(req, socket, head, (inWS) => {
try {


console.log(`Socket upgraded`);

console.log(`Connecting to ${url.toString()}`);
const ws = new WebSocket(url.toString(), {
headers: {
cookie: req.headers.cookie
}
});
ws.once("error", console.error);
ws.once("upgrade", (uh) => {

console.log(`Socket Connected`);
// do nothing... socket connected...

pipeWebSocket(ws, inWS);
pipeWebSocket(inWS, ws);

});
socket.on("error", (error) => {
console.error(error);
try {
if (ws.readyState === 1) {
ws.close();
}
} catch (e1) {
console.error(e1);
}
});
} catch (error) {
socket.end();
console.error(error);
}
});



}

0 comments on commit ea2342b

Please sign in to comment.