fix(PortProxy): Fix handling of optional outgoing socket in PortProxy
This commit is contained in:
parent
ad8c667dec
commit
a8131ece26
@ -1,5 +1,12 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-02-21 - 3.9.3 - fix(PortProxy)
|
||||
Fix handling of optional outgoing socket in PortProxy
|
||||
|
||||
- Refactored the cleanUpSockets function to correctly handle cases where the outgoing socket may be undefined.
|
||||
- Ensured correct handling of socket events with non-null assertions where applicable.
|
||||
- Improved robustness in connection establishment and cleanup processes.
|
||||
|
||||
## 2025-02-21 - 3.9.2 - fix(PortProxy)
|
||||
Improve timeout handling for port proxy connections
|
||||
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartproxy',
|
||||
version: '3.9.2',
|
||||
version: '3.9.3',
|
||||
description: 'a proxy for handling high workloads of proxying'
|
||||
}
|
||||
|
@ -131,15 +131,18 @@ export class PortProxy {
|
||||
}
|
||||
|
||||
public async start() {
|
||||
const cleanUpSockets = (from: plugins.net.Socket, to: plugins.net.Socket) => {
|
||||
// Adjusted cleanUpSockets to allow an optional outgoing socket.
|
||||
const cleanUpSockets = (from: plugins.net.Socket, to?: plugins.net.Socket) => {
|
||||
from.end();
|
||||
to.end();
|
||||
from.removeAllListeners();
|
||||
to.removeAllListeners();
|
||||
from.unpipe();
|
||||
to.unpipe();
|
||||
from.destroy();
|
||||
to.destroy();
|
||||
if (to) {
|
||||
to.end();
|
||||
to.removeAllListeners();
|
||||
to.unpipe();
|
||||
to.destroy();
|
||||
}
|
||||
};
|
||||
|
||||
const normalizeIP = (ip: string): string[] => {
|
||||
@ -194,7 +197,7 @@ export class PortProxy {
|
||||
const cleanupOnce = () => {
|
||||
if (!connectionClosed) {
|
||||
connectionClosed = true;
|
||||
cleanUpSockets(socket, to);
|
||||
cleanUpSockets(socket, to || undefined);
|
||||
this.incomingConnectionTimes.delete(socket);
|
||||
if (to) {
|
||||
this.outgoingConnectionTimes.delete(to);
|
||||
@ -206,7 +209,8 @@ export class PortProxy {
|
||||
}
|
||||
};
|
||||
|
||||
let to: plugins.net.Socket;
|
||||
// Declare the outgoing connection as possibly null.
|
||||
let to: plugins.net.Socket | null = null;
|
||||
|
||||
const handleError = (side: 'incoming' | 'outgoing') => (err: Error) => {
|
||||
const code = (err as any).code;
|
||||
@ -263,7 +267,9 @@ export class PortProxy {
|
||||
// Establish outgoing connection.
|
||||
to = plugins.net.connect(connectionOptions);
|
||||
// Record start time for the outgoing connection.
|
||||
this.outgoingConnectionTimes.set(to, Date.now());
|
||||
if (to) {
|
||||
this.outgoingConnectionTimes.set(to, Date.now());
|
||||
}
|
||||
console.log(`Connection established: ${remoteIP} -> ${targetHost}:${this.settings.toPort}${serverName ? ` (SNI: ${serverName})` : ''}`);
|
||||
|
||||
// Push back the initial chunk if provided.
|
||||
@ -271,24 +277,25 @@ export class PortProxy {
|
||||
socket.unshift(initialChunk);
|
||||
}
|
||||
socket.setTimeout(120000);
|
||||
socket.pipe(to);
|
||||
to.pipe(socket);
|
||||
// Since 'to' is not null here, we can use the non-null assertion.
|
||||
socket.pipe(to!);
|
||||
to!.pipe(socket);
|
||||
|
||||
// Attach error and close handlers for both sockets.
|
||||
socket.on('error', handleError('incoming'));
|
||||
to.on('error', handleError('outgoing'));
|
||||
to!.on('error', handleError('outgoing'));
|
||||
socket.on('close', handleClose('incoming'));
|
||||
to.on('close', handleClose('outgoing'));
|
||||
to!.on('close', handleClose('outgoing'));
|
||||
socket.on('timeout', () => {
|
||||
console.log(`Timeout on incoming side from ${remoteIP}`);
|
||||
cleanupOnce();
|
||||
});
|
||||
to.on('timeout', () => {
|
||||
to!.on('timeout', () => {
|
||||
console.log(`Timeout on outgoing side from ${remoteIP}`);
|
||||
cleanupOnce();
|
||||
});
|
||||
socket.on('end', handleClose('incoming'));
|
||||
to.on('end', handleClose('outgoing'));
|
||||
to!.on('end', handleClose('outgoing'));
|
||||
};
|
||||
|
||||
// For SNI-enabled connections, peek at the first chunk.
|
||||
|
Loading…
x
Reference in New Issue
Block a user