Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
a959c2ad0e | |||
88f5436c9a | |||
06101cd1b1 | |||
438d65107d |
13
changelog.md
13
changelog.md
@ -1,5 +1,18 @@
|
|||||||
# Changelog
|
# 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
|
||||||
|
|
||||||
|
- Consolidated plugin imports under a single 'plugins.ts' file.
|
||||||
|
- Replaced individual plugin imports in smartproxy files with the consolidated plugin imports.
|
||||||
|
- Fixed error handling for early socket errors in PortProxy setup.
|
||||||
|
|
||||||
## 2025-02-21 - 3.8.0 - feat(PortProxy)
|
## 2025-02-21 - 3.8.0 - feat(PortProxy)
|
||||||
Add active connection tracking and logging in PortProxy
|
Add active connection tracking and logging in PortProxy
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartproxy",
|
"name": "@push.rocks/smartproxy",
|
||||||
"version": "3.8.0",
|
"version": "3.9.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a proxy for handling high workloads of proxying",
|
"description": "a proxy for handling high workloads of proxying",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartproxy',
|
name: '@push.rocks/smartproxy',
|
||||||
version: '3.8.0',
|
version: '3.9.0',
|
||||||
description: 'a proxy for handling high workloads of proxying'
|
description: 'a proxy for handling high workloads of proxying'
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './smartproxy.plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
import { ProxyRouter } from './smartproxy.classes.router.js';
|
import { ProxyRouter } from './smartproxy.classes.router.js';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './smartproxy.plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
export class ProxyRouter {
|
export class ProxyRouter {
|
||||||
public reverseProxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
public reverseProxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './smartproxy.plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
export class SslRedirect {
|
export class SslRedirect {
|
||||||
httpServer: plugins.http.Server;
|
httpServer: plugins.http.Server;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './smartproxy.plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
export interface IDomainConfig {
|
export interface IDomainConfig {
|
||||||
domain: string; // glob pattern for domain
|
domain: string; // glob pattern for domain
|
||||||
@ -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,16 +172,33 @@ 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 || '';
|
||||||
|
|
||||||
// Track the new incoming connection.
|
// Record start time for the incoming connection.
|
||||||
this.activeConnections.add(socket);
|
this.activeConnections.add(socket);
|
||||||
|
this.incomingConnectionTimes.set(socket, Date.now());
|
||||||
console.log(`New connection from ${remoteIP}. Active connections: ${this.activeConnections.size}`);
|
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;
|
||||||
|
|
||||||
|
// Immediately attach an error handler to catch early errors.
|
||||||
|
socket.on('error', (err: Error) => {
|
||||||
|
if (!initialDataReceived) {
|
||||||
|
console.log(`(Premature) Incoming socket error from ${remoteIP} before data received: ${err.message}`);
|
||||||
|
} else {
|
||||||
|
console.log(`(Immediate) Incoming socket error from ${remoteIP}: ${err.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 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}`);
|
||||||
@ -241,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.
|
||||||
@ -265,6 +288,7 @@ export class PortProxy {
|
|||||||
// For SNI-enabled connections, peek at the first chunk.
|
// For SNI-enabled connections, peek at the first chunk.
|
||||||
if (this.settings.sniEnabled) {
|
if (this.settings.sniEnabled) {
|
||||||
socket.once('data', (chunk: Buffer) => {
|
socket.once('data', (chunk: Buffer) => {
|
||||||
|
initialDataReceived = true;
|
||||||
// Try to extract the server name from the ClientHello.
|
// Try to extract the server name from the ClientHello.
|
||||||
const serverName = extractSNI(chunk) || '';
|
const serverName = extractSNI(chunk) || '';
|
||||||
console.log(`Received connection from ${remoteIP} with SNI: ${serverName}`);
|
console.log(`Received connection from ${remoteIP} with SNI: ${serverName}`);
|
||||||
@ -272,6 +296,7 @@ export class PortProxy {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// For non-SNI connections, simply check defaultAllowedIPs.
|
// For non-SNI connections, simply check defaultAllowedIPs.
|
||||||
|
initialDataReceived = true;
|
||||||
if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
|
if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
|
||||||
console.log(`Connection rejected: IP ${remoteIP} not allowed for non-SNI connection`);
|
console.log(`Connection rejected: IP ${remoteIP} not allowed for non-SNI connection`);
|
||||||
socket.end();
|
socket.end();
|
||||||
@ -287,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user