@push.rocks/smartproxy 18.1.0 → 19.0.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 (98) hide show
  1. package/dist_ts/00_commitinfo_data.js +1 -1
  2. package/dist_ts/certificate/certificate-manager.d.ts +150 -0
  3. package/dist_ts/certificate/certificate-manager.js +505 -0
  4. package/dist_ts/certificate/events/simplified-events.d.ts +56 -0
  5. package/dist_ts/certificate/events/simplified-events.js +13 -0
  6. package/dist_ts/certificate/models/certificate-errors.d.ts +69 -0
  7. package/dist_ts/certificate/models/certificate-errors.js +141 -0
  8. package/dist_ts/certificate/models/certificate-strategy.d.ts +60 -0
  9. package/dist_ts/certificate/models/certificate-strategy.js +73 -0
  10. package/dist_ts/certificate/simplified-certificate-manager.d.ts +150 -0
  11. package/dist_ts/certificate/simplified-certificate-manager.js +501 -0
  12. package/dist_ts/common/eventUtils.d.ts +1 -2
  13. package/dist_ts/common/eventUtils.js +2 -1
  14. package/dist_ts/core/models/common-types.d.ts +1 -1
  15. package/dist_ts/core/models/common-types.js +1 -1
  16. package/dist_ts/core/utils/event-utils.d.ts +9 -9
  17. package/dist_ts/core/utils/event-utils.js +6 -14
  18. package/dist_ts/http/index.d.ts +1 -9
  19. package/dist_ts/http/index.js +5 -11
  20. package/dist_ts/http/models/http-types.d.ts +13 -1
  21. package/dist_ts/http/models/http-types.js +1 -1
  22. package/dist_ts/index.d.ts +4 -6
  23. package/dist_ts/index.js +4 -10
  24. package/dist_ts/plugins.d.ts +3 -1
  25. package/dist_ts/plugins.js +4 -2
  26. package/dist_ts/proxies/index.d.ts +3 -2
  27. package/dist_ts/proxies/index.js +4 -5
  28. package/dist_ts/proxies/network-proxy/certificate-manager.d.ts +31 -49
  29. package/dist_ts/proxies/network-proxy/certificate-manager.js +77 -374
  30. package/dist_ts/proxies/network-proxy/models/types.d.ts +12 -1
  31. package/dist_ts/proxies/network-proxy/models/types.js +1 -1
  32. package/dist_ts/proxies/network-proxy/network-proxy.d.ts +2 -7
  33. package/dist_ts/proxies/network-proxy/network-proxy.js +12 -19
  34. package/dist_ts/proxies/network-proxy/simplified-certificate-bridge.d.ts +48 -0
  35. package/dist_ts/proxies/network-proxy/simplified-certificate-bridge.js +76 -0
  36. package/dist_ts/proxies/network-proxy/websocket-handler.js +21 -7
  37. package/dist_ts/proxies/smart-proxy/cert-store.d.ts +10 -0
  38. package/dist_ts/proxies/smart-proxy/cert-store.js +70 -0
  39. package/dist_ts/proxies/smart-proxy/certificate-manager.d.ts +116 -0
  40. package/dist_ts/proxies/smart-proxy/certificate-manager.js +401 -0
  41. package/dist_ts/proxies/smart-proxy/legacy-smart-proxy.d.ts +168 -0
  42. package/dist_ts/proxies/smart-proxy/legacy-smart-proxy.js +642 -0
  43. package/dist_ts/proxies/smart-proxy/models/index.d.ts +1 -1
  44. package/dist_ts/proxies/smart-proxy/models/index.js +1 -5
  45. package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +13 -1
  46. package/dist_ts/proxies/smart-proxy/models/route-types.d.ts +26 -0
  47. package/dist_ts/proxies/smart-proxy/models/route-types.js +1 -1
  48. package/dist_ts/proxies/smart-proxy/models/simplified-smartproxy-config.d.ts +65 -0
  49. package/dist_ts/proxies/smart-proxy/models/simplified-smartproxy-config.js +31 -0
  50. package/dist_ts/proxies/smart-proxy/models/smartproxy-options.d.ts +102 -0
  51. package/dist_ts/proxies/smart-proxy/models/smartproxy-options.js +73 -0
  52. package/dist_ts/proxies/smart-proxy/network-proxy-bridge.d.ts +10 -44
  53. package/dist_ts/proxies/smart-proxy/network-proxy-bridge.js +66 -202
  54. package/dist_ts/proxies/smart-proxy/route-connection-handler.d.ts +4 -0
  55. package/dist_ts/proxies/smart-proxy/route-connection-handler.js +62 -2
  56. package/dist_ts/proxies/smart-proxy/simplified-smart-proxy.d.ts +41 -0
  57. package/dist_ts/proxies/smart-proxy/simplified-smart-proxy.js +132 -0
  58. package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +18 -13
  59. package/dist_ts/proxies/smart-proxy/smart-proxy.js +80 -198
  60. package/package.json +5 -3
  61. package/readme.md +13 -5
  62. package/readme.plan.md +1422 -617
  63. package/ts/00_commitinfo_data.ts +1 -1
  64. package/ts/common/eventUtils.ts +2 -2
  65. package/ts/core/models/common-types.ts +1 -1
  66. package/ts/core/utils/event-utils.ts +12 -21
  67. package/ts/http/index.ts +5 -12
  68. package/ts/http/models/http-types.ts +8 -4
  69. package/ts/index.ts +11 -14
  70. package/ts/plugins.ts +4 -1
  71. package/ts/proxies/index.ts +7 -4
  72. package/ts/proxies/network-proxy/certificate-manager.ts +92 -417
  73. package/ts/proxies/network-proxy/models/types.ts +14 -2
  74. package/ts/proxies/network-proxy/network-proxy.ts +13 -19
  75. package/ts/proxies/network-proxy/websocket-handler.ts +18 -6
  76. package/ts/proxies/smart-proxy/cert-store.ts +86 -0
  77. package/ts/proxies/smart-proxy/certificate-manager.ts +506 -0
  78. package/ts/proxies/smart-proxy/models/index.ts +2 -1
  79. package/ts/proxies/smart-proxy/models/interfaces.ts +14 -1
  80. package/ts/proxies/smart-proxy/models/route-types.ts +34 -4
  81. package/ts/proxies/smart-proxy/network-proxy-bridge.ts +86 -239
  82. package/ts/proxies/smart-proxy/route-connection-handler.ts +74 -1
  83. package/ts/proxies/smart-proxy/smart-proxy.ts +106 -224
  84. package/ts/certificate/acme/acme-factory.ts +0 -48
  85. package/ts/certificate/acme/challenge-handler.ts +0 -110
  86. package/ts/certificate/acme/index.ts +0 -3
  87. package/ts/certificate/events/certificate-events.ts +0 -36
  88. package/ts/certificate/index.ts +0 -75
  89. package/ts/certificate/models/certificate-types.ts +0 -109
  90. package/ts/certificate/providers/cert-provisioner.ts +0 -519
  91. package/ts/certificate/providers/index.ts +0 -3
  92. package/ts/certificate/storage/file-storage.ts +0 -234
  93. package/ts/certificate/storage/index.ts +0 -3
  94. package/ts/certificate/utils/certificate-helpers.ts +0 -50
  95. package/ts/http/port80/acme-interfaces.ts +0 -169
  96. package/ts/http/port80/challenge-responder.ts +0 -246
  97. package/ts/http/port80/index.ts +0 -13
  98. package/ts/http/port80/port80-handler.ts +0 -728
@@ -18,7 +18,6 @@ import { RequestHandler, type IMetricsTracker } from './request-handler.js';
18
18
  import { WebSocketHandler } from './websocket-handler.js';
19
19
  import { ProxyRouter } from '../../http/router/index.js';
20
20
  import { RouteRouter } from '../../http/router/route-router.js';
21
- import { Port80Handler } from '../../http/port80/port80-handler.js';
22
21
  import { FunctionCache } from './function-cache.js';
23
22
 
24
23
  /**
@@ -221,15 +220,10 @@ export class NetworkProxy implements IMetricsTracker {
221
220
  }
222
221
 
223
222
  /**
224
- * Sets an external Port80Handler for certificate management
225
- * This allows the NetworkProxy to use a centrally managed Port80Handler
226
- * instead of creating its own
227
- *
228
- * @param handler The Port80Handler instance to use
223
+ * @deprecated Use SmartCertManager instead
229
224
  */
230
- public setExternalPort80Handler(handler: Port80Handler): void {
231
- // Connect it to the certificate manager
232
- this.certificateManager.setExternalPort80Handler(handler);
225
+ public setExternalPort80Handler(handler: any): void {
226
+ this.logger.warn('Port80Handler is deprecated - use SmartCertManager instead');
233
227
  }
234
228
 
235
229
  /**
@@ -238,10 +232,7 @@ export class NetworkProxy implements IMetricsTracker {
238
232
  public async start(): Promise<void> {
239
233
  this.startTime = Date.now();
240
234
 
241
- // Initialize Port80Handler if enabled and not using external handler
242
- if (this.options.acme?.enabled && !this.options.useExternalPort80Handler) {
243
- await this.certificateManager.initializePort80Handler();
244
- }
235
+ // Certificate management is now handled by SmartCertManager
245
236
 
246
237
  // Create HTTP/2 server with HTTP/1 fallback
247
238
  this.httpsServer = plugins.http2.createSecureServer(
@@ -385,7 +376,7 @@ export class NetworkProxy implements IMetricsTracker {
385
376
 
386
377
  // Directly update the certificate manager with the new routes
387
378
  // This will extract domains and handle certificate provisioning
388
- this.certificateManager.updateRouteConfigs(routes);
379
+ this.certificateManager.updateRoutes(routes);
389
380
 
390
381
  // Collect all domains and certificates for configuration
391
382
  const currentHostnames = new Set<string>();
@@ -425,7 +416,7 @@ export class NetworkProxy implements IMetricsTracker {
425
416
  // Update certificate cache with any static certificates
426
417
  for (const [domain, certData] of certificateUpdates.entries()) {
427
418
  try {
428
- this.certificateManager.updateCertificateCache(
419
+ this.certificateManager.updateCertificate(
429
420
  domain,
430
421
  certData.cert,
431
422
  certData.key
@@ -500,6 +491,9 @@ export class NetworkProxy implements IMetricsTracker {
500
491
  this.logger.warn('Router has no recognized configuration method');
501
492
  }
502
493
 
494
+ // Update WebSocket handler with new routes
495
+ this.webSocketHandler.setRoutes(routes);
496
+
503
497
  this.logger.info(`Route configuration updated with ${routes.length} routes and ${legacyConfigs.length} proxy configs`);
504
498
  }
505
499
 
@@ -544,8 +538,7 @@ export class NetworkProxy implements IMetricsTracker {
544
538
  // Close all connection pool connections
545
539
  this.connectionPool.closeAllConnections();
546
540
 
547
- // Stop Port80Handler if internally managed
548
- await this.certificateManager.stopPort80Handler();
541
+ // Certificate management cleanup is handled by SmartCertManager
549
542
 
550
543
  // Close the HTTPS server
551
544
  return new Promise((resolve) => {
@@ -563,7 +556,8 @@ export class NetworkProxy implements IMetricsTracker {
563
556
  * @returns A promise that resolves when the request is submitted (not when the certificate is issued)
564
557
  */
565
558
  public async requestCertificate(domain: string): Promise<boolean> {
566
- return this.certificateManager.requestCertificate(domain);
559
+ this.logger.warn('requestCertificate is deprecated - use SmartCertManager instead');
560
+ return false;
567
561
  }
568
562
 
569
563
  /**
@@ -584,7 +578,7 @@ export class NetworkProxy implements IMetricsTracker {
584
578
  expiryDate?: Date
585
579
  ): void {
586
580
  this.logger.info(`Updating certificate for ${domain}`);
587
- this.certificateManager.updateCertificateCache(domain, certificate, privateKey, expiryDate);
581
+ this.certificateManager.updateCertificate(domain, certificate, privateKey);
588
582
  }
589
583
 
590
584
  /**
@@ -419,9 +419,15 @@ export class WebSocketHandler {
419
419
  wsIncoming.on('close', (code, reason) => {
420
420
  this.logger.debug(`WebSocket client connection closed: ${code} ${reason}`);
421
421
  if (wsOutgoing.readyState === wsOutgoing.OPEN) {
422
- const validCode = code || 1000;
423
- const reasonString = toBuffer(reason).toString();
424
- wsOutgoing.close(validCode, reasonString);
422
+ // Ensure code is a valid WebSocket close code number
423
+ const validCode = typeof code === 'number' && code >= 1000 && code <= 4999 ? code : 1000;
424
+ try {
425
+ const reasonString = reason ? toBuffer(reason).toString() : '';
426
+ wsOutgoing.close(validCode, reasonString);
427
+ } catch (err) {
428
+ this.logger.error('Error closing wsOutgoing:', err);
429
+ wsOutgoing.close(validCode);
430
+ }
425
431
  }
426
432
 
427
433
  // Clean up timers
@@ -432,9 +438,15 @@ export class WebSocketHandler {
432
438
  wsOutgoing.on('close', (code, reason) => {
433
439
  this.logger.debug(`WebSocket target connection closed: ${code} ${reason}`);
434
440
  if (wsIncoming.readyState === wsIncoming.OPEN) {
435
- const validCode = code || 1000;
436
- const reasonString = toBuffer(reason).toString();
437
- wsIncoming.close(validCode, reasonString);
441
+ // Ensure code is a valid WebSocket close code number
442
+ const validCode = typeof code === 'number' && code >= 1000 && code <= 4999 ? code : 1000;
443
+ try {
444
+ const reasonString = reason ? toBuffer(reason).toString() : '';
445
+ wsIncoming.close(validCode, reasonString);
446
+ } catch (err) {
447
+ this.logger.error('Error closing wsIncoming:', err);
448
+ wsIncoming.close(validCode);
449
+ }
438
450
  }
439
451
 
440
452
  // Clean up timers
@@ -0,0 +1,86 @@
1
+ import * as plugins from '../../plugins.js';
2
+ import type { ICertificateData } from './certificate-manager.js';
3
+
4
+ export class CertStore {
5
+ constructor(private certDir: string) {}
6
+
7
+ public async initialize(): Promise<void> {
8
+ await plugins.smartfile.fs.ensureDirSync(this.certDir);
9
+ }
10
+
11
+ public async getCertificate(routeName: string): Promise<ICertificateData | null> {
12
+ const certPath = this.getCertPath(routeName);
13
+ const metaPath = `${certPath}/meta.json`;
14
+
15
+ if (!await plugins.smartfile.fs.fileExistsSync(metaPath)) {
16
+ return null;
17
+ }
18
+
19
+ try {
20
+ const metaFile = await plugins.smartfile.SmartFile.fromFilePath(metaPath);
21
+ const meta = JSON.parse(metaFile.contents.toString());
22
+
23
+ const certFile = await plugins.smartfile.SmartFile.fromFilePath(`${certPath}/cert.pem`);
24
+ const cert = certFile.contents.toString();
25
+
26
+ const keyFile = await plugins.smartfile.SmartFile.fromFilePath(`${certPath}/key.pem`);
27
+ const key = keyFile.contents.toString();
28
+
29
+ let ca: string | undefined;
30
+ const caPath = `${certPath}/ca.pem`;
31
+ if (await plugins.smartfile.fs.fileExistsSync(caPath)) {
32
+ const caFile = await plugins.smartfile.SmartFile.fromFilePath(caPath);
33
+ ca = caFile.contents.toString();
34
+ }
35
+
36
+ return {
37
+ cert,
38
+ key,
39
+ ca,
40
+ expiryDate: new Date(meta.expiryDate),
41
+ issueDate: new Date(meta.issueDate)
42
+ };
43
+ } catch (error) {
44
+ console.error(`Failed to load certificate for ${routeName}: ${error}`);
45
+ return null;
46
+ }
47
+ }
48
+
49
+ public async saveCertificate(
50
+ routeName: string,
51
+ certData: ICertificateData
52
+ ): Promise<void> {
53
+ const certPath = this.getCertPath(routeName);
54
+ await plugins.smartfile.fs.ensureDirSync(certPath);
55
+
56
+ // Save certificate files
57
+ await plugins.smartfile.memory.toFs(certData.cert, `${certPath}/cert.pem`);
58
+ await plugins.smartfile.memory.toFs(certData.key, `${certPath}/key.pem`);
59
+
60
+ if (certData.ca) {
61
+ await plugins.smartfile.memory.toFs(certData.ca, `${certPath}/ca.pem`);
62
+ }
63
+
64
+ // Save metadata
65
+ const meta = {
66
+ expiryDate: certData.expiryDate.toISOString(),
67
+ issueDate: certData.issueDate.toISOString(),
68
+ savedAt: new Date().toISOString()
69
+ };
70
+
71
+ await plugins.smartfile.memory.toFs(JSON.stringify(meta, null, 2), `${certPath}/meta.json`);
72
+ }
73
+
74
+ public async deleteCertificate(routeName: string): Promise<void> {
75
+ const certPath = this.getCertPath(routeName);
76
+ if (await plugins.smartfile.fs.fileExistsSync(certPath)) {
77
+ await plugins.smartfile.fs.removeManySync([certPath]);
78
+ }
79
+ }
80
+
81
+ private getCertPath(routeName: string): string {
82
+ // Sanitize route name for filesystem
83
+ const safeName = routeName.replace(/[^a-zA-Z0-9-_]/g, '_');
84
+ return `${this.certDir}/${safeName}`;
85
+ }
86
+ }