Compare commits

...

4 Commits

4 changed files with 64 additions and 16 deletions

View File

@ -1,5 +1,18 @@
# Changelog # Changelog
## 2025-03-01 - 3.20.1 - fix(PortProxy)
Improve IP allowance check for forced domains
- Enhanced IP allowance check logic by incorporating blocked IPs and default allowed IPs for forced domains within port proxy configurations.
## 2025-03-01 - 3.20.0 - feat(PortProxy)
Enhance PortProxy with advanced connection cleanup and logging
- Introduced `cleanupConnection` method for improved connection management.
- Added logging for connection cleanup including special conditions.
- Implemented parity check to clean up connections when outgoing side closes but incoming remains active.
- Improved logging during interval checks for active connections and their durations.
## 2025-03-01 - 3.19.0 - feat(PortProxy) ## 2025-03-01 - 3.19.0 - feat(PortProxy)
Enhance PortProxy with default blocked IPs Enhance PortProxy with default blocked IPs

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartproxy", "name": "@push.rocks/smartproxy",
"version": "3.19.0", "version": "3.20.1",
"private": false, "private": false,
"description": "A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.", "description": "A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.",
"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.19.0', version: '3.20.1',
description: 'A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.' description: 'A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.'
} }

View File

@ -92,6 +92,7 @@ interface IConnectionRecord {
outgoing: plugins.net.Socket | null; outgoing: plugins.net.Socket | null;
incomingStartTime: number; incomingStartTime: number;
outgoingStartTime?: number; outgoingStartTime?: number;
outgoingClosedTime?: number;
lockedDomain?: string; // New field to lock this connection to the initial SNI lockedDomain?: string; // New field to lock this connection to the initial SNI
connectionClosed: boolean; connectionClosed: boolean;
cleanupTimer?: NodeJS.Timeout; // Timer to force cleanup after max lifetime/inactivity cleanupTimer?: NodeJS.Timeout; // Timer to force cleanup after max lifetime/inactivity
@ -157,6 +158,33 @@ export class PortProxy {
this.terminationStats[side][reason] = (this.terminationStats[side][reason] || 0) + 1; this.terminationStats[side][reason] = (this.terminationStats[side][reason] || 0) + 1;
} }
/**
* Cleans up a connection record if not already cleaned up.
* Destroys both incoming and outgoing sockets, clears timers, and removes the record.
* Logs the cleanup event.
*/
private cleanupConnection(record: IConnectionRecord, special: boolean = false): void {
if (!record.connectionClosed) {
record.connectionClosed = true;
if (record.cleanupTimer) {
clearTimeout(record.cleanupTimer);
}
if (!record.incoming.destroyed) {
record.incoming.destroy();
}
if (record.outgoing && !record.outgoing.destroyed) {
record.outgoing.destroy();
}
this.connectionRecords.delete(record);
const remoteIP = record.incoming.remoteAddress || 'unknown';
if (special) {
console.log(`Special parity cleanup: Connection from ${remoteIP} cleaned up due to duration difference.`);
} else {
console.log(`Connection from ${remoteIP} terminated. Active connections: ${this.connectionRecords.size}`);
}
}
}
private getTargetIP(domainConfig: IDomainConfig): string { private getTargetIP(domainConfig: IDomainConfig): string {
if (domainConfig.targetIPs && domainConfig.targetIPs.length > 0) { if (domainConfig.targetIPs && domainConfig.targetIPs.length > 0) {
const currentIndex = this.domainTargetIndices.get(domainConfig) || 0; const currentIndex = this.domainTargetIndices.get(domainConfig) || 0;
@ -185,18 +213,9 @@ export class PortProxy {
let incomingTerminationReason: string | null = null; let incomingTerminationReason: string | null = null;
let outgoingTerminationReason: string | null = null; let outgoingTerminationReason: string | null = null;
// Ensure cleanup happens only once for the entire connection record. // Local cleanup function that delegates to the class method.
const cleanupOnce = async () => { const cleanupOnce = () => {
if (!connectionRecord.connectionClosed) { this.cleanupConnection(connectionRecord);
connectionRecord.connectionClosed = true;
if (connectionRecord.cleanupTimer) {
clearTimeout(connectionRecord.cleanupTimer);
}
if (!socket.destroyed) socket.destroy();
if (connectionRecord.outgoing && !connectionRecord.outgoing.destroyed) connectionRecord.outgoing.destroy();
this.connectionRecords.delete(connectionRecord);
console.log(`Connection from ${remoteIP} terminated. Active connections: ${this.connectionRecords.size}`);
}
}; };
// Helper to reject an incoming connection. // Helper to reject an incoming connection.
@ -244,6 +263,8 @@ export class PortProxy {
} else if (side === 'outgoing' && outgoingTerminationReason === null) { } else if (side === 'outgoing' && outgoingTerminationReason === null) {
outgoingTerminationReason = 'normal'; outgoingTerminationReason = 'normal';
this.incrementTerminationStat('outgoing', 'normal'); this.incrementTerminationStat('outgoing', 'normal');
// Record the time when outgoing socket closed.
connectionRecord.outgoingClosedTime = Date.now();
} }
cleanupOnce(); cleanupOnce();
}; };
@ -392,7 +413,15 @@ export class PortProxy {
domain => domain.portRanges && domain.portRanges.length > 0 && isPortInRanges(localPort, domain.portRanges) domain => domain.portRanges && domain.portRanges.length > 0 && isPortInRanges(localPort, domain.portRanges)
); );
if (forcedDomain) { if (forcedDomain) {
if (!isAllowed(remoteIP, forcedDomain.allowedIPs)) { const effectiveAllowedIPs: string[] = [
...forcedDomain.allowedIPs,
...(this.settings.defaultAllowedIPs || [])
];
const effectiveBlockedIPs: string[] = [
...(forcedDomain.blockedIPs || []),
...(this.settings.defaultBlockedIPs || [])
];
if (!isGlobIPAllowed(remoteIP, effectiveAllowedIPs, effectiveBlockedIPs)) {
console.log(`Connection from ${remoteIP} rejected: IP not allowed for domain ${forcedDomain.domains.join(', ')} on port ${localPort}.`); console.log(`Connection from ${remoteIP} rejected: IP not allowed for domain ${forcedDomain.domains.join(', ')} on port ${localPort}.`);
socket.end(); socket.end();
return; return;
@ -473,7 +502,7 @@ export class PortProxy {
this.netServers.push(server); this.netServers.push(server);
} }
// Log active connection count and longest running durations every 10 seconds. // Log active connection count, longest running durations, and run parity checks every 10 seconds.
this.connectionLogger = setInterval(() => { this.connectionLogger = setInterval(() => {
const now = Date.now(); const now = Date.now();
let maxIncoming = 0; let maxIncoming = 0;
@ -483,6 +512,12 @@ export class PortProxy {
if (record.outgoingStartTime) { if (record.outgoingStartTime) {
maxOutgoing = Math.max(maxOutgoing, now - record.outgoingStartTime); maxOutgoing = Math.max(maxOutgoing, now - record.outgoingStartTime);
} }
// Parity check: if outgoing socket closed and incoming remains active for >1 minute, trigger special cleanup.
if (record.outgoingClosedTime && !record.incoming.destroyed && (now - record.outgoingClosedTime > 60000)) {
const remoteIP = record.incoming.remoteAddress || 'unknown';
console.log(`Parity check triggered: Incoming socket for ${remoteIP} has been active >1 minute after outgoing closed.`);
this.cleanupConnection(record, true);
}
} }
console.log( console.log(
`(Interval Log) Active connections: ${this.connectionRecords.size}. ` + `(Interval Log) Active connections: ${this.connectionRecords.size}. ` +