@push.rocks/smartproxy 21.1.7 → 22.6.0

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 (155) hide show
  1. package/changelog.md +109 -0
  2. package/dist_rust/rustproxy +0 -0
  3. package/dist_ts/00_commitinfo_data.js +1 -1
  4. package/dist_ts/core/utils/shared-security-manager.d.ts +17 -0
  5. package/dist_ts/core/utils/shared-security-manager.js +66 -1
  6. package/dist_ts/index.d.ts +1 -5
  7. package/dist_ts/index.js +3 -9
  8. package/dist_ts/protocols/common/fragment-handler.js +5 -1
  9. package/dist_ts/proxies/http-proxy/default-certificates.d.ts +54 -0
  10. package/dist_ts/proxies/http-proxy/default-certificates.js +127 -0
  11. package/dist_ts/proxies/http-proxy/http-proxy.d.ts +1 -1
  12. package/dist_ts/proxies/http-proxy/http-proxy.js +9 -14
  13. package/dist_ts/proxies/http-proxy/index.d.ts +5 -1
  14. package/dist_ts/proxies/http-proxy/index.js +6 -2
  15. package/dist_ts/proxies/http-proxy/security-manager.d.ts +4 -12
  16. package/dist_ts/proxies/http-proxy/security-manager.js +66 -99
  17. package/dist_ts/proxies/index.d.ts +1 -5
  18. package/dist_ts/proxies/index.js +2 -6
  19. package/dist_ts/proxies/nftables-proxy/index.d.ts +1 -0
  20. package/dist_ts/proxies/nftables-proxy/index.js +2 -1
  21. package/dist_ts/proxies/nftables-proxy/nftables-proxy.d.ts +4 -26
  22. package/dist_ts/proxies/nftables-proxy/nftables-proxy.js +84 -236
  23. package/dist_ts/proxies/nftables-proxy/utils/index.d.ts +9 -0
  24. package/dist_ts/proxies/nftables-proxy/utils/index.js +12 -0
  25. package/dist_ts/proxies/nftables-proxy/utils/nft-command-executor.d.ts +66 -0
  26. package/dist_ts/proxies/nftables-proxy/utils/nft-command-executor.js +131 -0
  27. package/dist_ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.d.ts +39 -0
  28. package/dist_ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.js +112 -0
  29. package/dist_ts/proxies/nftables-proxy/utils/nft-rule-validator.d.ts +59 -0
  30. package/dist_ts/proxies/nftables-proxy/utils/nft-rule-validator.js +130 -0
  31. package/dist_ts/proxies/smart-proxy/certificate-manager.js +4 -3
  32. package/dist_ts/proxies/smart-proxy/connection-manager.d.ts +13 -2
  33. package/dist_ts/proxies/smart-proxy/connection-manager.js +16 -6
  34. package/dist_ts/proxies/smart-proxy/http-proxy-bridge.js +35 -10
  35. package/dist_ts/proxies/smart-proxy/index.d.ts +5 -10
  36. package/dist_ts/proxies/smart-proxy/index.js +7 -13
  37. package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +5 -3
  38. package/dist_ts/proxies/smart-proxy/route-connection-handler.d.ts +17 -0
  39. package/dist_ts/proxies/smart-proxy/route-connection-handler.js +72 -9
  40. package/dist_ts/proxies/smart-proxy/route-preprocessor.d.ts +37 -0
  41. package/dist_ts/proxies/smart-proxy/route-preprocessor.js +103 -0
  42. package/dist_ts/proxies/smart-proxy/rust-binary-locator.d.ts +23 -0
  43. package/dist_ts/proxies/smart-proxy/rust-binary-locator.js +104 -0
  44. package/dist_ts/proxies/smart-proxy/rust-metrics-adapter.d.ts +74 -0
  45. package/dist_ts/proxies/smart-proxy/rust-metrics-adapter.js +146 -0
  46. package/dist_ts/proxies/smart-proxy/rust-proxy-bridge.d.ts +49 -0
  47. package/dist_ts/proxies/smart-proxy/rust-proxy-bridge.js +259 -0
  48. package/dist_ts/proxies/smart-proxy/security-manager.d.ts +14 -12
  49. package/dist_ts/proxies/smart-proxy/security-manager.js +80 -74
  50. package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +39 -157
  51. package/dist_ts/proxies/smart-proxy/smart-proxy.js +224 -622
  52. package/dist_ts/proxies/smart-proxy/socket-handler-server.d.ts +45 -0
  53. package/dist_ts/proxies/smart-proxy/socket-handler-server.js +253 -0
  54. package/dist_ts/proxies/smart-proxy/tls-manager.d.ts +2 -9
  55. package/dist_ts/proxies/smart-proxy/tls-manager.js +3 -26
  56. package/dist_ts/proxies/smart-proxy/utils/index.d.ts +1 -1
  57. package/dist_ts/proxies/smart-proxy/utils/index.js +3 -4
  58. package/dist_ts/proxies/smart-proxy/utils/route-helpers/api-helpers.d.ts +49 -0
  59. package/dist_ts/proxies/smart-proxy/utils/route-helpers/api-helpers.js +108 -0
  60. package/dist_ts/proxies/smart-proxy/utils/route-helpers/dynamic-helpers.d.ts +57 -0
  61. package/dist_ts/proxies/smart-proxy/utils/route-helpers/dynamic-helpers.js +89 -0
  62. package/dist_ts/proxies/smart-proxy/utils/route-helpers/http-helpers.d.ts +17 -0
  63. package/dist_ts/proxies/smart-proxy/utils/route-helpers/http-helpers.js +32 -0
  64. package/dist_ts/proxies/smart-proxy/utils/route-helpers/https-helpers.d.ts +68 -0
  65. package/dist_ts/proxies/smart-proxy/utils/route-helpers/https-helpers.js +117 -0
  66. package/dist_ts/proxies/smart-proxy/utils/route-helpers/index.d.ts +17 -0
  67. package/dist_ts/proxies/smart-proxy/utils/route-helpers/index.js +27 -0
  68. package/dist_ts/proxies/smart-proxy/utils/route-helpers/load-balancer-helpers.d.ts +63 -0
  69. package/dist_ts/proxies/smart-proxy/utils/route-helpers/load-balancer-helpers.js +105 -0
  70. package/dist_ts/proxies/smart-proxy/utils/route-helpers/nftables-helpers.d.ts +83 -0
  71. package/dist_ts/proxies/smart-proxy/utils/route-helpers/nftables-helpers.js +126 -0
  72. package/dist_ts/proxies/smart-proxy/utils/route-helpers/security-helpers.d.ts +47 -0
  73. package/dist_ts/proxies/smart-proxy/utils/route-helpers/security-helpers.js +66 -0
  74. package/dist_ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.d.ts +70 -0
  75. package/dist_ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.js +287 -0
  76. package/dist_ts/proxies/smart-proxy/utils/route-helpers/websocket-helpers.d.ts +46 -0
  77. package/dist_ts/proxies/smart-proxy/utils/route-helpers/websocket-helpers.js +67 -0
  78. package/dist_ts/proxies/smart-proxy/utils/route-helpers.d.ts +4 -457
  79. package/dist_ts/proxies/smart-proxy/utils/route-helpers.js +6 -950
  80. package/dist_ts/proxies/smart-proxy/utils/route-utils.js +2 -2
  81. package/dist_ts/proxies/smart-proxy/utils/route-validator.d.ts +67 -1
  82. package/dist_ts/proxies/smart-proxy/utils/route-validator.js +251 -3
  83. package/dist_ts/routing/index.d.ts +1 -1
  84. package/dist_ts/routing/index.js +3 -3
  85. package/dist_ts/routing/models/http-types.d.ts +119 -4
  86. package/dist_ts/routing/models/http-types.js +93 -5
  87. package/npmextra.json +12 -6
  88. package/package.json +34 -24
  89. package/readme.hints.md +184 -1
  90. package/readme.md +580 -266
  91. package/ts/00_commitinfo_data.ts +1 -1
  92. package/ts/core/utils/shared-security-manager.ts +98 -13
  93. package/ts/index.ts +4 -12
  94. package/ts/protocols/common/fragment-handler.ts +4 -0
  95. package/ts/proxies/index.ts +1 -9
  96. package/ts/proxies/nftables-proxy/index.ts +1 -0
  97. package/ts/proxies/nftables-proxy/nftables-proxy.ts +116 -290
  98. package/ts/proxies/nftables-proxy/utils/index.ts +38 -0
  99. package/ts/proxies/nftables-proxy/utils/nft-command-executor.ts +162 -0
  100. package/ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.ts +125 -0
  101. package/ts/proxies/nftables-proxy/utils/nft-rule-validator.ts +156 -0
  102. package/ts/proxies/smart-proxy/index.ts +6 -13
  103. package/ts/proxies/smart-proxy/models/interfaces.ts +6 -5
  104. package/ts/proxies/smart-proxy/route-preprocessor.ts +122 -0
  105. package/ts/proxies/smart-proxy/rust-binary-locator.ts +112 -0
  106. package/ts/proxies/smart-proxy/rust-metrics-adapter.ts +161 -0
  107. package/ts/proxies/smart-proxy/rust-proxy-bridge.ts +310 -0
  108. package/ts/proxies/smart-proxy/smart-proxy.ts +282 -800
  109. package/ts/proxies/smart-proxy/socket-handler-server.ts +279 -0
  110. package/ts/proxies/smart-proxy/utils/index.ts +3 -5
  111. package/ts/proxies/smart-proxy/utils/route-helpers/api-helpers.ts +144 -0
  112. package/ts/proxies/smart-proxy/utils/route-helpers/dynamic-helpers.ts +124 -0
  113. package/ts/proxies/smart-proxy/utils/route-helpers/http-helpers.ts +40 -0
  114. package/ts/proxies/smart-proxy/utils/route-helpers/https-helpers.ts +163 -0
  115. package/ts/proxies/smart-proxy/utils/route-helpers/index.ts +62 -0
  116. package/ts/proxies/smart-proxy/utils/route-helpers/load-balancer-helpers.ts +154 -0
  117. package/ts/proxies/smart-proxy/utils/route-helpers/nftables-helpers.ts +202 -0
  118. package/ts/proxies/smart-proxy/utils/route-helpers/security-helpers.ts +96 -0
  119. package/ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.ts +337 -0
  120. package/ts/proxies/smart-proxy/utils/route-helpers/websocket-helpers.ts +98 -0
  121. package/ts/proxies/smart-proxy/utils/route-helpers.ts +5 -1302
  122. package/ts/proxies/smart-proxy/utils/route-utils.ts +1 -1
  123. package/ts/proxies/smart-proxy/utils/route-validator.ts +274 -4
  124. package/ts/routing/index.ts +2 -2
  125. package/ts/routing/models/http-types.ts +147 -4
  126. package/ts/proxies/http-proxy/certificate-manager.ts +0 -244
  127. package/ts/proxies/http-proxy/connection-pool.ts +0 -228
  128. package/ts/proxies/http-proxy/context-creator.ts +0 -145
  129. package/ts/proxies/http-proxy/function-cache.ts +0 -279
  130. package/ts/proxies/http-proxy/handlers/index.ts +0 -5
  131. package/ts/proxies/http-proxy/http-proxy.ts +0 -675
  132. package/ts/proxies/http-proxy/http-request-handler.ts +0 -331
  133. package/ts/proxies/http-proxy/http2-request-handler.ts +0 -255
  134. package/ts/proxies/http-proxy/index.ts +0 -13
  135. package/ts/proxies/http-proxy/models/http-types.ts +0 -148
  136. package/ts/proxies/http-proxy/models/index.ts +0 -5
  137. package/ts/proxies/http-proxy/models/types.ts +0 -125
  138. package/ts/proxies/http-proxy/request-handler.ts +0 -878
  139. package/ts/proxies/http-proxy/security-manager.ts +0 -433
  140. package/ts/proxies/http-proxy/websocket-handler.ts +0 -581
  141. package/ts/proxies/smart-proxy/acme-state-manager.ts +0 -112
  142. package/ts/proxies/smart-proxy/cert-store.ts +0 -92
  143. package/ts/proxies/smart-proxy/certificate-manager.ts +0 -894
  144. package/ts/proxies/smart-proxy/connection-manager.ts +0 -796
  145. package/ts/proxies/smart-proxy/http-proxy-bridge.ts +0 -187
  146. package/ts/proxies/smart-proxy/metrics-collector.ts +0 -453
  147. package/ts/proxies/smart-proxy/nftables-manager.ts +0 -271
  148. package/ts/proxies/smart-proxy/port-manager.ts +0 -358
  149. package/ts/proxies/smart-proxy/route-connection-handler.ts +0 -1640
  150. package/ts/proxies/smart-proxy/route-orchestrator.ts +0 -297
  151. package/ts/proxies/smart-proxy/security-manager.ts +0 -257
  152. package/ts/proxies/smart-proxy/throughput-tracker.ts +0 -138
  153. package/ts/proxies/smart-proxy/timeout-manager.ts +0 -196
  154. package/ts/proxies/smart-proxy/tls-manager.ts +0 -207
  155. package/ts/proxies/smart-proxy/utils/route-validators.ts +0 -283
@@ -3,6 +3,6 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartproxy',
6
- version: '21.1.7',
6
+ version: '22.6.0',
7
7
  description: 'A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.'
8
8
  }
@@ -148,31 +148,66 @@ export class SharedSecurityManager {
148
148
 
149
149
  /**
150
150
  * Validate IP against rate limits and connection limits
151
- *
151
+ *
152
152
  * @param ip - The IP address to validate
153
153
  * @returns Result with allowed status and reason if blocked
154
154
  */
155
155
  public validateIP(ip: string): IIpValidationResult {
156
156
  // Check connection count limit
157
157
  const connectionResult = checkMaxConnections(
158
- ip,
159
- this.connectionsByIP,
158
+ ip,
159
+ this.connectionsByIP,
160
160
  this.maxConnectionsPerIP
161
161
  );
162
162
  if (!connectionResult.allowed) {
163
163
  return connectionResult;
164
164
  }
165
-
165
+
166
166
  // Check connection rate limit
167
167
  const rateResult = checkConnectionRate(
168
- ip,
169
- this.connectionsByIP,
168
+ ip,
169
+ this.connectionsByIP,
170
170
  this.connectionRateLimitPerMinute
171
171
  );
172
172
  if (!rateResult.allowed) {
173
173
  return rateResult;
174
174
  }
175
-
175
+
176
+ return { allowed: true };
177
+ }
178
+
179
+ /**
180
+ * Atomically validate an IP and track the connection if allowed.
181
+ * This prevents race conditions where concurrent connections could bypass per-IP limits.
182
+ *
183
+ * @param ip - The IP address to validate
184
+ * @param connectionId - The connection ID to track if validation passes
185
+ * @returns Object with validation result and reason
186
+ */
187
+ public validateAndTrackIP(ip: string, connectionId: string): IIpValidationResult {
188
+ // Check connection count limit BEFORE tracking
189
+ const connectionResult = checkMaxConnections(
190
+ ip,
191
+ this.connectionsByIP,
192
+ this.maxConnectionsPerIP
193
+ );
194
+ if (!connectionResult.allowed) {
195
+ return connectionResult;
196
+ }
197
+
198
+ // Check connection rate limit
199
+ const rateResult = checkConnectionRate(
200
+ ip,
201
+ this.connectionsByIP,
202
+ this.connectionRateLimitPerMinute
203
+ );
204
+ if (!rateResult.allowed) {
205
+ return rateResult;
206
+ }
207
+
208
+ // Validation passed - immediately track to prevent race conditions
209
+ this.trackConnectionByIP(ip, connectionId);
210
+
176
211
  return { allowed: true };
177
212
  }
178
213
 
@@ -304,7 +339,7 @@ export class SharedSecurityManager {
304
339
 
305
340
  /**
306
341
  * Validate HTTP Basic Authentication
307
- *
342
+ *
308
343
  * @param route - The route to check
309
344
  * @param authHeader - The Authorization header
310
345
  * @returns Whether authentication is valid
@@ -314,26 +349,76 @@ export class SharedSecurityManager {
314
349
  if (!route.security?.basicAuth?.enabled) {
315
350
  return true;
316
351
  }
317
-
352
+
318
353
  // No auth header means auth failed
319
354
  if (!authHeader) {
320
355
  return false;
321
356
  }
322
-
357
+
323
358
  // Parse auth header
324
359
  const credentials = parseBasicAuthHeader(authHeader);
325
360
  if (!credentials) {
326
361
  return false;
327
362
  }
328
-
363
+
329
364
  // Check credentials against configured users
330
365
  const { username, password } = credentials;
331
366
  const users = route.security.basicAuth.users;
332
-
333
- return users.some(user =>
367
+
368
+ return users.some(user =>
334
369
  user.username === username && user.password === password
335
370
  );
336
371
  }
372
+
373
+ /**
374
+ * Verify a JWT token against route configuration
375
+ *
376
+ * @param route - The route to verify the token for
377
+ * @param token - The JWT token to verify
378
+ * @returns True if the token is valid, false otherwise
379
+ */
380
+ public verifyJwtToken(route: IRouteConfig, token: string): boolean {
381
+ if (!route.security?.jwtAuth?.enabled) {
382
+ return true;
383
+ }
384
+
385
+ try {
386
+ const jwtAuth = route.security.jwtAuth;
387
+
388
+ // Verify structure (header.payload.signature)
389
+ const parts = token.split('.');
390
+ if (parts.length !== 3) {
391
+ return false;
392
+ }
393
+
394
+ // Decode payload
395
+ const payload = JSON.parse(Buffer.from(parts[1], 'base64').toString());
396
+
397
+ // Check expiration
398
+ if (payload.exp && payload.exp < Math.floor(Date.now() / 1000)) {
399
+ return false;
400
+ }
401
+
402
+ // Check issuer
403
+ if (jwtAuth.issuer && payload.iss !== jwtAuth.issuer) {
404
+ return false;
405
+ }
406
+
407
+ // Check audience
408
+ if (jwtAuth.audience && payload.aud !== jwtAuth.audience) {
409
+ return false;
410
+ }
411
+
412
+ // Note: In a real implementation, you'd also verify the signature
413
+ // using the secret and algorithm specified in jwtAuth.
414
+ // This requires a proper JWT library for cryptographic verification.
415
+
416
+ return true;
417
+ } catch (err) {
418
+ this.logger?.error?.(`Error verifying JWT: ${err}`);
419
+ return false;
420
+ }
421
+ }
337
422
 
338
423
  /**
339
424
  * Clean up caches to prevent memory leaks
package/ts/index.ts CHANGED
@@ -5,15 +5,10 @@
5
5
  // NFTables proxy exports
6
6
  export * from './proxies/nftables-proxy/index.js';
7
7
 
8
- // Export HttpProxy elements
9
- export { HttpProxy, CertificateManager, ConnectionPool, RequestHandler, WebSocketHandler } from './proxies/http-proxy/index.js';
10
- export type { IMetricsTracker, MetricsTracker } from './proxies/http-proxy/index.js';
11
- export type { IHttpProxyOptions, ICertificateEntry, ILogger } from './proxies/http-proxy/models/types.js';
12
- export { SharedRouteManager as HttpProxyRouteManager } from './core/routing/route-manager.js';
13
-
14
- // Export SmartProxy elements selectively to avoid RouteManager ambiguity
15
- export { SmartProxy, ConnectionManager, SecurityManager, TimeoutManager, TlsManager, HttpProxyBridge, RouteConnectionHandler, SmartCertManager } from './proxies/smart-proxy/index.js';
8
+ // Export SmartProxy elements
9
+ export { SmartProxy } from './proxies/smart-proxy/index.js';
16
10
  export { SharedRouteManager as RouteManager } from './core/routing/route-manager.js';
11
+
17
12
  // Export smart-proxy models
18
13
  export type { ISmartProxyOptions, IConnectionRecord, IRouteConfig, IRouteMatch, IRouteAction, IRouteTls, IRouteContext } from './proxies/smart-proxy/models/index.js';
19
14
  export type { TSmartProxyCertProvisionObject } from './proxies/smart-proxy/models/interfaces.js';
@@ -22,8 +17,6 @@ export * from './proxies/smart-proxy/utils/index.js';
22
17
  // Original: export * from './smartproxy/classes.pp.snihandler.js'
23
18
  // Now we export from the new module
24
19
  export { SniHandler } from './tls/sni/sni-handler.js';
25
- // Original: export * from './smartproxy/classes.pp.interfaces.js'
26
- // Now we export from the new module (selectively to avoid conflicts)
27
20
 
28
21
  // Core types and utilities
29
22
  export * from './core/models/common-types.js';
@@ -32,8 +25,7 @@ export * from './core/models/common-types.js';
32
25
  export type { IAcmeOptions } from './proxies/smart-proxy/models/interfaces.js';
33
26
 
34
27
  // Modular exports for new architecture
35
- // Certificate module has been removed - use SmartCertManager instead
36
28
  export * as tls from './tls/index.js';
37
29
  export * as routing from './routing/index.js';
38
30
  export * as detection from './detection/index.js';
39
- export * as protocols from './protocols/index.js';
31
+ export * as protocols from './protocols/index.js';
@@ -49,6 +49,10 @@ export class FragmentHandler {
49
49
  () => this.cleanup(),
50
50
  options.cleanupInterval
51
51
  );
52
+ // Don't let this timer prevent process exit
53
+ if (this.cleanupTimer.unref) {
54
+ this.cleanupTimer.unref();
55
+ }
52
56
  }
53
57
  }
54
58
 
@@ -2,16 +2,8 @@
2
2
  * Proxy implementations module
3
3
  */
4
4
 
5
- // Export HttpProxy with selective imports to avoid conflicts
6
- export { HttpProxy, CertificateManager, ConnectionPool, RequestHandler, WebSocketHandler } from './http-proxy/index.js';
7
- export type { IMetricsTracker, MetricsTracker } from './http-proxy/index.js';
8
- // Export http-proxy models except IAcmeOptions
9
- export type { IHttpProxyOptions, ICertificateEntry, ILogger } from './http-proxy/models/types.js';
10
- // RouteManager has been unified - use SharedRouteManager from core/routing
11
- export { SharedRouteManager as HttpProxyRouteManager } from '../core/routing/route-manager.js';
12
-
13
5
  // Export SmartProxy with selective imports to avoid conflicts
14
- export { SmartProxy, ConnectionManager, SecurityManager, TimeoutManager, TlsManager, HttpProxyBridge, RouteConnectionHandler } from './smart-proxy/index.js';
6
+ export { SmartProxy } from './smart-proxy/index.js';
15
7
  export { SharedRouteManager as SmartProxyRouteManager } from '../core/routing/route-manager.js';
16
8
  export * from './smart-proxy/utils/index.js';
17
9
  // Export smart-proxy models except IAcmeOptions
@@ -3,3 +3,4 @@
3
3
  */
4
4
  export * from './nftables-proxy.js';
5
5
  export * from './models/index.js';
6
+ export * from './utils/index.js';