@push.rocks/smartproxy 21.1.7 → 22.4.2
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.
- package/changelog.md +81 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/core/utils/shared-security-manager.d.ts +17 -0
- package/dist_ts/core/utils/shared-security-manager.js +66 -1
- package/dist_ts/proxies/http-proxy/default-certificates.d.ts +54 -0
- package/dist_ts/proxies/http-proxy/default-certificates.js +127 -0
- package/dist_ts/proxies/http-proxy/http-proxy.d.ts +1 -1
- package/dist_ts/proxies/http-proxy/http-proxy.js +9 -14
- package/dist_ts/proxies/http-proxy/index.d.ts +5 -1
- package/dist_ts/proxies/http-proxy/index.js +6 -2
- package/dist_ts/proxies/http-proxy/security-manager.d.ts +4 -12
- package/dist_ts/proxies/http-proxy/security-manager.js +66 -99
- package/dist_ts/proxies/nftables-proxy/index.d.ts +1 -0
- package/dist_ts/proxies/nftables-proxy/index.js +2 -1
- package/dist_ts/proxies/nftables-proxy/nftables-proxy.d.ts +4 -26
- package/dist_ts/proxies/nftables-proxy/nftables-proxy.js +84 -236
- package/dist_ts/proxies/nftables-proxy/utils/index.d.ts +9 -0
- package/dist_ts/proxies/nftables-proxy/utils/index.js +12 -0
- package/dist_ts/proxies/nftables-proxy/utils/nft-command-executor.d.ts +66 -0
- package/dist_ts/proxies/nftables-proxy/utils/nft-command-executor.js +131 -0
- package/dist_ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.d.ts +39 -0
- package/dist_ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.js +112 -0
- package/dist_ts/proxies/nftables-proxy/utils/nft-rule-validator.d.ts +59 -0
- package/dist_ts/proxies/nftables-proxy/utils/nft-rule-validator.js +130 -0
- package/dist_ts/proxies/smart-proxy/certificate-manager.js +4 -3
- package/dist_ts/proxies/smart-proxy/connection-manager.d.ts +13 -2
- package/dist_ts/proxies/smart-proxy/connection-manager.js +16 -6
- package/dist_ts/proxies/smart-proxy/http-proxy-bridge.js +35 -10
- package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +0 -1
- package/dist_ts/proxies/smart-proxy/route-connection-handler.d.ts +17 -0
- package/dist_ts/proxies/smart-proxy/route-connection-handler.js +72 -9
- package/dist_ts/proxies/smart-proxy/security-manager.d.ts +14 -12
- package/dist_ts/proxies/smart-proxy/security-manager.js +80 -74
- package/dist_ts/proxies/smart-proxy/smart-proxy.js +1 -2
- package/dist_ts/proxies/smart-proxy/tls-manager.d.ts +2 -9
- package/dist_ts/proxies/smart-proxy/tls-manager.js +3 -26
- package/dist_ts/proxies/smart-proxy/utils/index.d.ts +1 -1
- package/dist_ts/proxies/smart-proxy/utils/index.js +3 -4
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/api-helpers.d.ts +49 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/api-helpers.js +108 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/dynamic-helpers.d.ts +57 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/dynamic-helpers.js +89 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/http-helpers.d.ts +17 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/http-helpers.js +32 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/https-helpers.d.ts +68 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/https-helpers.js +117 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/index.d.ts +17 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/index.js +27 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/load-balancer-helpers.d.ts +63 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/load-balancer-helpers.js +105 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/nftables-helpers.d.ts +83 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/nftables-helpers.js +126 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/security-helpers.d.ts +47 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/security-helpers.js +66 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.d.ts +70 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.js +287 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/websocket-helpers.d.ts +46 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers/websocket-helpers.js +67 -0
- package/dist_ts/proxies/smart-proxy/utils/route-helpers.d.ts +4 -457
- package/dist_ts/proxies/smart-proxy/utils/route-helpers.js +6 -950
- package/dist_ts/proxies/smart-proxy/utils/route-utils.js +2 -2
- package/dist_ts/proxies/smart-proxy/utils/route-validator.d.ts +67 -1
- package/dist_ts/proxies/smart-proxy/utils/route-validator.js +251 -3
- package/npmextra.json +12 -6
- package/package.json +34 -24
- package/readme.hints.md +184 -1
- package/readme.md +235 -172
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/core/utils/shared-security-manager.ts +98 -13
- package/ts/proxies/http-proxy/default-certificates.ts +150 -0
- package/ts/proxies/http-proxy/http-proxy.ts +9 -15
- package/ts/proxies/http-proxy/index.ts +6 -1
- package/ts/proxies/http-proxy/security-manager.ts +141 -161
- package/ts/proxies/nftables-proxy/index.ts +1 -0
- package/ts/proxies/nftables-proxy/nftables-proxy.ts +116 -290
- package/ts/proxies/nftables-proxy/utils/index.ts +38 -0
- package/ts/proxies/nftables-proxy/utils/nft-command-executor.ts +162 -0
- package/ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.ts +125 -0
- package/ts/proxies/nftables-proxy/utils/nft-rule-validator.ts +156 -0
- package/ts/proxies/smart-proxy/certificate-manager.ts +3 -2
- package/ts/proxies/smart-proxy/connection-manager.ts +21 -8
- package/ts/proxies/smart-proxy/http-proxy-bridge.ts +39 -13
- package/ts/proxies/smart-proxy/models/interfaces.ts +0 -1
- package/ts/proxies/smart-proxy/route-connection-handler.ts +88 -16
- package/ts/proxies/smart-proxy/security-manager.ts +98 -86
- package/ts/proxies/smart-proxy/smart-proxy.ts +0 -2
- package/ts/proxies/smart-proxy/tls-manager.ts +1 -37
- package/ts/proxies/smart-proxy/utils/index.ts +3 -5
- package/ts/proxies/smart-proxy/utils/route-helpers/api-helpers.ts +144 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/dynamic-helpers.ts +124 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/http-helpers.ts +40 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/https-helpers.ts +163 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/index.ts +62 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/load-balancer-helpers.ts +154 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/nftables-helpers.ts +202 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/security-helpers.ts +96 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.ts +337 -0
- package/ts/proxies/smart-proxy/utils/route-helpers/websocket-helpers.ts +98 -0
- package/ts/proxies/smart-proxy/utils/route-helpers.ts +5 -1302
- package/ts/proxies/smart-proxy/utils/route-utils.ts +1 -1
- package/ts/proxies/smart-proxy/utils/route-validator.ts +274 -4
- package/ts/proxies/http-proxy/certificate-manager.ts +0 -244
- package/ts/proxies/smart-proxy/utils/route-validators.ts +0 -283
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dynamic Route Helper Functions
|
|
3
|
+
*
|
|
4
|
+
* This module provides utility functions for creating dynamic routes
|
|
5
|
+
* with context-based host and port mapping.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { IRouteConfig, IRouteMatch, IRouteAction, TPortRange, IRouteContext } from '../../models/route-types.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Create a helper function that applies a port offset
|
|
12
|
+
* @param offset The offset to apply to the matched port
|
|
13
|
+
* @returns A function that adds the offset to the matched port
|
|
14
|
+
*/
|
|
15
|
+
export function createPortOffset(offset: number): (context: IRouteContext) => number {
|
|
16
|
+
return (context: IRouteContext) => context.port + offset;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a port mapping route with context-based port function
|
|
21
|
+
* @param options Port mapping route options
|
|
22
|
+
* @returns Route configuration object
|
|
23
|
+
*/
|
|
24
|
+
export function createPortMappingRoute(options: {
|
|
25
|
+
sourcePortRange: TPortRange;
|
|
26
|
+
targetHost: string | string[] | ((context: IRouteContext) => string | string[]);
|
|
27
|
+
portMapper: (context: IRouteContext) => number;
|
|
28
|
+
name?: string;
|
|
29
|
+
domains?: string | string[];
|
|
30
|
+
priority?: number;
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
}): IRouteConfig {
|
|
33
|
+
// Create route match
|
|
34
|
+
const match: IRouteMatch = {
|
|
35
|
+
ports: options.sourcePortRange,
|
|
36
|
+
domains: options.domains
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Create route action
|
|
40
|
+
const action: IRouteAction = {
|
|
41
|
+
type: 'forward',
|
|
42
|
+
targets: [{
|
|
43
|
+
host: options.targetHost,
|
|
44
|
+
port: options.portMapper
|
|
45
|
+
}]
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Create the route config
|
|
49
|
+
return {
|
|
50
|
+
match,
|
|
51
|
+
action,
|
|
52
|
+
name: options.name || `Port Mapping Route for ${options.domains || 'all domains'}`,
|
|
53
|
+
priority: options.priority,
|
|
54
|
+
...options
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Create a simple offset port mapping route
|
|
60
|
+
* @param options Offset port mapping route options
|
|
61
|
+
* @returns Route configuration object
|
|
62
|
+
*/
|
|
63
|
+
export function createOffsetPortMappingRoute(options: {
|
|
64
|
+
ports: TPortRange;
|
|
65
|
+
targetHost: string | string[];
|
|
66
|
+
offset: number;
|
|
67
|
+
name?: string;
|
|
68
|
+
domains?: string | string[];
|
|
69
|
+
priority?: number;
|
|
70
|
+
[key: string]: any;
|
|
71
|
+
}): IRouteConfig {
|
|
72
|
+
return createPortMappingRoute({
|
|
73
|
+
sourcePortRange: options.ports,
|
|
74
|
+
targetHost: options.targetHost,
|
|
75
|
+
portMapper: (context) => context.port + options.offset,
|
|
76
|
+
name: options.name || `Offset Mapping (${options.offset > 0 ? '+' : ''}${options.offset}) for ${options.domains || 'all domains'}`,
|
|
77
|
+
domains: options.domains,
|
|
78
|
+
priority: options.priority,
|
|
79
|
+
...options
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Create a dynamic route with context-based host and port mapping
|
|
85
|
+
* @param options Dynamic route options
|
|
86
|
+
* @returns Route configuration object
|
|
87
|
+
*/
|
|
88
|
+
export function createDynamicRoute(options: {
|
|
89
|
+
ports: TPortRange;
|
|
90
|
+
targetHost: (context: IRouteContext) => string | string[];
|
|
91
|
+
portMapper: (context: IRouteContext) => number;
|
|
92
|
+
name?: string;
|
|
93
|
+
domains?: string | string[];
|
|
94
|
+
path?: string;
|
|
95
|
+
clientIp?: string[];
|
|
96
|
+
priority?: number;
|
|
97
|
+
[key: string]: any;
|
|
98
|
+
}): IRouteConfig {
|
|
99
|
+
// Create route match
|
|
100
|
+
const match: IRouteMatch = {
|
|
101
|
+
ports: options.ports,
|
|
102
|
+
domains: options.domains,
|
|
103
|
+
path: options.path,
|
|
104
|
+
clientIp: options.clientIp
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Create route action
|
|
108
|
+
const action: IRouteAction = {
|
|
109
|
+
type: 'forward',
|
|
110
|
+
targets: [{
|
|
111
|
+
host: options.targetHost,
|
|
112
|
+
port: options.portMapper
|
|
113
|
+
}]
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// Create the route config
|
|
117
|
+
return {
|
|
118
|
+
match,
|
|
119
|
+
action,
|
|
120
|
+
name: options.name || `Dynamic Route for ${options.domains || 'all domains'}`,
|
|
121
|
+
priority: options.priority,
|
|
122
|
+
...options
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Route Helper Functions
|
|
3
|
+
*
|
|
4
|
+
* This module provides utility functions for creating HTTP route configurations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { IRouteConfig, IRouteMatch, IRouteAction } from '../../models/route-types.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Create an HTTP-only route configuration
|
|
11
|
+
* @param domains Domain(s) to match
|
|
12
|
+
* @param target Target host and port
|
|
13
|
+
* @param options Additional route options
|
|
14
|
+
* @returns Route configuration object
|
|
15
|
+
*/
|
|
16
|
+
export function createHttpRoute(
|
|
17
|
+
domains: string | string[],
|
|
18
|
+
target: { host: string | string[]; port: number },
|
|
19
|
+
options: Partial<IRouteConfig> = {}
|
|
20
|
+
): IRouteConfig {
|
|
21
|
+
// Create route match
|
|
22
|
+
const match: IRouteMatch = {
|
|
23
|
+
ports: options.match?.ports || 80,
|
|
24
|
+
domains
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Create route action
|
|
28
|
+
const action: IRouteAction = {
|
|
29
|
+
type: 'forward',
|
|
30
|
+
targets: [target]
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Create the route config
|
|
34
|
+
return {
|
|
35
|
+
match,
|
|
36
|
+
action,
|
|
37
|
+
name: options.name || `HTTP Route for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
|
|
38
|
+
...options
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -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
|
+
}
|