@push.rocks/smartproxy 22.4.2 → 23.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +36 -0
- package/dist_rust/rustproxy +0 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/index.d.ts +1 -6
- package/dist_ts/index.js +3 -11
- package/dist_ts/protocols/common/fragment-handler.js +5 -1
- package/dist_ts/proxies/index.d.ts +1 -6
- package/dist_ts/proxies/index.js +2 -8
- package/dist_ts/proxies/smart-proxy/index.d.ts +5 -10
- package/dist_ts/proxies/smart-proxy/index.js +7 -13
- package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +5 -2
- package/dist_ts/proxies/smart-proxy/models/route-types.js +1 -1
- package/dist_ts/proxies/smart-proxy/route-preprocessor.d.ts +37 -0
- package/dist_ts/proxies/smart-proxy/route-preprocessor.js +103 -0
- package/dist_ts/proxies/smart-proxy/rust-binary-locator.d.ts +23 -0
- package/dist_ts/proxies/smart-proxy/rust-binary-locator.js +104 -0
- package/dist_ts/proxies/smart-proxy/rust-metrics-adapter.d.ts +74 -0
- package/dist_ts/proxies/smart-proxy/rust-metrics-adapter.js +146 -0
- package/dist_ts/proxies/smart-proxy/rust-proxy-bridge.d.ts +49 -0
- package/dist_ts/proxies/smart-proxy/rust-proxy-bridge.js +259 -0
- package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +39 -157
- package/dist_ts/proxies/smart-proxy/smart-proxy.js +224 -621
- package/dist_ts/proxies/smart-proxy/socket-handler-server.d.ts +45 -0
- package/dist_ts/proxies/smart-proxy/socket-handler-server.js +253 -0
- package/dist_ts/routing/index.d.ts +1 -1
- package/dist_ts/routing/index.js +3 -3
- package/dist_ts/routing/models/http-types.d.ts +119 -4
- package/dist_ts/routing/models/http-types.js +93 -5
- package/package.json +1 -1
- package/readme.md +444 -219
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/index.ts +4 -15
- package/ts/protocols/common/fragment-handler.ts +4 -0
- package/ts/proxies/index.ts +1 -12
- package/ts/proxies/smart-proxy/index.ts +6 -13
- package/ts/proxies/smart-proxy/models/interfaces.ts +6 -4
- package/ts/proxies/smart-proxy/models/route-types.ts +0 -2
- package/ts/proxies/smart-proxy/route-preprocessor.ts +122 -0
- package/ts/proxies/smart-proxy/rust-binary-locator.ts +112 -0
- package/ts/proxies/smart-proxy/rust-metrics-adapter.ts +161 -0
- package/ts/proxies/smart-proxy/rust-proxy-bridge.ts +310 -0
- package/ts/proxies/smart-proxy/smart-proxy.ts +282 -798
- package/ts/proxies/smart-proxy/socket-handler-server.ts +279 -0
- package/ts/routing/index.ts +2 -2
- package/ts/routing/models/http-types.ts +147 -4
- package/dist_ts/proxies/nftables-proxy/index.d.ts +0 -6
- package/dist_ts/proxies/nftables-proxy/index.js +0 -7
- package/dist_ts/proxies/nftables-proxy/models/errors.d.ts +0 -15
- package/dist_ts/proxies/nftables-proxy/models/errors.js +0 -28
- package/dist_ts/proxies/nftables-proxy/models/index.d.ts +0 -5
- package/dist_ts/proxies/nftables-proxy/models/index.js +0 -6
- package/dist_ts/proxies/nftables-proxy/models/interfaces.d.ts +0 -75
- package/dist_ts/proxies/nftables-proxy/models/interfaces.js +0 -5
- package/dist_ts/proxies/nftables-proxy/nftables-proxy.d.ts +0 -124
- package/dist_ts/proxies/nftables-proxy/nftables-proxy.js +0 -1374
- package/dist_ts/proxies/nftables-proxy/utils/index.d.ts +0 -9
- package/dist_ts/proxies/nftables-proxy/utils/index.js +0 -12
- package/dist_ts/proxies/nftables-proxy/utils/nft-command-executor.d.ts +0 -66
- package/dist_ts/proxies/nftables-proxy/utils/nft-command-executor.js +0 -131
- package/dist_ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.d.ts +0 -39
- package/dist_ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.js +0 -112
- package/dist_ts/proxies/nftables-proxy/utils/nft-rule-validator.d.ts +0 -59
- package/dist_ts/proxies/nftables-proxy/utils/nft-rule-validator.js +0 -130
- package/ts/proxies/http-proxy/connection-pool.ts +0 -228
- package/ts/proxies/http-proxy/context-creator.ts +0 -145
- package/ts/proxies/http-proxy/default-certificates.ts +0 -150
- package/ts/proxies/http-proxy/function-cache.ts +0 -279
- package/ts/proxies/http-proxy/handlers/index.ts +0 -5
- package/ts/proxies/http-proxy/http-proxy.ts +0 -669
- package/ts/proxies/http-proxy/http-request-handler.ts +0 -331
- package/ts/proxies/http-proxy/http2-request-handler.ts +0 -255
- package/ts/proxies/http-proxy/index.ts +0 -18
- package/ts/proxies/http-proxy/models/http-types.ts +0 -148
- package/ts/proxies/http-proxy/models/index.ts +0 -5
- package/ts/proxies/http-proxy/models/types.ts +0 -125
- package/ts/proxies/http-proxy/request-handler.ts +0 -878
- package/ts/proxies/http-proxy/security-manager.ts +0 -413
- package/ts/proxies/http-proxy/websocket-handler.ts +0 -581
- package/ts/proxies/nftables-proxy/index.ts +0 -6
- package/ts/proxies/nftables-proxy/models/errors.ts +0 -30
- package/ts/proxies/nftables-proxy/models/index.ts +0 -5
- package/ts/proxies/nftables-proxy/models/interfaces.ts +0 -94
- package/ts/proxies/nftables-proxy/nftables-proxy.ts +0 -1754
- package/ts/proxies/nftables-proxy/utils/index.ts +0 -38
- package/ts/proxies/nftables-proxy/utils/nft-command-executor.ts +0 -162
- package/ts/proxies/nftables-proxy/utils/nft-port-spec-normalizer.ts +0 -125
- package/ts/proxies/nftables-proxy/utils/nft-rule-validator.ts +0 -156
- package/ts/proxies/smart-proxy/acme-state-manager.ts +0 -112
- package/ts/proxies/smart-proxy/cert-store.ts +0 -92
- package/ts/proxies/smart-proxy/certificate-manager.ts +0 -895
- package/ts/proxies/smart-proxy/connection-manager.ts +0 -809
- package/ts/proxies/smart-proxy/http-proxy-bridge.ts +0 -213
- package/ts/proxies/smart-proxy/metrics-collector.ts +0 -453
- package/ts/proxies/smart-proxy/nftables-manager.ts +0 -271
- package/ts/proxies/smart-proxy/port-manager.ts +0 -358
- package/ts/proxies/smart-proxy/route-connection-handler.ts +0 -1712
- package/ts/proxies/smart-proxy/route-orchestrator.ts +0 -297
- package/ts/proxies/smart-proxy/security-manager.ts +0 -269
- package/ts/proxies/smart-proxy/throughput-tracker.ts +0 -138
- package/ts/proxies/smart-proxy/timeout-manager.ts +0 -196
- package/ts/proxies/smart-proxy/tls-manager.ts +0 -171
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NFTables Proxy Utilities
|
|
3
|
-
*
|
|
4
|
-
* This module exports utility functions and classes for NFTables operations.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Command execution
|
|
8
|
-
export { NftCommandExecutor } from './nft-command-executor.js';
|
|
9
|
-
export type { INftLoggerFn, INftExecutorOptions } from './nft-command-executor.js';
|
|
10
|
-
|
|
11
|
-
// Port specification normalization
|
|
12
|
-
export {
|
|
13
|
-
normalizePortSpec,
|
|
14
|
-
validatePorts,
|
|
15
|
-
formatPortRange,
|
|
16
|
-
portSpecToNftExpr,
|
|
17
|
-
rangesOverlap,
|
|
18
|
-
mergeOverlappingRanges,
|
|
19
|
-
countPorts,
|
|
20
|
-
isPortInSpec
|
|
21
|
-
} from './nft-port-spec-normalizer.js';
|
|
22
|
-
|
|
23
|
-
// Rule validation
|
|
24
|
-
export {
|
|
25
|
-
isValidIP,
|
|
26
|
-
isValidIPv4,
|
|
27
|
-
isValidIPv6,
|
|
28
|
-
isValidHostname,
|
|
29
|
-
isValidTableName,
|
|
30
|
-
isValidRate,
|
|
31
|
-
validateIPs,
|
|
32
|
-
validateHost,
|
|
33
|
-
validateTableName,
|
|
34
|
-
validateQosSettings,
|
|
35
|
-
validateSettings,
|
|
36
|
-
isIPForFamily,
|
|
37
|
-
filterIPsByFamily
|
|
38
|
-
} from './nft-rule-validator.js';
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NFTables Command Executor
|
|
3
|
-
*
|
|
4
|
-
* Handles command execution with retry logic, temp file management,
|
|
5
|
-
* and error handling for nftables operations.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { exec, execSync } from 'child_process';
|
|
9
|
-
import { promisify } from 'util';
|
|
10
|
-
import { delay } from '../../../core/utils/async-utils.js';
|
|
11
|
-
import { AsyncFileSystem } from '../../../core/utils/fs-utils.js';
|
|
12
|
-
import { NftExecutionError } from '../models/index.js';
|
|
13
|
-
|
|
14
|
-
const execAsync = promisify(exec);
|
|
15
|
-
|
|
16
|
-
export interface INftLoggerFn {
|
|
17
|
-
(level: 'info' | 'warn' | 'error' | 'debug', message: string, data?: Record<string, any>): void;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface INftExecutorOptions {
|
|
21
|
-
maxRetries?: number;
|
|
22
|
-
retryDelayMs?: number;
|
|
23
|
-
tempFilePath?: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* NFTables command executor with retry logic and temp file support
|
|
28
|
-
*/
|
|
29
|
-
export class NftCommandExecutor {
|
|
30
|
-
private static readonly NFT_CMD = 'nft';
|
|
31
|
-
private maxRetries: number;
|
|
32
|
-
private retryDelayMs: number;
|
|
33
|
-
private tempFilePath: string;
|
|
34
|
-
|
|
35
|
-
constructor(
|
|
36
|
-
private log: INftLoggerFn,
|
|
37
|
-
options: INftExecutorOptions = {}
|
|
38
|
-
) {
|
|
39
|
-
this.maxRetries = options.maxRetries || 3;
|
|
40
|
-
this.retryDelayMs = options.retryDelayMs || 1000;
|
|
41
|
-
this.tempFilePath = options.tempFilePath || `/tmp/nft-rules-${Date.now()}.nft`;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Execute a command with retry capability
|
|
46
|
-
*/
|
|
47
|
-
async executeWithRetry(command: string, maxRetries?: number, retryDelayMs?: number): Promise<string> {
|
|
48
|
-
const retries = maxRetries ?? this.maxRetries;
|
|
49
|
-
const delayMs = retryDelayMs ?? this.retryDelayMs;
|
|
50
|
-
let lastError: Error | undefined;
|
|
51
|
-
|
|
52
|
-
for (let i = 0; i < retries; i++) {
|
|
53
|
-
try {
|
|
54
|
-
const { stdout } = await execAsync(command);
|
|
55
|
-
return stdout;
|
|
56
|
-
} catch (err) {
|
|
57
|
-
lastError = err as Error;
|
|
58
|
-
this.log('warn', `Command failed (attempt ${i+1}/${retries}): ${command}`, { error: lastError.message });
|
|
59
|
-
|
|
60
|
-
// Wait before retry, unless it's the last attempt
|
|
61
|
-
if (i < retries - 1) {
|
|
62
|
-
await delay(delayMs);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
throw new NftExecutionError(`Failed after ${retries} attempts: ${lastError?.message || 'Unknown error'}`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Execute system command synchronously (single attempt, no retry)
|
|
72
|
-
* Used only for exit handlers where the process is terminating anyway.
|
|
73
|
-
*/
|
|
74
|
-
executeSync(command: string): string {
|
|
75
|
-
try {
|
|
76
|
-
return execSync(command, { timeout: 5000 }).toString();
|
|
77
|
-
} catch (err) {
|
|
78
|
-
this.log('warn', `Sync command failed: ${command}`, { error: (err as Error).message });
|
|
79
|
-
throw err;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Execute nftables commands with a temporary file
|
|
85
|
-
*/
|
|
86
|
-
async executeWithTempFile(rulesetContent: string): Promise<void> {
|
|
87
|
-
await AsyncFileSystem.writeFile(this.tempFilePath, rulesetContent);
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
await this.executeWithRetry(
|
|
91
|
-
`${NftCommandExecutor.NFT_CMD} -f ${this.tempFilePath}`,
|
|
92
|
-
this.maxRetries,
|
|
93
|
-
this.retryDelayMs
|
|
94
|
-
);
|
|
95
|
-
} finally {
|
|
96
|
-
// Always clean up the temp file
|
|
97
|
-
await AsyncFileSystem.remove(this.tempFilePath);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Check if nftables is available
|
|
103
|
-
*/
|
|
104
|
-
async checkAvailability(): Promise<boolean> {
|
|
105
|
-
try {
|
|
106
|
-
await this.executeWithRetry(`${NftCommandExecutor.NFT_CMD} --version`, this.maxRetries, this.retryDelayMs);
|
|
107
|
-
return true;
|
|
108
|
-
} catch (err) {
|
|
109
|
-
this.log('error', `nftables is not available: ${(err as Error).message}`);
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Check if connection tracking modules are loaded
|
|
116
|
-
*/
|
|
117
|
-
async checkConntrackModules(): Promise<boolean> {
|
|
118
|
-
try {
|
|
119
|
-
await this.executeWithRetry('lsmod | grep nf_conntrack', this.maxRetries, this.retryDelayMs);
|
|
120
|
-
return true;
|
|
121
|
-
} catch (err) {
|
|
122
|
-
this.log('warn', 'Connection tracking modules might not be loaded, advanced NAT features may not work');
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Run an nft command directly
|
|
129
|
-
*/
|
|
130
|
-
async nft(args: string): Promise<string> {
|
|
131
|
-
return this.executeWithRetry(`${NftCommandExecutor.NFT_CMD} ${args}`, this.maxRetries, this.retryDelayMs);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Run an nft command synchronously (for cleanup on exit)
|
|
136
|
-
*/
|
|
137
|
-
nftSync(args: string): string {
|
|
138
|
-
return this.executeSync(`${NftCommandExecutor.NFT_CMD} ${args}`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Get the NFT command path
|
|
143
|
-
*/
|
|
144
|
-
static get nftCmd(): string {
|
|
145
|
-
return NftCommandExecutor.NFT_CMD;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Update the temp file path
|
|
150
|
-
*/
|
|
151
|
-
setTempFilePath(path: string): void {
|
|
152
|
-
this.tempFilePath = path;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Update retry settings
|
|
157
|
-
*/
|
|
158
|
-
setRetryOptions(maxRetries: number, retryDelayMs: number): void {
|
|
159
|
-
this.maxRetries = maxRetries;
|
|
160
|
-
this.retryDelayMs = retryDelayMs;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NFTables Port Specification Normalizer
|
|
3
|
-
*
|
|
4
|
-
* Handles normalization and validation of port specifications
|
|
5
|
-
* for nftables rules.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { PortRange } from '../models/index.js';
|
|
9
|
-
import { NftValidationError } from '../models/index.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Normalizes port specifications into an array of port ranges
|
|
13
|
-
*/
|
|
14
|
-
export function normalizePortSpec(portSpec: number | PortRange | Array<number | PortRange>): PortRange[] {
|
|
15
|
-
const result: PortRange[] = [];
|
|
16
|
-
|
|
17
|
-
if (Array.isArray(portSpec)) {
|
|
18
|
-
// If it's an array, process each element
|
|
19
|
-
for (const spec of portSpec) {
|
|
20
|
-
result.push(...normalizePortSpec(spec));
|
|
21
|
-
}
|
|
22
|
-
} else if (typeof portSpec === 'number') {
|
|
23
|
-
// Single port becomes a range with the same start and end
|
|
24
|
-
result.push({ from: portSpec, to: portSpec });
|
|
25
|
-
} else {
|
|
26
|
-
// Already a range
|
|
27
|
-
result.push(portSpec);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return result;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Validates port numbers or ranges
|
|
35
|
-
*/
|
|
36
|
-
export function validatePorts(port: number | PortRange | Array<number | PortRange>): void {
|
|
37
|
-
if (Array.isArray(port)) {
|
|
38
|
-
port.forEach(p => validatePorts(p));
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (typeof port === 'number') {
|
|
43
|
-
if (port < 1 || port > 65535) {
|
|
44
|
-
throw new NftValidationError(`Invalid port number: ${port}`);
|
|
45
|
-
}
|
|
46
|
-
} else if (typeof port === 'object') {
|
|
47
|
-
if (port.from < 1 || port.from > 65535 || port.to < 1 || port.to > 65535 || port.from > port.to) {
|
|
48
|
-
throw new NftValidationError(`Invalid port range: ${port.from}-${port.to}`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Format port range for nftables rule
|
|
55
|
-
*/
|
|
56
|
-
export function formatPortRange(range: PortRange): string {
|
|
57
|
-
if (range.from === range.to) {
|
|
58
|
-
return String(range.from);
|
|
59
|
-
}
|
|
60
|
-
return `${range.from}-${range.to}`;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Convert port spec to nftables expression
|
|
65
|
-
*/
|
|
66
|
-
export function portSpecToNftExpr(portSpec: number | PortRange | Array<number | PortRange>): string {
|
|
67
|
-
const ranges = normalizePortSpec(portSpec);
|
|
68
|
-
|
|
69
|
-
if (ranges.length === 1) {
|
|
70
|
-
return formatPortRange(ranges[0]);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Multiple ports/ranges need to use a set
|
|
74
|
-
const ports = ranges.map(formatPortRange);
|
|
75
|
-
return `{ ${ports.join(', ')} }`;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Check if two port ranges overlap
|
|
80
|
-
*/
|
|
81
|
-
export function rangesOverlap(range1: PortRange, range2: PortRange): boolean {
|
|
82
|
-
return range1.from <= range2.to && range2.from <= range1.to;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Merge overlapping port ranges
|
|
87
|
-
*/
|
|
88
|
-
export function mergeOverlappingRanges(ranges: PortRange[]): PortRange[] {
|
|
89
|
-
if (ranges.length <= 1) return ranges;
|
|
90
|
-
|
|
91
|
-
// Sort by start port
|
|
92
|
-
const sorted = [...ranges].sort((a, b) => a.from - b.from);
|
|
93
|
-
const merged: PortRange[] = [sorted[0]];
|
|
94
|
-
|
|
95
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
96
|
-
const current = sorted[i];
|
|
97
|
-
const lastMerged = merged[merged.length - 1];
|
|
98
|
-
|
|
99
|
-
if (current.from <= lastMerged.to + 1) {
|
|
100
|
-
// Ranges overlap or are adjacent, merge them
|
|
101
|
-
lastMerged.to = Math.max(lastMerged.to, current.to);
|
|
102
|
-
} else {
|
|
103
|
-
// No overlap, add as new range
|
|
104
|
-
merged.push(current);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return merged;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Calculate the total number of ports in a port specification
|
|
113
|
-
*/
|
|
114
|
-
export function countPorts(portSpec: number | PortRange | Array<number | PortRange>): number {
|
|
115
|
-
const ranges = normalizePortSpec(portSpec);
|
|
116
|
-
return ranges.reduce((total, range) => total + (range.to - range.from + 1), 0);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Check if a port is within the given specification
|
|
121
|
-
*/
|
|
122
|
-
export function isPortInSpec(port: number, portSpec: number | PortRange | Array<number | PortRange>): boolean {
|
|
123
|
-
const ranges = normalizePortSpec(portSpec);
|
|
124
|
-
return ranges.some(range => port >= range.from && port <= range.to);
|
|
125
|
-
}
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NFTables Rule Validator
|
|
3
|
-
*
|
|
4
|
-
* Handles validation of settings and inputs for nftables operations.
|
|
5
|
-
* Prevents command injection and ensures valid values.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { PortRange, NfTableProxyOptions } from '../models/index.js';
|
|
9
|
-
import { NftValidationError } from '../models/index.js';
|
|
10
|
-
import { validatePorts } from './nft-port-spec-normalizer.js';
|
|
11
|
-
|
|
12
|
-
// IP address validation patterns
|
|
13
|
-
const IPV4_REGEX = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))?$/;
|
|
14
|
-
const IPV6_REGEX = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/;
|
|
15
|
-
const HOSTNAME_REGEX = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
|
|
16
|
-
const TABLE_NAME_REGEX = /^[a-zA-Z0-9_]+$/;
|
|
17
|
-
const RATE_REGEX = /^[0-9]+[kKmMgG]?bps$/;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Validates an IP address (IPv4 or IPv6)
|
|
21
|
-
*/
|
|
22
|
-
export function isValidIP(ip: string): boolean {
|
|
23
|
-
return IPV4_REGEX.test(ip) || IPV6_REGEX.test(ip);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Validates an IPv4 address
|
|
28
|
-
*/
|
|
29
|
-
export function isValidIPv4(ip: string): boolean {
|
|
30
|
-
return IPV4_REGEX.test(ip);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Validates an IPv6 address
|
|
35
|
-
*/
|
|
36
|
-
export function isValidIPv6(ip: string): boolean {
|
|
37
|
-
return IPV6_REGEX.test(ip);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Validates a hostname
|
|
42
|
-
*/
|
|
43
|
-
export function isValidHostname(hostname: string): boolean {
|
|
44
|
-
return HOSTNAME_REGEX.test(hostname);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Validates a table name for nftables
|
|
49
|
-
*/
|
|
50
|
-
export function isValidTableName(tableName: string): boolean {
|
|
51
|
-
return TABLE_NAME_REGEX.test(tableName);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Validates a rate specification (e.g., "10mbps")
|
|
56
|
-
*/
|
|
57
|
-
export function isValidRate(rate: string): boolean {
|
|
58
|
-
return RATE_REGEX.test(rate);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Validates an array of IP addresses
|
|
63
|
-
*/
|
|
64
|
-
export function validateIPs(ips?: string[]): void {
|
|
65
|
-
if (!ips) return;
|
|
66
|
-
|
|
67
|
-
for (const ip of ips) {
|
|
68
|
-
if (!isValidIP(ip)) {
|
|
69
|
-
throw new NftValidationError(`Invalid IP address format: ${ip}`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Validates a host (can be hostname or IP)
|
|
76
|
-
*/
|
|
77
|
-
export function validateHost(host?: string): void {
|
|
78
|
-
if (!host) return;
|
|
79
|
-
|
|
80
|
-
if (!isValidHostname(host) && !isValidIP(host)) {
|
|
81
|
-
throw new NftValidationError(`Invalid host format: ${host}`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Validates a table name
|
|
87
|
-
*/
|
|
88
|
-
export function validateTableName(tableName?: string): void {
|
|
89
|
-
if (!tableName) return;
|
|
90
|
-
|
|
91
|
-
if (!isValidTableName(tableName)) {
|
|
92
|
-
throw new NftValidationError(
|
|
93
|
-
`Invalid table name: ${tableName}. Only alphanumeric characters and underscores are allowed.`
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Validates QoS settings
|
|
100
|
-
*/
|
|
101
|
-
export function validateQosSettings(qos?: NfTableProxyOptions['qos']): void {
|
|
102
|
-
if (!qos?.enabled) return;
|
|
103
|
-
|
|
104
|
-
if (qos.maxRate && !isValidRate(qos.maxRate)) {
|
|
105
|
-
throw new NftValidationError(
|
|
106
|
-
`Invalid rate format: ${qos.maxRate}. Use format like "10mbps", "1gbps", etc.`
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (qos.priority !== undefined) {
|
|
111
|
-
if (qos.priority < 1 || qos.priority > 10 || !Number.isInteger(qos.priority)) {
|
|
112
|
-
throw new NftValidationError(
|
|
113
|
-
`Invalid priority: ${qos.priority}. Must be an integer between 1 and 10.`
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Validates all NfTablesProxy settings
|
|
121
|
-
*/
|
|
122
|
-
export function validateSettings(settings: NfTableProxyOptions): void {
|
|
123
|
-
// Validate port numbers
|
|
124
|
-
validatePorts(settings.fromPort);
|
|
125
|
-
validatePorts(settings.toPort);
|
|
126
|
-
|
|
127
|
-
// Validate IP addresses
|
|
128
|
-
validateIPs(settings.ipAllowList);
|
|
129
|
-
validateIPs(settings.ipBlockList);
|
|
130
|
-
|
|
131
|
-
// Validate target host
|
|
132
|
-
validateHost(settings.toHost);
|
|
133
|
-
|
|
134
|
-
// Validate table name
|
|
135
|
-
validateTableName(settings.tableName);
|
|
136
|
-
|
|
137
|
-
// Validate QoS settings
|
|
138
|
-
validateQosSettings(settings.qos);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Check if an IP matches the given family (ip or ip6)
|
|
143
|
-
*/
|
|
144
|
-
export function isIPForFamily(ip: string, family: 'ip' | 'ip6'): boolean {
|
|
145
|
-
if (family === 'ip6') {
|
|
146
|
-
return ip.includes(':');
|
|
147
|
-
}
|
|
148
|
-
return ip.includes('.');
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Filter IPs by family
|
|
153
|
-
*/
|
|
154
|
-
export function filterIPsByFamily(ips: string[], family: 'ip' | 'ip6'): string[] {
|
|
155
|
-
return ips.filter(ip => isIPForFamily(ip, family));
|
|
156
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import type { IRouteConfig } from './models/route-types.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Global state store for ACME operations
|
|
5
|
-
* Tracks active challenge routes and port allocations
|
|
6
|
-
*/
|
|
7
|
-
export class AcmeStateManager {
|
|
8
|
-
private activeChallengeRoutes: Map<string, IRouteConfig> = new Map();
|
|
9
|
-
private acmePortAllocations: Set<number> = new Set();
|
|
10
|
-
private primaryChallengeRoute: IRouteConfig | null = null;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Check if a challenge route is active
|
|
14
|
-
*/
|
|
15
|
-
public isChallengeRouteActive(): boolean {
|
|
16
|
-
return this.activeChallengeRoutes.size > 0;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Register a challenge route as active
|
|
21
|
-
*/
|
|
22
|
-
public addChallengeRoute(route: IRouteConfig): void {
|
|
23
|
-
this.activeChallengeRoutes.set(route.name, route);
|
|
24
|
-
|
|
25
|
-
// Track the primary challenge route
|
|
26
|
-
if (!this.primaryChallengeRoute || route.priority > (this.primaryChallengeRoute.priority || 0)) {
|
|
27
|
-
this.primaryChallengeRoute = route;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Track port allocations
|
|
31
|
-
const ports = Array.isArray(route.match.ports) ? route.match.ports : [route.match.ports];
|
|
32
|
-
ports.forEach(port => this.acmePortAllocations.add(port));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Remove a challenge route
|
|
37
|
-
*/
|
|
38
|
-
public removeChallengeRoute(routeName: string): void {
|
|
39
|
-
const route = this.activeChallengeRoutes.get(routeName);
|
|
40
|
-
if (!route) return;
|
|
41
|
-
|
|
42
|
-
this.activeChallengeRoutes.delete(routeName);
|
|
43
|
-
|
|
44
|
-
// Update primary challenge route if needed
|
|
45
|
-
if (this.primaryChallengeRoute?.name === routeName) {
|
|
46
|
-
this.primaryChallengeRoute = null;
|
|
47
|
-
// Find new primary route with highest priority
|
|
48
|
-
let highestPriority = -1;
|
|
49
|
-
for (const [_, activeRoute] of this.activeChallengeRoutes) {
|
|
50
|
-
const priority = activeRoute.priority || 0;
|
|
51
|
-
if (priority > highestPriority) {
|
|
52
|
-
highestPriority = priority;
|
|
53
|
-
this.primaryChallengeRoute = activeRoute;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Update port allocations - only remove if no other routes use this port
|
|
59
|
-
const ports = Array.isArray(route.match.ports) ? route.match.ports : [route.match.ports];
|
|
60
|
-
ports.forEach(port => {
|
|
61
|
-
let portStillUsed = false;
|
|
62
|
-
for (const [_, activeRoute] of this.activeChallengeRoutes) {
|
|
63
|
-
const activePorts = Array.isArray(activeRoute.match.ports) ?
|
|
64
|
-
activeRoute.match.ports : [activeRoute.match.ports];
|
|
65
|
-
if (activePorts.includes(port)) {
|
|
66
|
-
portStillUsed = true;
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
if (!portStillUsed) {
|
|
71
|
-
this.acmePortAllocations.delete(port);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Get all active challenge routes
|
|
78
|
-
*/
|
|
79
|
-
public getActiveChallengeRoutes(): IRouteConfig[] {
|
|
80
|
-
return Array.from(this.activeChallengeRoutes.values());
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Get the primary challenge route
|
|
85
|
-
*/
|
|
86
|
-
public getPrimaryChallengeRoute(): IRouteConfig | null {
|
|
87
|
-
return this.primaryChallengeRoute;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Check if a port is allocated for ACME
|
|
92
|
-
*/
|
|
93
|
-
public isPortAllocatedForAcme(port: number): boolean {
|
|
94
|
-
return this.acmePortAllocations.has(port);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Get all ACME ports
|
|
99
|
-
*/
|
|
100
|
-
public getAcmePorts(): number[] {
|
|
101
|
-
return Array.from(this.acmePortAllocations);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Clear all state (for shutdown or reset)
|
|
106
|
-
*/
|
|
107
|
-
public clear(): void {
|
|
108
|
-
this.activeChallengeRoutes.clear();
|
|
109
|
-
this.acmePortAllocations.clear();
|
|
110
|
-
this.primaryChallengeRoute = null;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import * as plugins from '../../plugins.js';
|
|
2
|
-
import { AsyncFileSystem } from '../../core/utils/fs-utils.js';
|
|
3
|
-
import type { ICertificateData } from './certificate-manager.js';
|
|
4
|
-
|
|
5
|
-
export class CertStore {
|
|
6
|
-
constructor(private certDir: string) {}
|
|
7
|
-
|
|
8
|
-
public async initialize(): Promise<void> {
|
|
9
|
-
await AsyncFileSystem.ensureDir(this.certDir);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public async getCertificate(routeName: string): Promise<ICertificateData | null> {
|
|
13
|
-
const certPath = this.getCertPath(routeName);
|
|
14
|
-
const metaPath = `${certPath}/meta.json`;
|
|
15
|
-
|
|
16
|
-
if (!await AsyncFileSystem.exists(metaPath)) {
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
const meta = await AsyncFileSystem.readJSON(metaPath);
|
|
22
|
-
|
|
23
|
-
const [cert, key] = await Promise.all([
|
|
24
|
-
AsyncFileSystem.readFile(`${certPath}/cert.pem`),
|
|
25
|
-
AsyncFileSystem.readFile(`${certPath}/key.pem`)
|
|
26
|
-
]);
|
|
27
|
-
|
|
28
|
-
let ca: string | undefined;
|
|
29
|
-
const caPath = `${certPath}/ca.pem`;
|
|
30
|
-
if (await AsyncFileSystem.exists(caPath)) {
|
|
31
|
-
ca = await AsyncFileSystem.readFile(caPath);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
cert,
|
|
36
|
-
key,
|
|
37
|
-
ca,
|
|
38
|
-
expiryDate: new Date(meta.expiryDate),
|
|
39
|
-
issueDate: new Date(meta.issueDate)
|
|
40
|
-
};
|
|
41
|
-
} catch (error) {
|
|
42
|
-
console.error(`Failed to load certificate for ${routeName}: ${error}`);
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public async saveCertificate(
|
|
48
|
-
routeName: string,
|
|
49
|
-
certData: ICertificateData
|
|
50
|
-
): Promise<void> {
|
|
51
|
-
const certPath = this.getCertPath(routeName);
|
|
52
|
-
await AsyncFileSystem.ensureDir(certPath);
|
|
53
|
-
|
|
54
|
-
// Save certificate files in parallel
|
|
55
|
-
const savePromises = [
|
|
56
|
-
AsyncFileSystem.writeFile(`${certPath}/cert.pem`, certData.cert),
|
|
57
|
-
AsyncFileSystem.writeFile(`${certPath}/key.pem`, certData.key)
|
|
58
|
-
];
|
|
59
|
-
|
|
60
|
-
if (certData.ca) {
|
|
61
|
-
savePromises.push(
|
|
62
|
-
AsyncFileSystem.writeFile(`${certPath}/ca.pem`, certData.ca)
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Save metadata
|
|
67
|
-
const meta = {
|
|
68
|
-
expiryDate: certData.expiryDate.toISOString(),
|
|
69
|
-
issueDate: certData.issueDate.toISOString(),
|
|
70
|
-
savedAt: new Date().toISOString()
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
savePromises.push(
|
|
74
|
-
AsyncFileSystem.writeJSON(`${certPath}/meta.json`, meta)
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
await Promise.all(savePromises);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public async deleteCertificate(routeName: string): Promise<void> {
|
|
81
|
-
const certPath = this.getCertPath(routeName);
|
|
82
|
-
if (await AsyncFileSystem.isDirectory(certPath)) {
|
|
83
|
-
await AsyncFileSystem.removeDir(certPath);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private getCertPath(routeName: string): string {
|
|
88
|
-
// Sanitize route name for filesystem
|
|
89
|
-
const safeName = routeName.replace(/[^a-zA-Z0-9-_]/g, '_');
|
|
90
|
-
return `${this.certDir}/${safeName}`;
|
|
91
|
-
}
|
|
92
|
-
}
|