Skip to content

Commit

Permalink
close socket on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKhanj committed Dec 18, 2023
1 parent c343dea commit 23dc4da
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 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.4",
"version": "0.0.5",
"description": "Distributed transparent proxy with traffic control facilities",
"author": "Pooyan Khanjankhani <[email protected]>",
"license": "MIT",
Expand Down
17 changes: 9 additions & 8 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class TcpProxy implements Proxy {
public listen() {
return new Promise<void>((res, rej) => {
const server = this.server.listen(this.listeningPort, () => {
this.logger.log(
`Started tcp proxy`
);
this.logger.log("Started tcp proxy");
res();
});
server.on("error", rej);
Expand All @@ -52,9 +50,7 @@ class TcpProxy implements Proxy {
return new Promise<void>((res, rej) => {
this.server.close((err) => {
if (err) rej(err);
this.logger.log(
`Destroyed tcp proxy`
);
this.logger.log("Destroyed tcp proxy");
res();
});
});
Expand All @@ -67,7 +63,7 @@ class TcpProxy implements Proxy {
this.forwardingPort,
this.forwardingAddress,
() => {
this.logger.log(`Connected to forward port`);
this.logger.log("Connected to forward port");

const upCounter = createCounterStream(
this.eventEmmiter,
Expand All @@ -92,12 +88,17 @@ class TcpProxy implements Proxy {
);

clientSocket.on("close", () => {
this.logger.log(`Client disconnected`);
this.logger.log("Client disconnected");
forwardSocket.end();
});

clientSocket.on("error", (err) => {
this.logger.error(`Client socket error: ${err}`);
clientSocket.destroy();
});

forwardSocket.on("close", () => {
this.logger.log("Forward disconnected");
});

forwardSocket.on("error", (err) => {
Expand Down

0 comments on commit 23dc4da

Please sign in to comment.