diff --git a/package-lock.json b/package-lock.json index 2d286f8..61a9d0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "moxyjs", - "version": "0.0.5", + "version": "0.0.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "moxyjs", - "version": "0.0.5", + "version": "0.0.6", "license": "MIT", "dependencies": { "@nestjs/common": "^10.2.10", diff --git a/package.json b/package.json index 0abfa40..3b9ccab 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", diff --git a/src/proxy.ts b/src/proxy.ts index afe6b01..b9edfbc 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -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, @@ -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", () => { @@ -105,7 +115,9 @@ class TcpProxy implements Proxy { this.logger.error(`Forward socket error: ${err}`); clientSocket.destroy(); }); - }); + } + + return net.createServer(handleClient.bind(this)); } }