@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
@@ -0,0 +1,163 @@
1
+ /**
2
+ * HTTPS Route Helper Functions
3
+ *
4
+ * This module provides utility functions for creating HTTPS route configurations
5
+ * including TLS termination and passthrough routes.
6
+ */
7
+
8
+ import type { IRouteConfig, IRouteMatch, IRouteAction } from '../../models/route-types.js';
9
+ import { SocketHandlers } from './socket-handlers.js';
10
+
11
+ /**
12
+ * Create an HTTPS route with TLS termination
13
+ * @param domains Domain(s) to match
14
+ * @param target Target host and port
15
+ * @param options Additional route options
16
+ * @returns Route configuration object
17
+ */
18
+ export function createHttpsTerminateRoute(
19
+ domains: string | string[],
20
+ target: { host: string | string[]; port: number },
21
+ options: {
22
+ certificate?: 'auto' | { key: string; cert: string };
23
+ httpPort?: number | number[];
24
+ httpsPort?: number | number[];
25
+ reencrypt?: boolean;
26
+ name?: string;
27
+ [key: string]: any;
28
+ } = {}
29
+ ): IRouteConfig {
30
+ // Create route match
31
+ const match: IRouteMatch = {
32
+ ports: options.httpsPort || 443,
33
+ domains
34
+ };
35
+
36
+ // Create route action
37
+ const action: IRouteAction = {
38
+ type: 'forward',
39
+ targets: [target],
40
+ tls: {
41
+ mode: options.reencrypt ? 'terminate-and-reencrypt' : 'terminate',
42
+ certificate: options.certificate || 'auto'
43
+ }
44
+ };
45
+
46
+ // Create the route config
47
+ return {
48
+ match,
49
+ action,
50
+ name: options.name || `HTTPS Route for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
51
+ ...options
52
+ };
53
+ }
54
+
55
+ /**
56
+ * Create an HTTP to HTTPS redirect route
57
+ * @param domains Domain(s) to match
58
+ * @param httpsPort HTTPS port to redirect to (default: 443)
59
+ * @param options Additional route options
60
+ * @returns Route configuration object
61
+ */
62
+ export function createHttpToHttpsRedirect(
63
+ domains: string | string[],
64
+ httpsPort: number = 443,
65
+ options: Partial<IRouteConfig> = {}
66
+ ): IRouteConfig {
67
+ // Create route match
68
+ const match: IRouteMatch = {
69
+ ports: options.match?.ports || 80,
70
+ domains
71
+ };
72
+
73
+ // Create route action
74
+ const action: IRouteAction = {
75
+ type: 'socket-handler',
76
+ socketHandler: SocketHandlers.httpRedirect(`https://{domain}:${httpsPort}{path}`, 301)
77
+ };
78
+
79
+ // Create the route config
80
+ return {
81
+ match,
82
+ action,
83
+ name: options.name || `HTTP to HTTPS Redirect for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
84
+ ...options
85
+ };
86
+ }
87
+
88
+ /**
89
+ * Create an HTTPS passthrough route (SNI-based forwarding without TLS termination)
90
+ * @param domains Domain(s) to match
91
+ * @param target Target host and port
92
+ * @param options Additional route options
93
+ * @returns Route configuration object
94
+ */
95
+ export function createHttpsPassthroughRoute(
96
+ domains: string | string[],
97
+ target: { host: string | string[]; port: number },
98
+ options: Partial<IRouteConfig> = {}
99
+ ): IRouteConfig {
100
+ // Create route match
101
+ const match: IRouteMatch = {
102
+ ports: options.match?.ports || 443,
103
+ domains
104
+ };
105
+
106
+ // Create route action
107
+ const action: IRouteAction = {
108
+ type: 'forward',
109
+ targets: [target],
110
+ tls: {
111
+ mode: 'passthrough'
112
+ }
113
+ };
114
+
115
+ // Create the route config
116
+ return {
117
+ match,
118
+ action,
119
+ name: options.name || `HTTPS Passthrough for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
120
+ ...options
121
+ };
122
+ }
123
+
124
+ /**
125
+ * Create a complete HTTPS server with HTTP to HTTPS redirects
126
+ * @param domains Domain(s) to match
127
+ * @param target Target host and port
128
+ * @param options Additional configuration options
129
+ * @returns Array of two route configurations (HTTPS and HTTP redirect)
130
+ */
131
+ export function createCompleteHttpsServer(
132
+ domains: string | string[],
133
+ target: { host: string | string[]; port: number },
134
+ options: {
135
+ certificate?: 'auto' | { key: string; cert: string };
136
+ httpPort?: number | number[];
137
+ httpsPort?: number | number[];
138
+ reencrypt?: boolean;
139
+ name?: string;
140
+ [key: string]: any;
141
+ } = {}
142
+ ): IRouteConfig[] {
143
+ // Create the HTTPS route
144
+ const httpsRoute = createHttpsTerminateRoute(domains, target, options);
145
+
146
+ // Create the HTTP redirect route
147
+ const httpRedirectRoute = createHttpToHttpsRedirect(
148
+ domains,
149
+ // Extract the HTTPS port from the HTTPS route - ensure it's a number
150
+ typeof options.httpsPort === 'number' ? options.httpsPort :
151
+ Array.isArray(options.httpsPort) ? options.httpsPort[0] : 443,
152
+ {
153
+ // Set the HTTP port
154
+ match: {
155
+ ports: options.httpPort || 80,
156
+ domains
157
+ },
158
+ name: `HTTP to HTTPS Redirect for ${Array.isArray(domains) ? domains.join(', ') : domains}`
159
+ }
160
+ );
161
+
162
+ return [httpsRoute, httpRedirectRoute];
163
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Route Helper Functions
3
+ *
4
+ * This module 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 barrel file re-exports all helper functions for backwards compatibility.
8
+ */
9
+
10
+ // HTTP helpers
11
+ export { createHttpRoute } from './http-helpers.js';
12
+
13
+ // HTTPS helpers
14
+ export {
15
+ createHttpsTerminateRoute,
16
+ createHttpToHttpsRedirect,
17
+ createHttpsPassthroughRoute,
18
+ createCompleteHttpsServer
19
+ } from './https-helpers.js';
20
+
21
+ // WebSocket helpers
22
+ export { createWebSocketRoute } from './websocket-helpers.js';
23
+
24
+ // Load balancer helpers
25
+ export {
26
+ createLoadBalancerRoute,
27
+ createSmartLoadBalancer
28
+ } from './load-balancer-helpers.js';
29
+
30
+ // NFTables helpers
31
+ export {
32
+ createNfTablesRoute,
33
+ createNfTablesTerminateRoute,
34
+ createCompleteNfTablesHttpsServer
35
+ } from './nftables-helpers.js';
36
+
37
+ // Dynamic routing helpers
38
+ export {
39
+ createPortOffset,
40
+ createPortMappingRoute,
41
+ createOffsetPortMappingRoute,
42
+ createDynamicRoute
43
+ } from './dynamic-helpers.js';
44
+
45
+ // API helpers
46
+ export {
47
+ createApiRoute,
48
+ createApiGatewayRoute
49
+ } from './api-helpers.js';
50
+
51
+ // Security helpers
52
+ export {
53
+ addRateLimiting,
54
+ addBasicAuth,
55
+ addJwtAuth
56
+ } from './security-helpers.js';
57
+
58
+ // Socket handlers
59
+ export {
60
+ SocketHandlers,
61
+ createSocketHandlerRoute
62
+ } from './socket-handlers.js';
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Load Balancer Route Helper Functions
3
+ *
4
+ * This module provides utility functions for creating load balancer route configurations.
5
+ */
6
+
7
+ import type { IRouteConfig, IRouteMatch, IRouteAction, IRouteTarget, TPortRange, IRouteContext } from '../../models/route-types.js';
8
+
9
+ /**
10
+ * Create a load balancer route (round-robin between multiple backend hosts)
11
+ * @param domains Domain(s) to match
12
+ * @param backendsOrHosts Array of backend servers OR array of host strings (legacy)
13
+ * @param portOrOptions Port number (legacy) OR options object
14
+ * @param options Additional route options (legacy)
15
+ * @returns Route configuration object
16
+ */
17
+ export function createLoadBalancerRoute(
18
+ domains: string | string[],
19
+ backendsOrHosts: Array<{ host: string; port: number }> | string[],
20
+ portOrOptions?: number | {
21
+ tls?: {
22
+ mode: 'passthrough' | 'terminate' | 'terminate-and-reencrypt';
23
+ certificate?: 'auto' | { key: string; cert: string };
24
+ };
25
+ useTls?: boolean;
26
+ certificate?: 'auto' | { key: string; cert: string };
27
+ algorithm?: 'round-robin' | 'least-connections' | 'ip-hash';
28
+ healthCheck?: {
29
+ path: string;
30
+ interval: number;
31
+ timeout: number;
32
+ unhealthyThreshold: number;
33
+ healthyThreshold: number;
34
+ };
35
+ [key: string]: any;
36
+ },
37
+ options?: {
38
+ tls?: {
39
+ mode: 'passthrough' | 'terminate' | 'terminate-and-reencrypt';
40
+ certificate?: 'auto' | { key: string; cert: string };
41
+ };
42
+ [key: string]: any;
43
+ }
44
+ ): IRouteConfig {
45
+ // Handle legacy signature: (domains, hosts[], port, options)
46
+ let backends: Array<{ host: string; port: number }>;
47
+ let finalOptions: any;
48
+
49
+ if (Array.isArray(backendsOrHosts) && backendsOrHosts.length > 0 && typeof backendsOrHosts[0] === 'string') {
50
+ // Legacy signature
51
+ const hosts = backendsOrHosts as string[];
52
+ const port = portOrOptions as number;
53
+ backends = hosts.map(host => ({ host, port }));
54
+ finalOptions = options || {};
55
+ } else {
56
+ // New signature
57
+ backends = backendsOrHosts as Array<{ host: string; port: number }>;
58
+ finalOptions = (portOrOptions as any) || {};
59
+ }
60
+
61
+ // Extract hosts and ensure all backends use the same port
62
+ const port = backends[0].port;
63
+ const hosts = backends.map(backend => backend.host);
64
+
65
+ // Create route match
66
+ const match: IRouteMatch = {
67
+ ports: finalOptions.match?.ports || (finalOptions.tls || finalOptions.useTls ? 443 : 80),
68
+ domains
69
+ };
70
+
71
+ // Create route target
72
+ const target: IRouteTarget = {
73
+ host: hosts,
74
+ port
75
+ };
76
+
77
+ // Create route action
78
+ const action: IRouteAction = {
79
+ type: 'forward',
80
+ targets: [target]
81
+ };
82
+
83
+ // Add TLS configuration if provided
84
+ if (finalOptions.tls || finalOptions.useTls) {
85
+ action.tls = {
86
+ mode: finalOptions.tls?.mode || 'terminate',
87
+ certificate: finalOptions.tls?.certificate || finalOptions.certificate || 'auto'
88
+ };
89
+ }
90
+
91
+ // Add load balancing options
92
+ if (finalOptions.algorithm || finalOptions.healthCheck) {
93
+ action.loadBalancing = {
94
+ algorithm: finalOptions.algorithm || 'round-robin',
95
+ healthCheck: finalOptions.healthCheck
96
+ };
97
+ }
98
+
99
+ // Create the route config
100
+ return {
101
+ match,
102
+ action,
103
+ name: finalOptions.name || `Load Balancer for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
104
+ ...finalOptions
105
+ };
106
+ }
107
+
108
+ /**
109
+ * Create a smart load balancer with dynamic domain-based backend selection
110
+ * @param options Smart load balancer options
111
+ * @returns Route configuration object
112
+ */
113
+ export function createSmartLoadBalancer(options: {
114
+ ports: TPortRange;
115
+ domainTargets: Record<string, string | string[]>;
116
+ portMapper: (context: IRouteContext) => number;
117
+ name?: string;
118
+ defaultTarget?: string | string[];
119
+ priority?: number;
120
+ [key: string]: any;
121
+ }): IRouteConfig {
122
+ // Extract all domain keys to create the match criteria
123
+ const domains = Object.keys(options.domainTargets);
124
+
125
+ // Create the smart host selector function
126
+ const hostSelector = (context: IRouteContext) => {
127
+ const domain = context.domain || '';
128
+ return options.domainTargets[domain] || options.defaultTarget || 'localhost';
129
+ };
130
+
131
+ // Create route match
132
+ const match: IRouteMatch = {
133
+ ports: options.ports,
134
+ domains
135
+ };
136
+
137
+ // Create route action
138
+ const action: IRouteAction = {
139
+ type: 'forward',
140
+ targets: [{
141
+ host: hostSelector,
142
+ port: options.portMapper
143
+ }]
144
+ };
145
+
146
+ // Create the route config
147
+ return {
148
+ match,
149
+ action,
150
+ name: options.name || `Smart Load Balancer for ${domains.join(', ')}`,
151
+ priority: options.priority,
152
+ ...options
153
+ };
154
+ }
@@ -0,0 +1,202 @@
1
+ /**
2
+ * NFTables Route Helper Functions
3
+ *
4
+ * This module provides utility functions for creating NFTables-based route configurations
5
+ * for high-performance packet forwarding at the kernel level.
6
+ */
7
+
8
+ import type { IRouteConfig, IRouteMatch, IRouteAction, TPortRange } from '../../models/route-types.js';
9
+ import { createHttpToHttpsRedirect } from './https-helpers.js';
10
+
11
+ /**
12
+ * Create an NFTables-based route for high-performance packet forwarding
13
+ * @param nameOrDomains Name or domain(s) to match
14
+ * @param target Target host and port
15
+ * @param options Additional route options
16
+ * @returns Route configuration object
17
+ */
18
+ export function createNfTablesRoute(
19
+ nameOrDomains: string | string[],
20
+ target: { host: string; port: number | 'preserve' },
21
+ options: {
22
+ ports?: TPortRange;
23
+ protocol?: 'tcp' | 'udp' | 'all';
24
+ preserveSourceIP?: boolean;
25
+ ipAllowList?: string[];
26
+ ipBlockList?: string[];
27
+ maxRate?: string;
28
+ priority?: number;
29
+ useTls?: boolean;
30
+ tableName?: string;
31
+ useIPSets?: boolean;
32
+ useAdvancedNAT?: boolean;
33
+ } = {}
34
+ ): IRouteConfig {
35
+ // Determine if this is a name or domain
36
+ let name: string;
37
+ let domains: string | string[] | undefined;
38
+
39
+ if (Array.isArray(nameOrDomains) || (typeof nameOrDomains === 'string' && nameOrDomains.includes('.'))) {
40
+ domains = nameOrDomains;
41
+ name = Array.isArray(nameOrDomains) ? nameOrDomains[0] : nameOrDomains;
42
+ } else {
43
+ name = nameOrDomains;
44
+ domains = undefined; // No domains
45
+ }
46
+
47
+ // Create route match
48
+ const match: IRouteMatch = {
49
+ domains,
50
+ ports: options.ports || 80
51
+ };
52
+
53
+ // Create route action
54
+ const action: IRouteAction = {
55
+ type: 'forward',
56
+ targets: [{
57
+ host: target.host,
58
+ port: target.port
59
+ }],
60
+ forwardingEngine: 'nftables',
61
+ nftables: {
62
+ protocol: options.protocol || 'tcp',
63
+ preserveSourceIP: options.preserveSourceIP,
64
+ maxRate: options.maxRate,
65
+ priority: options.priority,
66
+ tableName: options.tableName,
67
+ useIPSets: options.useIPSets,
68
+ useAdvancedNAT: options.useAdvancedNAT
69
+ }
70
+ };
71
+
72
+ // Add TLS options if needed
73
+ if (options.useTls) {
74
+ action.tls = {
75
+ mode: 'passthrough'
76
+ };
77
+ }
78
+
79
+ // Create the route config
80
+ const routeConfig: IRouteConfig = {
81
+ name,
82
+ match,
83
+ action
84
+ };
85
+
86
+ // Add security if allowed or blocked IPs are specified
87
+ if (options.ipAllowList?.length || options.ipBlockList?.length) {
88
+ routeConfig.security = {
89
+ ipAllowList: options.ipAllowList,
90
+ ipBlockList: options.ipBlockList
91
+ };
92
+ }
93
+
94
+ return routeConfig;
95
+ }
96
+
97
+ /**
98
+ * Create an NFTables-based TLS termination route
99
+ * @param nameOrDomains Name or domain(s) to match
100
+ * @param target Target host and port
101
+ * @param options Additional route options
102
+ * @returns Route configuration object
103
+ */
104
+ export function createNfTablesTerminateRoute(
105
+ nameOrDomains: string | string[],
106
+ target: { host: string; port: number | 'preserve' },
107
+ options: {
108
+ ports?: TPortRange;
109
+ protocol?: 'tcp' | 'udp' | 'all';
110
+ preserveSourceIP?: boolean;
111
+ ipAllowList?: string[];
112
+ ipBlockList?: string[];
113
+ maxRate?: string;
114
+ priority?: number;
115
+ tableName?: string;
116
+ useIPSets?: boolean;
117
+ useAdvancedNAT?: boolean;
118
+ certificate?: 'auto' | { key: string; cert: string };
119
+ } = {}
120
+ ): IRouteConfig {
121
+ // Create basic NFTables route
122
+ const route = createNfTablesRoute(
123
+ nameOrDomains,
124
+ target,
125
+ {
126
+ ...options,
127
+ ports: options.ports || 443,
128
+ useTls: false
129
+ }
130
+ );
131
+
132
+ // Set TLS termination
133
+ route.action.tls = {
134
+ mode: 'terminate',
135
+ certificate: options.certificate || 'auto'
136
+ };
137
+
138
+ return route;
139
+ }
140
+
141
+ /**
142
+ * Create a complete NFTables-based HTTPS setup with HTTP redirect
143
+ * @param nameOrDomains Name or domain(s) to match
144
+ * @param target Target host and port
145
+ * @param options Additional route options
146
+ * @returns Array of two route configurations (HTTPS and HTTP redirect)
147
+ */
148
+ export function createCompleteNfTablesHttpsServer(
149
+ nameOrDomains: string | string[],
150
+ target: { host: string; port: number | 'preserve' },
151
+ options: {
152
+ httpPort?: TPortRange;
153
+ httpsPort?: TPortRange;
154
+ protocol?: 'tcp' | 'udp' | 'all';
155
+ preserveSourceIP?: boolean;
156
+ ipAllowList?: string[];
157
+ ipBlockList?: string[];
158
+ maxRate?: string;
159
+ priority?: number;
160
+ tableName?: string;
161
+ useIPSets?: boolean;
162
+ useAdvancedNAT?: boolean;
163
+ certificate?: 'auto' | { key: string; cert: string };
164
+ } = {}
165
+ ): IRouteConfig[] {
166
+ // Create the HTTPS route using NFTables
167
+ const httpsRoute = createNfTablesTerminateRoute(
168
+ nameOrDomains,
169
+ target,
170
+ {
171
+ ...options,
172
+ ports: options.httpsPort || 443
173
+ }
174
+ );
175
+
176
+ // Determine the domain(s) for HTTP redirect
177
+ const domains = typeof nameOrDomains === 'string' && !nameOrDomains.includes('.')
178
+ ? undefined
179
+ : nameOrDomains;
180
+
181
+ // Extract the HTTPS port for the redirect destination
182
+ const httpsPort = typeof options.httpsPort === 'number'
183
+ ? options.httpsPort
184
+ : Array.isArray(options.httpsPort) && typeof options.httpsPort[0] === 'number'
185
+ ? options.httpsPort[0]
186
+ : 443;
187
+
188
+ // Create the HTTP redirect route (this uses standard forwarding, not NFTables)
189
+ const httpRedirectRoute = createHttpToHttpsRedirect(
190
+ domains as any, // Type cast needed since domains can be undefined now
191
+ httpsPort,
192
+ {
193
+ match: {
194
+ ports: options.httpPort || 80,
195
+ domains: domains as any // Type cast needed since domains can be undefined now
196
+ },
197
+ name: `HTTP to HTTPS Redirect for ${Array.isArray(domains) ? domains.join(', ') : domains || 'all domains'}`
198
+ }
199
+ );
200
+
201
+ return [httpsRoute, httpRedirectRoute];
202
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Security Route Helper Functions
3
+ *
4
+ * This module provides utility functions for adding security features to routes.
5
+ */
6
+
7
+ import type { IRouteConfig } from '../../models/route-types.js';
8
+ import { mergeRouteConfigs } from '../route-utils.js';
9
+
10
+ /**
11
+ * Create a rate limiting route pattern
12
+ * @param baseRoute Base route to add rate limiting to
13
+ * @param rateLimit Rate limiting configuration
14
+ * @returns Route with rate limiting
15
+ */
16
+ export function addRateLimiting(
17
+ baseRoute: IRouteConfig,
18
+ rateLimit: {
19
+ maxRequests: number;
20
+ window: number; // Time window in seconds
21
+ keyBy?: 'ip' | 'path' | 'header';
22
+ headerName?: string; // Required if keyBy is 'header'
23
+ errorMessage?: string;
24
+ }
25
+ ): IRouteConfig {
26
+ return mergeRouteConfigs(baseRoute, {
27
+ security: {
28
+ rateLimit: {
29
+ enabled: true,
30
+ maxRequests: rateLimit.maxRequests,
31
+ window: rateLimit.window,
32
+ keyBy: rateLimit.keyBy || 'ip',
33
+ headerName: rateLimit.headerName,
34
+ errorMessage: rateLimit.errorMessage || 'Rate limit exceeded. Please try again later.'
35
+ }
36
+ }
37
+ });
38
+ }
39
+
40
+ /**
41
+ * Create a basic authentication route pattern
42
+ * @param baseRoute Base route to add authentication to
43
+ * @param auth Authentication configuration
44
+ * @returns Route with basic authentication
45
+ */
46
+ export function addBasicAuth(
47
+ baseRoute: IRouteConfig,
48
+ auth: {
49
+ users: Array<{ username: string; password: string }>;
50
+ realm?: string;
51
+ excludePaths?: string[];
52
+ }
53
+ ): IRouteConfig {
54
+ return mergeRouteConfigs(baseRoute, {
55
+ security: {
56
+ basicAuth: {
57
+ enabled: true,
58
+ users: auth.users,
59
+ realm: auth.realm || 'Restricted Area',
60
+ excludePaths: auth.excludePaths || []
61
+ }
62
+ }
63
+ });
64
+ }
65
+
66
+ /**
67
+ * Create a JWT authentication route pattern
68
+ * @param baseRoute Base route to add JWT authentication to
69
+ * @param jwt JWT authentication configuration
70
+ * @returns Route with JWT authentication
71
+ */
72
+ export function addJwtAuth(
73
+ baseRoute: IRouteConfig,
74
+ jwt: {
75
+ secret: string;
76
+ algorithm?: string;
77
+ issuer?: string;
78
+ audience?: string;
79
+ expiresIn?: number; // Time in seconds
80
+ excludePaths?: string[];
81
+ }
82
+ ): IRouteConfig {
83
+ return mergeRouteConfigs(baseRoute, {
84
+ security: {
85
+ jwtAuth: {
86
+ enabled: true,
87
+ secret: jwt.secret,
88
+ algorithm: jwt.algorithm || 'HS256',
89
+ issuer: jwt.issuer,
90
+ audience: jwt.audience,
91
+ expiresIn: jwt.expiresIn,
92
+ excludePaths: jwt.excludePaths || []
93
+ }
94
+ }
95
+ });
96
+ }