@push.rocks/smartproxy 15.0.2 → 16.0.3

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 (160) hide show
  1. package/dist_ts/00_commitinfo_data.js +1 -1
  2. package/dist_ts/certificate/index.d.ts +10 -4
  3. package/dist_ts/certificate/index.js +5 -7
  4. package/dist_ts/certificate/models/certificate-types.d.ts +35 -15
  5. package/dist_ts/certificate/providers/cert-provisioner.d.ts +41 -15
  6. package/dist_ts/certificate/providers/cert-provisioner.js +201 -41
  7. package/dist_ts/core/models/index.d.ts +2 -0
  8. package/dist_ts/core/models/index.js +3 -1
  9. package/dist_ts/core/models/route-context.d.ts +62 -0
  10. package/dist_ts/core/models/route-context.js +43 -0
  11. package/dist_ts/core/models/socket-augmentation.d.ts +12 -0
  12. package/dist_ts/core/models/socket-augmentation.js +18 -0
  13. package/dist_ts/core/utils/event-system.d.ts +200 -0
  14. package/dist_ts/core/utils/event-system.js +224 -0
  15. package/dist_ts/core/utils/index.d.ts +7 -0
  16. package/dist_ts/core/utils/index.js +8 -1
  17. package/dist_ts/core/utils/route-manager.d.ts +118 -0
  18. package/dist_ts/core/utils/route-manager.js +383 -0
  19. package/dist_ts/core/utils/route-utils.d.ts +94 -0
  20. package/dist_ts/core/utils/route-utils.js +264 -0
  21. package/dist_ts/core/utils/security-utils.d.ts +111 -0
  22. package/dist_ts/core/utils/security-utils.js +212 -0
  23. package/dist_ts/core/utils/shared-security-manager.d.ts +110 -0
  24. package/dist_ts/core/utils/shared-security-manager.js +252 -0
  25. package/dist_ts/core/utils/template-utils.d.ts +37 -0
  26. package/dist_ts/core/utils/template-utils.js +104 -0
  27. package/dist_ts/core/utils/websocket-utils.d.ts +23 -0
  28. package/dist_ts/core/utils/websocket-utils.js +86 -0
  29. package/dist_ts/forwarding/config/forwarding-types.d.ts +40 -76
  30. package/dist_ts/forwarding/config/forwarding-types.js +19 -18
  31. package/dist_ts/forwarding/config/index.d.ts +4 -2
  32. package/dist_ts/forwarding/config/index.js +5 -3
  33. package/dist_ts/forwarding/handlers/base-handler.js +3 -1
  34. package/dist_ts/forwarding/index.d.ts +5 -6
  35. package/dist_ts/forwarding/index.js +3 -3
  36. package/dist_ts/http/models/http-types.js +1 -1
  37. package/dist_ts/http/port80/acme-interfaces.d.ts +30 -0
  38. package/dist_ts/http/port80/acme-interfaces.js +46 -1
  39. package/dist_ts/http/port80/port80-handler.d.ts +17 -2
  40. package/dist_ts/http/port80/port80-handler.js +49 -11
  41. package/dist_ts/http/router/index.d.ts +5 -1
  42. package/dist_ts/http/router/index.js +4 -2
  43. package/dist_ts/http/router/route-router.d.ts +108 -0
  44. package/dist_ts/http/router/route-router.js +393 -0
  45. package/dist_ts/index.d.ts +8 -2
  46. package/dist_ts/index.js +10 -3
  47. package/dist_ts/proxies/index.d.ts +7 -2
  48. package/dist_ts/proxies/index.js +10 -4
  49. package/dist_ts/proxies/network-proxy/certificate-manager.d.ts +21 -0
  50. package/dist_ts/proxies/network-proxy/certificate-manager.js +92 -1
  51. package/dist_ts/proxies/network-proxy/context-creator.d.ts +34 -0
  52. package/dist_ts/proxies/network-proxy/context-creator.js +108 -0
  53. package/dist_ts/proxies/network-proxy/function-cache.d.ts +90 -0
  54. package/dist_ts/proxies/network-proxy/function-cache.js +198 -0
  55. package/dist_ts/proxies/network-proxy/http-request-handler.d.ts +40 -0
  56. package/dist_ts/proxies/network-proxy/http-request-handler.js +256 -0
  57. package/dist_ts/proxies/network-proxy/http2-request-handler.d.ts +24 -0
  58. package/dist_ts/proxies/network-proxy/http2-request-handler.js +201 -0
  59. package/dist_ts/proxies/network-proxy/models/types.d.ts +73 -1
  60. package/dist_ts/proxies/network-proxy/models/types.js +242 -1
  61. package/dist_ts/proxies/network-proxy/network-proxy.d.ts +23 -20
  62. package/dist_ts/proxies/network-proxy/network-proxy.js +147 -60
  63. package/dist_ts/proxies/network-proxy/request-handler.d.ts +38 -5
  64. package/dist_ts/proxies/network-proxy/request-handler.js +584 -198
  65. package/dist_ts/proxies/network-proxy/security-manager.d.ts +65 -0
  66. package/dist_ts/proxies/network-proxy/security-manager.js +255 -0
  67. package/dist_ts/proxies/network-proxy/websocket-handler.d.ts +13 -2
  68. package/dist_ts/proxies/network-proxy/websocket-handler.js +238 -20
  69. package/dist_ts/proxies/smart-proxy/index.d.ts +1 -1
  70. package/dist_ts/proxies/smart-proxy/index.js +3 -3
  71. package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +5 -66
  72. package/dist_ts/proxies/smart-proxy/models/interfaces.js +5 -4
  73. package/dist_ts/proxies/smart-proxy/models/route-types.d.ts +173 -6
  74. package/dist_ts/proxies/smart-proxy/network-proxy-bridge.d.ts +20 -7
  75. package/dist_ts/proxies/smart-proxy/network-proxy-bridge.js +49 -108
  76. package/dist_ts/proxies/smart-proxy/port-manager.d.ts +81 -0
  77. package/dist_ts/proxies/smart-proxy/port-manager.js +166 -0
  78. package/dist_ts/proxies/smart-proxy/route-connection-handler.d.ts +7 -5
  79. package/dist_ts/proxies/smart-proxy/route-connection-handler.js +155 -160
  80. package/dist_ts/proxies/smart-proxy/route-helpers/index.d.ts +9 -0
  81. package/dist_ts/proxies/smart-proxy/route-helpers/index.js +11 -0
  82. package/dist_ts/proxies/smart-proxy/route-helpers.d.ts +5 -125
  83. package/dist_ts/proxies/smart-proxy/route-helpers.js +8 -195
  84. package/dist_ts/proxies/smart-proxy/route-manager.d.ts +14 -11
  85. package/dist_ts/proxies/smart-proxy/route-manager.js +81 -124
  86. package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +101 -12
  87. package/dist_ts/proxies/smart-proxy/smart-proxy.js +178 -306
  88. package/dist_ts/proxies/smart-proxy/timeout-manager.js +3 -3
  89. package/dist_ts/proxies/smart-proxy/utils/index.d.ts +12 -0
  90. package/dist_ts/proxies/smart-proxy/utils/index.js +19 -0
  91. package/dist_ts/proxies/smart-proxy/utils/route-helpers.d.ts +240 -0
  92. package/dist_ts/proxies/smart-proxy/utils/route-helpers.js +451 -0
  93. package/dist_ts/proxies/smart-proxy/utils/route-migration-utils.d.ts +51 -0
  94. package/dist_ts/proxies/smart-proxy/utils/route-migration-utils.js +124 -0
  95. package/dist_ts/proxies/smart-proxy/utils/route-patterns.d.ts +131 -0
  96. package/dist_ts/proxies/smart-proxy/utils/route-patterns.js +217 -0
  97. package/dist_ts/proxies/smart-proxy/utils/route-utils.d.ts +79 -0
  98. package/dist_ts/proxies/smart-proxy/utils/route-utils.js +266 -0
  99. package/dist_ts/proxies/smart-proxy/utils/route-validators.d.ts +73 -0
  100. package/dist_ts/proxies/smart-proxy/utils/route-validators.js +264 -0
  101. package/package.json +1 -1
  102. package/readme.md +241 -125
  103. package/readme.plan.md +73 -286
  104. package/ts/00_commitinfo_data.ts +1 -1
  105. package/ts/certificate/index.ts +17 -9
  106. package/ts/certificate/models/certificate-types.ts +37 -16
  107. package/ts/certificate/providers/cert-provisioner.ts +247 -54
  108. package/ts/core/models/index.ts +2 -0
  109. package/ts/core/models/route-context.ts +113 -0
  110. package/ts/core/models/socket-augmentation.ts +33 -0
  111. package/ts/core/utils/event-system.ts +376 -0
  112. package/ts/core/utils/index.ts +7 -0
  113. package/ts/core/utils/route-manager.ts +489 -0
  114. package/ts/core/utils/route-utils.ts +312 -0
  115. package/ts/core/utils/security-utils.ts +309 -0
  116. package/ts/core/utils/shared-security-manager.ts +333 -0
  117. package/ts/core/utils/template-utils.ts +124 -0
  118. package/ts/core/utils/websocket-utils.ts +81 -0
  119. package/ts/forwarding/config/forwarding-types.ts +79 -107
  120. package/ts/forwarding/config/index.ts +4 -2
  121. package/ts/forwarding/handlers/base-handler.ts +4 -2
  122. package/ts/forwarding/index.ts +3 -2
  123. package/ts/http/models/http-types.ts +0 -1
  124. package/ts/http/port80/acme-interfaces.ts +84 -0
  125. package/ts/http/port80/port80-handler.ts +61 -15
  126. package/ts/http/router/index.ts +8 -1
  127. package/ts/http/router/route-router.ts +482 -0
  128. package/ts/index.ts +14 -2
  129. package/ts/proxies/index.ts +12 -3
  130. package/ts/proxies/network-proxy/certificate-manager.ts +114 -10
  131. package/ts/proxies/network-proxy/context-creator.ts +145 -0
  132. package/ts/proxies/network-proxy/function-cache.ts +259 -0
  133. package/ts/proxies/network-proxy/http-request-handler.ts +330 -0
  134. package/ts/proxies/network-proxy/http2-request-handler.ts +255 -0
  135. package/ts/proxies/network-proxy/models/types.ts +312 -1
  136. package/ts/proxies/network-proxy/network-proxy.ts +195 -86
  137. package/ts/proxies/network-proxy/request-handler.ts +698 -246
  138. package/ts/proxies/network-proxy/security-manager.ts +298 -0
  139. package/ts/proxies/network-proxy/websocket-handler.ts +276 -33
  140. package/ts/proxies/smart-proxy/index.ts +2 -12
  141. package/ts/proxies/smart-proxy/models/interfaces.ts +13 -67
  142. package/ts/proxies/smart-proxy/models/route-types.ts +223 -25
  143. package/ts/proxies/smart-proxy/network-proxy-bridge.ts +57 -123
  144. package/ts/proxies/smart-proxy/port-manager.ts +195 -0
  145. package/ts/proxies/smart-proxy/route-connection-handler.ts +191 -225
  146. package/ts/proxies/smart-proxy/route-manager.ts +101 -144
  147. package/ts/proxies/smart-proxy/smart-proxy.ts +206 -377
  148. package/ts/proxies/smart-proxy/timeout-manager.ts +2 -2
  149. package/ts/proxies/smart-proxy/utils/index.ts +40 -0
  150. package/ts/proxies/smart-proxy/utils/route-helpers.ts +621 -0
  151. package/ts/proxies/smart-proxy/utils/route-migration-utils.ts +165 -0
  152. package/ts/proxies/smart-proxy/utils/route-patterns.ts +309 -0
  153. package/ts/proxies/smart-proxy/utils/route-utils.ts +330 -0
  154. package/ts/proxies/smart-proxy/utils/route-validators.ts +288 -0
  155. package/ts/forwarding/config/domain-config.ts +0 -28
  156. package/ts/forwarding/config/domain-manager.ts +0 -283
  157. package/ts/proxies/smart-proxy/connection-handler.ts +0 -1240
  158. package/ts/proxies/smart-proxy/domain-config-manager.ts +0 -441
  159. package/ts/proxies/smart-proxy/port-range-manager.ts +0 -211
  160. package/ts/proxies/smart-proxy/route-helpers.ts +0 -344
@@ -1,441 +0,0 @@
1
- import * as plugins from '../../plugins.js';
2
- import type { IDomainConfig, ISmartProxyOptions } from './models/interfaces.js';
3
- import type { TForwardingType, IForwardConfig } from '../../forwarding/config/forwarding-types.js';
4
- import type { ForwardingHandler } from '../../forwarding/handlers/base-handler.js';
5
- import { ForwardingHandlerFactory } from '../../forwarding/factory/forwarding-factory.js';
6
- import type { IRouteConfig } from './models/route-types.js';
7
- import { RouteManager } from './route-manager.js';
8
-
9
- /**
10
- * Manages domain configurations and target selection
11
- */
12
- export class DomainConfigManager {
13
- // Track round-robin indices for domain configs
14
- private domainTargetIndices: Map<IDomainConfig, number> = new Map();
15
-
16
- // Cache forwarding handlers for each domain config
17
- private forwardingHandlers: Map<IDomainConfig, ForwardingHandler> = new Map();
18
-
19
- // Store derived domain configs from routes
20
- private derivedDomainConfigs: IDomainConfig[] = [];
21
-
22
- // Reference to RouteManager for route-based configuration
23
- private routeManager?: RouteManager;
24
-
25
- constructor(private settings: ISmartProxyOptions) {
26
- // Initialize with derived domain configs if using route-based configuration
27
- if (settings.routes && !settings.domainConfigs) {
28
- this.generateDomainConfigsFromRoutes();
29
- }
30
- }
31
-
32
- /**
33
- * Set the route manager reference for route-based queries
34
- */
35
- public setRouteManager(routeManager: RouteManager): void {
36
- this.routeManager = routeManager;
37
-
38
- // Regenerate domain configs from routes if needed
39
- if (this.settings.routes && (!this.settings.domainConfigs || this.settings.domainConfigs.length === 0)) {
40
- this.generateDomainConfigsFromRoutes();
41
- }
42
- }
43
-
44
- /**
45
- * Generate domain configs from routes
46
- */
47
- public generateDomainConfigsFromRoutes(): void {
48
- this.derivedDomainConfigs = [];
49
-
50
- if (!this.settings.routes) return;
51
-
52
- for (const route of this.settings.routes) {
53
- if (route.action.type !== 'forward' || !route.match.domains) continue;
54
-
55
- // Convert route to domain config
56
- const domainConfig = this.routeToDomainConfig(route);
57
- if (domainConfig) {
58
- this.derivedDomainConfigs.push(domainConfig);
59
- }
60
- }
61
- }
62
-
63
- /**
64
- * Convert a route to a domain config
65
- */
66
- private routeToDomainConfig(route: IRouteConfig): IDomainConfig | null {
67
- if (route.action.type !== 'forward' || !route.action.target) return null;
68
-
69
- // Get domains from route
70
- const domains = Array.isArray(route.match.domains) ?
71
- route.match.domains :
72
- (route.match.domains ? [route.match.domains] : []);
73
-
74
- if (domains.length === 0) return null;
75
-
76
- // Determine forwarding type based on TLS mode
77
- let forwardingType: TForwardingType = 'http-only';
78
- if (route.action.tls) {
79
- switch (route.action.tls.mode) {
80
- case 'passthrough':
81
- forwardingType = 'https-passthrough';
82
- break;
83
- case 'terminate':
84
- forwardingType = 'https-terminate-to-http';
85
- break;
86
- case 'terminate-and-reencrypt':
87
- forwardingType = 'https-terminate-to-https';
88
- break;
89
- }
90
- }
91
-
92
- // Create domain config
93
- return {
94
- domains,
95
- forwarding: {
96
- type: forwardingType,
97
- target: {
98
- host: route.action.target.host,
99
- port: route.action.target.port
100
- },
101
- security: route.action.security ? {
102
- allowedIps: route.action.security.allowedIps,
103
- blockedIps: route.action.security.blockedIps,
104
- maxConnections: route.action.security.maxConnections
105
- } : undefined,
106
- https: route.action.tls && route.action.tls.certificate !== 'auto' ? {
107
- customCert: route.action.tls.certificate
108
- } : undefined,
109
- advanced: route.action.advanced
110
- }
111
- };
112
- }
113
-
114
- /**
115
- * Updates the domain configurations
116
- */
117
- public updateDomainConfigs(newDomainConfigs: IDomainConfig[]): void {
118
- // If we're using domainConfigs property, update it
119
- if (this.settings.domainConfigs) {
120
- this.settings.domainConfigs = newDomainConfigs;
121
- } else {
122
- // Otherwise update our derived configs
123
- this.derivedDomainConfigs = newDomainConfigs;
124
- }
125
-
126
- // Reset target indices for removed configs
127
- const currentConfigSet = new Set(newDomainConfigs);
128
- for (const [config] of this.domainTargetIndices) {
129
- if (!currentConfigSet.has(config)) {
130
- this.domainTargetIndices.delete(config);
131
- }
132
- }
133
-
134
- // Clear handlers for removed configs and create handlers for new configs
135
- const handlersToRemove: IDomainConfig[] = [];
136
- for (const [config] of this.forwardingHandlers) {
137
- if (!currentConfigSet.has(config)) {
138
- handlersToRemove.push(config);
139
- }
140
- }
141
-
142
- // Remove handlers that are no longer needed
143
- for (const config of handlersToRemove) {
144
- this.forwardingHandlers.delete(config);
145
- }
146
-
147
- // Create handlers for new configs
148
- for (const config of newDomainConfigs) {
149
- if (!this.forwardingHandlers.has(config)) {
150
- try {
151
- const handler = this.createForwardingHandler(config);
152
- this.forwardingHandlers.set(config, handler);
153
- } catch (err) {
154
- console.log(`Error creating forwarding handler for domain ${config.domains.join(', ')}: ${err}`);
155
- }
156
- }
157
- }
158
- }
159
-
160
- /**
161
- * Get all domain configurations
162
- */
163
- public getDomainConfigs(): IDomainConfig[] {
164
- // Use domainConfigs from settings if available, otherwise use derived configs
165
- return this.settings.domainConfigs || this.derivedDomainConfigs;
166
- }
167
-
168
- /**
169
- * Find domain config matching a server name
170
- */
171
- public findDomainConfig(serverName: string): IDomainConfig | undefined {
172
- if (!serverName) return undefined;
173
-
174
- // Get domain configs from the appropriate source
175
- const domainConfigs = this.getDomainConfigs();
176
-
177
- // Check for direct match
178
- for (const config of domainConfigs) {
179
- if (config.domains.some(d => plugins.minimatch(serverName, d))) {
180
- return config;
181
- }
182
- }
183
-
184
- // No match found
185
- return undefined;
186
- }
187
-
188
- /**
189
- * Find domain config for a specific port
190
- */
191
- public findDomainConfigForPort(port: number): IDomainConfig | undefined {
192
- // Get domain configs from the appropriate source
193
- const domainConfigs = this.getDomainConfigs();
194
-
195
- // Check if any domain config has a matching port range
196
- for (const domain of domainConfigs) {
197
- const portRanges = domain.forwarding?.advanced?.portRanges;
198
- if (portRanges && portRanges.length > 0 && this.isPortInRanges(port, portRanges)) {
199
- return domain;
200
- }
201
- }
202
-
203
- // If we're in route-based mode, also check routes for this port
204
- if (this.settings.routes && (!this.settings.domainConfigs || this.settings.domainConfigs.length === 0)) {
205
- const routesForPort = this.settings.routes.filter(route => {
206
- // Check if this port is in the route's ports
207
- if (typeof route.match.ports === 'number') {
208
- return route.match.ports === port;
209
- } else if (Array.isArray(route.match.ports)) {
210
- return route.match.ports.some(p => {
211
- if (typeof p === 'number') {
212
- return p === port;
213
- } else if (p.from && p.to) {
214
- return port >= p.from && port <= p.to;
215
- }
216
- return false;
217
- });
218
- }
219
- return false;
220
- });
221
-
222
- // If we found any routes for this port, convert the first one to a domain config
223
- if (routesForPort.length > 0 && routesForPort[0].action.type === 'forward') {
224
- const domainConfig = this.routeToDomainConfig(routesForPort[0]);
225
- if (domainConfig) {
226
- return domainConfig;
227
- }
228
- }
229
- }
230
-
231
- return undefined;
232
- }
233
-
234
- /**
235
- * Check if a port is within any of the given ranges
236
- */
237
- public isPortInRanges(port: number, ranges: Array<{ from: number; to: number }>): boolean {
238
- return ranges.some((range) => port >= range.from && port <= range.to);
239
- }
240
-
241
- /**
242
- * Get target IP with round-robin support
243
- */
244
- public getTargetIP(domainConfig: IDomainConfig): string {
245
- const targetHosts = Array.isArray(domainConfig.forwarding.target.host)
246
- ? domainConfig.forwarding.target.host
247
- : [domainConfig.forwarding.target.host];
248
-
249
- if (targetHosts.length > 0) {
250
- const currentIndex = this.domainTargetIndices.get(domainConfig) || 0;
251
- const ip = targetHosts[currentIndex % targetHosts.length];
252
- this.domainTargetIndices.set(domainConfig, currentIndex + 1);
253
- return ip;
254
- }
255
-
256
- return this.settings.targetIP || 'localhost';
257
- }
258
-
259
- /**
260
- * Get target host with round-robin support (for tests)
261
- * This is just an alias for getTargetIP for easier test compatibility
262
- */
263
- public getTargetHost(domainConfig: IDomainConfig): string {
264
- return this.getTargetIP(domainConfig);
265
- }
266
-
267
- /**
268
- * Get target port from domain config
269
- */
270
- public getTargetPort(domainConfig: IDomainConfig, defaultPort: number): number {
271
- return domainConfig.forwarding.target.port || defaultPort;
272
- }
273
-
274
- /**
275
- * Checks if a domain should use NetworkProxy
276
- */
277
- public shouldUseNetworkProxy(domainConfig: IDomainConfig): boolean {
278
- const forwardingType = this.getForwardingType(domainConfig);
279
- return forwardingType === 'https-terminate-to-http' ||
280
- forwardingType === 'https-terminate-to-https';
281
- }
282
-
283
- /**
284
- * Gets the NetworkProxy port for a domain
285
- */
286
- public getNetworkProxyPort(domainConfig: IDomainConfig): number | undefined {
287
- // First check if we should use NetworkProxy at all
288
- if (!this.shouldUseNetworkProxy(domainConfig)) {
289
- return undefined;
290
- }
291
-
292
- return domainConfig.forwarding.advanced?.networkProxyPort || this.settings.networkProxyPort;
293
- }
294
-
295
- /**
296
- * Get effective allowed and blocked IPs for a domain
297
- *
298
- * This method combines domain-specific security rules from the forwarding configuration
299
- * with global security defaults when necessary.
300
- */
301
- public getEffectiveIPRules(domainConfig: IDomainConfig): {
302
- allowedIPs: string[],
303
- blockedIPs: string[]
304
- } {
305
- // Start with empty arrays
306
- const allowedIPs: string[] = [];
307
- const blockedIPs: string[] = [];
308
-
309
- // Add IPs from forwarding security settings if available
310
- if (domainConfig.forwarding?.security?.allowedIps) {
311
- allowedIPs.push(...domainConfig.forwarding.security.allowedIps);
312
- } else {
313
- // If no allowed IPs are specified in forwarding config and global defaults exist, use them
314
- if (this.settings.defaultAllowedIPs && this.settings.defaultAllowedIPs.length > 0) {
315
- allowedIPs.push(...this.settings.defaultAllowedIPs);
316
- } else {
317
- // Default to allow all if no specific rules
318
- allowedIPs.push('*');
319
- }
320
- }
321
-
322
- // Add blocked IPs from forwarding security settings if available
323
- if (domainConfig.forwarding?.security?.blockedIps) {
324
- blockedIPs.push(...domainConfig.forwarding.security.blockedIps);
325
- }
326
-
327
- // Always add global blocked IPs, even if domain has its own rules
328
- // This ensures that global blocks take precedence
329
- if (this.settings.defaultBlockedIPs && this.settings.defaultBlockedIPs.length > 0) {
330
- // Add only unique IPs that aren't already in the list
331
- for (const ip of this.settings.defaultBlockedIPs) {
332
- if (!blockedIPs.includes(ip)) {
333
- blockedIPs.push(ip);
334
- }
335
- }
336
- }
337
-
338
- return {
339
- allowedIPs,
340
- blockedIPs
341
- };
342
- }
343
-
344
- /**
345
- * Get connection timeout for a domain
346
- */
347
- public getConnectionTimeout(domainConfig?: IDomainConfig): number {
348
- if (domainConfig?.forwarding.advanced?.timeout) {
349
- return domainConfig.forwarding.advanced.timeout;
350
- }
351
-
352
- return this.settings.maxConnectionLifetime || 86400000; // 24 hours default
353
- }
354
-
355
- /**
356
- * Creates a forwarding handler for a domain configuration
357
- */
358
- private createForwardingHandler(domainConfig: IDomainConfig): ForwardingHandler {
359
- // Create a new handler using the factory
360
- const handler = ForwardingHandlerFactory.createHandler(domainConfig.forwarding);
361
-
362
- // Initialize the handler
363
- handler.initialize().catch(err => {
364
- console.log(`Error initializing forwarding handler for ${domainConfig.domains.join(', ')}: ${err}`);
365
- });
366
-
367
- return handler;
368
- }
369
-
370
- /**
371
- * Gets a forwarding handler for a domain config
372
- * If no handler exists, creates one
373
- */
374
- public getForwardingHandler(domainConfig: IDomainConfig): ForwardingHandler {
375
- // If we already have a handler, return it
376
- if (this.forwardingHandlers.has(domainConfig)) {
377
- return this.forwardingHandlers.get(domainConfig)!;
378
- }
379
-
380
- // Otherwise create a new handler
381
- const handler = this.createForwardingHandler(domainConfig);
382
- this.forwardingHandlers.set(domainConfig, handler);
383
-
384
- return handler;
385
- }
386
-
387
- /**
388
- * Gets the forwarding type for a domain config
389
- */
390
- public getForwardingType(domainConfig?: IDomainConfig): TForwardingType | undefined {
391
- if (!domainConfig?.forwarding) return undefined;
392
- return domainConfig.forwarding.type;
393
- }
394
-
395
- /**
396
- * Checks if the forwarding type requires TLS termination
397
- */
398
- public requiresTlsTermination(domainConfig?: IDomainConfig): boolean {
399
- if (!domainConfig) return false;
400
-
401
- const forwardingType = this.getForwardingType(domainConfig);
402
- return forwardingType === 'https-terminate-to-http' ||
403
- forwardingType === 'https-terminate-to-https';
404
- }
405
-
406
- /**
407
- * Checks if the forwarding type supports HTTP
408
- */
409
- public supportsHttp(domainConfig?: IDomainConfig): boolean {
410
- if (!domainConfig) return false;
411
-
412
- const forwardingType = this.getForwardingType(domainConfig);
413
-
414
- // HTTP-only always supports HTTP
415
- if (forwardingType === 'http-only') return true;
416
-
417
- // For termination types, check the HTTP settings
418
- if (forwardingType === 'https-terminate-to-http' ||
419
- forwardingType === 'https-terminate-to-https') {
420
- // HTTP is supported by default for termination types
421
- return domainConfig.forwarding?.http?.enabled !== false;
422
- }
423
-
424
- // HTTPS-passthrough doesn't support HTTP
425
- return false;
426
- }
427
-
428
- /**
429
- * Checks if HTTP requests should be redirected to HTTPS
430
- */
431
- public shouldRedirectToHttps(domainConfig?: IDomainConfig): boolean {
432
- if (!domainConfig?.forwarding) return false;
433
-
434
- // Only check for redirect if HTTP is enabled
435
- if (this.supportsHttp(domainConfig)) {
436
- return !!domainConfig.forwarding.http?.redirectToHttps;
437
- }
438
-
439
- return false;
440
- }
441
- }
@@ -1,211 +0,0 @@
1
- import type { ISmartProxyOptions } from './models/interfaces.js';
2
-
3
- /**
4
- * Manages port ranges and port-based configuration
5
- */
6
- export class PortRangeManager {
7
- constructor(private settings: ISmartProxyOptions) {}
8
-
9
- /**
10
- * Get all ports that should be listened on
11
- */
12
- public getListeningPorts(): Set<number> {
13
- const listeningPorts = new Set<number>();
14
-
15
- // Always include the main fromPort
16
- listeningPorts.add(this.settings.fromPort);
17
-
18
- // Add ports from global port ranges if defined
19
- if (this.settings.globalPortRanges && this.settings.globalPortRanges.length > 0) {
20
- for (const range of this.settings.globalPortRanges) {
21
- for (let port = range.from; port <= range.to; port++) {
22
- listeningPorts.add(port);
23
- }
24
- }
25
- }
26
-
27
- return listeningPorts;
28
- }
29
-
30
- /**
31
- * Check if a port should use NetworkProxy for forwarding
32
- */
33
- public shouldUseNetworkProxy(port: number): boolean {
34
- return !!this.settings.useNetworkProxy && this.settings.useNetworkProxy.includes(port);
35
- }
36
-
37
- /**
38
- * Check if port should use global forwarding
39
- */
40
- public shouldUseGlobalForwarding(port: number): boolean {
41
- return (
42
- !!this.settings.forwardAllGlobalRanges &&
43
- this.isPortInGlobalRanges(port)
44
- );
45
- }
46
-
47
- /**
48
- * Check if a port is in global ranges
49
- */
50
- public isPortInGlobalRanges(port: number): boolean {
51
- return (
52
- this.settings.globalPortRanges &&
53
- this.isPortInRanges(port, this.settings.globalPortRanges)
54
- );
55
- }
56
-
57
- /**
58
- * Check if a port falls within the specified ranges
59
- */
60
- public isPortInRanges(port: number, ranges: Array<{ from: number; to: number }>): boolean {
61
- return ranges.some((range) => port >= range.from && port <= range.to);
62
- }
63
-
64
- /**
65
- * Get forwarding port for a specific listening port
66
- * This determines what port to connect to on the target
67
- */
68
- public getForwardingPort(listeningPort: number): number {
69
- // If using global forwarding, forward to the original port
70
- if (this.settings.forwardAllGlobalRanges && this.isPortInGlobalRanges(listeningPort)) {
71
- return listeningPort;
72
- }
73
-
74
- // Otherwise use the configured toPort
75
- return this.settings.toPort;
76
- }
77
-
78
- /**
79
- * Find domain-specific port ranges that include a given port
80
- */
81
- public findDomainPortRange(port: number): {
82
- domainIndex: number,
83
- range: { from: number, to: number }
84
- } | undefined {
85
- for (let i = 0; i < this.settings.domainConfigs.length; i++) {
86
- const domain = this.settings.domainConfigs[i];
87
- // Get port ranges from forwarding.advanced if available
88
- const portRanges = domain.forwarding?.advanced?.portRanges;
89
- if (portRanges && portRanges.length > 0) {
90
- for (const range of portRanges) {
91
- if (port >= range.from && port <= range.to) {
92
- return { domainIndex: i, range };
93
- }
94
- }
95
- }
96
- }
97
- return undefined;
98
- }
99
-
100
- /**
101
- * Get a list of all configured ports
102
- * This includes the fromPort, NetworkProxy ports, and ports from all ranges
103
- */
104
- public getAllConfiguredPorts(): number[] {
105
- const ports = new Set<number>();
106
-
107
- // Add main listening port
108
- ports.add(this.settings.fromPort);
109
-
110
- // Add NetworkProxy port if configured
111
- if (this.settings.networkProxyPort) {
112
- ports.add(this.settings.networkProxyPort);
113
- }
114
-
115
- // Add NetworkProxy ports
116
- if (this.settings.useNetworkProxy) {
117
- for (const port of this.settings.useNetworkProxy) {
118
- ports.add(port);
119
- }
120
- }
121
-
122
-
123
- // Add global port ranges
124
- if (this.settings.globalPortRanges) {
125
- for (const range of this.settings.globalPortRanges) {
126
- for (let port = range.from; port <= range.to; port++) {
127
- ports.add(port);
128
- }
129
- }
130
- }
131
-
132
- // Add domain-specific port ranges
133
- for (const domain of this.settings.domainConfigs) {
134
- // Get port ranges from forwarding.advanced
135
- const portRanges = domain.forwarding?.advanced?.portRanges;
136
- if (portRanges && portRanges.length > 0) {
137
- for (const range of portRanges) {
138
- for (let port = range.from; port <= range.to; port++) {
139
- ports.add(port);
140
- }
141
- }
142
- }
143
-
144
- // Add domain-specific NetworkProxy port if configured in forwarding.advanced
145
- const networkProxyPort = domain.forwarding?.advanced?.networkProxyPort;
146
- if (networkProxyPort) {
147
- ports.add(networkProxyPort);
148
- }
149
- }
150
-
151
- return Array.from(ports);
152
- }
153
-
154
- /**
155
- * Validate port configuration
156
- * Returns array of warning messages
157
- */
158
- public validateConfiguration(): string[] {
159
- const warnings: string[] = [];
160
-
161
- // Check for overlapping port ranges
162
- const portMappings = new Map<number, string[]>();
163
-
164
- // Track global port ranges
165
- if (this.settings.globalPortRanges) {
166
- for (const range of this.settings.globalPortRanges) {
167
- for (let port = range.from; port <= range.to; port++) {
168
- if (!portMappings.has(port)) {
169
- portMappings.set(port, []);
170
- }
171
- portMappings.get(port)!.push('Global Port Range');
172
- }
173
- }
174
- }
175
-
176
- // Track domain-specific port ranges
177
- for (const domain of this.settings.domainConfigs) {
178
- // Get port ranges from forwarding.advanced
179
- const portRanges = domain.forwarding?.advanced?.portRanges;
180
- if (portRanges && portRanges.length > 0) {
181
- for (const range of portRanges) {
182
- for (let port = range.from; port <= range.to; port++) {
183
- if (!portMappings.has(port)) {
184
- portMappings.set(port, []);
185
- }
186
- portMappings.get(port)!.push(`Domain: ${domain.domains.join(', ')}`);
187
- }
188
- }
189
- }
190
- }
191
-
192
- // Check for ports with multiple mappings
193
- for (const [port, mappings] of portMappings.entries()) {
194
- if (mappings.length > 1) {
195
- warnings.push(`Port ${port} has multiple mappings: ${mappings.join(', ')}`);
196
- }
197
- }
198
-
199
- // Check if main ports are used elsewhere
200
- if (portMappings.has(this.settings.fromPort) && portMappings.get(this.settings.fromPort)!.length > 0) {
201
- warnings.push(`Main listening port ${this.settings.fromPort} is also used in port ranges`);
202
- }
203
-
204
- if (this.settings.networkProxyPort && portMappings.has(this.settings.networkProxyPort)) {
205
- warnings.push(`NetworkProxy port ${this.settings.networkProxyPort} is also used in port ranges`);
206
- }
207
-
208
-
209
- return warnings;
210
- }
211
- }