|
|
|
@ -123,6 +123,15 @@ export class PortProxy {
|
|
|
|
|
private outgoingConnectionTimes: Map<plugins.net.Socket, number> = new Map();
|
|
|
|
|
private connectionLogger: NodeJS.Timeout | null = null;
|
|
|
|
|
|
|
|
|
|
// Overall termination statistics
|
|
|
|
|
private terminationStats: {
|
|
|
|
|
incoming: Record<string, number>;
|
|
|
|
|
outgoing: Record<string, number>;
|
|
|
|
|
} = {
|
|
|
|
|
incoming: {},
|
|
|
|
|
outgoing: {},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constructor(settings: IProxySettings) {
|
|
|
|
|
this.settings = {
|
|
|
|
|
...settings,
|
|
|
|
@ -130,16 +139,24 @@ export class PortProxy {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper to update termination stats.
|
|
|
|
|
private incrementTerminationStat(side: 'incoming' | 'outgoing', reason: string): void {
|
|
|
|
|
if (!this.terminationStats[side][reason]) {
|
|
|
|
|
this.terminationStats[side][reason] = 1;
|
|
|
|
|
} else {
|
|
|
|
|
this.terminationStats[side][reason]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async start() {
|
|
|
|
|
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();
|
|
|
|
|
// Adjusted cleanUpSockets: forcefully destroy both sockets if they haven't been destroyed.
|
|
|
|
|
const cleanUpSockets = (from: plugins.net.Socket, to?: plugins.net.Socket) => {
|
|
|
|
|
if (!from.destroyed) {
|
|
|
|
|
from.destroy();
|
|
|
|
|
}
|
|
|
|
|
if (to && !to.destroyed) {
|
|
|
|
|
to.destroy();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const normalizeIP = (ip: string): string[] => {
|
|
|
|
@ -180,6 +197,10 @@ export class PortProxy {
|
|
|
|
|
// Flag to detect if we've received the first data chunk.
|
|
|
|
|
let initialDataReceived = false;
|
|
|
|
|
|
|
|
|
|
// Local termination reason trackers for each side.
|
|
|
|
|
let incomingTermReason: string | null = null;
|
|
|
|
|
let outgoingTermReason: string | null = null;
|
|
|
|
|
|
|
|
|
|
// Immediately attach an error handler to catch early errors.
|
|
|
|
|
socket.on('error', (err: Error) => {
|
|
|
|
|
if (!initialDataReceived) {
|
|
|
|
@ -189,12 +210,12 @@ export class PortProxy {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Flag to ensure cleanup happens only once.
|
|
|
|
|
// Ensure cleanup happens only once.
|
|
|
|
|
let connectionClosed = false;
|
|
|
|
|
const cleanupOnce = () => {
|
|
|
|
|
if (!connectionClosed) {
|
|
|
|
|
connectionClosed = true;
|
|
|
|
|
cleanUpSockets(socket, to);
|
|
|
|
|
cleanUpSockets(socket, to || undefined);
|
|
|
|
|
this.incomingConnectionTimes.delete(socket);
|
|
|
|
|
if (to) {
|
|
|
|
|
this.outgoingConnectionTimes.delete(to);
|
|
|
|
@ -206,20 +227,39 @@ export class PortProxy {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let to: plugins.net.Socket;
|
|
|
|
|
// Outgoing connection placeholder.
|
|
|
|
|
let to: plugins.net.Socket | null = null;
|
|
|
|
|
|
|
|
|
|
// Handle errors by recording termination reason and cleaning up.
|
|
|
|
|
const handleError = (side: 'incoming' | 'outgoing') => (err: Error) => {
|
|
|
|
|
const code = (err as any).code;
|
|
|
|
|
let reason = 'error';
|
|
|
|
|
if (code === 'ECONNRESET') {
|
|
|
|
|
reason = 'econnreset';
|
|
|
|
|
console.log(`ECONNRESET on ${side} side from ${remoteIP}: ${err.message}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`Error on ${side} side from ${remoteIP}: ${err.message}`);
|
|
|
|
|
}
|
|
|
|
|
if (side === 'incoming' && incomingTermReason === null) {
|
|
|
|
|
incomingTermReason = reason;
|
|
|
|
|
this.incrementTerminationStat('incoming', reason);
|
|
|
|
|
} else if (side === 'outgoing' && outgoingTermReason === null) {
|
|
|
|
|
outgoingTermReason = reason;
|
|
|
|
|
this.incrementTerminationStat('outgoing', reason);
|
|
|
|
|
}
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Handle close events. If no termination reason was recorded, mark as "normal".
|
|
|
|
|
const handleClose = (side: 'incoming' | 'outgoing') => () => {
|
|
|
|
|
console.log(`Connection closed on ${side} side from ${remoteIP}`);
|
|
|
|
|
if (side === 'incoming' && incomingTermReason === null) {
|
|
|
|
|
incomingTermReason = 'normal';
|
|
|
|
|
this.incrementTerminationStat('incoming', 'normal');
|
|
|
|
|
} else if (side === 'outgoing' && outgoingTermReason === null) {
|
|
|
|
|
outgoingTermReason = 'normal';
|
|
|
|
|
this.incrementTerminationStat('outgoing', 'normal');
|
|
|
|
|
}
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -232,16 +272,31 @@ export class PortProxy {
|
|
|
|
|
if (!domainConfig) {
|
|
|
|
|
console.log(`Connection rejected: No matching domain config for ${serverName} from ${remoteIP}`);
|
|
|
|
|
socket.end();
|
|
|
|
|
if (incomingTermReason === null) {
|
|
|
|
|
incomingTermReason = 'rejected';
|
|
|
|
|
this.incrementTerminationStat('incoming', 'rejected');
|
|
|
|
|
}
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!isAllowed(remoteIP, domainConfig.allowedIPs)) {
|
|
|
|
|
console.log(`Connection rejected: IP ${remoteIP} not allowed for domain ${serverName}`);
|
|
|
|
|
socket.end();
|
|
|
|
|
if (incomingTermReason === null) {
|
|
|
|
|
incomingTermReason = 'rejected';
|
|
|
|
|
this.incrementTerminationStat('incoming', 'rejected');
|
|
|
|
|
}
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else if (!isDefaultAllowed && !serverName) {
|
|
|
|
|
console.log(`Connection rejected: No SNI and IP ${remoteIP} not in default allowed list`);
|
|
|
|
|
socket.end();
|
|
|
|
|
if (incomingTermReason === null) {
|
|
|
|
|
incomingTermReason = 'rejected';
|
|
|
|
|
this.incrementTerminationStat('incoming', 'rejected');
|
|
|
|
|
}
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`Connection allowed: IP ${remoteIP} is in default allowed list`);
|
|
|
|
@ -262,8 +317,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,25 +327,47 @@ export class PortProxy {
|
|
|
|
|
socket.unshift(initialChunk);
|
|
|
|
|
}
|
|
|
|
|
socket.setTimeout(120000);
|
|
|
|
|
socket.pipe(to);
|
|
|
|
|
to.pipe(socket);
|
|
|
|
|
socket.pipe(to!);
|
|
|
|
|
to!.pipe(socket);
|
|
|
|
|
|
|
|
|
|
// Attach error and close handlers for both sockets.
|
|
|
|
|
// Attach event 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'));
|
|
|
|
|
socket.on('timeout', handleError('incoming'));
|
|
|
|
|
to.on('timeout', handleError('outgoing'));
|
|
|
|
|
to!.on('close', handleClose('outgoing'));
|
|
|
|
|
socket.on('timeout', () => {
|
|
|
|
|
console.log(`Timeout on incoming side from ${remoteIP}`);
|
|
|
|
|
if (incomingTermReason === null) {
|
|
|
|
|
incomingTermReason = 'timeout';
|
|
|
|
|
this.incrementTerminationStat('incoming', 'timeout');
|
|
|
|
|
}
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
});
|
|
|
|
|
to!.on('timeout', () => {
|
|
|
|
|
console.log(`Timeout on outgoing side from ${remoteIP}`);
|
|
|
|
|
if (outgoingTermReason === null) {
|
|
|
|
|
outgoingTermReason = 'timeout';
|
|
|
|
|
this.incrementTerminationStat('outgoing', 'timeout');
|
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
// For SNI-enabled connections, set an initial data timeout before waiting for data.
|
|
|
|
|
if (this.settings.sniEnabled) {
|
|
|
|
|
// Set an initial timeout for receiving data (e.g., 5 seconds)
|
|
|
|
|
socket.setTimeout(5000, () => {
|
|
|
|
|
console.log(`Initial data timeout for ${remoteIP}`);
|
|
|
|
|
socket.end();
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.once('data', (chunk: Buffer) => {
|
|
|
|
|
// Clear the initial timeout since data has been received
|
|
|
|
|
socket.setTimeout(0);
|
|
|
|
|
initialDataReceived = true;
|
|
|
|
|
// Try to extract the server name from the ClientHello.
|
|
|
|
|
const serverName = extractSNI(chunk) || '';
|
|
|
|
|
console.log(`Received connection from ${remoteIP} with SNI: ${serverName}`);
|
|
|
|
|
setupConnection(serverName, chunk);
|
|
|
|
@ -300,6 +378,11 @@ export class PortProxy {
|
|
|
|
|
if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
|
|
|
|
|
console.log(`Connection rejected: IP ${remoteIP} not allowed for non-SNI connection`);
|
|
|
|
|
socket.end();
|
|
|
|
|
if (incomingTermReason === null) {
|
|
|
|
|
incomingTermReason = 'rejected';
|
|
|
|
|
this.incrementTerminationStat('incoming', 'rejected');
|
|
|
|
|
}
|
|
|
|
|
cleanupOnce();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setupConnection('');
|
|
|
|
@ -312,7 +395,8 @@ export class PortProxy {
|
|
|
|
|
console.log(`PortProxy -> OK: Now listening on port ${this.settings.fromPort}${this.settings.sniEnabled ? ' (SNI passthrough enabled)' : ''}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Log active connection count and longest running connections every 10 seconds.
|
|
|
|
|
// Log active connection count, longest running connection durations,
|
|
|
|
|
// and termination statistics every 10 seconds.
|
|
|
|
|
this.connectionLogger = setInterval(() => {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
let maxIncoming = 0;
|
|
|
|
@ -329,7 +413,7 @@ export class PortProxy {
|
|
|
|
|
maxOutgoing = duration;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log(`(Interval Log) Active connections: ${this.activeConnections.size}. Longest running incoming: ${plugins.prettyMs(maxIncoming)}, outgoing: ${plugins.prettyMs(maxOutgoing)}`);
|
|
|
|
|
console.log(`(Interval Log) Active connections: ${this.activeConnections.size}. Longest running incoming: ${plugins.prettyMs(maxIncoming)}, outgoing: ${plugins.prettyMs(maxOutgoing)}. Termination stats (incoming): ${JSON.stringify(this.terminationStats.incoming)}, (outgoing): ${JSON.stringify(this.terminationStats.outgoing)}`);
|
|
|
|
|
}, 10000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|