@push.rocks/smartproxy 7.1.2 → 10.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/dist_ts/00_commitinfo_data.js +2 -2
- package/dist_ts/classes.router.d.ts +9 -10
- package/dist_ts/classes.router.js +3 -5
- package/dist_ts/common/acmeFactory.d.ts +9 -0
- package/dist_ts/common/acmeFactory.js +20 -0
- package/dist_ts/common/eventUtils.d.ts +15 -0
- package/dist_ts/common/eventUtils.js +19 -0
- package/dist_ts/common/types.d.ts +82 -0
- package/dist_ts/common/types.js +17 -0
- package/dist_ts/networkproxy/classes.np.certificatemanager.js +23 -19
- package/dist_ts/networkproxy/classes.np.networkproxy.d.ts +1 -0
- package/dist_ts/networkproxy/classes.np.networkproxy.js +5 -1
- package/dist_ts/networkproxy/classes.np.types.d.ts +5 -10
- package/dist_ts/networkproxy/classes.np.types.js +1 -1
- package/dist_ts/plugins.d.ts +6 -3
- package/dist_ts/plugins.js +7 -4
- package/dist_ts/port80handler/classes.port80handler.d.ts +14 -111
- package/dist_ts/port80handler/classes.port80handler.js +94 -373
- package/dist_ts/smartproxy/classes.pp.certprovisioner.d.ts +54 -0
- package/dist_ts/smartproxy/classes.pp.certprovisioner.js +166 -0
- package/dist_ts/smartproxy/classes.pp.interfaces.d.ts +11 -33
- package/dist_ts/smartproxy/classes.pp.networkproxybridge.d.ts +5 -0
- package/dist_ts/smartproxy/classes.pp.networkproxybridge.js +21 -11
- package/dist_ts/smartproxy/classes.pp.portrangemanager.js +1 -11
- package/dist_ts/smartproxy/classes.smartproxy.d.ts +3 -5
- package/dist_ts/smartproxy/classes.smartproxy.js +94 -180
- package/package.json +12 -10
- package/readme.hints.md +64 -1
- package/readme.md +253 -408
- package/readme.plan.md +29 -0
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/classes.router.ts +13 -15
- package/ts/common/acmeFactory.ts +23 -0
- package/ts/common/eventUtils.ts +34 -0
- package/ts/common/types.ts +89 -0
- package/ts/networkproxy/classes.np.certificatemanager.ts +23 -19
- package/ts/networkproxy/classes.np.networkproxy.ts +4 -0
- package/ts/networkproxy/classes.np.types.ts +6 -10
- package/ts/plugins.ts +17 -4
- package/ts/port80handler/classes.port80handler.ts +108 -509
- package/ts/smartproxy/classes.pp.certprovisioner.ts +188 -0
- package/ts/smartproxy/classes.pp.interfaces.ts +13 -36
- package/ts/smartproxy/classes.pp.networkproxybridge.ts +22 -10
- package/ts/smartproxy/classes.pp.portrangemanager.ts +0 -10
- package/ts/smartproxy/classes.smartproxy.ts +103 -195
package/readme.md
CHANGED
|
@@ -1,11 +1,178 @@
|
|
|
1
1
|
# @push.rocks/smartproxy
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A high-performance proxy toolkit for Node.js, offering:
|
|
4
|
+
- HTTP/HTTPS reverse proxy with TLS termination and WebSocket support
|
|
5
|
+
- Automatic ACME certificate management (HTTP-01)
|
|
6
|
+
- Low-level port forwarding via nftables
|
|
7
|
+
- HTTP-to-HTTPS and custom URL redirects
|
|
8
|
+
- Advanced TCP/SNI-based proxying with IP filtering and rules
|
|
9
|
+
|
|
10
|
+
## Exports
|
|
11
|
+
The following classes and interfaces are provided:
|
|
12
|
+
|
|
13
|
+
- **NetworkProxy** (ts/networkproxy/classes.np.networkproxy.ts)
|
|
14
|
+
HTTP/HTTPS reverse proxy with TLS termination, WebSocket support,
|
|
15
|
+
connection pooling, and optional ACME integration.
|
|
16
|
+
- **Port80Handler** (ts/port80handler/classes.port80handler.ts)
|
|
17
|
+
ACME HTTP-01 challenge handler and certificate manager.
|
|
18
|
+
- **NfTablesProxy** (ts/nfttablesproxy/classes.nftablesproxy.ts)
|
|
19
|
+
Low-level port forwarding using nftables NAT rules.
|
|
20
|
+
- **Redirect**, **SslRedirect** (ts/redirect/classes.redirect.ts)
|
|
21
|
+
HTTP/HTTPS redirect server and shortcut for HTTP→HTTPS.
|
|
22
|
+
- **SmartProxy** (ts/smartproxy/classes.smartproxy.ts)
|
|
23
|
+
TCP/SNI-based proxy with dynamic routing, IP filtering, and unified certificates.
|
|
24
|
+
- **SniHandler** (ts/smartproxy/classes.pp.snihandler.ts)
|
|
25
|
+
Static utilities to extract SNI hostnames from TLS handshakes.
|
|
26
|
+
- **Interfaces**
|
|
27
|
+
- IPortProxySettings, IDomainConfig (ts/smartproxy/classes.pp.interfaces.ts)
|
|
28
|
+
- INetworkProxyOptions (ts/networkproxy/classes.np.types.ts)
|
|
29
|
+
- IAcmeOptions, IDomainOptions, IForwardConfig (ts/common/types.ts)
|
|
30
|
+
- INfTableProxySettings (ts/nfttablesproxy/classes.nftablesproxy.ts)
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
Install via npm:
|
|
34
|
+
```bash
|
|
35
|
+
npm install @push.rocks/smartproxy
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
### 1. HTTP(S) Reverse Proxy (NetworkProxy)
|
|
41
|
+
```typescript
|
|
42
|
+
import { NetworkProxy } from '@push.rocks/smartproxy';
|
|
43
|
+
|
|
44
|
+
const proxy = new NetworkProxy({ port: 443 });
|
|
45
|
+
await proxy.start();
|
|
46
|
+
|
|
47
|
+
await proxy.updateProxyConfigs([
|
|
48
|
+
{
|
|
49
|
+
hostName: 'example.com',
|
|
50
|
+
destinationIps: ['127.0.0.1'],
|
|
51
|
+
destinationPorts: [3000],
|
|
52
|
+
publicKey: fs.readFileSync('cert.pem', 'utf8'),
|
|
53
|
+
privateKey: fs.readFileSync('key.pem', 'utf8'),
|
|
54
|
+
}
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
// Add default headers to all responses
|
|
58
|
+
await proxy.addDefaultHeaders({
|
|
59
|
+
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains'
|
|
60
|
+
});
|
|
61
|
+
// ...
|
|
62
|
+
await proxy.stop();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 2. HTTP→HTTPS Redirect (Redirect / SslRedirect)
|
|
66
|
+
```typescript
|
|
67
|
+
import { Redirect, SslRedirect } from '@push.rocks/smartproxy';
|
|
68
|
+
import * as fs from 'fs';
|
|
69
|
+
|
|
70
|
+
// Custom redirect rules
|
|
71
|
+
const redirect = new Redirect({
|
|
72
|
+
httpPort: 80,
|
|
73
|
+
httpsPort: 443,
|
|
74
|
+
sslOptions: {
|
|
75
|
+
key: fs.readFileSync('key.pem'),
|
|
76
|
+
cert: fs.readFileSync('cert.pem'),
|
|
77
|
+
},
|
|
78
|
+
rules: [
|
|
79
|
+
{
|
|
80
|
+
fromProtocol: 'http',
|
|
81
|
+
fromHost: '*',
|
|
82
|
+
toProtocol: 'https',
|
|
83
|
+
toHost: '$1',
|
|
84
|
+
statusCode: 301
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
});
|
|
88
|
+
await redirect.start();
|
|
89
|
+
|
|
90
|
+
// Quick HTTP→HTTPS helper on port 80
|
|
91
|
+
const quick = new SslRedirect(80);
|
|
92
|
+
await quick.start();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 3. Automatic Certificates (ACME Port80Handler)
|
|
96
|
+
```typescript
|
|
97
|
+
import { Port80Handler } from '@push.rocks/smartproxy';
|
|
98
|
+
|
|
99
|
+
// Configure ACME on port 80 with contact email
|
|
100
|
+
const acme = new Port80Handler({
|
|
101
|
+
port: 80,
|
|
102
|
+
contactEmail: 'admin@example.com',
|
|
103
|
+
useProduction: true,
|
|
104
|
+
renewThresholdDays: 30
|
|
105
|
+
});
|
|
106
|
+
acme.on('certificate-issued', evt => {
|
|
107
|
+
console.log(`Certificate ready for ${evt.domain}, expires ${evt.expiryDate}`);
|
|
108
|
+
});
|
|
109
|
+
await acme.start();
|
|
110
|
+
acme.addDomain({
|
|
111
|
+
domainName: 'example.com',
|
|
112
|
+
sslRedirect: true,
|
|
113
|
+
acmeMaintenance: true
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 4. Low-Level Port Forwarding (NfTablesProxy)
|
|
118
|
+
```typescript
|
|
119
|
+
import { NfTablesProxy } from '@push.rocks/smartproxy';
|
|
120
|
+
|
|
121
|
+
// Forward port 80→8080 with source IP preservation
|
|
122
|
+
const nft = new NfTablesProxy({
|
|
123
|
+
fromPort: 80,
|
|
124
|
+
toPort: 8080,
|
|
125
|
+
toHost: 'localhost',
|
|
126
|
+
preserveSourceIP: true,
|
|
127
|
+
deleteOnExit: true
|
|
128
|
+
});
|
|
129
|
+
await nft.start();
|
|
130
|
+
// ...
|
|
131
|
+
await nft.stop();
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 5. TCP/SNI Proxy (SmartProxy)
|
|
135
|
+
```typescript
|
|
136
|
+
import { SmartProxy } from '@push.rocks/smartproxy';
|
|
137
|
+
|
|
138
|
+
const smart = new SmartProxy({
|
|
139
|
+
fromPort: 443,
|
|
140
|
+
toPort: 8443,
|
|
141
|
+
domainConfigs: [
|
|
142
|
+
{
|
|
143
|
+
domains: ['example.com', '*.example.com'],
|
|
144
|
+
allowedIPs: ['*'],
|
|
145
|
+
targetIPs: ['127.0.0.1'],
|
|
146
|
+
}
|
|
147
|
+
],
|
|
148
|
+
sniEnabled: true
|
|
149
|
+
});
|
|
150
|
+
smart.on('certificate', evt => console.log(evt));
|
|
151
|
+
await smart.start();
|
|
152
|
+
// Update domains later
|
|
153
|
+
await smart.updateDomainConfigs([/* new configs */]);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 6. SNI Utilities (SniHandler)
|
|
157
|
+
```js
|
|
158
|
+
import { SniHandler } from '@push.rocks/smartproxy';
|
|
159
|
+
|
|
160
|
+
// Extract SNI from a TLS ClientHello buffer
|
|
161
|
+
const sni = SniHandler.extractSNI(buffer);
|
|
162
|
+
|
|
163
|
+
// Reassemble fragmented ClientHello
|
|
164
|
+
const complete = SniHandler.handleFragmentedClientHello(buf, connId);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## API Reference
|
|
168
|
+
For full configuration options and type definitions, see the TypeScript interfaces in the `ts/` directory:
|
|
169
|
+
- `INetworkProxyOptions` (ts/networkproxy/classes.np.types.ts)
|
|
170
|
+
- `IAcmeOptions`, `IDomainOptions`, `IForwardConfig` (ts/common/types.ts)
|
|
171
|
+
- `INfTableProxySettings` (ts/nfttablesproxy/classes.nftablesproxy.ts)
|
|
172
|
+
- `IPortProxySettings`, `IDomainConfig` (ts/smartproxy/classes.pp.interfaces.ts)
|
|
4
173
|
|
|
5
174
|
## Architecture & Flow Diagrams
|
|
6
175
|
|
|
7
|
-
### Component Architecture
|
|
8
|
-
The diagram below illustrates the main components of SmartProxy and how they interact:
|
|
9
176
|
|
|
10
177
|
```mermaid
|
|
11
178
|
flowchart TB
|
|
@@ -13,12 +180,12 @@ flowchart TB
|
|
|
13
180
|
|
|
14
181
|
subgraph "SmartProxy Components"
|
|
15
182
|
direction TB
|
|
16
|
-
HTTP80[HTTP Port 80\
|
|
183
|
+
HTTP80[HTTP Port 80\nRedirect / SslRedirect]
|
|
17
184
|
HTTPS443[HTTPS Port 443\nNetworkProxy]
|
|
18
|
-
SmartProxy[SmartProxy\
|
|
185
|
+
SmartProxy[SmartProxy\n(TCP/SNI Proxy)]
|
|
19
186
|
NfTables[NfTablesProxy]
|
|
20
187
|
Router[ProxyRouter]
|
|
21
|
-
ACME[Port80Handler\
|
|
188
|
+
ACME[Port80Handler\n(ACME HTTP-01)]
|
|
22
189
|
Certs[(SSL Certificates)]
|
|
23
190
|
end
|
|
24
191
|
|
|
@@ -190,426 +357,104 @@ sequenceDiagram
|
|
|
190
357
|
|
|
191
358
|
## Features
|
|
192
359
|
|
|
193
|
-
-
|
|
194
|
-
|
|
195
|
-
- **TCP Connection Handling** - Advanced connection handling with SNI inspection and domain-based routing
|
|
196
|
-
- **Enhanced TLS Handling** - Robust TLS handshake processing with improved certificate error handling
|
|
197
|
-
- **HTTP to HTTPS Redirection** - Automatically redirect HTTP requests to HTTPS
|
|
198
|
-
- **Let's Encrypt Integration** - Automatic certificate management using ACME protocol
|
|
199
|
-
- **IP Filtering** - Control access with IP allow/block lists using glob patterns
|
|
200
|
-
- **NfTables Integration** - Direct manipulation of nftables for advanced low-level port forwarding
|
|
201
|
-
|
|
202
|
-
## Configuration Options
|
|
203
|
-
|
|
204
|
-
### backendProtocol
|
|
205
|
-
|
|
206
|
-
Type: 'http1' | 'http2' (default: 'http1')
|
|
360
|
+
- HTTP/HTTPS Reverse Proxy (NetworkProxy)
|
|
361
|
+
• TLS termination, virtual-host routing, HTTP/2 & WebSocket support, pooling & metrics
|
|
207
362
|
|
|
208
|
-
|
|
363
|
+
- Automatic ACME Certificates (Port80Handler)
|
|
364
|
+
• HTTP-01 challenge handling, certificate issuance/renewal, pluggable storage
|
|
209
365
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
import { NetworkProxy } from '@push.rocks/smartproxy';
|
|
366
|
+
- Low-Level Port Forwarding (NfTablesProxy)
|
|
367
|
+
• nftables NAT rules for ports/ranges, IPv4/IPv6, IP filtering, QoS & ipset support
|
|
213
368
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
backendProtocol: 'http2',
|
|
217
|
-
// other options...
|
|
218
|
-
});
|
|
219
|
-
proxy.start();
|
|
220
|
-
```
|
|
221
|
-
- **Basic Authentication** - Support for basic auth on proxied routes
|
|
222
|
-
- **Connection Management** - Intelligent connection tracking and cleanup with configurable timeouts
|
|
223
|
-
- **Browser Compatibility** - Optimized for modern browsers with fixes for common TLS handshake issues
|
|
369
|
+
- Custom Redirects (Redirect / SslRedirect)
|
|
370
|
+
• URL redirects with wildcard host/path, template variables & status codes
|
|
224
371
|
|
|
225
|
-
|
|
372
|
+
- TCP/SNI Proxy (SmartProxy)
|
|
373
|
+
• SNI-based routing, IP allow/block lists, port ranges, timeouts & graceful shutdown
|
|
226
374
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
```
|
|
375
|
+
- SNI Utilities (SniHandler)
|
|
376
|
+
• Robust ClientHello parsing, fragmentation & session resumption support
|
|
230
377
|
|
|
231
|
-
##
|
|
378
|
+
## Certificate Hooks & Events
|
|
232
379
|
|
|
233
|
-
|
|
380
|
+
Listen for certificate events via EventEmitter:
|
|
381
|
+
- **Port80Handler**:
|
|
382
|
+
- `certificate-issued`, `certificate-renewed`, `certificate-failed`
|
|
383
|
+
- `manager-started`, `manager-stopped`, `request-forwarded`
|
|
384
|
+
- **SmartProxy**:
|
|
385
|
+
- `certificate` (domain, publicKey, privateKey, expiryDate, source, isRenewal)
|
|
234
386
|
|
|
235
|
-
|
|
236
|
-
import { NetworkProxy } from '@push.rocks/smartproxy';
|
|
237
|
-
|
|
238
|
-
// Create a reverse proxy listening on port 443
|
|
239
|
-
const proxy = new NetworkProxy({
|
|
240
|
-
port: 443
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
// Define reverse proxy configurations
|
|
244
|
-
const proxyConfigs = [
|
|
245
|
-
{
|
|
246
|
-
hostName: 'example.com',
|
|
247
|
-
destinationIps: ['127.0.0.1'],
|
|
248
|
-
destinationPorts: [3000],
|
|
249
|
-
publicKey: 'your-cert-content',
|
|
250
|
-
privateKey: 'your-key-content',
|
|
251
|
-
rewriteHostHeader: true
|
|
252
|
-
},
|
|
253
|
-
{
|
|
254
|
-
hostName: 'api.example.com',
|
|
255
|
-
destinationIps: ['127.0.0.1'],
|
|
256
|
-
destinationPorts: [4000],
|
|
257
|
-
publicKey: 'your-cert-content',
|
|
258
|
-
privateKey: 'your-key-content',
|
|
259
|
-
// Optional basic auth
|
|
260
|
-
authentication: {
|
|
261
|
-
type: 'Basic',
|
|
262
|
-
user: 'admin',
|
|
263
|
-
pass: 'secret'
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
];
|
|
267
|
-
|
|
268
|
-
// Start the proxy and update configurations
|
|
269
|
-
(async () => {
|
|
270
|
-
await proxy.start();
|
|
271
|
-
await proxy.updateProxyConfigs(proxyConfigs);
|
|
272
|
-
|
|
273
|
-
// Add default headers to all responses
|
|
274
|
-
await proxy.addDefaultHeaders({
|
|
275
|
-
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload'
|
|
276
|
-
});
|
|
277
|
-
})();
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
### HTTP to HTTPS Redirection
|
|
281
|
-
|
|
282
|
-
```typescript
|
|
283
|
-
import { SslRedirect } from '@push.rocks/smartproxy';
|
|
284
|
-
|
|
285
|
-
// Create and start HTTP to HTTPS redirect service on port 80
|
|
286
|
-
const redirector = new SslRedirect(80);
|
|
287
|
-
redirector.start();
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
### TCP Connection Handling with Domain-based Routing
|
|
291
|
-
|
|
292
|
-
```typescript
|
|
293
|
-
import { SmartProxy } from '@push.rocks/smartproxy';
|
|
294
|
-
|
|
295
|
-
// Configure SmartProxy with domain-based routing
|
|
296
|
-
const smartProxy = new SmartProxy({
|
|
297
|
-
fromPort: 443,
|
|
298
|
-
toPort: 8443,
|
|
299
|
-
targetIP: 'localhost', // Default target host
|
|
300
|
-
sniEnabled: true, // Enable SNI inspection
|
|
301
|
-
|
|
302
|
-
// Enhanced reliability settings
|
|
303
|
-
initialDataTimeout: 60000, // 60 seconds for initial TLS handshake
|
|
304
|
-
socketTimeout: 3600000, // 1 hour socket timeout
|
|
305
|
-
maxConnectionLifetime: 3600000, // 1 hour connection lifetime
|
|
306
|
-
inactivityTimeout: 3600000, // 1 hour inactivity timeout
|
|
307
|
-
maxPendingDataSize: 10 * 1024 * 1024, // 10MB buffer for large TLS handshakes
|
|
308
|
-
|
|
309
|
-
// Browser compatibility enhancement
|
|
310
|
-
enableTlsDebugLogging: false, // Enable for troubleshooting TLS issues
|
|
311
|
-
|
|
312
|
-
// Port and IP configuration
|
|
313
|
-
globalPortRanges: [{ from: 443, to: 443 }],
|
|
314
|
-
defaultAllowedIPs: ['*'], // Allow all IPs by default
|
|
315
|
-
|
|
316
|
-
// Socket optimizations for better connection stability
|
|
317
|
-
noDelay: true, // Disable Nagle's algorithm
|
|
318
|
-
keepAlive: true, // Enable TCP keepalive
|
|
319
|
-
enableKeepAliveProbes: true, // Enhanced keepalive for stability
|
|
320
|
-
|
|
321
|
-
// Domain-specific routing configuration
|
|
322
|
-
domainConfigs: [
|
|
323
|
-
{
|
|
324
|
-
domains: ['example.com', '*.example.com'], // Glob patterns for matching domains
|
|
325
|
-
allowedIPs: ['192.168.1.*'], // Restrict access by IP
|
|
326
|
-
blockedIPs: ['192.168.1.100'], // Block specific IPs
|
|
327
|
-
targetIPs: ['10.0.0.1', '10.0.0.2'], // Round-robin between multiple targets
|
|
328
|
-
portRanges: [{ from: 443, to: 443 }],
|
|
329
|
-
connectionTimeout: 7200000 // Domain-specific timeout (2 hours)
|
|
330
|
-
}
|
|
331
|
-
],
|
|
332
|
-
|
|
333
|
-
preserveSourceIP: true
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
smartProxy.start();
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
### NfTables Port Forwarding
|
|
340
|
-
|
|
341
|
-
```typescript
|
|
342
|
-
import { NfTablesProxy } from '@push.rocks/smartproxy';
|
|
343
|
-
|
|
344
|
-
// Basic usage - forward single port
|
|
345
|
-
const basicProxy = new NfTablesProxy({
|
|
346
|
-
fromPort: 80,
|
|
347
|
-
toPort: 8080,
|
|
348
|
-
toHost: 'localhost',
|
|
349
|
-
preserveSourceIP: true,
|
|
350
|
-
deleteOnExit: true // Automatically clean up rules on process exit
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
// Forward port ranges
|
|
354
|
-
const rangeProxy = new NfTablesProxy({
|
|
355
|
-
fromPort: { from: 3000, to: 3010 }, // Forward ports 3000-3010
|
|
356
|
-
toPort: { from: 8000, to: 8010 }, // To ports 8000-8010
|
|
357
|
-
protocol: 'tcp', // TCP protocol (default)
|
|
358
|
-
ipv6Support: true, // Enable IPv6 support
|
|
359
|
-
enableLogging: true // Enable detailed logging
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
// Multiple port specifications with IP filtering
|
|
363
|
-
const advancedProxy = new NfTablesProxy({
|
|
364
|
-
fromPort: [80, 443, { from: 8000, to: 8010 }], // Multiple ports/ranges
|
|
365
|
-
toPort: [8080, 8443, { from: 18000, to: 18010 }],
|
|
366
|
-
allowedSourceIPs: ['10.0.0.0/8', '192.168.1.0/24'], // Only allow these IPs
|
|
367
|
-
bannedSourceIPs: ['192.168.1.100'], // Explicitly block these IPs
|
|
368
|
-
useIPSets: true, // Use IP sets for efficient IP management
|
|
369
|
-
forceCleanSlate: false // Clean all NfTablesProxy rules before starting
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
// Advanced features: QoS, connection tracking, and NetworkProxy integration
|
|
373
|
-
const advancedProxy = new NfTablesProxy({
|
|
374
|
-
fromPort: 443,
|
|
375
|
-
toPort: 8443,
|
|
376
|
-
toHost: 'localhost',
|
|
377
|
-
useAdvancedNAT: true, // Use connection tracking for stateful NAT
|
|
378
|
-
qos: {
|
|
379
|
-
enabled: true,
|
|
380
|
-
maxRate: '10mbps', // Limit bandwidth
|
|
381
|
-
priority: 1 // Set traffic priority (1-10)
|
|
382
|
-
},
|
|
383
|
-
netProxyIntegration: {
|
|
384
|
-
enabled: true,
|
|
385
|
-
redirectLocalhost: true, // Redirect localhost traffic to NetworkProxy
|
|
386
|
-
sslTerminationPort: 8443 // Port where NetworkProxy handles SSL
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
// Start any of the proxies
|
|
391
|
-
await basicProxy.start();
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
### Automatic HTTPS Certificate Management
|
|
395
|
-
|
|
396
|
-
```typescript
|
|
397
|
-
import { Port80Handler } from '@push.rocks/smartproxy';
|
|
398
|
-
|
|
399
|
-
// Create an ACME handler for Let's Encrypt
|
|
400
|
-
const acmeHandler = new Port80Handler({
|
|
401
|
-
port: 80,
|
|
402
|
-
contactEmail: 'admin@example.com',
|
|
403
|
-
useProduction: true, // Use Let's Encrypt production servers (default is staging)
|
|
404
|
-
renewThresholdDays: 30, // Renew certificates 30 days before expiry
|
|
405
|
-
httpsRedirectPort: 443 // Redirect HTTP to HTTPS on this port
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
// Add domains to manage certificates for
|
|
409
|
-
acmeHandler.addDomain({
|
|
410
|
-
domainName: 'example.com',
|
|
411
|
-
sslRedirect: true,
|
|
412
|
-
acmeMaintenance: true
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
acmeHandler.addDomain({
|
|
416
|
-
domainName: 'api.example.com',
|
|
417
|
-
sslRedirect: true,
|
|
418
|
-
acmeMaintenance: true
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
// Support for glob pattern domains for routing (certificates not issued for glob patterns)
|
|
422
|
-
acmeHandler.addDomain({
|
|
423
|
-
domainName: '*.example.com',
|
|
424
|
-
sslRedirect: true,
|
|
425
|
-
acmeMaintenance: false, // Can't issue certificates for wildcard domains via HTTP-01
|
|
426
|
-
forward: { ip: '192.168.1.10', port: 8080 } // Forward requests to this target
|
|
427
|
-
});
|
|
428
|
-
```
|
|
387
|
+
Provide a `certProvider(domain)` in SmartProxy settings to supply static certs or return `'http01'`.
|
|
429
388
|
|
|
430
389
|
## Configuration Options
|
|
431
390
|
|
|
432
|
-
### NetworkProxy
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|-----------------------|---------------------------------------------------|-------------|
|
|
476
|
-
| `fromPort` | Source port(s) or range(s) to forward from | - |
|
|
477
|
-
| `toPort` | Destination port(s) or range(s) to forward to | - |
|
|
478
|
-
| `toHost` | Destination host to forward to | 'localhost' |
|
|
479
|
-
| `preserveSourceIP` | Preserve the original client IP | false |
|
|
480
|
-
| `deleteOnExit` | Remove nftables rules when process exits | false |
|
|
481
|
-
| `protocol` | Protocol to forward ('tcp', 'udp', or 'all') | 'tcp' |
|
|
482
|
-
| `enableLogging` | Enable detailed logging | false |
|
|
483
|
-
| `logFormat` | Format for logs ('plain' or 'json') | 'plain' |
|
|
484
|
-
| `ipv6Support` | Enable IPv6 support | false |
|
|
485
|
-
| `allowedSourceIPs` | Array of IP addresses/CIDR allowed to connect | - |
|
|
486
|
-
| `bannedSourceIPs` | Array of IP addresses/CIDR blocked from connecting | - |
|
|
487
|
-
| `useIPSets` | Use nftables sets for efficient IP management | true |
|
|
488
|
-
| `forceCleanSlate` | Clear all NfTablesProxy rules before starting | false |
|
|
489
|
-
| `tableName` | Custom table name | 'portproxy' |
|
|
490
|
-
| `maxRetries` | Maximum number of retries for failed commands | 3 |
|
|
491
|
-
| `retryDelayMs` | Delay between retries in milliseconds | 1000 |
|
|
492
|
-
| `useAdvancedNAT` | Use connection tracking for stateful NAT | false |
|
|
493
|
-
| `qos` | Quality of Service options (object) | - |
|
|
494
|
-
| `netProxyIntegration` | NetworkProxy integration options (object) | - |
|
|
495
|
-
|
|
496
|
-
## Advanced Features
|
|
497
|
-
|
|
498
|
-
### TLS Handshake Optimization
|
|
499
|
-
|
|
500
|
-
The enhanced `SmartProxy` implementation includes significant improvements for TLS handshake handling:
|
|
501
|
-
|
|
502
|
-
- Robust SNI extraction with improved error handling
|
|
503
|
-
- Increased buffer size for complex TLS handshakes (10MB)
|
|
504
|
-
- Longer initial handshake timeout (60 seconds)
|
|
505
|
-
- Detection and tracking of TLS connection states
|
|
506
|
-
- Optional detailed TLS debug logging for troubleshooting
|
|
507
|
-
- Browser compatibility fixes for Chrome certificate errors
|
|
508
|
-
|
|
509
|
-
```typescript
|
|
510
|
-
// Example configuration to solve Chrome certificate errors
|
|
511
|
-
const portProxy = new SmartProxy({
|
|
512
|
-
// ... other settings
|
|
513
|
-
initialDataTimeout: 60000, // Give browser more time for handshake
|
|
514
|
-
maxPendingDataSize: 10 * 1024 * 1024, // Larger buffer for complex handshakes
|
|
515
|
-
enableTlsDebugLogging: true, // Enable when troubleshooting
|
|
516
|
-
});
|
|
517
|
-
```
|
|
518
|
-
|
|
519
|
-
### Connection Management and Monitoring
|
|
520
|
-
|
|
521
|
-
The `SmartProxy` class includes built-in connection tracking and monitoring:
|
|
522
|
-
|
|
523
|
-
- Automatic cleanup of idle connections with configurable timeouts
|
|
524
|
-
- Timeouts for connections that exceed maximum lifetime
|
|
525
|
-
- Detailed logging of connection states
|
|
526
|
-
- Termination statistics
|
|
527
|
-
- Randomized timeouts to prevent "thundering herd" problems
|
|
528
|
-
- Per-domain timeout configuration
|
|
529
|
-
|
|
530
|
-
### WebSocket Support
|
|
531
|
-
|
|
532
|
-
The `NetworkProxy` class provides WebSocket support with:
|
|
533
|
-
|
|
534
|
-
- WebSocket connection proxying
|
|
535
|
-
- Automatic heartbeat monitoring
|
|
536
|
-
- Connection cleanup for inactive WebSockets
|
|
537
|
-
|
|
538
|
-
### SNI-based Routing
|
|
539
|
-
|
|
540
|
-
The `SmartProxy` class can inspect the SNI (Server Name Indication) field in TLS handshakes to route connections based on the requested domain:
|
|
541
|
-
|
|
542
|
-
- Multiple backend targets per domain
|
|
543
|
-
- Round-robin load balancing
|
|
544
|
-
- Domain-specific allowed IP ranges
|
|
545
|
-
- Protection against SNI renegotiation attacks
|
|
546
|
-
|
|
547
|
-
### Enhanced NfTables Management
|
|
548
|
-
|
|
549
|
-
The `NfTablesProxy` class offers advanced capabilities:
|
|
550
|
-
|
|
551
|
-
- Support for multiple port ranges and individual ports
|
|
552
|
-
- More efficient IP filtering using nftables sets
|
|
553
|
-
- IPv6 support with full feature parity
|
|
554
|
-
- Quality of Service (QoS) features including bandwidth limiting and traffic prioritization
|
|
555
|
-
- Advanced connection tracking for stateful NAT
|
|
556
|
-
- Robust error handling with retry mechanisms
|
|
557
|
-
- Structured logging with JSON support
|
|
558
|
-
- NetworkProxy integration for SSL termination
|
|
559
|
-
- Comprehensive cleanup on shutdown
|
|
560
|
-
|
|
561
|
-
### Port80Handler with Glob Pattern Support
|
|
562
|
-
|
|
563
|
-
The `Port80Handler` class includes support for glob pattern domain matching:
|
|
564
|
-
|
|
565
|
-
- Supports wildcard domains like `*.example.com` for HTTP request routing
|
|
566
|
-
- Detects glob patterns and skips certificate issuance for them
|
|
567
|
-
- Smart routing that first attempts exact matches, then tries pattern matching
|
|
568
|
-
- Supports forwarding HTTP requests to backend services
|
|
569
|
-
- Separate forwarding configuration for ACME challenges
|
|
391
|
+
### NetworkProxy (INetworkProxyOptions)
|
|
392
|
+
- `port` (number, required)
|
|
393
|
+
- `backendProtocol` ('http1'|'http2', default 'http1')
|
|
394
|
+
- `maxConnections` (number, default 10000)
|
|
395
|
+
- `keepAliveTimeout` (ms, default 120000)
|
|
396
|
+
- `headersTimeout` (ms, default 60000)
|
|
397
|
+
- `cors` (object)
|
|
398
|
+
- `connectionPoolSize` (number, default 50)
|
|
399
|
+
- `logLevel` ('error'|'warn'|'info'|'debug')
|
|
400
|
+
- `acme` (IAcmeOptions)
|
|
401
|
+
- `useExternalPort80Handler` (boolean)
|
|
402
|
+
- `portProxyIntegration` (boolean)
|
|
403
|
+
|
|
404
|
+
### Port80Handler (IAcmeOptions)
|
|
405
|
+
- `enabled` (boolean, default true)
|
|
406
|
+
- `port` (number, default 80)
|
|
407
|
+
- `contactEmail` (string)
|
|
408
|
+
- `useProduction` (boolean, default false)
|
|
409
|
+
- `renewThresholdDays` (number, default 30)
|
|
410
|
+
- `autoRenew` (boolean, default true)
|
|
411
|
+
- `certificateStore` (string)
|
|
412
|
+
- `skipConfiguredCerts` (boolean)
|
|
413
|
+
- `domainForwards` (IDomainForwardConfig[])
|
|
414
|
+
|
|
415
|
+
### NfTablesProxy (INfTableProxySettings)
|
|
416
|
+
- `fromPort` / `toPort` (number|range|array)
|
|
417
|
+
- `toHost` (string, default 'localhost')
|
|
418
|
+
- `preserveSourceIP`, `deleteOnExit`, `protocol`, `enableLogging`, `ipv6Support` (booleans)
|
|
419
|
+
- `allowedSourceIPs`, `bannedSourceIPs` (string[])
|
|
420
|
+
- `useIPSets` (boolean, default true)
|
|
421
|
+
- `qos`, `netProxyIntegration` (objects)
|
|
422
|
+
|
|
423
|
+
### Redirect / SslRedirect
|
|
424
|
+
- Constructor options: `httpPort`, `httpsPort`, `sslOptions`, `rules` (RedirectRule[])
|
|
425
|
+
|
|
426
|
+
### SmartProxy (IPortProxySettings)
|
|
427
|
+
- `fromPort`, `toPort` (number)
|
|
428
|
+
- `domainConfigs` (IDomainConfig[])
|
|
429
|
+
- `sniEnabled`, `defaultAllowedIPs`, `preserveSourceIP` (booleans)
|
|
430
|
+
- Timeouts: `initialDataTimeout`, `socketTimeout`, `inactivityTimeout`, etc.
|
|
431
|
+
- Socket opts: `noDelay`, `keepAlive`, `enableKeepAliveProbes`
|
|
432
|
+
- `acme` (IAcmeOptions), `certProvider` (callback)
|
|
433
|
+
- `useNetworkProxy` (number[]), `networkProxyPort` (number)
|
|
570
434
|
|
|
571
435
|
## Troubleshooting
|
|
572
436
|
|
|
573
|
-
###
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
1. **Increase Initial Data Timeout**: Set `initialDataTimeout` to 60 seconds or higher
|
|
578
|
-
2. **Increase Buffer Size**: Set `maxPendingDataSize` to 10MB or higher
|
|
579
|
-
3. **Enable TLS Debug Logging**: Set `enableTlsDebugLogging: true` to troubleshoot handshake issues
|
|
580
|
-
4. **Enable Keep-Alive Probes**: Set `enableKeepAliveProbes: true` for better connection stability
|
|
581
|
-
5. **Check Certificate Chain**: Ensure your certificate chain is complete and in the correct order
|
|
582
|
-
|
|
583
|
-
```typescript
|
|
584
|
-
// Configuration to fix Chrome certificate errors
|
|
585
|
-
const smartProxy = new SmartProxy({
|
|
586
|
-
// ... other settings
|
|
587
|
-
initialDataTimeout: 60000,
|
|
588
|
-
maxPendingDataSize: 10 * 1024 * 1024,
|
|
589
|
-
enableTlsDebugLogging: true,
|
|
590
|
-
enableKeepAliveProbes: true
|
|
591
|
-
});
|
|
592
|
-
```
|
|
593
|
-
|
|
594
|
-
### Connection Stability
|
|
595
|
-
|
|
596
|
-
For improved connection stability in high-traffic environments:
|
|
437
|
+
### NetworkProxy
|
|
438
|
+
- Verify ports, certificates and `rejectUnauthorized` for TLS errors
|
|
439
|
+
- Configure CORS or use `addDefaultHeaders` for preflight issues
|
|
440
|
+
- Increase `maxConnections` or `connectionPoolSize` under load
|
|
597
441
|
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
4. **Monitor Connection Statistics**: Enable detailed logging to track termination reasons
|
|
602
|
-
5. **Fine-tune Inactivity Checks**: Adjust `inactivityCheckInterval` based on your traffic patterns
|
|
442
|
+
### Port80Handler
|
|
443
|
+
- Run as root or grant CAP_NET_BIND_SERVICE for port 80
|
|
444
|
+
- Inspect `certificate-failed` events and switch staging/production
|
|
603
445
|
|
|
604
|
-
###
|
|
446
|
+
### NfTablesProxy
|
|
447
|
+
- Ensure `nft` is installed and run with sufficient privileges
|
|
448
|
+
- Use `forceCleanSlate:true` to clear conflicting rules
|
|
605
449
|
|
|
606
|
-
|
|
450
|
+
### Redirect / SslRedirect
|
|
451
|
+
- Check `fromHost`/`fromPath` patterns and Host headers
|
|
452
|
+
- Validate `sslOptions` key/cert correctness
|
|
607
453
|
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
5. **Verify IPv6 Support**: If using `ipv6Support: true`, ensure ip6tables is available
|
|
454
|
+
### SmartProxy & SniHandler
|
|
455
|
+
- Increase `initialDataTimeout`/`maxPendingDataSize` for large ClientHello
|
|
456
|
+
- Enable `enableTlsDebugLogging` to trace handshake
|
|
457
|
+
- Ensure `allowSessionTicket` and fragmentation support for resumption
|
|
613
458
|
|
|
614
459
|
## License and Legal Information
|
|
615
460
|
|