@push.rocks/smartproxy 19.6.6 → 19.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist_ts/core/models/wrapped-socket.d.ts +4 -0
  2. package/dist_ts/core/models/wrapped-socket.js +18 -1
  3. package/dist_ts/core/routing/matchers/path.js +3 -2
  4. package/dist_ts/proxies/smart-proxy/connection-manager.d.ts +4 -7
  5. package/dist_ts/proxies/smart-proxy/connection-manager.js +22 -22
  6. package/dist_ts/proxies/smart-proxy/http-proxy-bridge.d.ts +4 -3
  7. package/dist_ts/proxies/smart-proxy/http-proxy-bridge.js +9 -9
  8. package/dist_ts/proxies/smart-proxy/metrics-collector.d.ts +68 -56
  9. package/dist_ts/proxies/smart-proxy/metrics-collector.js +252 -176
  10. package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +6 -1
  11. package/dist_ts/proxies/smart-proxy/models/metrics-types.d.ts +99 -47
  12. package/dist_ts/proxies/smart-proxy/nftables-manager.d.ts +4 -4
  13. package/dist_ts/proxies/smart-proxy/nftables-manager.js +6 -6
  14. package/dist_ts/proxies/smart-proxy/port-manager.d.ts +4 -7
  15. package/dist_ts/proxies/smart-proxy/port-manager.js +6 -9
  16. package/dist_ts/proxies/smart-proxy/route-connection-handler.d.ts +4 -15
  17. package/dist_ts/proxies/smart-proxy/route-connection-handler.js +133 -130
  18. package/dist_ts/proxies/smart-proxy/security-manager.d.ts +3 -3
  19. package/dist_ts/proxies/smart-proxy/security-manager.js +9 -9
  20. package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +20 -13
  21. package/dist_ts/proxies/smart-proxy/smart-proxy.js +16 -13
  22. package/dist_ts/proxies/smart-proxy/throughput-tracker.d.ts +36 -0
  23. package/dist_ts/proxies/smart-proxy/throughput-tracker.js +117 -0
  24. package/dist_ts/proxies/smart-proxy/timeout-manager.d.ts +5 -4
  25. package/dist_ts/proxies/smart-proxy/timeout-manager.js +20 -16
  26. package/dist_ts/proxies/smart-proxy/tls-manager.d.ts +3 -3
  27. package/dist_ts/proxies/smart-proxy/tls-manager.js +12 -12
  28. package/dist_ts/routing/router/http-router.js +2 -2
  29. package/package.json +1 -1
  30. package/readme.hints.md +0 -0
  31. package/readme.md +239 -73
  32. package/readme.plan.md +364 -0
  33. package/ts/core/models/wrapped-socket.ts +18 -0
  34. package/ts/core/routing/matchers/path.ts +2 -1
  35. package/ts/proxies/smart-proxy/connection-manager.ts +23 -21
  36. package/ts/proxies/smart-proxy/http-proxy-bridge.ts +9 -8
  37. package/ts/proxies/smart-proxy/metrics-collector.ts +305 -188
  38. package/ts/proxies/smart-proxy/models/interfaces.ts +8 -1
  39. package/ts/proxies/smart-proxy/models/metrics-types.ts +99 -41
  40. package/ts/proxies/smart-proxy/nftables-manager.ts +5 -5
  41. package/ts/proxies/smart-proxy/port-manager.ts +6 -14
  42. package/ts/proxies/smart-proxy/route-connection-handler.ts +141 -138
  43. package/ts/proxies/smart-proxy/security-manager.ts +8 -8
  44. package/ts/proxies/smart-proxy/smart-proxy.ts +26 -35
  45. package/ts/proxies/smart-proxy/throughput-tracker.ts +144 -0
  46. package/ts/proxies/smart-proxy/timeout-manager.ts +22 -16
  47. package/ts/proxies/smart-proxy/tls-manager.ts +11 -11
  48. package/ts/routing/router/http-router.ts +1 -1
@@ -1,54 +1,112 @@
1
1
  /**
2
- * Interface for proxy statistics and metrics
2
+ * Interface for throughput sample data
3
3
  */
4
- export interface IProxyStats {
5
- /**
6
- * Get the current number of active connections
7
- */
8
- getActiveConnections(): number;
9
-
10
- /**
11
- * Get connection counts grouped by route name
12
- */
13
- getConnectionsByRoute(): Map<string, number>;
4
+ export interface IThroughputSample {
5
+ timestamp: number;
6
+ bytesIn: number;
7
+ bytesOut: number;
8
+ tags?: {
9
+ route?: string;
10
+ ip?: string;
11
+ [key: string]: string | undefined;
12
+ };
13
+ }
14
+
15
+ /**
16
+ * Interface for throughput data
17
+ */
18
+ export interface IThroughputData {
19
+ in: number;
20
+ out: number;
21
+ }
22
+
23
+ /**
24
+ * Interface for time-series throughput data
25
+ */
26
+ export interface IThroughputHistoryPoint {
27
+ timestamp: number;
28
+ in: number;
29
+ out: number;
30
+ }
31
+
32
+ /**
33
+ * Main metrics interface with clean, grouped API
34
+ */
35
+ export interface IMetrics {
36
+ // Connection metrics
37
+ connections: {
38
+ active(): number;
39
+ total(): number;
40
+ byRoute(): Map<string, number>;
41
+ byIP(): Map<string, number>;
42
+ topIPs(limit?: number): Array<{ ip: string; count: number }>;
43
+ };
14
44
 
15
- /**
16
- * Get connection counts grouped by IP address
17
- */
18
- getConnectionsByIP(): Map<string, number>;
45
+ // Throughput metrics (bytes per second)
46
+ throughput: {
47
+ instant(): IThroughputData; // Last 1 second
48
+ recent(): IThroughputData; // Last 10 seconds
49
+ average(): IThroughputData; // Last 60 seconds
50
+ custom(seconds: number): IThroughputData;
51
+ history(seconds: number): Array<IThroughputHistoryPoint>;
52
+ byRoute(windowSeconds?: number): Map<string, IThroughputData>;
53
+ byIP(windowSeconds?: number): Map<string, IThroughputData>;
54
+ };
19
55
 
20
- /**
21
- * Get the total number of connections since proxy start
22
- */
23
- getTotalConnections(): number;
56
+ // Request metrics
57
+ requests: {
58
+ perSecond(): number;
59
+ perMinute(): number;
60
+ total(): number;
61
+ };
24
62
 
25
- /**
26
- * Get the current requests per second rate
27
- */
28
- getRequestsPerSecond(): number;
63
+ // Cumulative totals
64
+ totals: {
65
+ bytesIn(): number;
66
+ bytesOut(): number;
67
+ connections(): number;
68
+ };
29
69
 
30
- /**
31
- * Get total throughput (bytes transferred)
32
- */
33
- getThroughput(): { bytesIn: number; bytesOut: number };
70
+ // Performance metrics
71
+ percentiles: {
72
+ connectionDuration(): { p50: number; p95: number; p99: number };
73
+ bytesTransferred(): {
74
+ in: { p50: number; p95: number; p99: number };
75
+ out: { p50: number; p95: number; p99: number };
76
+ };
77
+ };
34
78
  }
35
79
 
36
80
  /**
37
- * Extended interface for additional metrics helpers
81
+ * Configuration for metrics collection
38
82
  */
39
- export interface IProxyStatsExtended extends IProxyStats {
40
- /**
41
- * Get throughput rate (bytes per second) for last minute
42
- */
43
- getThroughputRate(): { bytesInPerSec: number; bytesOutPerSec: number };
83
+ export interface IMetricsConfig {
84
+ enabled: boolean;
85
+
86
+ // Sampling configuration
87
+ sampleIntervalMs: number; // Default: 1000 (1 second)
88
+ retentionSeconds: number; // Default: 3600 (1 hour)
44
89
 
45
- /**
46
- * Get top IPs by connection count
47
- */
48
- getTopIPs(limit?: number): Array<{ ip: string; connections: number }>;
90
+ // Performance tuning
91
+ enableDetailedTracking: boolean; // Per-connection byte history
92
+ enablePercentiles: boolean; // Calculate percentiles
93
+ cacheResultsMs: number; // Cache expensive calculations
49
94
 
50
- /**
51
- * Check if an IP has reached the connection limit
52
- */
53
- isIPBlocked(ip: string, maxConnectionsPerIP: number): boolean;
95
+ // Export configuration
96
+ prometheusEnabled: boolean;
97
+ prometheusPath: string; // Default: /metrics
98
+ prometheusPrefix: string; // Default: smartproxy_
99
+ }
100
+
101
+ /**
102
+ * Internal interface for connection byte tracking
103
+ */
104
+ export interface IByteTracker {
105
+ connectionId: string;
106
+ routeName: string;
107
+ remoteIP: string;
108
+ bytesIn: number;
109
+ bytesOut: number;
110
+ startTime: number;
111
+ lastUpdate: number;
54
112
  }
@@ -10,7 +10,7 @@ import type {
10
10
  TPortRange,
11
11
  INfTablesOptions
12
12
  } from './models/route-types.js';
13
- import type { ISmartProxyOptions } from './models/interfaces.js';
13
+ import type { SmartProxy } from './smart-proxy.js';
14
14
 
15
15
  /**
16
16
  * Manages NFTables rules based on SmartProxy route configurations
@@ -25,9 +25,9 @@ export class NFTablesManager {
25
25
  /**
26
26
  * Creates a new NFTablesManager
27
27
  *
28
- * @param options The SmartProxy options
28
+ * @param smartProxy The SmartProxy instance
29
29
  */
30
- constructor(private options: ISmartProxyOptions) {}
30
+ constructor(private smartProxy: SmartProxy) {}
31
31
 
32
32
  /**
33
33
  * Provision NFTables rules for a route
@@ -166,10 +166,10 @@ export class NFTablesManager {
166
166
  protocol: action.nftables?.protocol || 'tcp',
167
167
  preserveSourceIP: action.nftables?.preserveSourceIP !== undefined ?
168
168
  action.nftables.preserveSourceIP :
169
- this.options.preserveSourceIP,
169
+ this.smartProxy.settings.preserveSourceIP,
170
170
  useIPSets: action.nftables?.useIPSets !== false,
171
171
  useAdvancedNAT: action.nftables?.useAdvancedNAT,
172
- enableLogging: this.options.enableDetailedLogging,
172
+ enableLogging: this.smartProxy.settings.enableDetailedLogging,
173
173
  deleteOnExit: true,
174
174
  tableName: action.nftables?.tableName || 'smartproxy'
175
175
  };
@@ -1,8 +1,7 @@
1
1
  import * as plugins from '../../plugins.js';
2
- import type { ISmartProxyOptions } from './models/interfaces.js';
3
- import { RouteConnectionHandler } from './route-connection-handler.js';
4
2
  import { logger } from '../../core/utils/logger.js';
5
3
  import { cleanupSocket } from '../../core/utils/socket-utils.js';
4
+ import type { SmartProxy } from './smart-proxy.js';
6
5
 
7
6
  /**
8
7
  * PortManager handles the dynamic creation and removal of port listeners
@@ -16,8 +15,6 @@ import { cleanupSocket } from '../../core/utils/socket-utils.js';
16
15
  */
17
16
  export class PortManager {
18
17
  private servers: Map<number, plugins.net.Server> = new Map();
19
- private settings: ISmartProxyOptions;
20
- private routeConnectionHandler: RouteConnectionHandler;
21
18
  private isShuttingDown: boolean = false;
22
19
  // Track how many routes are using each port
23
20
  private portRefCounts: Map<number, number> = new Map();
@@ -25,16 +22,11 @@ export class PortManager {
25
22
  /**
26
23
  * Create a new PortManager
27
24
  *
28
- * @param settings The SmartProxy settings
29
- * @param routeConnectionHandler The handler for new connections
25
+ * @param smartProxy The SmartProxy instance
30
26
  */
31
27
  constructor(
32
- settings: ISmartProxyOptions,
33
- routeConnectionHandler: RouteConnectionHandler
34
- ) {
35
- this.settings = settings;
36
- this.routeConnectionHandler = routeConnectionHandler;
37
- }
28
+ private smartProxy: SmartProxy
29
+ ) {}
38
30
 
39
31
  /**
40
32
  * Start listening on a specific port
@@ -70,7 +62,7 @@ export class PortManager {
70
62
  }
71
63
 
72
64
  // Delegate to route connection handler
73
- this.routeConnectionHandler.handleConnection(socket);
65
+ this.smartProxy.routeConnectionHandler.handleConnection(socket);
74
66
  }).on('error', (err: Error) => {
75
67
  try {
76
68
  logger.log('error', `Server Error on port ${port}: ${err.message}`, {
@@ -86,7 +78,7 @@ export class PortManager {
86
78
  // Start listening on the port
87
79
  return new Promise<void>((resolve, reject) => {
88
80
  server.listen(port, () => {
89
- const isHttpProxyPort = this.settings.useHttpProxy?.includes(port);
81
+ const isHttpProxyPort = this.smartProxy.settings.useHttpProxy?.includes(port);
90
82
  try {
91
83
  logger.log('info', `SmartProxy -> OK: Now listening on port ${port}${
92
84
  isHttpProxyPort ? ' (HttpProxy forwarding enabled)' : ''