|
|
|
@ -92,6 +92,7 @@ interface IConnectionRecord {
|
|
|
|
|
outgoing: plugins.net.Socket | null;
|
|
|
|
|
incomingStartTime: number;
|
|
|
|
|
outgoingStartTime?: number;
|
|
|
|
|
outgoingClosedTime?: number;
|
|
|
|
|
lockedDomain?: string; // New field to lock this connection to the initial SNI
|
|
|
|
|
connectionClosed: boolean;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 {
|
|
|
|
|
if (domainConfig.targetIPs && domainConfig.targetIPs.length > 0) {
|
|
|
|
|
const currentIndex = this.domainTargetIndices.get(domainConfig) || 0;
|
|
|
|
@ -185,18 +213,9 @@ export class PortProxy {
|
|
|
|
|
let incomingTerminationReason: string | null = null;
|
|
|
|
|
let outgoingTerminationReason: string | null = null;
|
|
|
|
|
|
|
|
|
|
// Ensure cleanup happens only once for the entire connection record.
|
|
|
|
|
const cleanupOnce = async () => {
|
|
|
|
|
if (!connectionRecord.connectionClosed) {
|
|
|
|
|
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}`);
|
|
|
|
|
}
|
|
|
|
|
// Local cleanup function that delegates to the class method.
|
|
|
|
|
const cleanupOnce = () => {
|
|
|
|
|
this.cleanupConnection(connectionRecord);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Helper to reject an incoming connection.
|
|
|
|
@ -244,6 +263,8 @@ export class PortProxy {
|
|
|
|
|
} else if (side === 'outgoing' && outgoingTerminationReason === null) {
|
|
|
|
|
outgoingTerminationReason = 'normal';
|
|
|
|
|
this.incrementTerminationStat('outgoing', 'normal');
|
|
|
|
|
// Record the time when outgoing socket closed.
|
|
|
|
|
connectionRecord.outgoingClosedTime = Date.now();
|
|
|
|
|
}
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
};
|
|
|
|
@ -333,6 +354,7 @@ export class PortProxy {
|
|
|
|
|
|
|
|
|
|
// Initialize a cleanup timer for max connection lifetime.
|
|
|
|
|
if (this.settings.maxConnectionLifetime) {
|
|
|
|
|
// Flags to track if data was seen from each side.
|
|
|
|
|
let incomingActive = false;
|
|
|
|
|
let outgoingActive = false;
|
|
|
|
|
const resetCleanupTimer = () => {
|
|
|
|
@ -349,15 +371,19 @@ export class PortProxy {
|
|
|
|
|
|
|
|
|
|
resetCleanupTimer();
|
|
|
|
|
|
|
|
|
|
// Only reset the timer if outgoing socket is still active.
|
|
|
|
|
socket.on('data', () => {
|
|
|
|
|
incomingActive = true;
|
|
|
|
|
if (incomingActive && outgoingActive) {
|
|
|
|
|
// Check if outgoing has not been closed before resetting timer.
|
|
|
|
|
if (!connectionRecord.outgoingClosedTime && incomingActive && outgoingActive) {
|
|
|
|
|
resetCleanupTimer();
|
|
|
|
|
incomingActive = false;
|
|
|
|
|
outgoingActive = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
targetSocket.on('data', () => {
|
|
|
|
|
// If outgoing is closed, do not set outgoingActive.
|
|
|
|
|
if (connectionRecord.outgoingClosedTime) return;
|
|
|
|
|
outgoingActive = true;
|
|
|
|
|
if (incomingActive && outgoingActive) {
|
|
|
|
|
resetCleanupTimer();
|
|
|
|
@ -392,7 +418,15 @@ export class PortProxy {
|
|
|
|
|
domain => domain.portRanges && domain.portRanges.length > 0 && isPortInRanges(localPort, domain.portRanges)
|
|
|
|
|
);
|
|
|
|
|
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}.`);
|
|
|
|
|
socket.end();
|
|
|
|
|
return;
|
|
|
|
@ -473,7 +507,7 @@ export class PortProxy {
|
|
|
|
|
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(() => {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
let maxIncoming = 0;
|
|
|
|
@ -483,6 +517,12 @@ export class PortProxy {
|
|
|
|
|
if (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(
|
|
|
|
|
`(Interval Log) Active connections: ${this.connectionRecords.size}. ` +
|
|
|
|
|