Skip to content

Commit

Permalink
just change logs and publish package
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKhanj committed Dec 18, 2023
1 parent f58e39b commit c343dea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 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.3",
"version": "0.0.4",
"description": "Distributed transparent proxy with traffic control facilities",
"author": "Pooyan Khanjankhani <[email protected]>",
"license": "MIT",
Expand Down
37 changes: 20 additions & 17 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Proxy {
}

class TcpProxy implements Proxy {
private readonly logger = new Logger("TcpProxy");
private readonly logger: Logger;
private readonly server: net.Server;

public constructor(
Expand All @@ -28,16 +28,19 @@ class TcpProxy implements Proxy {
private readonly forwardingPort: number,
private readonly forwardingAddress: string,
private readonly eventEmmiter: MoxyEventEmitter,
private readonly counterFlushTimeout: number,
private readonly counterFlushTimeout: number
) {
this.logger = new Logger(
`TcpProxy :${this.listeningPort} -> ${this.forwardingAddress}:${this.forwardingPort}`
);
this.server = this.getServer();
}

public listen() {
return new Promise<void>((res, rej) => {
const server = this.server.listen(this.listeningPort, () => {
this.logger.log(
`Started tcp proxy ${this.listeningPort} -> ${this.forwardingAddress}:${this.forwardingPort}`,
`Started tcp proxy`
);
res();
});
Expand All @@ -50,7 +53,7 @@ class TcpProxy implements Proxy {
this.server.close((err) => {
if (err) rej(err);
this.logger.log(
`Destroyed tcp proxy ${this.listeningPort} -> ${this.forwardingAddress}:${this.forwardingPort}`,
`Destroyed tcp proxy`
);
res();
});
Expand All @@ -64,28 +67,28 @@ class TcpProxy implements Proxy {
this.forwardingPort,
this.forwardingAddress,
() => {
this.logger.log(`Connected to forward port: ${this.forwardingPort}`);
this.logger.log(`Connected to forward port`);

const upCounter = createCounterStream(
this.eventEmmiter,
"up",
this.userKey,
this.counterFlushTimeout,
this.counterFlushTimeout
);

const downCounter = createCounterStream(
this.eventEmmiter,
"down",
this.userKey,
this.counterFlushTimeout,
this.counterFlushTimeout
);

clientSocket.pipe(upCounter);
upCounter.pipe(forwardSocket);

forwardSocket.pipe(downCounter);
downCounter.pipe(clientSocket);
},
}
);

clientSocket.on("close", () => {
Expand Down Expand Up @@ -116,22 +119,22 @@ export class ProxyStorage {
@Inject("CounterFlushTimeout")
private readonly counterTimeout: number,
userFactory: UserFactory,
userStatsService: UserStatsService,
userStatsService: UserStatsService
) {
this.eventEmitter.on("enable-user", (userKey) =>
withErrorLogging(async () => {
await userStatsService.assert(userKey);
const user = await userFactory.get(userKey);
if (!user.isEnabled()) return;
await this.add(user.config.key, user.config.proxy);
}, this.logger),
}, this.logger)
);
this.eventEmitter.on("disable-user", (userKey) =>
withErrorLogging(async () => {
await userStatsService.assert(userKey);
const user = await userFactory.get(userKey);
await this.delete(user.config.key);
}, this.logger),
}, this.logger)
);
this.eventEmitter.on("update-user", (prev, curr) =>
withErrorLogging(async () => {
Expand All @@ -141,11 +144,11 @@ export class ProxyStorage {
await userStatsService.assert(curr.key);
const user = await userFactory.get(curr.key);
await this.delete(user.config.key).catch((err) =>
this.logger.warn(err),
this.logger.warn(err)
);
if (!user.isEnabled()) return;
await this.add(user.config.key, user.config.proxy);
}, this.logger),
}, this.logger)
);
}

Expand All @@ -159,12 +162,12 @@ export class ProxyStorage {
config.forwardingPort,
config.forwardingAddress,
this.eventEmitter,
this.counterTimeout,
this.counterTimeout
);
break;
default:
throw new Error(
`No implementation is available for ${config.protocol} proxy`,
`No implementation is available for ${config.protocol} proxy`
);
}

Expand Down Expand Up @@ -194,7 +197,7 @@ function createCounterStream(
eventEmitter: MoxyEventEmitter,
type: "up" | "down",
userKey: string,
timeout: number,
timeout: number
) {
let length = 0;
let flushTimeout: NodeJS.Timeout | null = null;
Expand Down Expand Up @@ -234,7 +237,7 @@ export class ProxyModule {
public static register(
config: ProxyConfig,
configModule: DynamicModule,
userModule: DynamicModule,
userModule: DynamicModule
): DynamicModule {
return {
module: ProxyModule,
Expand Down

0 comments on commit c343dea

Please sign in to comment.