Compare commits

...

6 Commits

4 changed files with 52 additions and 17 deletions

View File

@ -1,5 +1,23 @@
# Changelog # Changelog
## 2025-02-22 - 3.9.4 - fix(PortProxy)
Ensure proper cleanup on connection rejection in PortProxy
- Added cleanup calls after socket end in connection rejection scenarios within PortProxy
## 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
- Added console logging for both incoming and outgoing side timeouts in the PortProxy class.
- Updated the timeout event handlers to ensure proper cleanup of connections.
## 2025-02-21 - 3.9.1 - fix(dependencies) ## 2025-02-21 - 3.9.1 - fix(dependencies)
Ensure correct ordering of dependencies and improve logging format. Ensure correct ordering of dependencies and improve logging format.

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartproxy", "name": "@push.rocks/smartproxy",
"version": "3.9.1", "version": "3.9.4",
"private": false, "private": false,
"description": "a proxy for handling high workloads of proxying", "description": "a proxy for handling high workloads of proxying",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartproxy', name: '@push.rocks/smartproxy',
version: '3.9.1', version: '3.9.4',
description: 'a proxy for handling high workloads of proxying' description: 'a proxy for handling high workloads of proxying'
} }

View File

@ -131,15 +131,18 @@ export class PortProxy {
} }
public async start() { 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(); from.end();
to.end();
from.removeAllListeners(); from.removeAllListeners();
to.removeAllListeners();
from.unpipe(); from.unpipe();
to.unpipe();
from.destroy(); from.destroy();
to.destroy(); if (to) {
to.end();
to.removeAllListeners();
to.unpipe();
to.destroy();
}
}; };
const normalizeIP = (ip: string): string[] => { const normalizeIP = (ip: string): string[] => {
@ -194,7 +197,7 @@ export class PortProxy {
const cleanupOnce = () => { const cleanupOnce = () => {
if (!connectionClosed) { if (!connectionClosed) {
connectionClosed = true; connectionClosed = true;
cleanUpSockets(socket, to); cleanUpSockets(socket, to || undefined);
this.incomingConnectionTimes.delete(socket); this.incomingConnectionTimes.delete(socket);
if (to) { if (to) {
this.outgoingConnectionTimes.delete(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 handleError = (side: 'incoming' | 'outgoing') => (err: Error) => {
const code = (err as any).code; const code = (err as any).code;
@ -232,16 +236,19 @@ export class PortProxy {
if (!domainConfig) { if (!domainConfig) {
console.log(`Connection rejected: No matching domain config for ${serverName} from ${remoteIP}`); console.log(`Connection rejected: No matching domain config for ${serverName} from ${remoteIP}`);
socket.end(); socket.end();
cleanupOnce();
return; return;
} }
if (!isAllowed(remoteIP, domainConfig.allowedIPs)) { if (!isAllowed(remoteIP, domainConfig.allowedIPs)) {
console.log(`Connection rejected: IP ${remoteIP} not allowed for domain ${serverName}`); console.log(`Connection rejected: IP ${remoteIP} not allowed for domain ${serverName}`);
socket.end(); socket.end();
cleanupOnce();
return; return;
} }
} else if (!isDefaultAllowed && !serverName) { } else if (!isDefaultAllowed && !serverName) {
console.log(`Connection rejected: No SNI and IP ${remoteIP} not in default allowed list`); console.log(`Connection rejected: No SNI and IP ${remoteIP} not in default allowed list`);
socket.end(); socket.end();
cleanupOnce();
return; return;
} else { } else {
console.log(`Connection allowed: IP ${remoteIP} is in default allowed list`); console.log(`Connection allowed: IP ${remoteIP} is in default allowed list`);
@ -263,7 +270,9 @@ export class PortProxy {
// Establish outgoing connection. // Establish outgoing connection.
to = plugins.net.connect(connectionOptions); to = plugins.net.connect(connectionOptions);
// Record start time for the outgoing connection. // 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})` : ''}`); console.log(`Connection established: ${remoteIP} -> ${targetHost}:${this.settings.toPort}${serverName ? ` (SNI: ${serverName})` : ''}`);
// Push back the initial chunk if provided. // Push back the initial chunk if provided.
@ -271,18 +280,25 @@ export class PortProxy {
socket.unshift(initialChunk); socket.unshift(initialChunk);
} }
socket.setTimeout(120000); socket.setTimeout(120000);
socket.pipe(to); // Since 'to' is not null here, we can use the non-null assertion.
to.pipe(socket); socket.pipe(to!);
to!.pipe(socket);
// Attach error and close handlers for both sockets. // Attach error and close handlers for both sockets.
socket.on('error', handleError('incoming')); socket.on('error', handleError('incoming'));
to.on('error', handleError('outgoing')); to!.on('error', handleError('outgoing'));
socket.on('close', handleClose('incoming')); socket.on('close', handleClose('incoming'));
to.on('close', handleClose('outgoing')); to!.on('close', handleClose('outgoing'));
socket.on('timeout', handleError('incoming')); socket.on('timeout', () => {
to.on('timeout', handleError('outgoing')); console.log(`Timeout on incoming side from ${remoteIP}`);
cleanupOnce();
});
to!.on('timeout', () => {
console.log(`Timeout on outgoing side from ${remoteIP}`);
cleanupOnce();
});
socket.on('end', handleClose('incoming')); socket.on('end', handleClose('incoming'));
to.on('end', handleClose('outgoing')); to!.on('end', handleClose('outgoing'));
}; };
// For SNI-enabled connections, peek at the first chunk. // For SNI-enabled connections, peek at the first chunk.
@ -300,6 +316,7 @@ export class PortProxy {
if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) { if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
console.log(`Connection rejected: IP ${remoteIP} not allowed for non-SNI connection`); console.log(`Connection rejected: IP ${remoteIP} not allowed for non-SNI connection`);
socket.end(); socket.end();
cleanupOnce();
return; return;
} }
setupConnection(''); setupConnection('');