@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
@@ -61,8 +61,8 @@ export class TimeoutManager {
61
61
  * Calculate effective max lifetime based on connection type
62
62
  */
63
63
  public getEffectiveMaxLifetime(record: IConnectionRecord): number {
64
- // Use domain-specific timeout from forwarding.advanced if available
65
- const baseTimeout = record.domainConfig?.forwarding?.advanced?.timeout ||
64
+ // Use route-specific timeout if available from the routeConfig
65
+ const baseTimeout = record.routeConfig?.action.advanced?.timeout ||
66
66
  this.settings.maxConnectionLifetime ||
67
67
  86400000; // 24 hours default
68
68
 
@@ -0,0 +1,40 @@
1
+ /**
2
+ * SmartProxy Route Utilities
3
+ *
4
+ * This file exports all route-related utilities for the SmartProxy module,
5
+ * including helpers, validators, utilities, and patterns for working with routes.
6
+ */
7
+
8
+ // Export route helpers for creating routes
9
+ export * from './route-helpers.js';
10
+
11
+ // Export route validators for validating route configurations
12
+ export * from './route-validators.js';
13
+
14
+ // Export route utilities for route operations
15
+ export * from './route-utils.js';
16
+
17
+ // Export route patterns with renamed exports to avoid conflicts
18
+ import {
19
+ createWebSocketRoute as createWebSocketPatternRoute,
20
+ createLoadBalancerRoute as createLoadBalancerPatternRoute,
21
+ createApiGatewayRoute,
22
+ createStaticFileServerRoute,
23
+ addRateLimiting,
24
+ addBasicAuth,
25
+ addJwtAuth
26
+ } from './route-patterns.js';
27
+
28
+ export {
29
+ createWebSocketPatternRoute,
30
+ createLoadBalancerPatternRoute,
31
+ createApiGatewayRoute,
32
+ createStaticFileServerRoute,
33
+ addRateLimiting,
34
+ addBasicAuth,
35
+ addJwtAuth
36
+ };
37
+
38
+ // Export migration utilities for transitioning from domain-based to route-based configs
39
+ // Note: These will be removed in a future version once migration is complete
40
+ export * from './route-migration-utils.js';
@@ -0,0 +1,621 @@
1
+ /**
2
+ * Route Helper Functions
3
+ *
4
+ * This file provides utility functions for creating route configurations for common scenarios.
5
+ * These functions aim to simplify the creation of route configurations for typical use cases.
6
+ *
7
+ * This module includes helper functions for creating:
8
+ * - HTTP routes (createHttpRoute)
9
+ * - HTTPS routes with TLS termination (createHttpsTerminateRoute)
10
+ * - HTTP to HTTPS redirects (createHttpToHttpsRedirect)
11
+ * - HTTPS passthrough routes (createHttpsPassthroughRoute)
12
+ * - Complete HTTPS servers with redirects (createCompleteHttpsServer)
13
+ * - Load balancer routes (createLoadBalancerRoute)
14
+ * - Static file server routes (createStaticFileRoute)
15
+ * - API routes (createApiRoute)
16
+ * - WebSocket routes (createWebSocketRoute)
17
+ * - Port mapping routes (createPortMappingRoute, createOffsetPortMappingRoute)
18
+ * - Dynamic routing (createDynamicRoute, createSmartLoadBalancer)
19
+ */
20
+
21
+ import type { IRouteConfig, IRouteMatch, IRouteAction, IRouteTarget, TPortRange, IRouteContext } from '../models/route-types.js';
22
+
23
+ /**
24
+ * Create an HTTP-only route configuration
25
+ * @param domains Domain(s) to match
26
+ * @param target Target host and port
27
+ * @param options Additional route options
28
+ * @returns Route configuration object
29
+ */
30
+ export function createHttpRoute(
31
+ domains: string | string[],
32
+ target: { host: string | string[]; port: number },
33
+ options: Partial<IRouteConfig> = {}
34
+ ): IRouteConfig {
35
+ // Create route match
36
+ const match: IRouteMatch = {
37
+ ports: options.match?.ports || 80,
38
+ domains
39
+ };
40
+
41
+ // Create route action
42
+ const action: IRouteAction = {
43
+ type: 'forward',
44
+ target
45
+ };
46
+
47
+ // Create the route config
48
+ return {
49
+ match,
50
+ action,
51
+ name: options.name || `HTTP Route for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
52
+ ...options
53
+ };
54
+ }
55
+
56
+ /**
57
+ * Create an HTTPS route with TLS termination (including HTTP redirect to HTTPS)
58
+ * @param domains Domain(s) to match
59
+ * @param target Target host and port
60
+ * @param options Additional route options
61
+ * @returns Route configuration object
62
+ */
63
+ export function createHttpsTerminateRoute(
64
+ domains: string | string[],
65
+ target: { host: string | string[]; port: number },
66
+ options: {
67
+ certificate?: 'auto' | { key: string; cert: string };
68
+ httpPort?: number | number[];
69
+ httpsPort?: number | number[];
70
+ reencrypt?: boolean;
71
+ name?: string;
72
+ [key: string]: any;
73
+ } = {}
74
+ ): IRouteConfig {
75
+ // Create route match
76
+ const match: IRouteMatch = {
77
+ ports: options.httpsPort || 443,
78
+ domains
79
+ };
80
+
81
+ // Create route action
82
+ const action: IRouteAction = {
83
+ type: 'forward',
84
+ target,
85
+ tls: {
86
+ mode: options.reencrypt ? 'terminate-and-reencrypt' : 'terminate',
87
+ certificate: options.certificate || 'auto'
88
+ }
89
+ };
90
+
91
+ // Create the route config
92
+ return {
93
+ match,
94
+ action,
95
+ name: options.name || `HTTPS Route for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
96
+ ...options
97
+ };
98
+ }
99
+
100
+ /**
101
+ * Create an HTTP to HTTPS redirect route
102
+ * @param domains Domain(s) to match
103
+ * @param httpsPort HTTPS port to redirect to (default: 443)
104
+ * @param options Additional route options
105
+ * @returns Route configuration object
106
+ */
107
+ export function createHttpToHttpsRedirect(
108
+ domains: string | string[],
109
+ httpsPort: number = 443,
110
+ options: Partial<IRouteConfig> = {}
111
+ ): IRouteConfig {
112
+ // Create route match
113
+ const match: IRouteMatch = {
114
+ ports: options.match?.ports || 80,
115
+ domains
116
+ };
117
+
118
+ // Create route action
119
+ const action: IRouteAction = {
120
+ type: 'redirect',
121
+ redirect: {
122
+ to: `https://{domain}:${httpsPort}{path}`,
123
+ status: 301
124
+ }
125
+ };
126
+
127
+ // Create the route config
128
+ return {
129
+ match,
130
+ action,
131
+ name: options.name || `HTTP to HTTPS Redirect for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
132
+ ...options
133
+ };
134
+ }
135
+
136
+ /**
137
+ * Create an HTTPS passthrough route (SNI-based forwarding without TLS termination)
138
+ * @param domains Domain(s) to match
139
+ * @param target Target host and port
140
+ * @param options Additional route options
141
+ * @returns Route configuration object
142
+ */
143
+ export function createHttpsPassthroughRoute(
144
+ domains: string | string[],
145
+ target: { host: string | string[]; port: number },
146
+ options: Partial<IRouteConfig> = {}
147
+ ): IRouteConfig {
148
+ // Create route match
149
+ const match: IRouteMatch = {
150
+ ports: options.match?.ports || 443,
151
+ domains
152
+ };
153
+
154
+ // Create route action
155
+ const action: IRouteAction = {
156
+ type: 'forward',
157
+ target,
158
+ tls: {
159
+ mode: 'passthrough'
160
+ }
161
+ };
162
+
163
+ // Create the route config
164
+ return {
165
+ match,
166
+ action,
167
+ name: options.name || `HTTPS Passthrough for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
168
+ ...options
169
+ };
170
+ }
171
+
172
+ /**
173
+ * Create a complete HTTPS server with HTTP to HTTPS redirects
174
+ * @param domains Domain(s) to match
175
+ * @param target Target host and port
176
+ * @param options Additional configuration options
177
+ * @returns Array of two route configurations (HTTPS and HTTP redirect)
178
+ */
179
+ export function createCompleteHttpsServer(
180
+ domains: string | string[],
181
+ target: { host: string | string[]; port: number },
182
+ options: {
183
+ certificate?: 'auto' | { key: string; cert: string };
184
+ httpPort?: number | number[];
185
+ httpsPort?: number | number[];
186
+ reencrypt?: boolean;
187
+ name?: string;
188
+ [key: string]: any;
189
+ } = {}
190
+ ): IRouteConfig[] {
191
+ // Create the HTTPS route
192
+ const httpsRoute = createHttpsTerminateRoute(domains, target, options);
193
+
194
+ // Create the HTTP redirect route
195
+ const httpRedirectRoute = createHttpToHttpsRedirect(
196
+ domains,
197
+ // Extract the HTTPS port from the HTTPS route - ensure it's a number
198
+ typeof options.httpsPort === 'number' ? options.httpsPort :
199
+ Array.isArray(options.httpsPort) ? options.httpsPort[0] : 443,
200
+ {
201
+ // Set the HTTP port
202
+ match: {
203
+ ports: options.httpPort || 80,
204
+ domains
205
+ },
206
+ name: `HTTP to HTTPS Redirect for ${Array.isArray(domains) ? domains.join(', ') : domains}`
207
+ }
208
+ );
209
+
210
+ return [httpsRoute, httpRedirectRoute];
211
+ }
212
+
213
+ /**
214
+ * Create a load balancer route (round-robin between multiple backend hosts)
215
+ * @param domains Domain(s) to match
216
+ * @param hosts Array of backend hosts to load balance between
217
+ * @param port Backend port
218
+ * @param options Additional route options
219
+ * @returns Route configuration object
220
+ */
221
+ export function createLoadBalancerRoute(
222
+ domains: string | string[],
223
+ hosts: string[],
224
+ port: number,
225
+ options: {
226
+ tls?: {
227
+ mode: 'passthrough' | 'terminate' | 'terminate-and-reencrypt';
228
+ certificate?: 'auto' | { key: string; cert: string };
229
+ };
230
+ [key: string]: any;
231
+ } = {}
232
+ ): IRouteConfig {
233
+ // Create route match
234
+ const match: IRouteMatch = {
235
+ ports: options.match?.ports || (options.tls ? 443 : 80),
236
+ domains
237
+ };
238
+
239
+ // Create route target
240
+ const target: IRouteTarget = {
241
+ host: hosts,
242
+ port
243
+ };
244
+
245
+ // Create route action
246
+ const action: IRouteAction = {
247
+ type: 'forward',
248
+ target
249
+ };
250
+
251
+ // Add TLS configuration if provided
252
+ if (options.tls) {
253
+ action.tls = {
254
+ mode: options.tls.mode,
255
+ certificate: options.tls.certificate || 'auto'
256
+ };
257
+ }
258
+
259
+ // Create the route config
260
+ return {
261
+ match,
262
+ action,
263
+ name: options.name || `Load Balancer for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
264
+ ...options
265
+ };
266
+ }
267
+
268
+ /**
269
+ * Create a static file server route
270
+ * @param domains Domain(s) to match
271
+ * @param rootDir Root directory path for static files
272
+ * @param options Additional route options
273
+ * @returns Route configuration object
274
+ */
275
+ export function createStaticFileRoute(
276
+ domains: string | string[],
277
+ rootDir: string,
278
+ options: {
279
+ indexFiles?: string[];
280
+ serveOnHttps?: boolean;
281
+ certificate?: 'auto' | { key: string; cert: string };
282
+ httpPort?: number | number[];
283
+ httpsPort?: number | number[];
284
+ name?: string;
285
+ [key: string]: any;
286
+ } = {}
287
+ ): IRouteConfig {
288
+ // Create route match
289
+ const match: IRouteMatch = {
290
+ ports: options.serveOnHttps
291
+ ? (options.httpsPort || 443)
292
+ : (options.httpPort || 80),
293
+ domains
294
+ };
295
+
296
+ // Create route action
297
+ const action: IRouteAction = {
298
+ type: 'static',
299
+ static: {
300
+ root: rootDir,
301
+ index: options.indexFiles || ['index.html', 'index.htm']
302
+ }
303
+ };
304
+
305
+ // Add TLS configuration if serving on HTTPS
306
+ if (options.serveOnHttps) {
307
+ action.tls = {
308
+ mode: 'terminate',
309
+ certificate: options.certificate || 'auto'
310
+ };
311
+ }
312
+
313
+ // Create the route config
314
+ return {
315
+ match,
316
+ action,
317
+ name: options.name || `Static Files for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
318
+ ...options
319
+ };
320
+ }
321
+
322
+ /**
323
+ * Create an API route configuration
324
+ * @param domains Domain(s) to match
325
+ * @param apiPath API base path (e.g., "/api")
326
+ * @param target Target host and port
327
+ * @param options Additional route options
328
+ * @returns Route configuration object
329
+ */
330
+ export function createApiRoute(
331
+ domains: string | string[],
332
+ apiPath: string,
333
+ target: { host: string | string[]; port: number },
334
+ options: {
335
+ useTls?: boolean;
336
+ certificate?: 'auto' | { key: string; cert: string };
337
+ addCorsHeaders?: boolean;
338
+ httpPort?: number | number[];
339
+ httpsPort?: number | number[];
340
+ name?: string;
341
+ [key: string]: any;
342
+ } = {}
343
+ ): IRouteConfig {
344
+ // Normalize API path
345
+ const normalizedPath = apiPath.startsWith('/') ? apiPath : `/${apiPath}`;
346
+ const pathWithWildcard = normalizedPath.endsWith('/')
347
+ ? `${normalizedPath}*`
348
+ : `${normalizedPath}/*`;
349
+
350
+ // Create route match
351
+ const match: IRouteMatch = {
352
+ ports: options.useTls
353
+ ? (options.httpsPort || 443)
354
+ : (options.httpPort || 80),
355
+ domains,
356
+ path: pathWithWildcard
357
+ };
358
+
359
+ // Create route action
360
+ const action: IRouteAction = {
361
+ type: 'forward',
362
+ target
363
+ };
364
+
365
+ // Add TLS configuration if using HTTPS
366
+ if (options.useTls) {
367
+ action.tls = {
368
+ mode: 'terminate',
369
+ certificate: options.certificate || 'auto'
370
+ };
371
+ }
372
+
373
+ // Add CORS headers if requested
374
+ const headers: Record<string, Record<string, string>> = {};
375
+ if (options.addCorsHeaders) {
376
+ headers.response = {
377
+ 'Access-Control-Allow-Origin': '*',
378
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
379
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
380
+ 'Access-Control-Max-Age': '86400'
381
+ };
382
+ }
383
+
384
+ // Create the route config
385
+ return {
386
+ match,
387
+ action,
388
+ headers: Object.keys(headers).length > 0 ? headers : undefined,
389
+ name: options.name || `API Route ${normalizedPath} for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
390
+ priority: options.priority || 100, // Higher priority for specific path matches
391
+ ...options
392
+ };
393
+ }
394
+
395
+ /**
396
+ * Create a WebSocket route configuration
397
+ * @param domains Domain(s) to match
398
+ * @param wsPath WebSocket path (e.g., "/ws")
399
+ * @param target Target WebSocket server host and port
400
+ * @param options Additional route options
401
+ * @returns Route configuration object
402
+ */
403
+ export function createWebSocketRoute(
404
+ domains: string | string[],
405
+ wsPath: string,
406
+ target: { host: string | string[]; port: number },
407
+ options: {
408
+ useTls?: boolean;
409
+ certificate?: 'auto' | { key: string; cert: string };
410
+ httpPort?: number | number[];
411
+ httpsPort?: number | number[];
412
+ pingInterval?: number;
413
+ pingTimeout?: number;
414
+ name?: string;
415
+ [key: string]: any;
416
+ } = {}
417
+ ): IRouteConfig {
418
+ // Normalize WebSocket path
419
+ const normalizedPath = wsPath.startsWith('/') ? wsPath : `/${wsPath}`;
420
+
421
+ // Create route match
422
+ const match: IRouteMatch = {
423
+ ports: options.useTls
424
+ ? (options.httpsPort || 443)
425
+ : (options.httpPort || 80),
426
+ domains,
427
+ path: normalizedPath
428
+ };
429
+
430
+ // Create route action
431
+ const action: IRouteAction = {
432
+ type: 'forward',
433
+ target,
434
+ websocket: {
435
+ enabled: true,
436
+ pingInterval: options.pingInterval || 30000, // 30 seconds
437
+ pingTimeout: options.pingTimeout || 5000 // 5 seconds
438
+ }
439
+ };
440
+
441
+ // Add TLS configuration if using HTTPS
442
+ if (options.useTls) {
443
+ action.tls = {
444
+ mode: 'terminate',
445
+ certificate: options.certificate || 'auto'
446
+ };
447
+ }
448
+
449
+ // Create the route config
450
+ return {
451
+ match,
452
+ action,
453
+ name: options.name || `WebSocket Route ${normalizedPath} for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
454
+ priority: options.priority || 100, // Higher priority for WebSocket routes
455
+ ...options
456
+ };
457
+ }
458
+
459
+ /**
460
+ * Create a helper function that applies a port offset
461
+ * @param offset The offset to apply to the matched port
462
+ * @returns A function that adds the offset to the matched port
463
+ */
464
+ export function createPortOffset(offset: number): (context: IRouteContext) => number {
465
+ return (context: IRouteContext) => context.port + offset;
466
+ }
467
+
468
+ /**
469
+ * Create a port mapping route with context-based port function
470
+ * @param options Port mapping route options
471
+ * @returns Route configuration object
472
+ */
473
+ export function createPortMappingRoute(options: {
474
+ sourcePortRange: TPortRange;
475
+ targetHost: string | string[] | ((context: IRouteContext) => string | string[]);
476
+ portMapper: (context: IRouteContext) => number;
477
+ name?: string;
478
+ domains?: string | string[];
479
+ priority?: number;
480
+ [key: string]: any;
481
+ }): IRouteConfig {
482
+ // Create route match
483
+ const match: IRouteMatch = {
484
+ ports: options.sourcePortRange,
485
+ domains: options.domains
486
+ };
487
+
488
+ // Create route action
489
+ const action: IRouteAction = {
490
+ type: 'forward',
491
+ target: {
492
+ host: options.targetHost,
493
+ port: options.portMapper
494
+ }
495
+ };
496
+
497
+ // Create the route config
498
+ return {
499
+ match,
500
+ action,
501
+ name: options.name || `Port Mapping Route for ${options.domains || 'all domains'}`,
502
+ priority: options.priority,
503
+ ...options
504
+ };
505
+ }
506
+
507
+ /**
508
+ * Create a simple offset port mapping route
509
+ * @param options Offset port mapping route options
510
+ * @returns Route configuration object
511
+ */
512
+ export function createOffsetPortMappingRoute(options: {
513
+ ports: TPortRange;
514
+ targetHost: string | string[];
515
+ offset: number;
516
+ name?: string;
517
+ domains?: string | string[];
518
+ priority?: number;
519
+ [key: string]: any;
520
+ }): IRouteConfig {
521
+ return createPortMappingRoute({
522
+ sourcePortRange: options.ports,
523
+ targetHost: options.targetHost,
524
+ portMapper: (context) => context.port + options.offset,
525
+ name: options.name || `Offset Mapping (${options.offset > 0 ? '+' : ''}${options.offset}) for ${options.domains || 'all domains'}`,
526
+ domains: options.domains,
527
+ priority: options.priority,
528
+ ...options
529
+ });
530
+ }
531
+
532
+ /**
533
+ * Create a dynamic route with context-based host and port mapping
534
+ * @param options Dynamic route options
535
+ * @returns Route configuration object
536
+ */
537
+ export function createDynamicRoute(options: {
538
+ ports: TPortRange;
539
+ targetHost: (context: IRouteContext) => string | string[];
540
+ portMapper: (context: IRouteContext) => number;
541
+ name?: string;
542
+ domains?: string | string[];
543
+ path?: string;
544
+ clientIp?: string[];
545
+ priority?: number;
546
+ [key: string]: any;
547
+ }): IRouteConfig {
548
+ // Create route match
549
+ const match: IRouteMatch = {
550
+ ports: options.ports,
551
+ domains: options.domains,
552
+ path: options.path,
553
+ clientIp: options.clientIp
554
+ };
555
+
556
+ // Create route action
557
+ const action: IRouteAction = {
558
+ type: 'forward',
559
+ target: {
560
+ host: options.targetHost,
561
+ port: options.portMapper
562
+ }
563
+ };
564
+
565
+ // Create the route config
566
+ return {
567
+ match,
568
+ action,
569
+ name: options.name || `Dynamic Route for ${options.domains || 'all domains'}`,
570
+ priority: options.priority,
571
+ ...options
572
+ };
573
+ }
574
+
575
+ /**
576
+ * Create a smart load balancer with dynamic domain-based backend selection
577
+ * @param options Smart load balancer options
578
+ * @returns Route configuration object
579
+ */
580
+ export function createSmartLoadBalancer(options: {
581
+ ports: TPortRange;
582
+ domainTargets: Record<string, string | string[]>;
583
+ portMapper: (context: IRouteContext) => number;
584
+ name?: string;
585
+ defaultTarget?: string | string[];
586
+ priority?: number;
587
+ [key: string]: any;
588
+ }): IRouteConfig {
589
+ // Extract all domain keys to create the match criteria
590
+ const domains = Object.keys(options.domainTargets);
591
+
592
+ // Create the smart host selector function
593
+ const hostSelector = (context: IRouteContext) => {
594
+ const domain = context.domain || '';
595
+ return options.domainTargets[domain] || options.defaultTarget || 'localhost';
596
+ };
597
+
598
+ // Create route match
599
+ const match: IRouteMatch = {
600
+ ports: options.ports,
601
+ domains
602
+ };
603
+
604
+ // Create route action
605
+ const action: IRouteAction = {
606
+ type: 'forward',
607
+ target: {
608
+ host: hostSelector,
609
+ port: options.portMapper
610
+ }
611
+ };
612
+
613
+ // Create the route config
614
+ return {
615
+ match,
616
+ action,
617
+ name: options.name || `Smart Load Balancer for ${domains.join(', ')}`,
618
+ priority: options.priority,
619
+ ...options
620
+ };
621
+ }