|
|
@ -117,6 +117,10 @@ export class PortProxy {
|
|
|
|
settings: IProxySettings;
|
|
|
|
settings: IProxySettings;
|
|
|
|
// Track active incoming connections
|
|
|
|
// Track active incoming connections
|
|
|
|
private activeConnections: Set<plugins.net.Socket> = new Set();
|
|
|
|
private activeConnections: Set<plugins.net.Socket> = new Set();
|
|
|
|
|
|
|
|
// Record start times for incoming connections
|
|
|
|
|
|
|
|
private incomingConnectionTimes: Map<plugins.net.Socket, number> = new Map();
|
|
|
|
|
|
|
|
// Record start times for outgoing connections
|
|
|
|
|
|
|
|
private outgoingConnectionTimes: Map<plugins.net.Socket, number> = new Map();
|
|
|
|
private connectionLogger: NodeJS.Timeout | null = null;
|
|
|
|
private connectionLogger: NodeJS.Timeout | null = null;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(settings: IProxySettings) {
|
|
|
|
constructor(settings: IProxySettings) {
|
|
|
@ -168,6 +172,11 @@ export class PortProxy {
|
|
|
|
this.netServer = plugins.net.createServer((socket: plugins.net.Socket) => {
|
|
|
|
this.netServer = plugins.net.createServer((socket: plugins.net.Socket) => {
|
|
|
|
const remoteIP = socket.remoteAddress || '';
|
|
|
|
const remoteIP = socket.remoteAddress || '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Record start time for the incoming connection.
|
|
|
|
|
|
|
|
this.activeConnections.add(socket);
|
|
|
|
|
|
|
|
this.incomingConnectionTimes.set(socket, Date.now());
|
|
|
|
|
|
|
|
console.log(`New connection from ${remoteIP}. Active connections: ${this.activeConnections.size}`);
|
|
|
|
|
|
|
|
|
|
|
|
// Flag to detect if we've received the first data chunk.
|
|
|
|
// Flag to detect if we've received the first data chunk.
|
|
|
|
let initialDataReceived = false;
|
|
|
|
let initialDataReceived = false;
|
|
|
|
|
|
|
|
|
|
|
@ -180,16 +189,16 @@ export class PortProxy {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Track the new incoming connection.
|
|
|
|
|
|
|
|
this.activeConnections.add(socket);
|
|
|
|
|
|
|
|
console.log(`New connection from ${remoteIP}. Active connections: ${this.activeConnections.size}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Flag to ensure cleanup happens only once.
|
|
|
|
// Flag to ensure cleanup happens only once.
|
|
|
|
let connectionClosed = false;
|
|
|
|
let connectionClosed = false;
|
|
|
|
const cleanupOnce = () => {
|
|
|
|
const cleanupOnce = () => {
|
|
|
|
if (!connectionClosed) {
|
|
|
|
if (!connectionClosed) {
|
|
|
|
connectionClosed = true;
|
|
|
|
connectionClosed = true;
|
|
|
|
cleanUpSockets(socket, to);
|
|
|
|
cleanUpSockets(socket, to);
|
|
|
|
|
|
|
|
this.incomingConnectionTimes.delete(socket);
|
|
|
|
|
|
|
|
if (to) {
|
|
|
|
|
|
|
|
this.outgoingConnectionTimes.delete(to);
|
|
|
|
|
|
|
|
}
|
|
|
|
if (this.activeConnections.has(socket)) {
|
|
|
|
if (this.activeConnections.has(socket)) {
|
|
|
|
this.activeConnections.delete(socket);
|
|
|
|
this.activeConnections.delete(socket);
|
|
|
|
console.log(`Connection from ${remoteIP} terminated. Active connections: ${this.activeConnections.size}`);
|
|
|
|
console.log(`Connection from ${remoteIP} terminated. Active connections: ${this.activeConnections.size}`);
|
|
|
@ -253,6 +262,8 @@ 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.
|
|
|
|
|
|
|
|
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.
|
|
|
@ -301,9 +312,24 @@ export class PortProxy {
|
|
|
|
console.log(`PortProxy -> OK: Now listening on port ${this.settings.fromPort}${this.settings.sniEnabled ? ' (SNI passthrough enabled)' : ''}`);
|
|
|
|
console.log(`PortProxy -> OK: Now listening on port ${this.settings.fromPort}${this.settings.sniEnabled ? ' (SNI passthrough enabled)' : ''}`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Log active connection count every 10 seconds.
|
|
|
|
// Log active connection count and longest running connections every 10 seconds.
|
|
|
|
this.connectionLogger = setInterval(() => {
|
|
|
|
this.connectionLogger = setInterval(() => {
|
|
|
|
console.log(`(Interval Log) Active connections: ${this.activeConnections.size}`);
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
let maxIncoming = 0;
|
|
|
|
|
|
|
|
for (const startTime of this.incomingConnectionTimes.values()) {
|
|
|
|
|
|
|
|
const duration = now - startTime;
|
|
|
|
|
|
|
|
if (duration > maxIncoming) {
|
|
|
|
|
|
|
|
maxIncoming = duration;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let maxOutgoing = 0;
|
|
|
|
|
|
|
|
for (const startTime of this.outgoingConnectionTimes.values()) {
|
|
|
|
|
|
|
|
const duration = now - startTime;
|
|
|
|
|
|
|
|
if (duration > maxOutgoing) {
|
|
|
|
|
|
|
|
maxOutgoing = duration;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`(Interval Log) Active connections: ${this.activeConnections.size}. Longest running incoming: ${maxIncoming}ms, outgoing: ${maxOutgoing}ms`);
|
|
|
|
}, 10000);
|
|
|
|
}, 10000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|