Skip to content

Commit

Permalink
just testing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKhanj committed Dec 18, 2023
1 parent 23dc4da commit a98ed06
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moxyjs",
"version": "0.0.5",
"version": "0.0.6",
"description": "Distributed transparent proxy with traffic control facilities",
"author": "Pooyan Khanjankhani <[email protected]>",
"license": "MIT",
Expand Down
20 changes: 16 additions & 4 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ class TcpProxy implements Proxy {
}

private getServer() {
return net.createServer((clientSocket) => {
function handleClient(this: TcpProxy, clientSocket: net.Socket) {
// TODO: add recovery mechanism
const retryConnection = () => {
this.logger.log(`Retrying connection in ${500 / 1000} seconds...`);
setTimeout(() => handleClient.bind(this)(clientSocket), 500);
};

const forwardSocket = net.createConnection(
this.forwardingPort,
this.forwardingAddress,
Expand Down Expand Up @@ -93,8 +98,13 @@ class TcpProxy implements Proxy {
});

clientSocket.on("error", (err) => {
this.logger.error(`Client socket error: ${err}`);
clientSocket.destroy();
if ((err as any).code === "ECONNRESET") {
this.logger.log("Local socket connection reset. Retrying...");
retryConnection();
} else {
this.logger.error("Local socket error:", err);
clientSocket.destroy();
}
});

forwardSocket.on("close", () => {
Expand All @@ -105,7 +115,9 @@ class TcpProxy implements Proxy {
this.logger.error(`Forward socket error: ${err}`);
clientSocket.destroy();
});
});
}

return net.createServer(handleClient.bind(this));
}
}

Expand Down

0 comments on commit a98ed06

Please sign in to comment.