@serve.zone/remoteingress 4.17.0 → 4.17.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/readme.md CHANGED
@@ -1,490 +1,216 @@
1
1
  # @serve.zone/remoteingress
2
2
 
3
- Edge ingress tunnel for DcRouter — tunnels **TCP and UDP** traffic from the network edge to a private DcRouter/SmartProxy cluster over encrypted TLS or QUIC connections, preserving the original client IP via PROXY protocol. Includes **hub-controlled nftables firewall** for IP blocking, rate limiting, and custom firewall rules applied directly at the edge.
3
+ `@serve.zone/remoteingress` is a Rust-powered, TypeScript-controlled edge tunnel for moving TCP and UDP traffic from public edge nodes into a private dcrouter or SmartProxy host while preserving the original client IP through PROXY protocol.
4
4
 
5
5
  ## Issue Reporting and Security
6
6
 
7
7
  For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
8
8
 
9
- ## Install
9
+ ## What It Does
10
+
11
+ Remote ingress solves the common edge problem: your workload or gateway lives behind NAT, a firewall, or a private network, but public traffic should enter through one or more hardened edge VPS nodes.
10
12
 
11
- ```sh
12
- pnpm install @serve.zone/remoteingress
13
+ ```
14
+ Internet clients
15
+ |
16
+ v
17
+ public edge node: RemoteIngressEdge
18
+ |
19
+ | TLS or QUIC tunnel
20
+ v
21
+ private site: RemoteIngressHub -> dcrouter / SmartProxy
13
22
  ```
14
23
 
15
- ## Architecture
24
+ The edge binds public TCP and UDP ports assigned by the hub. Each accepted connection or datagram is tunneled to the hub, which forwards it to a local target host with PROXY headers so downstream routing can still see the real client address.
16
25
 
17
- `@serve.zone/remoteingress` uses a **Hub/Edge** topology with a high-performance Rust core and a TypeScript API surface:
26
+ ## Highlights
18
27
 
28
+ - ⚡ Rust networking core managed from TypeScript through `@push.rocks/smartrust`
29
+ - 🔁 Hub/edge model with dynamic `updateAllowedEdges()` reconciliation
30
+ - 🌐 TCP forwarding over frame-multiplexed TLS or native QUIC streams
31
+ - 📡 UDP forwarding over TCP frames or QUIC datagrams
32
+ - 🧾 PROXY protocol preservation for client IP visibility at SmartProxy
33
+ - 🛡️ Hub-pushed nftables firewall snapshots for blocklists, rate limits, and custom rules
34
+ - 🔐 Shared-secret edge authentication and compact connection tokens
35
+ - 🚦 Transport modes: `tcpTls`, `quic`, and `quicWithFallback`
36
+ - 📊 EventEmitter status events for edge lifecycle, streams, port assignments, and crash recovery
37
+
38
+ ## Install
39
+
40
+ ```bash
41
+ pnpm add @serve.zone/remoteingress
19
42
  ```
20
- TLS or QUIC Tunnel
21
- ┌─────────────────────┐ ◄══════════════════════════► ┌─────────────────────┐
22
- │ Network Edge │ TCP+TLS: frame mux │ Private Cluster │
23
- │ │ QUIC: native streams │ │
24
- │ RemoteIngressEdge │ UDP: QUIC datagrams │ RemoteIngressHub │
25
- │ │ │ │
26
- │ • TCP/UDP listeners│ ◄─── FRAME_CONFIG pushes ─── │ • Port assignments │
27
- │ • nftables firewall│ ports + firewall rules │ • Firewall config │
28
- │ • Rate limiting │ at any time │ • Rate limit rules │
29
- └─────────────────────┘ └─────────────────────┘
30
- ▲ │
31
- │ TCP + UDP from end users ▼
32
- Internet DcRouter / SmartProxy
33
- ```
34
43
 
35
- | Component | Role |
36
- |-----------|------|
37
- | **RemoteIngressEdge** | Deployed at the network edge (VPS, cloud instance). Runs as root. Listens on hub-assigned TCP/UDP ports, tunnels traffic to the hub, and applies hub-pushed nftables rules (IP blocking, rate limiting). All config is hot-reloadable at runtime. |
38
- | **RemoteIngressHub** | Deployed alongside DcRouter/SmartProxy in a private cluster. Accepts edge connections, demuxes streams/datagrams, and forwards each to SmartProxy with PROXY protocol headers so the real client IP is preserved. Pushes all edge config (ports, firewall) via a single API. |
39
- | **Rust Binary** (`remoteingress-bin`) | The performance-critical networking core. Managed via `@push.rocks/smartrust` RustBridge IPC — you never interact with it directly. Cross-compiled for `linux/amd64` and `linux/arm64`. |
40
-
41
- ### ⚡ Key Features
42
-
43
- - **Dual transport** — choose between TCP+TLS (frame-multiplexed) or QUIC (native stream multiplexing, zero head-of-line blocking)
44
- - **TCP + UDP tunneling** — tunnel any TCP connection or UDP datagram through the same edge/hub pair
45
- - **PROXY protocol v1 & v2** — SmartProxy sees the real client IP for both TCP (v1 text) and UDP (v2 binary)
46
- - **Hub-controlled firewall** — push nftables rules (IP blocking, rate limiting, custom firewall rules) to edges as part of the same config update that assigns ports — powered by `@push.rocks/smartnftables`
47
- - **Multiplexed streams** — thousands of concurrent TCP connections over a single tunnel
48
- - **QUIC datagrams** — UDP traffic forwarded via QUIC unreliable datagrams for lowest possible latency
49
- - **Shared-secret authentication** — edges must present valid credentials to connect
50
- - **Connection tokens** — encode all connection details into a single opaque base64url string
51
- - **STUN-based public IP discovery** — edges automatically discover their public IP via Cloudflare STUN
52
- - **Auto-reconnect** with exponential backoff if the tunnel drops
53
- - **Dynamic runtime configuration** — the hub pushes ports, firewall rules, and rate limits to edges at any time via a single `updateAllowedEdges()` call
54
- - **Event-driven** — both Hub and Edge extend `EventEmitter` for real-time monitoring
55
- - **3-tier QoS** — control frames, normal data, and sustained (elephant flow) traffic each get their own priority queue
56
- - **Adaptive flow control** — per-stream windows scale with active stream count to prevent memory overuse
57
- - **UDP session management** — automatic session tracking with 60s idle timeout and cleanup
58
- - **Crash recovery** — automatic restart with exponential backoff if the Rust binary crashes unexpectedly
59
-
60
- ## Usage
61
-
62
- Both classes are imported from the package and communicate with the Rust binary under the hood.
63
-
64
- ### Setting Up the Hub (Private Cluster Side)
44
+ ## Hub Side
45
+
46
+ Run the hub next to the private service you want to expose. In dcrouter deployments the target is usually SmartProxy on `127.0.0.1`.
65
47
 
66
48
  ```typescript
67
49
  import { RemoteIngressHub } from '@serve.zone/remoteingress';
68
50
 
69
51
  const hub = new RemoteIngressHub();
70
52
 
71
- // Listen for events
72
- hub.on('edgeConnected', ({ edgeId }) => console.log(`Edge ${edgeId} connected`));
73
- hub.on('edgeDisconnected', ({ edgeId }) => console.log(`Edge ${edgeId} disconnected`));
74
- hub.on('streamOpened', ({ edgeId, streamId }) => console.log(`Stream ${streamId} from ${edgeId}`));
75
- hub.on('streamClosed', ({ edgeId, streamId }) => console.log(`Stream ${streamId} closed`));
53
+ hub.on('edgeConnected', ({ edgeId }) => console.log('edge connected', edgeId));
54
+ hub.on('edgeDisconnected', ({ edgeId }) => console.log('edge disconnected', edgeId));
55
+ hub.on('streamOpened', ({ edgeId, streamId }) => console.log('stream', edgeId, streamId));
76
56
 
77
- // Start the hub — listens for edge connections on both TCP and QUIC (same port)
78
57
  await hub.start({
79
- tunnelPort: 8443, // port edges connect to (default: 8443)
80
- targetHost: '127.0.0.1', // SmartProxy host to forward traffic to
58
+ tunnelPort: 8443,
59
+ targetHost: '127.0.0.1',
81
60
  });
82
61
 
83
- // Register allowed edges with TCP and UDP listen ports + firewall config
84
62
  await hub.updateAllowedEdges([
85
63
  {
86
- id: 'edge-nyc-01',
87
- secret: 'supersecrettoken1',
88
- listenPorts: [80, 443], // TCP ports the edge should listen on
89
- listenPortsUdp: [53, 51820], // UDP ports (e.g., DNS, WireGuard)
64
+ id: 'edge-fra-01',
65
+ secret: 'replace-with-a-long-random-secret',
66
+ listenPorts: [80, 443],
67
+ listenPortsUdp: [443],
90
68
  stunIntervalSecs: 300,
91
69
  firewallConfig: {
92
- blockedIps: ['192.168.1.100', '10.0.0.0/8'],
93
- rateLimits: [
94
- { id: 'http-rate', port: 80, protocol: 'tcp', rate: '100/second', perSourceIP: true },
95
- ],
96
- rules: [
97
- { id: 'allow-ssh', direction: 'input', action: 'accept', sourceIP: '10.0.0.0/24', destPort: 22, protocol: 'tcp' },
98
- ],
99
- },
100
- },
101
- {
102
- id: 'edge-fra-02',
103
- secret: 'supersecrettoken2',
104
- listenPorts: [443, 8080],
105
- },
106
- ]);
107
-
108
- // Dynamically update ports and firewall — changes are pushed instantly to connected edges
109
- await hub.updateAllowedEdges([
110
- {
111
- id: 'edge-nyc-01',
112
- secret: 'supersecrettoken1',
113
- listenPorts: [80, 443, 8443], // added TCP port 8443
114
- listenPortsUdp: [53], // removed WireGuard UDP port
115
- firewallConfig: {
116
- blockedIps: ['192.168.1.100', '10.0.0.0/8', '203.0.113.50'], // added new blocked IP
70
+ blockedIps: ['198.51.100.25'],
117
71
  rateLimits: [
118
- { id: 'http-rate', port: 80, protocol: 'tcp', rate: '200/second', perSourceIP: true },
72
+ { id: 'https-per-ip', port: 443, protocol: 'tcp', rate: '200/second', burst: 100, perSourceIP: true },
119
73
  ],
120
74
  },
121
75
  },
122
76
  ]);
123
77
 
124
- // Check status
125
78
  const status = await hub.getStatus();
126
- // { running: true, tunnelPort: 8443, connectedEdges: [...] }
79
+ console.log(status.connectedEdges);
127
80
 
128
81
  await hub.stop();
129
82
  ```
130
83
 
131
- ### Setting Up the Edge (Network Edge Side)
84
+ ## Edge Side
132
85
 
133
- The edge connects via **QUIC with TCP+TLS fallback** by default. Edges run as **root** so they can bind to privileged ports and apply nftables firewall rules.
134
-
135
- #### Option A: Connection Token (Recommended)
86
+ The edge runs on the public node. It normally needs root privileges to bind privileged ports and apply nftables rules. If nftables cannot be initialized, the tunnel can still run, but kernel-level edge firewalling is skipped.
136
87
 
137
88
  ```typescript
138
89
  import { RemoteIngressEdge } from '@serve.zone/remoteingress';
139
90
 
140
91
  const edge = new RemoteIngressEdge();
141
92
 
142
- edge.on('tunnelConnected', () => console.log('Tunnel established'));
143
- edge.on('tunnelDisconnected', () => console.log('Tunnel lost — will auto-reconnect'));
144
- edge.on('publicIpDiscovered', ({ ip }) => console.log(`Public IP: ${ip}`));
145
- edge.on('portsAssigned', ({ listenPorts }) => console.log(`TCP ports: ${listenPorts}`));
146
- edge.on('firewallConfigUpdated', () => console.log('Firewall rules applied'));
147
-
148
- await edge.start({
149
- token: 'eyJoIjoiaHViLmV4YW1wbGUuY29tIiwi...',
150
- });
151
- ```
152
-
153
- #### Option B: Explicit Config with QUIC Transport
154
-
155
- ```typescript
156
- import { RemoteIngressEdge } from '@serve.zone/remoteingress';
157
-
158
- const edge = new RemoteIngressEdge();
93
+ edge.on('tunnelConnected', () => console.log('tunnel connected'));
94
+ edge.on('portsAssigned', ({ listenPorts }) => console.log('TCP ports', listenPorts));
95
+ edge.on('firewallConfigUpdated', () => console.log('firewall snapshot applied'));
159
96
 
160
97
  await edge.start({
161
- hubHost: 'hub.example.com',
98
+ hubHost: 'ingress-hub.example.com',
162
99
  hubPort: 8443,
163
- edgeId: 'edge-nyc-01',
164
- secret: 'supersecrettoken1',
165
- transportMode: 'quic', // 'tcpTls' | 'quic' | 'quicWithFallback' (default)
100
+ edgeId: 'edge-fra-01',
101
+ secret: 'replace-with-a-long-random-secret',
102
+ transportMode: 'quicWithFallback',
166
103
  });
167
104
 
168
- const edgeStatus = await edge.getStatus();
169
- // { running: true, connected: true, publicIp: '203.0.113.42', activeStreams: 5, listenPorts: [80, 443] }
105
+ console.log(await edge.getStatus());
170
106
 
171
107
  await edge.stop();
172
108
  ```
173
109
 
174
- #### Transport Modes
175
-
176
- | Mode | Description |
177
- |------|-------------|
178
- | `'tcpTls'` | Single TLS connection with frame-based multiplexing. Universal compatibility. |
179
- | `'quic'` | QUIC with native stream multiplexing. Eliminates head-of-line blocking. Uses QUIC datagrams for UDP traffic. |
180
- | `'quicWithFallback'` | **Default.** Tries QUIC first (5s timeout), falls back to TCP+TLS if UDP is blocked by the network. |
110
+ ## Connection Tokens
181
111
 
182
- ### Connection Tokens
183
-
184
- Encode all connection details into a single opaque string for easy distribution:
112
+ Tokens are base64url-encoded compact JSON. They are useful when the hub operator provisions an edge and wants to hand over one opaque string.
185
113
 
186
114
  ```typescript
187
- import { encodeConnectionToken, decodeConnectionToken } from '@serve.zone/remoteingress';
115
+ import {
116
+ RemoteIngressEdge,
117
+ encodeConnectionToken,
118
+ decodeConnectionToken,
119
+ } from '@serve.zone/remoteingress';
188
120
 
189
- // Hub operator generates a token
190
121
  const token = encodeConnectionToken({
191
- hubHost: 'hub.example.com',
122
+ hubHost: 'ingress-hub.example.com',
192
123
  hubPort: 8443,
193
- edgeId: 'edge-nyc-01',
194
- secret: 'supersecrettoken1',
124
+ edgeId: 'edge-fra-01',
125
+ secret: 'replace-with-a-long-random-secret',
195
126
  });
196
- // => 'eyJoIjoiaHViLmV4YW1wbGUuY29tIiwi...'
197
127
 
198
- // Edge operator decodes (optional — start() does this automatically)
199
- const data = decodeConnectionToken(token);
200
- // { hubHost: 'hub.example.com', hubPort: 8443, edgeId: 'edge-nyc-01', secret: '...' }
128
+ console.log(decodeConnectionToken(token));
129
+
130
+ const edge = new RemoteIngressEdge();
131
+ await edge.start({ token });
201
132
  ```
202
133
 
203
- Tokens are base64url-encoded — safe for environment variables, CLI arguments, and config files.
134
+ ## CLI Mode
204
135
 
205
- ## 🔥 Firewall Config
136
+ The package entry point exports `runCli()`, and the repo's `cli.js` wrapper can run hub or edge mode.
206
137
 
207
- The `firewallConfig` field in `updateAllowedEdges()` works exactly like `listenPorts` — it travels in the same `FRAME_CONFIG` frame, is delivered on initial handshake and on every subsequent update, and is applied atomically at the edge using `@push.rocks/smartnftables`. Each update fully replaces the previous ruleset.
138
+ ```bash
139
+ node cli.js hub --tunnel-port 8443 --target-host 127.0.0.1
140
+ node cli.js edge --token eyJoIjoiaW5ncmVzcy1odWIuZXhhbXBsZS5jb20i...
141
+ ```
208
142
 
209
- Since edges run as root, the rules are applied directly to the Linux kernel via nftables. If the edge isn't root or nftables is unavailable, it logs a warning and continues — the tunnel works fine, just without kernel-level firewall rules.
143
+ Environment-based startup is also supported:
210
144
 
211
- ### Config Structure
145
+ | Variable | Purpose |
146
+ | --- | --- |
147
+ | `REMOTEINGRESS_MODE` | `hub` or `edge` |
148
+ | `REMOTEINGRESS_TOKEN` | Edge connection token |
149
+ | `REMOTEINGRESS_HUB_HOST` / `REMOTEINGRESS_HUB_PORT` | Explicit edge connection target |
150
+ | `REMOTEINGRESS_EDGE_ID` / `REMOTEINGRESS_SECRET` | Explicit edge credentials |
151
+ | `REMOTEINGRESS_TARGET_HOST` | Hub-side forwarding target, default `127.0.0.1` |
152
+ | `REMOTEINGRESS_ALLOWED_EDGES_JSON` | Hub-side allowed edge list |
153
+ | `REMOTEINGRESS_PERFORMANCE_JSON` | Optional performance configuration |
212
154
 
213
- ```typescript
214
- interface IFirewallConfig {
215
- blockedIps?: string[]; // IPs or CIDRs to block (e.g., '1.2.3.4', '10.0.0.0/8')
216
- rateLimits?: IFirewallRateLimit[];
217
- rules?: IFirewallRule[];
218
- }
219
-
220
- interface IFirewallRateLimit {
221
- id: string; // unique identifier for this rate limit
222
- port: number; // port to rate-limit
223
- protocol?: 'tcp' | 'udp'; // default: both
224
- rate: string; // e.g., '100/second', '1000/minute'
225
- burst?: number; // burst allowance
226
- perSourceIP?: boolean; // per-client rate limiting (recommended)
227
- }
228
-
229
- interface IFirewallRule {
230
- id: string; // unique identifier for this rule
231
- direction: 'input' | 'output' | 'forward';
232
- action: 'accept' | 'drop' | 'reject';
233
- sourceIP?: string; // source IP or CIDR
234
- destPort?: number; // destination port
235
- protocol?: 'tcp' | 'udp';
236
- comment?: string;
237
- }
238
- ```
155
+ ## Transport Modes
156
+
157
+ | Mode | Behavior |
158
+ | --- | --- |
159
+ | `tcpTls` | Single TLS connection with frame-based stream multiplexing. Good for conservative networks. |
160
+ | `quic` | QUIC streams for TCP and QUIC datagrams for UDP. Best latency and no TCP head-of-line blocking. |
161
+ | `quicWithFallback` | Default edge mode. Tries QUIC and falls back to TCP/TLS when UDP is blocked. |
239
162
 
240
- ### Example: Rate Limiting + IP Blocking
163
+ ## Firewall Config
164
+
165
+ `firewallConfig` travels in the same hub-to-edge configuration update as port assignments. Each update is a full desired-state snapshot.
241
166
 
242
167
  ```typescript
243
168
  await hub.updateAllowedEdges([
244
169
  {
245
- id: 'edge-nyc-01',
170
+ id: 'edge-fra-01',
246
171
  secret: 'secret',
247
172
  listenPorts: [80, 443],
248
173
  firewallConfig: {
249
- // Block known bad actors
250
- blockedIps: ['198.51.100.0/24', '203.0.113.50'],
251
-
252
- // Rate limit HTTP traffic per source IP
174
+ blockedIps: ['203.0.113.0/24'],
253
175
  rateLimits: [
254
- { id: 'http', port: 80, protocol: 'tcp', rate: '100/second', burst: 50, perSourceIP: true },
255
- { id: 'https', port: 443, protocol: 'tcp', rate: '200/second', burst: 100, perSourceIP: true },
176
+ { id: 'http', port: 80, protocol: 'tcp', rate: '100/second', perSourceIP: true },
256
177
  ],
257
-
258
- // Allow monitoring from trusted subnet
259
178
  rules: [
260
- { id: 'monitoring', direction: 'input', action: 'accept', sourceIP: '10.0.0.0/24', destPort: 9090, protocol: 'tcp', comment: 'Prometheus scraping' },
179
+ { id: 'allow-monitoring', direction: 'input', action: 'accept', sourceIP: '10.0.0.0/8', destPort: 9090, protocol: 'tcp' },
261
180
  ],
262
181
  },
263
182
  },
264
183
  ]);
265
184
  ```
266
185
 
267
- ## API Reference
268
-
269
- ### `RemoteIngressHub`
270
-
271
- | Method / Property | Description |
272
- |-------------------|-------------|
273
- | `start(config?)` | Start the hub. Config: `{ tunnelPort?, targetHost?, tls?: { certPem?, keyPem? } }`. Listens on both TCP and UDP (QUIC) on the tunnel port. |
274
- | `stop()` | Graceful shutdown. |
275
- | `updateAllowedEdges(edges)` | Set authorized edges. Each: `{ id, secret, listenPorts?, listenPortsUdp?, stunIntervalSecs?, firewallConfig? }`. Port and firewall changes are pushed to connected edges in real time. |
276
- | `getStatus()` | Returns `{ running, tunnelPort, connectedEdges: [...] }`. |
277
- | `running` | `boolean` — whether the Rust binary is alive. |
278
-
279
- **Events:** `edgeConnected`, `edgeDisconnected`, `streamOpened`, `streamClosed`, `crashRecovered`, `crashRecoveryFailed`
280
-
281
- ### `RemoteIngressEdge`
282
-
283
- | Method / Property | Description |
284
- |-------------------|-------------|
285
- | `start(config)` | Connect to hub. Accepts `{ token }` or `{ hubHost, hubPort, edgeId, secret, bindAddress?, transportMode? }`. |
286
- | `stop()` | Graceful shutdown. Cleans up all nftables rules. |
287
- | `getStatus()` | Returns `{ running, connected, publicIp, activeStreams, listenPorts }`. |
288
- | `running` | `boolean` — whether the Rust binary is alive. |
289
-
290
- **Events:** `tunnelConnected`, `tunnelDisconnected`, `publicIpDiscovered`, `portsAssigned`, `portsUpdated`, `firewallConfigUpdated`, `crashRecovered`, `crashRecoveryFailed`
291
-
292
- ### Token Utilities
293
-
294
- | Function | Description |
295
- |----------|-------------|
296
- | `encodeConnectionToken(data)` | Encodes connection info into a base64url token. |
297
- | `decodeConnectionToken(token)` | Decodes a token. Throws on malformed input. |
298
-
299
- ### Interfaces
300
-
301
- ```typescript
302
- interface IHubConfig {
303
- tunnelPort?: number; // default: 8443
304
- targetHost?: string; // default: '127.0.0.1'
305
- tls?: {
306
- certPem?: string; // PEM-encoded TLS certificate
307
- keyPem?: string; // PEM-encoded TLS private key
308
- };
309
- }
310
-
311
- interface IEdgeConfig {
312
- hubHost: string;
313
- hubPort?: number; // default: 8443
314
- edgeId: string;
315
- secret: string;
316
- bindAddress?: string;
317
- transportMode?: 'tcpTls' | 'quic' | 'quicWithFallback';
318
- }
319
-
320
- interface IConnectionTokenData {
321
- hubHost: string;
322
- hubPort: number;
323
- edgeId: string;
324
- secret: string;
325
- }
326
- ```
327
-
328
- ## Wire Protocol
186
+ ## API Surface
329
187
 
330
- ### TCP+TLS Transport (Frame Protocol)
188
+ | Export | Purpose |
189
+ | --- | --- |
190
+ | `RemoteIngressHub` | Starts/stops the private hub, authorizes edges, pushes runtime config, and reports connected edges. |
191
+ | `RemoteIngressEdge` | Starts/stops the public edge, connects to the hub, binds assigned ports, and applies firewall rules. |
192
+ | `encodeConnectionToken()` | Encodes hub host, port, edge ID, and secret into a token. |
193
+ | `decodeConnectionToken()` | Decodes and validates a token. |
194
+ | `IHubConfig`, `IEdgeConfig`, `TAllowedEdge` | Primary TypeScript shapes for integrating the module. |
331
195
 
332
- The tunnel uses a custom binary frame protocol over a single TLS connection:
196
+ ## Development
333
197
 
198
+ ```bash
199
+ pnpm run build
200
+ pnpm test
334
201
  ```
335
- [stream_id: 4 bytes BE][type: 1 byte][length: 4 bytes BE][payload: N bytes]
336
- ```
337
-
338
- | Frame Type | Value | Direction | Purpose |
339
- |------------|-------|-----------|---------|
340
- | `OPEN` | `0x01` | Edge → Hub | Open TCP stream; payload is PROXY v1 header |
341
- | `DATA` | `0x02` | Edge → Hub | Client data (upload) |
342
- | `CLOSE` | `0x03` | Edge → Hub | Client closed connection |
343
- | `DATA_BACK` | `0x04` | Hub → Edge | Response data (download) |
344
- | `CLOSE_BACK` | `0x05` | Hub → Edge | Upstream closed connection |
345
- | `CONFIG` | `0x06` | Hub → Edge | Runtime config update (JSON: ports + firewall config) |
346
- | `PING` | `0x07` | Hub → Edge | Heartbeat probe (every 15s) |
347
- | `PONG` | `0x08` | Edge → Hub | Heartbeat response |
348
- | `WINDOW_UPDATE` | `0x09` | Edge → Hub | Flow control: edge consumed N bytes |
349
- | `WINDOW_UPDATE_BACK` | `0x0A` | Hub → Edge | Flow control: hub consumed N bytes |
350
- | `UDP_OPEN` | `0x0B` | Edge → Hub | Open UDP session; payload is PROXY v2 header |
351
- | `UDP_DATA` | `0x0C` | Edge → Hub | UDP datagram (upload) |
352
- | `UDP_DATA_BACK` | `0x0D` | Hub → Edge | UDP datagram (download) |
353
- | `UDP_CLOSE` | `0x0E` | Either | Close UDP session |
354
-
355
- ### QUIC Transport
356
-
357
- When using QUIC, the frame protocol is replaced by native QUIC primitives:
358
-
359
- - **TCP connections:** Each tunneled TCP connection gets its own QUIC bidirectional stream. No framing overhead.
360
- - **UDP datagrams:** Forwarded via QUIC unreliable datagrams (RFC 9221). Format: `[session_id: 4 bytes][payload]`. Session open uses magic byte `0xFF`: `[session_id: 4][0xFF][PROXY v2 header]`.
361
- - **Control channel:** First QUIC bidirectional stream carries auth handshake + config updates using `[type: 1][length: 4][payload]` format.
362
202
 
363
- ### Handshake Sequence
203
+ Useful source entry points:
364
204
 
365
- 1. Edge opens a TLS or QUIC connection to the hub
366
- 2. Edge sends: `EDGE <edgeId> <secret>\n`
367
- 3. Hub verifies credentials (constant-time comparison) and responds with JSON:
368
- `{"listenPorts":[...],"listenPortsUdp":[...],"stunIntervalSecs":300,"firewallConfig":{...}}\n`
369
- 4. Edge starts TCP and UDP listeners on the assigned ports
370
- 5. Edge applies firewall config via nftables (if present and running as root)
371
- 6. Data flows — TCP frames/QUIC streams for TCP traffic, UDP frames/QUIC datagrams for UDP traffic
372
-
373
- ## QoS & Flow Control
374
-
375
- ### Priority Tiers (TCP+TLS Transport)
376
-
377
- | Tier | Frames | Behavior |
378
- |------|--------|----------|
379
- | **Control** | PING, PONG, WINDOW_UPDATE, OPEN, CLOSE, CONFIG | Always drained first. Never delayed. |
380
- | **Data** | DATA/DATA_BACK from normal streams, UDP frames | Drained when control queue is empty. |
381
- | **Sustained** | DATA/DATA_BACK from elephant flows | Lowest priority with guaranteed **1 MB/s** drain rate. |
382
-
383
- ### Sustained Stream Classification
384
-
385
- A TCP stream is classified as **sustained** (elephant flow) when:
386
- - Active for **>10 seconds**, AND
387
- - Average throughput exceeds **20 Mbit/s** (2.5 MB/s)
388
-
389
- Once classified, its flow control window locks to 1 MB and data frames move to the lowest-priority queue.
390
-
391
- ### Adaptive Per-Stream Windows
392
-
393
- Each TCP stream has a send window from a shared **200 MB budget**:
394
-
395
- | Active Streams | Window per Stream |
396
- |---|---|
397
- | 1–50 | 4 MB (maximum) |
398
- | 51–200 | Scales down (4 MB → 1 MB) |
399
- | 200+ | 1 MB (floor) |
400
-
401
- UDP traffic uses no flow control — datagrams are fire-and-forget, matching UDP semantics.
402
-
403
- ## Example Scenarios
404
-
405
- ### 1. 🌐 Expose a Private Cluster to the Internet
406
-
407
- Deploy an Edge on a public VPS, point DNS to its IP. The Edge tunnels all TCP and UDP traffic to the Hub running inside your private cluster. No public ports needed on the cluster.
408
-
409
- ### 2. 🗺️ Multi-Region Edge Ingress
410
-
411
- Run Edges in NYC, Frankfurt, and Tokyo — all connecting to a single Hub. Use GeoDNS to route users to their nearest Edge. PROXY protocol ensures the Hub sees real client IPs regardless of which Edge they entered through.
412
-
413
- ### 3. 📡 UDP Forwarding (DNS, Gaming, VoIP)
414
-
415
- Configure UDP listen ports alongside TCP ports. DNS queries, game server traffic, or VoIP packets are tunneled through the same edge/hub connection and forwarded to SmartProxy with a PROXY v2 binary header preserving the client's real IP.
416
-
417
- ```typescript
418
- await hub.updateAllowedEdges([
419
- {
420
- id: 'edge-nyc-01',
421
- secret: 'secret',
422
- listenPorts: [80, 443], // TCP
423
- listenPortsUdp: [53, 27015], // DNS + game server
424
- },
425
- ]);
426
- ```
427
-
428
- ### 4. 🚀 QUIC Transport for Low-Latency
429
-
430
- Use QUIC transport to eliminate head-of-line blocking — a lost packet on one stream doesn't stall others. QUIC also enables 0-RTT reconnection and connection migration.
431
-
432
- ```typescript
433
- await edge.start({
434
- hubHost: 'hub.example.com',
435
- hubPort: 8443,
436
- edgeId: 'edge-01',
437
- secret: 'secret',
438
- transportMode: 'quicWithFallback', // try QUIC, fall back to TLS if UDP blocked
439
- });
440
- ```
441
-
442
- ### 5. 🔑 Token-Based Edge Provisioning
443
-
444
- Generate connection tokens on the hub side and distribute them to edge operators:
445
-
446
- ```typescript
447
- import { encodeConnectionToken, RemoteIngressEdge } from '@serve.zone/remoteingress';
448
-
449
- const token = encodeConnectionToken({
450
- hubHost: 'hub.prod.example.com',
451
- hubPort: 8443,
452
- edgeId: 'edge-tokyo-01',
453
- secret: 'generated-secret-abc123',
454
- });
455
- // Send `token` to the edge operator — a single string is all they need
456
-
457
- const edge = new RemoteIngressEdge();
458
- await edge.start({ token });
459
- ```
460
-
461
- ### 6. 🛡️ Centralized Firewall Management
462
-
463
- Push firewall rules from the hub to all your edge nodes. Block bad actors, rate-limit abusive traffic, and whitelist trusted subnets — all from a single control plane:
464
-
465
- ```typescript
466
- await hub.updateAllowedEdges([
467
- {
468
- id: 'edge-nyc-01',
469
- secret: 'secret',
470
- listenPorts: [80, 443],
471
- firewallConfig: {
472
- blockedIps: ['198.51.100.0/24'],
473
- rateLimits: [
474
- { id: 'https', port: 443, protocol: 'tcp', rate: '500/second', perSourceIP: true, burst: 100 },
475
- ],
476
- rules: [
477
- { id: 'allow-monitoring', direction: 'input', action: 'accept', sourceIP: '10.0.0.0/8', destPort: 9090, protocol: 'tcp' },
478
- ],
479
- },
480
- },
481
- ]);
482
- // Firewall rules are applied at the edge via nftables within seconds
483
- ```
205
+ - `ts/index.ts` exports the public API and CLI runner.
206
+ - `ts/classes.remoteingresshub.ts` wraps hub management commands and hub events.
207
+ - `ts/classes.remoteingressedge.ts` wraps edge management commands, nftables application, and edge events.
208
+ - `ts/classes.token.ts` implements compact connection tokens.
209
+ - `rust/` contains the performance-critical tunnel implementation compiled by `tsrust`.
484
210
 
485
211
  ## License and Legal Information
486
212
 
487
- This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
213
+ This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](./license) file.
488
214
 
489
215
  **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
490
216
 
@@ -3,6 +3,6 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@serve.zone/remoteingress',
6
- version: '4.17.0',
6
+ version: '4.17.2',
7
7
  description: 'Edge ingress tunnel for DcRouter - tunnels TCP and UDP traffic from the network edge to SmartProxy over TLS or QUIC, preserving client IP via PROXY protocol.'
8
8
  }