feat(smartproxy.portproxy): Add logging of connection durations to PortProxy

This commit is contained in:
Philipp Kunz 2025-02-21 23:18:17 +00:00
parent 06101cd1b1
commit 88f5436c9a
3 changed files with 39 additions and 7 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## 2025-02-21 - 3.9.0 - feat(smartproxy.portproxy)
Add logging of connection durations to PortProxy
- Track start times for incoming and outgoing connections.
- Log duration of longest running incoming and outgoing connections every 10 seconds.
## 2025-02-21 - 3.8.1 - fix(plugins)
Simplified plugin import structure across codebase

View File

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

View File

@ -117,6 +117,10 @@ export class PortProxy {
settings: IProxySettings;
// Track active incoming connections
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;
constructor(settings: IProxySettings) {
@ -168,6 +172,11 @@ export class PortProxy {
this.netServer = plugins.net.createServer((socket: plugins.net.Socket) => {
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.
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.
let connectionClosed = false;
const cleanupOnce = () => {
if (!connectionClosed) {
connectionClosed = true;
cleanUpSockets(socket, to);
this.incomingConnectionTimes.delete(socket);
if (to) {
this.outgoingConnectionTimes.delete(to);
}
if (this.activeConnections.has(socket)) {
this.activeConnections.delete(socket);
console.log(`Connection from ${remoteIP} terminated. Active connections: ${this.activeConnections.size}`);
@ -253,6 +262,8 @@ export class PortProxy {
// Establish outgoing connection.
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})` : ''}`);
// 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)' : ''}`);
});
// Log active connection count every 10 seconds.
// Log active connection count and longest running connections every 10 seconds.
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);
}