Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a3af2487b7 | |||
| 51de25d767 | |||
| 7b8c4e1af5 | |||
| 0459cd2af6 | |||
| 6fdc9ea918 | |||
| d869589663 | |||
| 072362a8e6 | |||
| b628a5f964 | |||
| 19e8003c77 | |||
| 93592bf909 | |||
| 73fc4ea28e | |||
| 5321e5f0e0 | |||
| 1f90b91252 | |||
| e25b193f59 | |||
| ad67f2e265 | |||
| ce5074c57d | |||
| f79b5cf541 | |||
| 601a13de1a | |||
| 1d1fbaed80 | |||
| a144f5a798 | |||
| a3970edf23 | |||
| c8fe27143c | |||
| 0fb7788b97 | |||
| 6d14056cfd |
1
.dockerignore
Normal file
1
.dockerignore
Normal file
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -17,4 +17,5 @@ node_modules/
|
||||
dist/
|
||||
dist_*/
|
||||
|
||||
# custom
|
||||
# custom
|
||||
rust/target/
|
||||
46
Dockerfile
Normal file
46
Dockerfile
Normal file
@@ -0,0 +1,46 @@
|
||||
# gitzone dockerfile_service
|
||||
## STAGE 1 // BUILD
|
||||
FROM registry.gitlab.com/hosttoday/ht-docker-node:npmci as node1
|
||||
COPY ./ /app
|
||||
WORKDIR /app
|
||||
ARG NPMCI_TOKEN_NPM2
|
||||
ENV NPMCI_TOKEN_NPM2 $NPMCI_TOKEN_NPM2
|
||||
RUN npmci npm prepare
|
||||
RUN pnpm config set store-dir .pnpm-store
|
||||
RUN rm -rf node_modules && pnpm install
|
||||
RUN pnpm run build
|
||||
|
||||
# gitzone dockerfile_service
|
||||
## STAGE 2 // install production
|
||||
FROM registry.gitlab.com/hosttoday/ht-docker-node:npmci as node2
|
||||
WORKDIR /app
|
||||
COPY --from=node1 /app /app
|
||||
RUN rm -rf .pnpm-store
|
||||
ARG NPMCI_TOKEN_NPM2
|
||||
ENV NPMCI_TOKEN_NPM2 $NPMCI_TOKEN_NPM2
|
||||
RUN npmci npm prepare
|
||||
RUN pnpm config set store-dir .pnpm-store
|
||||
RUN rm -rf node_modules/ && pnpm install --prod
|
||||
|
||||
|
||||
## STAGE 3 // rebuild dependencies for alpine
|
||||
FROM registry.gitlab.com/hosttoday/ht-docker-node:alpinenpmci as node3
|
||||
WORKDIR /app
|
||||
COPY --from=node2 /app /app
|
||||
ARG NPMCI_TOKEN_NPM2
|
||||
ENV NPMCI_TOKEN_NPM2 $NPMCI_TOKEN_NPM2
|
||||
RUN npmci npm prepare
|
||||
RUN pnpm config set store-dir .pnpm-store
|
||||
RUN pnpm rebuild -r
|
||||
|
||||
## STAGE 4 // the final production image with all dependencies in place
|
||||
FROM registry.gitlab.com/hosttoday/ht-docker-node:alpine as node4
|
||||
WORKDIR /app
|
||||
COPY --from=node3 /app /app
|
||||
|
||||
### Healthchecks
|
||||
RUN pnpm install -g @servezone/healthy
|
||||
HEALTHCHECK --interval=30s --timeout=30s --start-period=30s --retries=3 CMD [ "healthy" ]
|
||||
|
||||
EXPOSE 80
|
||||
CMD ["npm", "start"]
|
||||
112
changelog.md
Normal file
112
changelog.md
Normal file
@@ -0,0 +1,112 @@
|
||||
# Changelog
|
||||
|
||||
## 2026-02-18 - 3.3.0 - feat(readme)
|
||||
document dynamic port assignment and runtime port updates; clarify TLS multiplexing, frame format, and handshake sequence
|
||||
|
||||
- Adds documentation for dynamic port configuration: hub-assigned listen ports, hot-reloadable via FRAME_CONFIG frames
|
||||
- Introduces new FRAME type CONFIG (0x06) and describes payload as JSON; notes immediate push of port changes to connected edges
|
||||
- Clarifies that the tunnel is a single encrypted TLS multiplexed connection to the hub (preserves PROXY v1 behavior)
|
||||
- Specifies frame integer fields are big-endian and that stream IDs are 32-bit unsigned integers
|
||||
- Adds new events: portsAssigned and portsUpdated, and updates examples showing updateAllowedEdges usage and live port changes
|
||||
|
||||
## 2026-02-18 - 3.2.1 - fix(tests)
|
||||
add comprehensive unit and async tests across Rust crates and TypeScript runtime
|
||||
|
||||
- Added IPC serialization tests in remoteingress-bin (IPC request/response/event)
|
||||
- Added serde and async tests for Edge and Handshake configs and EdgeEvent/EdgeStatus in remoteingress-core (edge.rs)
|
||||
- Added extensive Hub tests: constant_time_eq, PROXY header port parsing, serde/camelCase checks, Hub events and async TunnelHub behavior (hub.rs)
|
||||
- Added STUN parser unit tests including XOR_MAPPED_ADDRESS, MAPPED_ADDRESS fallback, truncated attribute handling and other edge cases (stun.rs)
|
||||
- Added protocol frame encoding and FrameReader tests covering all frame types, payload limits and EOF conditions (remoteingress-protocol)
|
||||
- Added TypeScript Node tests for token encode/decode edge cases and RemoteIngressHub/RemoteIngressEdge class basics (test/*.node.ts)
|
||||
|
||||
## 2026-02-18 - 3.2.0 - feat(remoteingress (edge/hub/protocol))
|
||||
add dynamic port configuration: handshake, FRAME_CONFIG frames, and hot-reloadable listeners
|
||||
|
||||
- Introduce a JSON handshake from hub -> edge with initial listen ports and stun interval so edges can configure listeners at connect time.
|
||||
- Add FRAME_CONFIG (0x06) to the protocol and implement runtime config updates pushed from hub to connected edges.
|
||||
- Edge now applies initial ports and supports hot-reloading: spawn/abort listeners when ports change, and emit PortsAssigned / PortsUpdated events.
|
||||
- Hub now stores allowed edge metadata (listen_ports, stun_interval_secs), sends handshake responses on auth, and forwards config updates to connected edges.
|
||||
- TypeScript bridge/client updated to emit new port events and periodically log status; updateAllowedEdges API accepts listenPorts and stunIntervalSecs.
|
||||
- Stun interval handling moved to use handshake-provided/stored value instead of config.listen_ports being static.
|
||||
|
||||
## 2026-02-18 - 3.1.1 - fix(readme)
|
||||
update README: add issue reporting/security section, document connection tokens and token utilities, clarify architecture/API and improve examples/formatting
|
||||
|
||||
- Added an 'Issue Reporting and Security' section linking to community.foss.global for bug/security reports and contributor onboarding.
|
||||
- Documented connection tokens: encodeConnectionToken/decodeConnectionToken utilities, token format (base64url), and examples for hub and edge provisioning.
|
||||
- Clarified Hub/Edge usage and examples: condensed event handlers, added token-based start() example, and provided explicit config alternative.
|
||||
- Improved README formatting: added emojis, rephrased architecture descriptions, fixed wording and license path capitalization, and expanded example scenarios and interfaces.
|
||||
|
||||
## 2026-02-17 - 3.1.0 - feat(edge)
|
||||
support connection tokens when starting an edge and add token encode/decode utilities
|
||||
|
||||
- Add classes.token.ts with encodeConnectionToken/decodeConnectionToken using a base64url compact JSON format
|
||||
- Export token utilities from ts/index.ts
|
||||
- RemoteIngressEdge.start now accepts a { token } option and decodes it to an IEdgeConfig before starting
|
||||
- Add tests covering export availability, encode→decode roundtrip, malformed token, and missing fields
|
||||
- Non-breaking change — recommend a minor version bump
|
||||
|
||||
## 2026-02-17 - 3.0.4 - fix(build)
|
||||
bump dev dependencies, update build script, and refresh README docs
|
||||
|
||||
- Bumped devDependencies: @git.zone/tsbuild ^2.1.25 → ^4.1.2, @git.zone/tsbundle ^2.0.5 → ^2.8.3, @git.zone/tsrun ^1.2.46 → ^2.0.1, @git.zone/tstest ^1.0.44 → ^3.1.8, @push.rocks/tapbundle ^5.0.15 → ^6.0.3, @types/node ^20.8.7 → ^25.2.3
|
||||
- Bumped runtime dependency: @push.rocks/qenv ^6.0.5 → ^6.1.3
|
||||
- Changed build script: replaced "tsbuild --web --allowimplicitany" with "tsbuild tsfolders --allowimplicitany" (kept tsrust invocation)
|
||||
- README updates: added RustBridge notes (localPaths must be full file paths), production binary naming conventions, rust core uses ring as rustls provider; removed emoji from example console output; clarified stunIntervalSecs is optional; renamed example status variable to edgeStatus; minor wire-protocol formatting and wording/legal text tweaks
|
||||
|
||||
## 2026-02-17 - 3.0.3 - fix(rust,ts)
|
||||
initialize rustls ring CryptoProvider at startup; add rustls dependency and features; make native binary lookup platform-aware
|
||||
|
||||
- Install rustls::crypto::ring default_provider at startup to ensure ring-based crypto is available before any TLS usage.
|
||||
- Add rustls dependency to remoteingress-bin and update remoteingress-core rustls configuration (disable default-features; enable ring, logging, std, tls12).
|
||||
- Adjust TS classes to prefer platform-suffixed production binaries, add exact fallback names, and include explicit cargo output paths for release/debug.
|
||||
- Cargo.lock updated to include rustls entry.
|
||||
|
||||
## 2026-02-16 - 3.0.2 - fix(readme)
|
||||
Document Hub/Edge architecture and new RemoteIngressHub/RemoteIngressEdge API; add Rust core binary, protocol and usage details; note removal of ConnectorPublic/ConnectorPrivate (breaking change)
|
||||
|
||||
- Adds comprehensive README describing v3 Hub↔Edge topology and usage examples
|
||||
- Introduces Rust core binary (remoteingress-bin) and RustBridge IPC via @push.rocks/smartrust
|
||||
- Documents custom multiplexed binary frame protocol over TLS and PROXY protocol v1 for client IP preservation
|
||||
- Notes STUN-based public IP discovery and cross-compiled linux/amd64 and linux/arm64 binaries
|
||||
- Calls out removal/rename of ConnectorPublic/ConnectorPrivate to RemoteIngressHub/RemoteIngressEdge (breaking API change)
|
||||
- Updates install instruction to use pnpm and expands API reference, events, and examples
|
||||
|
||||
## 2026-02-16 - 3.0.1 - fix(remoteingress)
|
||||
no changes detected in diff; no code modifications to release
|
||||
|
||||
- No files changed in the provided diff.
|
||||
- No release or version bump required based on current changes.
|
||||
|
||||
## 2026-02-16 - 3.0.0 - BREAKING CHANGE(remoteingress)
|
||||
migrate core to Rust, add RemoteIngressHub/RemoteIngressEdge JS bridge, and bump package to v2.0.0
|
||||
|
||||
- Added Rust workspace and crates: remoteingress-protocol, remoteingress-core, remoteingress-bin (IPC management mode via JSON over stdin/stdout).
|
||||
- Implemented protocol framing, PROXY v1 header builder, and async FrameReader in remoteingress-protocol.
|
||||
- Implemented hub and edge tunnel logic in Rust including TLS handling, PROXY parsing, and STUN public IP discovery.
|
||||
- Added TypeScript runtime bridge classes RemoteIngressHub and RemoteIngressEdge that use @push.rocks/smartrust to spawn/manage the Rust binary.
|
||||
- Removed legacy connector public/private TS files and simplified ts/index exports to expose hub/edge classes.
|
||||
- Updated package.json: bumped version to 2.0.0, adjusted description, added tsrust build step, new dependency @push.rocks/smartrust and keywords, and included dist_rust in files/glob.
|
||||
- Added rust build config for cross-target linkers and new Cargo.toml manifests for the workspace.
|
||||
|
||||
## 2024-04-14 - 1.0.2 - 1.0.4 - releases
|
||||
Version-only tag commits (no code changes) for recent releases.
|
||||
|
||||
- 1.0.2 (2024-03-24) — release tag / version bump only
|
||||
- 1.0.3 (2024-04-14) — release tag / version bump only
|
||||
- 1.0.4 (2024-04-14) — release tag / version bump only
|
||||
|
||||
## 2024-04-14 - 1.0.3 - core
|
||||
Core updates and fixes.
|
||||
|
||||
- fix(core): update
|
||||
|
||||
## 2024-04-14 - 1.0.2 - core
|
||||
Core updates and fixes.
|
||||
|
||||
- fix(core): update
|
||||
|
||||
## 2024-03-24 - 1.0.1 - core
|
||||
Core updates and fixes.
|
||||
|
||||
- fix(core): update
|
||||
4
cli.child.ts
Normal file
4
cli.child.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
process.env.CLI_CALL = 'true';
|
||||
import * as cliTool from './ts/index.js';
|
||||
cliTool.runCli();
|
||||
4
cli.js
Normal file
4
cli.js
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
process.env.CLI_CALL = 'true';
|
||||
const cliTool = await import('./dist_ts/index.js');
|
||||
cliTool.runCli();
|
||||
5
cli.ts.js
Normal file
5
cli.ts.js
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
process.env.CLI_CALL = 'true';
|
||||
|
||||
import * as tsrun from '@git.zone/tsrun';
|
||||
tsrun.runPath('./cli.child.js', import.meta.url);
|
||||
@@ -1,18 +1,50 @@
|
||||
{
|
||||
"gitzone": {
|
||||
"@git.zone/tsrust": {
|
||||
"targets": [
|
||||
"linux_amd64",
|
||||
"linux_arm64"
|
||||
]
|
||||
},
|
||||
"@git.zone/cli": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "serve.zone",
|
||||
"gitrepo": "remoteingress",
|
||||
"description": "a remoteingress service for serve.zone",
|
||||
"description": "Provides a service for creating private tunnels and reaching private clusters from the outside, facilitating secure remote access as part of the @serve.zone stack.",
|
||||
"npmPackagename": "@serve.zone/remoteingress",
|
||||
"license": "MIT",
|
||||
"projectDomain": "serve.zone"
|
||||
"projectDomain": "serve.zone",
|
||||
"keywords": [
|
||||
"remote access",
|
||||
"private tunnels",
|
||||
"network security",
|
||||
"TLS encryption",
|
||||
"connector",
|
||||
"serve.zone stack",
|
||||
"private clusters access",
|
||||
"public access management",
|
||||
"TypeScript application",
|
||||
"node.js package",
|
||||
"secure communications",
|
||||
"TLS/SSL certificates",
|
||||
"development tools",
|
||||
"software development",
|
||||
"private network integration"
|
||||
]
|
||||
},
|
||||
"release": {
|
||||
"registries": [
|
||||
"https://verdaccio.lossless.digital",
|
||||
"https://registry.npmjs.org"
|
||||
],
|
||||
"accessLevel": "public"
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
"@git.zone/tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**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.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
},
|
||||
"@ship.zone/szci": {
|
||||
"npmGlobalTools": []
|
||||
}
|
||||
}
|
||||
40
package.json
40
package.json
@@ -1,27 +1,31 @@
|
||||
{
|
||||
"name": "@serve.zone/remoteingress",
|
||||
"version": "1.0.2",
|
||||
"version": "3.3.0",
|
||||
"private": false,
|
||||
"description": "a remoteingress service for serve.zone",
|
||||
"description": "Edge ingress tunnel for DcRouter - accepts incoming TCP connections at network edge and tunnels them to DcRouter SmartProxy preserving client IP via PROXY protocol v1.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"author": "Task Venture Capital GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web --allowimplicitany)",
|
||||
"test": "(tstest test/ --verbose --logfile --timeout 60)",
|
||||
"build": "(tsbuild tsfolders --allowimplicitany && tsrust)",
|
||||
"buildDocs": "(tsdoc)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.1.25",
|
||||
"@git.zone/tsbundle": "^2.0.5",
|
||||
"@git.zone/tsrun": "^1.2.46",
|
||||
"@git.zone/tstest": "^1.0.44",
|
||||
"@push.rocks/tapbundle": "^5.0.15",
|
||||
"@types/node": "^20.8.7"
|
||||
"@git.zone/tsbuild": "^4.1.2",
|
||||
"@git.zone/tsbundle": "^2.8.3",
|
||||
"@git.zone/tsrun": "^2.0.1",
|
||||
"@git.zone/tsrust": "^1.3.0",
|
||||
"@git.zone/tstest": "^3.1.8",
|
||||
"@push.rocks/tapbundle": "^6.0.3",
|
||||
"@types/node": "^25.2.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@push.rocks/qenv": "^6.1.3",
|
||||
"@push.rocks/smartrust": "^1.2.1"
|
||||
},
|
||||
"dependencies": {},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://code.foss.global/serve.zone/remoteingress.git"
|
||||
@@ -40,9 +44,23 @@
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"dist_rust/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"keywords": [
|
||||
"remote access",
|
||||
"ingress tunnel",
|
||||
"network edge",
|
||||
"PROXY protocol",
|
||||
"multiplexed tunnel",
|
||||
"TCP proxy",
|
||||
"TLS tunnel",
|
||||
"serve.zone stack",
|
||||
"TypeScript",
|
||||
"Rust",
|
||||
"SmartProxy"
|
||||
]
|
||||
}
|
||||
|
||||
11979
pnpm-lock.yaml
generated
11979
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
12
readme.hints.md
Normal file
12
readme.hints.md
Normal file
@@ -0,0 +1,12 @@
|
||||
* This module is part of the @serve.zone stack
|
||||
* v3.0.0+ uses a Hub/Edge architecture with a Rust core binary (`remoteingress-bin`)
|
||||
* TypeScript classes `RemoteIngressHub` and `RemoteIngressEdge` bridge to Rust via `@push.rocks/smartrust` RustBridge IPC
|
||||
* Custom multiplexed binary frame protocol over TLS for tunneling TCP connections
|
||||
* PROXY protocol v1 preserves client IP when forwarding to SmartProxy/DcRouter
|
||||
* Edge authenticates to Hub via shared secret over TLS
|
||||
* STUN-based public IP discovery at the edge (Cloudflare STUN server)
|
||||
* Cross-compiled Rust binary for linux/amd64 and linux/arm64
|
||||
* Old `ConnectorPublic`/`ConnectorPrivate` classes no longer exist (removed in v2.0.0/v3.0.0)
|
||||
* `localPaths` in RustBridge config must be full file paths (not directories) — smartrust's `RustBinaryLocator` checks `isExecutable()` on each entry directly
|
||||
* Production binaries are named with platform suffixes: `remoteingress-bin_linux_amd64`, `remoteingress-bin_linux_arm64`
|
||||
* Rust core uses `ring` as the rustls CryptoProvider (explicitly installed in main.rs, aws-lc-rs disabled via default-features=false)
|
||||
382
readme.md
382
readme.md
@@ -1,31 +1,363 @@
|
||||
# @serve.zone/remoteingress
|
||||
a remoteingress service for serve.zone
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@serve.zone/remoteingress)
|
||||
* [gitlab.com (source)](https://code.foss.global/serve.zone/remoteingress)
|
||||
* [github.com (source mirror)](https://github.com/serve.zone/remoteingress)
|
||||
* [docs (typedoc)](https://serve.zone.gitlab.io/remoteingress/)
|
||||
Edge ingress tunnel for DcRouter — accepts incoming TCP connections at the network edge and tunnels them over a single encrypted TLS connection to a DcRouter SmartProxy instance, preserving the original client IP via PROXY protocol v1.
|
||||
|
||||
## Status for master
|
||||
## Issue Reporting and Security
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
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.
|
||||
|
||||
## Usage
|
||||
Use TypeScript for best in class intellisense
|
||||
For further information read the linked docs at the top of this readme.
|
||||
## Install
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
```sh
|
||||
pnpm install @serve.zone/remoteingress
|
||||
```
|
||||
|
||||
## 🏗️ Architecture
|
||||
|
||||
`@serve.zone/remoteingress` uses a **Hub/Edge** topology with a high-performance Rust core and a TypeScript API surface:
|
||||
|
||||
```
|
||||
┌─────────────────────┐ TLS Tunnel ┌─────────────────────┐
|
||||
│ Network Edge │ ◄══════════════════════════► │ Private Cluster │
|
||||
│ │ (multiplexed frames + │ │
|
||||
│ RemoteIngressEdge │ shared-secret auth) │ RemoteIngressHub │
|
||||
│ Accepts client TCP │ │ Forwards to │
|
||||
│ connections on │ │ SmartProxy on │
|
||||
│ hub-assigned ports │ │ local ports │
|
||||
└─────────────────────┘ └─────────────────────┘
|
||||
▲ │
|
||||
│ TCP from end users ▼
|
||||
Internet DcRouter / SmartProxy
|
||||
```
|
||||
|
||||
| Component | Role |
|
||||
|-----------|------|
|
||||
| **RemoteIngressEdge** | Deployed at the network edge (e.g. a VPS or cloud instance). Listens on ports assigned by the hub, accepts raw TCP connections, and multiplexes them over a single TLS tunnel to the hub. Ports are hot-reloadable — the hub can change them at runtime. |
|
||||
| **RemoteIngressHub** | Deployed alongside DcRouter/SmartProxy in a private cluster. Accepts edge connections, demuxes streams, and forwards each to SmartProxy with a PROXY protocol v1 header so the real client IP is preserved. Controls which ports each edge listens on. |
|
||||
| **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`. |
|
||||
|
||||
### ✨ Key Features
|
||||
|
||||
- 🔒 **TLS-encrypted tunnel** between edge and hub (auto-generated self-signed cert or bring your own)
|
||||
- 🔀 **Multiplexed streams** — thousands of client connections flow over a single tunnel
|
||||
- 🌐 **PROXY protocol v1** — SmartProxy sees the real client IP, not the tunnel IP
|
||||
- 🔑 **Shared-secret authentication** — edges must present valid credentials to connect
|
||||
- 🎫 **Connection tokens** — encode all connection details into a single opaque string
|
||||
- 📡 **STUN-based public IP discovery** — the edge automatically discovers its public IP via Cloudflare STUN
|
||||
- 🔄 **Auto-reconnect** with exponential backoff if the tunnel drops
|
||||
- 🎛️ **Dynamic port configuration** — the hub assigns listen ports per edge and can hot-reload them at runtime via `FRAME_CONFIG` frames
|
||||
- 📣 **Event-driven** — both Hub and Edge extend `EventEmitter` for real-time monitoring
|
||||
- ⚡ **Rust core** — all frame encoding, TLS, and TCP proxying happen in native code for maximum throughput
|
||||
|
||||
## 🚀 Usage
|
||||
|
||||
Both classes are imported from the package and communicate with the Rust binary under the hood. All you need to do is configure and start them.
|
||||
|
||||
### Setting Up the Hub (Private Cluster Side)
|
||||
|
||||
```typescript
|
||||
import { RemoteIngressHub } from '@serve.zone/remoteingress';
|
||||
|
||||
const hub = new RemoteIngressHub();
|
||||
|
||||
// Listen for events
|
||||
hub.on('edgeConnected', ({ edgeId }) => {
|
||||
console.log(`Edge ${edgeId} connected`);
|
||||
});
|
||||
hub.on('edgeDisconnected', ({ edgeId }) => {
|
||||
console.log(`Edge ${edgeId} disconnected`);
|
||||
});
|
||||
hub.on('streamOpened', ({ edgeId, streamId }) => {
|
||||
console.log(`Stream ${streamId} opened from edge ${edgeId}`);
|
||||
});
|
||||
hub.on('streamClosed', ({ edgeId, streamId }) => {
|
||||
console.log(`Stream ${streamId} closed from edge ${edgeId}`);
|
||||
});
|
||||
|
||||
// Start the hub — it will listen for incoming edge TLS connections
|
||||
await hub.start({
|
||||
tunnelPort: 8443, // port edges connect to (default: 8443)
|
||||
targetHost: '127.0.0.1', // SmartProxy host to forward streams to (default: 127.0.0.1)
|
||||
});
|
||||
|
||||
// Register which edges are allowed to connect, including their listen ports
|
||||
await hub.updateAllowedEdges([
|
||||
{
|
||||
id: 'edge-nyc-01',
|
||||
secret: 'supersecrettoken1',
|
||||
listenPorts: [80, 443], // ports the edge should listen on
|
||||
stunIntervalSecs: 300, // STUN discovery interval (default: 300)
|
||||
},
|
||||
{
|
||||
id: 'edge-fra-02',
|
||||
secret: 'supersecrettoken2',
|
||||
listenPorts: [443, 8080],
|
||||
},
|
||||
]);
|
||||
|
||||
// Dynamically update ports for a connected edge — changes are pushed instantly
|
||||
await hub.updateAllowedEdges([
|
||||
{
|
||||
id: 'edge-nyc-01',
|
||||
secret: 'supersecrettoken1',
|
||||
listenPorts: [80, 443, 8443], // added port 8443 — edge picks it up in real time
|
||||
},
|
||||
]);
|
||||
|
||||
// Check status at any time
|
||||
const status = await hub.getStatus();
|
||||
console.log(status);
|
||||
// {
|
||||
// running: true,
|
||||
// tunnelPort: 8443,
|
||||
// connectedEdges: [
|
||||
// { edgeId: 'edge-nyc-01', connectedAt: 1700000000, activeStreams: 12 }
|
||||
// ]
|
||||
// }
|
||||
|
||||
// Graceful shutdown
|
||||
await hub.stop();
|
||||
```
|
||||
|
||||
### Setting Up the Edge (Network Edge Side)
|
||||
|
||||
The edge can be configured in two ways: with an **opaque connection token** (recommended) or with explicit config fields.
|
||||
|
||||
#### Option A: Connection Token (Recommended)
|
||||
|
||||
A single token encodes all connection details — ideal for provisioning edges at scale:
|
||||
|
||||
```typescript
|
||||
import { RemoteIngressEdge } from '@serve.zone/remoteingress';
|
||||
|
||||
const edge = new RemoteIngressEdge();
|
||||
|
||||
edge.on('tunnelConnected', () => console.log('Tunnel established'));
|
||||
edge.on('tunnelDisconnected', () => console.log('Tunnel lost — will auto-reconnect'));
|
||||
edge.on('publicIpDiscovered', ({ ip }) => console.log(`Public IP: ${ip}`));
|
||||
edge.on('portsAssigned', ({ listenPorts }) => console.log(`Listening on ports: ${listenPorts}`));
|
||||
edge.on('portsUpdated', ({ listenPorts }) => console.log(`Ports updated: ${listenPorts}`));
|
||||
|
||||
// Single token contains hubHost, hubPort, edgeId, and secret
|
||||
await edge.start({
|
||||
token: 'eyJoIjoiaHViLmV4YW1wbGUuY29tIiwicCI6ODQ0MywiZSI6ImVkZ2UtbnljLTAxIiwicyI6InN1cGVyc2VjcmV0dG9rZW4xIn0',
|
||||
});
|
||||
```
|
||||
|
||||
#### Option B: Explicit Config
|
||||
|
||||
```typescript
|
||||
import { RemoteIngressEdge } from '@serve.zone/remoteingress';
|
||||
|
||||
const edge = new RemoteIngressEdge();
|
||||
|
||||
edge.on('tunnelConnected', () => console.log('Tunnel established'));
|
||||
edge.on('tunnelDisconnected', () => console.log('Tunnel lost — will auto-reconnect'));
|
||||
edge.on('publicIpDiscovered', ({ ip }) => console.log(`Public IP: ${ip}`));
|
||||
edge.on('portsAssigned', ({ listenPorts }) => console.log(`Listening on ports: ${listenPorts}`));
|
||||
edge.on('portsUpdated', ({ listenPorts }) => console.log(`Ports updated: ${listenPorts}`));
|
||||
|
||||
await edge.start({
|
||||
hubHost: 'hub.example.com', // hostname or IP of the hub
|
||||
hubPort: 8443, // must match hub's tunnelPort (default: 8443)
|
||||
edgeId: 'edge-nyc-01', // unique edge identifier
|
||||
secret: 'supersecrettoken1', // must match the hub's allowed edge secret
|
||||
});
|
||||
|
||||
// Check status at any time
|
||||
const edgeStatus = await edge.getStatus();
|
||||
console.log(edgeStatus);
|
||||
// {
|
||||
// running: true,
|
||||
// connected: true,
|
||||
// publicIp: '203.0.113.42',
|
||||
// activeStreams: 5,
|
||||
// listenPorts: [80, 443]
|
||||
// }
|
||||
|
||||
// Graceful shutdown
|
||||
await edge.stop();
|
||||
```
|
||||
|
||||
### 🎫 Connection Tokens
|
||||
|
||||
Connection tokens let you distribute a single opaque string instead of four separate config values. The hub operator generates the token; the edge operator just pastes it in.
|
||||
|
||||
```typescript
|
||||
import { encodeConnectionToken, decodeConnectionToken } from '@serve.zone/remoteingress';
|
||||
|
||||
// Hub side: generate a token for a new edge
|
||||
const token = encodeConnectionToken({
|
||||
hubHost: 'hub.example.com',
|
||||
hubPort: 8443,
|
||||
edgeId: 'edge-nyc-01',
|
||||
secret: 'supersecrettoken1',
|
||||
});
|
||||
console.log(token);
|
||||
// => 'eyJoIjoiaHViLmV4YW1wbGUuY29tIiwi...'
|
||||
|
||||
// Edge side: inspect a token (optional — start() does this automatically)
|
||||
const data = decodeConnectionToken(token);
|
||||
console.log(data);
|
||||
// {
|
||||
// hubHost: 'hub.example.com',
|
||||
// hubPort: 8443,
|
||||
// edgeId: 'edge-nyc-01',
|
||||
// secret: 'supersecrettoken1'
|
||||
// }
|
||||
```
|
||||
|
||||
Tokens are base64url-encoded (URL-safe, no padding) — safe to pass as environment variables, CLI arguments, or store in config files.
|
||||
|
||||
## 📖 API Reference
|
||||
|
||||
### `RemoteIngressHub`
|
||||
|
||||
| Method / Property | Description |
|
||||
|-------------------|-------------|
|
||||
| `start(config?)` | Spawns the Rust binary and starts the tunnel listener. Config: `{ tunnelPort?: number, targetHost?: string }` |
|
||||
| `stop()` | Gracefully shuts down the hub and kills the Rust process. |
|
||||
| `updateAllowedEdges(edges)` | Dynamically update which edges are authorized and what ports they listen on. Each edge: `{ id: string, secret: string, listenPorts?: number[], stunIntervalSecs?: number }`. If ports change for a connected edge, the update is pushed immediately via a `FRAME_CONFIG` frame. |
|
||||
| `getStatus()` | Returns current hub status including connected edges and active stream counts. |
|
||||
| `running` | `boolean` — whether the Rust binary is alive. |
|
||||
|
||||
**Events:** `edgeConnected`, `edgeDisconnected`, `streamOpened`, `streamClosed`
|
||||
|
||||
### `RemoteIngressEdge`
|
||||
|
||||
| Method / Property | Description |
|
||||
|-------------------|-------------|
|
||||
| `start(config)` | Spawns the Rust binary and connects to the hub. Accepts `{ token: string }` or `IEdgeConfig`. Listen ports are received from the hub during handshake. |
|
||||
| `stop()` | Gracefully shuts down the edge and kills the Rust process. |
|
||||
| `getStatus()` | Returns current edge status including connection state, public IP, listen ports, and active streams. |
|
||||
| `running` | `boolean` — whether the Rust binary is alive. |
|
||||
|
||||
**Events:** `tunnelConnected`, `tunnelDisconnected`, `publicIpDiscovered`, `portsAssigned`, `portsUpdated`
|
||||
|
||||
### Token Utilities
|
||||
|
||||
| Function | Description |
|
||||
|----------|-------------|
|
||||
| `encodeConnectionToken(data)` | Encodes `IConnectionTokenData` into a base64url token string. |
|
||||
| `decodeConnectionToken(token)` | Decodes a token back into `IConnectionTokenData`. Throws on malformed or incomplete tokens. |
|
||||
|
||||
### Interfaces
|
||||
|
||||
```typescript
|
||||
interface IHubConfig {
|
||||
tunnelPort?: number; // default: 8443
|
||||
targetHost?: string; // default: '127.0.0.1'
|
||||
}
|
||||
|
||||
interface IEdgeConfig {
|
||||
hubHost: string;
|
||||
hubPort?: number; // default: 8443
|
||||
edgeId: string;
|
||||
secret: string;
|
||||
}
|
||||
|
||||
interface IConnectionTokenData {
|
||||
hubHost: string;
|
||||
hubPort: number;
|
||||
edgeId: string;
|
||||
secret: string;
|
||||
}
|
||||
```
|
||||
|
||||
## 🔌 Wire Protocol
|
||||
|
||||
The tunnel uses a custom binary frame protocol over TLS:
|
||||
|
||||
```
|
||||
[stream_id: 4 bytes BE][type: 1 byte][length: 4 bytes BE][payload: N bytes]
|
||||
```
|
||||
|
||||
| Frame Type | Value | Direction | Purpose |
|
||||
|------------|-------|-----------|---------|
|
||||
| `OPEN` | `0x01` | Edge → Hub | Open a new stream; payload is PROXY v1 header |
|
||||
| `DATA` | `0x02` | Edge → Hub | Client data flowing upstream |
|
||||
| `CLOSE` | `0x03` | Edge → Hub | Client closed the connection |
|
||||
| `DATA_BACK` | `0x04` | Hub → Edge | Response data flowing downstream |
|
||||
| `CLOSE_BACK` | `0x05` | Hub → Edge | Upstream (SmartProxy) closed the connection |
|
||||
| `CONFIG` | `0x06` | Hub → Edge | Runtime configuration update (e.g. port changes); payload is JSON |
|
||||
|
||||
Max payload size per frame: **16 MB**. Stream IDs are 32-bit unsigned integers.
|
||||
|
||||
### Handshake Sequence
|
||||
|
||||
1. Edge opens a TLS connection to the hub
|
||||
2. Edge sends: `EDGE <edgeId> <secret>\n`
|
||||
3. Hub verifies credentials (constant-time comparison) and responds with JSON: `{"listenPorts":[...],"stunIntervalSecs":300}\n`
|
||||
4. Edge starts TCP listeners on the assigned ports
|
||||
5. Frame protocol begins — `OPEN`/`DATA`/`CLOSE` frames flow in both directions
|
||||
6. Hub can push `CONFIG` frames at any time to update the edge's listen ports
|
||||
|
||||
## 💡 Example Scenarios
|
||||
|
||||
### 1. Expose a Private Kubernetes Cluster to the Internet
|
||||
|
||||
Deploy an Edge on a public VPS, point your DNS to the VPS IP. The Edge tunnels all traffic to the Hub running inside the cluster, which hands it off to SmartProxy/DcRouter. Your cluster stays fully private — no public-facing ports needed.
|
||||
|
||||
### 2. Multi-Region Edge Ingress
|
||||
|
||||
Run multiple Edges in different geographic regions (NYC, Frankfurt, Tokyo) all connecting to a single Hub. Use GeoDNS to route users to their nearest Edge. The Hub sees the real client IPs via PROXY protocol regardless of which edge they connected through.
|
||||
|
||||
### 3. Secure API Exposure
|
||||
|
||||
Your backend runs on a private network with no direct internet access. An Edge on a minimal cloud instance acts as the only public entry point. TLS tunnel + shared-secret auth ensure only your authorized Edge can forward traffic.
|
||||
|
||||
### 4. Token-Based Edge Provisioning
|
||||
|
||||
Generate connection tokens on the hub side and distribute them to edge operators. Each edge only needs a single token string to connect — no manual configuration of host, port, ID, and secret.
|
||||
|
||||
```typescript
|
||||
// Hub operator generates token
|
||||
const token = encodeConnectionToken({
|
||||
hubHost: 'hub.prod.example.com',
|
||||
hubPort: 8443,
|
||||
edgeId: 'edge-tokyo-01',
|
||||
secret: 'generated-secret-abc123',
|
||||
});
|
||||
// Send `token` to the edge operator via secure channel
|
||||
|
||||
// Edge operator starts with just the token
|
||||
const edge = new RemoteIngressEdge();
|
||||
await edge.start({ token });
|
||||
```
|
||||
|
||||
### 5. Dynamic Port Management
|
||||
|
||||
The hub controls which ports each edge listens on. Ports can be changed at runtime without restarting the edge — the hub pushes a `CONFIG` frame and the edge hot-reloads its TCP listeners.
|
||||
|
||||
```typescript
|
||||
// Initially assign ports 80 and 443
|
||||
await hub.updateAllowedEdges([
|
||||
{ id: 'edge-nyc-01', secret: 'secret', listenPorts: [80, 443] },
|
||||
]);
|
||||
|
||||
// Later, add port 8080 — the connected edge picks it up instantly
|
||||
await hub.updateAllowedEdges([
|
||||
{ id: 'edge-nyc-01', secret: 'secret', listenPorts: [80, 443, 8080] },
|
||||
]);
|
||||
```
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
||||
|
||||
**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.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
||||
|
||||
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
|
||||
5
rust/.cargo/config.toml
Normal file
5
rust/.cargo/config.toml
Normal file
@@ -0,0 +1,5 @@
|
||||
[target.x86_64-unknown-linux-gnu]
|
||||
linker = "x86_64-linux-gnu-gcc"
|
||||
|
||||
[target.aarch64-unknown-linux-gnu]
|
||||
linker = "aarch64-linux-gnu-gcc"
|
||||
981
rust/Cargo.lock
generated
Normal file
981
rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,981 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aws-lc-rs"
|
||||
version = "1.15.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b7b6141e96a8c160799cc2d5adecd5cbbe5054cb8c7c4af53da0f83bb7ad256"
|
||||
dependencies = [
|
||||
"aws-lc-sys",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aws-lc-sys"
|
||||
version = "0.37.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b092fe214090261288111db7a2b2c2118e5a7f30dc2569f1732c4069a6840549"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cmake",
|
||||
"dunce",
|
||||
"fs_extra",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.22.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.56"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"jobserver",
|
||||
"libc",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "63be97961acde393029492ce0be7a1af7e323e6bae9511ebfac33751be5e6806"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f13174bda5dfd69d7e947827e5af4b0f2f94a4a3ee92912fba07a66150f21e2"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.5.55"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3a822ea5bc7590f9d40f1ba12c0dc3c2760f3482c6984db1573ad11031420831"
|
||||
|
||||
[[package]]
|
||||
name = "cmake"
|
||||
version = "0.1.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75443c44cd6b379beb8c5b45d85d0773baf31cce901fe7bb252f4eff3008ef7d"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
||||
|
||||
[[package]]
|
||||
name = "deranged"
|
||||
version = "0.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc3dc5ad92c2e2d1c193bbbbdf2ea477cb81331de4f3103f267ca18368b988c4"
|
||||
dependencies = [
|
||||
"powerfmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dunce"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813"
|
||||
|
||||
[[package]]
|
||||
name = "env_filter"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a1c3cc8e57274ec99de65301228b537f1e4eedc1b8e0f9411c6caac8ae7308f"
|
||||
dependencies = [
|
||||
"log",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.11.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2daee4ea451f429a58296525ddf28b45a3b64f1acf6587e2067437bb11e218d"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"env_filter",
|
||||
"jiff",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
|
||||
|
||||
[[package]]
|
||||
name = "fs_extra"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"r-efi",
|
||||
"wasip2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
|
||||
|
||||
[[package]]
|
||||
name = "jiff"
|
||||
version = "0.2.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c867c356cc096b33f4981825ab281ecba3db0acefe60329f044c1789d94c6543"
|
||||
dependencies = [
|
||||
"jiff-static",
|
||||
"log",
|
||||
"portable-atomic",
|
||||
"portable-atomic-util",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jiff-static"
|
||||
version = "0.2.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7946b4325269738f270bb55b3c19ab5c5040525f83fd625259422a9d25d9be5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jobserver"
|
||||
version = "0.1.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9afb3de4395d6b3e67a780b6de64b51c978ecf11cb9a462c66be7d4ca9039d33"
|
||||
dependencies = [
|
||||
"getrandom 0.3.4",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.182"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
|
||||
dependencies = [
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "1.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"wasi",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-conv"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf97ec579c3c42f953ef76dbf8d55ac91fb219dde70e49aa4a6b7d74e9919050"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pem"
|
||||
version = "3.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d30c53c26bc5b31a98cd02d20f25a7c8567146caf63ed593a9d87b2775291be"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic-util"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a9db96d7fa8782dd8c15ce32ffe8680bbd1e978a43bf51a34d39483540495f5"
|
||||
dependencies = [
|
||||
"portable-atomic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "powerfmt"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "r-efi"
|
||||
version = "5.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||
|
||||
[[package]]
|
||||
name = "rcgen"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75e669e5202259b5314d1ea5397316ad400819437857b90861765f24c4cf80a2"
|
||||
dependencies = [
|
||||
"pem",
|
||||
"ring",
|
||||
"rustls-pki-types",
|
||||
"time",
|
||||
"yasna",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c"
|
||||
|
||||
[[package]]
|
||||
name = "remoteingress-bin"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"env_logger",
|
||||
"log",
|
||||
"remoteingress-core",
|
||||
"remoteingress-protocol",
|
||||
"rustls",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "remoteingress-core"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"log",
|
||||
"rcgen",
|
||||
"remoteingress-protocol",
|
||||
"rustls",
|
||||
"rustls-pemfile",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "remoteingress-protocol"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ring"
|
||||
version = "0.17.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"getrandom 0.2.17",
|
||||
"libc",
|
||||
"untrusted",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.23.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c665f33d38cea657d9614f766881e4d510e0eda4239891eea56b4cadcf01801b"
|
||||
dependencies = [
|
||||
"aws-lc-rs",
|
||||
"log",
|
||||
"once_cell",
|
||||
"ring",
|
||||
"rustls-pki-types",
|
||||
"rustls-webpki",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-pemfile"
|
||||
version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50"
|
||||
dependencies = [
|
||||
"rustls-pki-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-pki-types"
|
||||
version = "1.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be040f8b0a225e40375822a563fa9524378b9d63112f53e19ffff34df5d33fdd"
|
||||
dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-webpki"
|
||||
version = "0.103.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7df23109aa6c1567d1c575b9952556388da57401e4ace1d15f79eedad0d8f53"
|
||||
dependencies = [
|
||||
"aws-lc-rs",
|
||||
"ring",
|
||||
"rustls-pki-types",
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.149"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
|
||||
dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.15.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86f4aa3ad99f2088c990dfa82d367e19cb29268ed67c574d10d0a4bfe71f07e0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.116"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3df424c70518695237746f84cede799c9c58fcb37450d7b23716568cc8bc69cb"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "743bd48c283afc0388f9b8827b976905fb217ad9e647fae3a379a9283c4def2c"
|
||||
dependencies = [
|
||||
"deranged",
|
||||
"num-conv",
|
||||
"powerfmt",
|
||||
"serde_core",
|
||||
"time-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time-core"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7694e1cfe791f8d31026952abf09c69ca6f6fa4e1a1229e18988f06a04a12dca"
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.49.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"libc",
|
||||
"mio",
|
||||
"parking_lot",
|
||||
"pin-project-lite",
|
||||
"signal-hook-registry",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-rustls"
|
||||
version = "0.26.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61"
|
||||
dependencies = [
|
||||
"rustls",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||
|
||||
[[package]]
|
||||
name = "wasip2"
|
||||
version = "1.0.2+wasi-0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5"
|
||||
dependencies = [
|
||||
"wit-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||
dependencies = [
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.60.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb"
|
||||
dependencies = [
|
||||
"windows-targets 0.53.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.52.6",
|
||||
"windows_aarch64_msvc 0.52.6",
|
||||
"windows_i686_gnu 0.52.6",
|
||||
"windows_i686_gnullvm 0.52.6",
|
||||
"windows_i686_msvc 0.52.6",
|
||||
"windows_x86_64_gnu 0.52.6",
|
||||
"windows_x86_64_gnullvm 0.52.6",
|
||||
"windows_x86_64_msvc 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.53.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
"windows_aarch64_gnullvm 0.53.1",
|
||||
"windows_aarch64_msvc 0.53.1",
|
||||
"windows_i686_gnu 0.53.1",
|
||||
"windows_i686_gnullvm 0.53.1",
|
||||
"windows_i686_msvc 0.53.1",
|
||||
"windows_x86_64_gnu 0.53.1",
|
||||
"windows_x86_64_gnullvm 0.53.1",
|
||||
"windows_x86_64_msvc 0.53.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen"
|
||||
version = "0.51.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5"
|
||||
|
||||
[[package]]
|
||||
name = "yasna"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e17bb3549cc1321ae1296b9cdc2698e2b6cb1992adfa19a8c72e5b7a738f44cd"
|
||||
dependencies = [
|
||||
"time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
version = "1.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0"
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
|
||||
7
rust/Cargo.toml
Normal file
7
rust/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"crates/remoteingress-protocol",
|
||||
"crates/remoteingress-core",
|
||||
"crates/remoteingress-bin",
|
||||
]
|
||||
19
rust/crates/remoteingress-bin/Cargo.toml
Normal file
19
rust/crates/remoteingress-bin/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "remoteingress-bin"
|
||||
version = "2.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "remoteingress-bin"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
remoteingress-core = { path = "../remoteingress-core" }
|
||||
remoteingress-protocol = { path = "../remoteingress-protocol" }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
log = "0.4"
|
||||
env_logger = "0.11"
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring"] }
|
||||
426
rust/crates/remoteingress-bin/src/main.rs
Normal file
426
rust/crates/remoteingress-bin/src/main.rs
Normal file
@@ -0,0 +1,426 @@
|
||||
use clap::Parser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncBufReadExt, BufReader};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use remoteingress_core::hub::{AllowedEdge, HubConfig, HubEvent, TunnelHub};
|
||||
use remoteingress_core::edge::{EdgeConfig, EdgeEvent, TunnelEdge};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "remoteingress-bin", version = "2.0.0")]
|
||||
struct Cli {
|
||||
/// Run in IPC management mode (JSON over stdin/stdout)
|
||||
#[arg(long)]
|
||||
management: bool,
|
||||
}
|
||||
|
||||
// IPC message types
|
||||
#[derive(Deserialize)]
|
||||
struct IpcRequest {
|
||||
id: String,
|
||||
method: String,
|
||||
params: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct IpcResponse {
|
||||
id: String,
|
||||
success: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
result: Option<serde_json::Value>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct IpcEvent {
|
||||
event: String,
|
||||
data: serde_json::Value,
|
||||
}
|
||||
|
||||
fn send_ipc_line(line: &str) {
|
||||
// Write to stdout synchronously, since we're line-buffered
|
||||
use std::io::Write;
|
||||
let stdout = std::io::stdout();
|
||||
let mut out = stdout.lock();
|
||||
let _ = out.write_all(line.as_bytes());
|
||||
let _ = out.write_all(b"\n");
|
||||
let _ = out.flush();
|
||||
}
|
||||
|
||||
fn send_event(event: &str, data: serde_json::Value) {
|
||||
let evt = IpcEvent {
|
||||
event: event.to_string(),
|
||||
data,
|
||||
};
|
||||
if let Ok(json) = serde_json::to_string(&evt) {
|
||||
send_ipc_line(&json);
|
||||
}
|
||||
}
|
||||
|
||||
fn send_response(id: &str, result: serde_json::Value) {
|
||||
let resp = IpcResponse {
|
||||
id: id.to_string(),
|
||||
success: true,
|
||||
result: Some(result),
|
||||
error: None,
|
||||
};
|
||||
if let Ok(json) = serde_json::to_string(&resp) {
|
||||
send_ipc_line(&json);
|
||||
}
|
||||
}
|
||||
|
||||
fn send_error(id: &str, error: &str) {
|
||||
let resp = IpcResponse {
|
||||
id: id.to_string(),
|
||||
success: false,
|
||||
result: None,
|
||||
error: Some(error.to_string()),
|
||||
};
|
||||
if let Ok(json) = serde_json::to_string(&resp) {
|
||||
send_ipc_line(&json);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Install the ring CryptoProvider before any TLS usage
|
||||
rustls::crypto::ring::default_provider()
|
||||
.install_default()
|
||||
.expect("Failed to install rustls ring CryptoProvider");
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
||||
if !cli.management {
|
||||
eprintln!("remoteingress-bin: use --management for IPC mode");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
// Initialize logging to stderr (stdout is for IPC)
|
||||
env_logger::Builder::from_default_env()
|
||||
.target(env_logger::Target::Stderr)
|
||||
.filter_level(log::LevelFilter::Info)
|
||||
.init();
|
||||
|
||||
// Send ready event
|
||||
send_event("ready", serde_json::json!({ "version": "2.0.0" }));
|
||||
|
||||
// State
|
||||
let hub: Arc<Mutex<Option<Arc<TunnelHub>>>> = Arc::new(Mutex::new(None));
|
||||
let edge: Arc<Mutex<Option<Arc<TunnelEdge>>>> = Arc::new(Mutex::new(None));
|
||||
|
||||
// Read commands from stdin
|
||||
let stdin = tokio::io::stdin();
|
||||
let reader = BufReader::new(stdin);
|
||||
let mut lines = reader.lines();
|
||||
|
||||
while let Ok(Some(line)) = lines.next_line().await {
|
||||
let line = line.trim().to_string();
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let request: IpcRequest = match serde_json::from_str(&line) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
log::error!("Invalid IPC request: {}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let hub = hub.clone();
|
||||
let edge = edge.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
handle_request(request, hub, edge).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_request(
|
||||
req: IpcRequest,
|
||||
hub: Arc<Mutex<Option<Arc<TunnelHub>>>>,
|
||||
edge: Arc<Mutex<Option<Arc<TunnelEdge>>>>,
|
||||
) {
|
||||
match req.method.as_str() {
|
||||
"ping" => {
|
||||
send_response(&req.id, serde_json::json!({ "pong": true }));
|
||||
}
|
||||
|
||||
"startHub" => {
|
||||
let config: HubConfig = match serde_json::from_value(req.params.clone()) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
send_error(&req.id, &format!("invalid hub config: {}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let tunnel_hub = Arc::new(TunnelHub::new(config));
|
||||
|
||||
// Forward hub events to IPC
|
||||
if let Some(mut event_rx) = tunnel_hub.take_event_rx().await {
|
||||
tokio::spawn(async move {
|
||||
while let Some(event) = event_rx.recv().await {
|
||||
match &event {
|
||||
HubEvent::EdgeConnected { edge_id } => {
|
||||
send_event(
|
||||
"edgeConnected",
|
||||
serde_json::json!({ "edgeId": edge_id }),
|
||||
);
|
||||
}
|
||||
HubEvent::EdgeDisconnected { edge_id } => {
|
||||
send_event(
|
||||
"edgeDisconnected",
|
||||
serde_json::json!({ "edgeId": edge_id }),
|
||||
);
|
||||
}
|
||||
HubEvent::StreamOpened {
|
||||
edge_id,
|
||||
stream_id,
|
||||
} => {
|
||||
send_event(
|
||||
"streamOpened",
|
||||
serde_json::json!({
|
||||
"edgeId": edge_id,
|
||||
"streamId": stream_id,
|
||||
}),
|
||||
);
|
||||
}
|
||||
HubEvent::StreamClosed {
|
||||
edge_id,
|
||||
stream_id,
|
||||
} => {
|
||||
send_event(
|
||||
"streamClosed",
|
||||
serde_json::json!({
|
||||
"edgeId": edge_id,
|
||||
"streamId": stream_id,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
match tunnel_hub.start().await {
|
||||
Ok(()) => {
|
||||
*hub.lock().await = Some(tunnel_hub);
|
||||
send_response(&req.id, serde_json::json!({ "started": true }));
|
||||
}
|
||||
Err(e) => {
|
||||
send_error(&req.id, &format!("failed to start hub: {}", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"stopHub" => {
|
||||
let mut h = hub.lock().await;
|
||||
if let Some(hub_instance) = h.take() {
|
||||
hub_instance.stop().await;
|
||||
send_response(&req.id, serde_json::json!({ "stopped": true }));
|
||||
} else {
|
||||
send_response(
|
||||
&req.id,
|
||||
serde_json::json!({ "stopped": true, "wasRunning": false }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
"updateAllowedEdges" => {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct UpdateEdgesParams {
|
||||
edges: Vec<AllowedEdge>,
|
||||
}
|
||||
|
||||
let params: UpdateEdgesParams = match serde_json::from_value(req.params.clone()) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
send_error(&req.id, &format!("invalid params: {}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let h = hub.lock().await;
|
||||
if let Some(hub_instance) = h.as_ref() {
|
||||
hub_instance.update_allowed_edges(params.edges).await;
|
||||
send_response(&req.id, serde_json::json!({ "updated": true }));
|
||||
} else {
|
||||
send_error(&req.id, "hub not running");
|
||||
}
|
||||
}
|
||||
|
||||
"getHubStatus" => {
|
||||
let h = hub.lock().await;
|
||||
if let Some(hub_instance) = h.as_ref() {
|
||||
let status = hub_instance.get_status().await;
|
||||
send_response(
|
||||
&req.id,
|
||||
serde_json::to_value(&status).unwrap_or_default(),
|
||||
);
|
||||
} else {
|
||||
send_response(
|
||||
&req.id,
|
||||
serde_json::json!({
|
||||
"running": false,
|
||||
"tunnelPort": 0,
|
||||
"connectedEdges": []
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
"startEdge" => {
|
||||
let config: EdgeConfig = match serde_json::from_value(req.params.clone()) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
send_error(&req.id, &format!("invalid edge config: {}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let tunnel_edge = Arc::new(TunnelEdge::new(config));
|
||||
|
||||
// Forward edge events to IPC
|
||||
if let Some(mut event_rx) = tunnel_edge.take_event_rx().await {
|
||||
tokio::spawn(async move {
|
||||
while let Some(event) = event_rx.recv().await {
|
||||
match &event {
|
||||
EdgeEvent::TunnelConnected => {
|
||||
send_event("tunnelConnected", serde_json::json!({}));
|
||||
}
|
||||
EdgeEvent::TunnelDisconnected => {
|
||||
send_event("tunnelDisconnected", serde_json::json!({}));
|
||||
}
|
||||
EdgeEvent::PublicIpDiscovered { ip } => {
|
||||
send_event(
|
||||
"publicIpDiscovered",
|
||||
serde_json::json!({ "ip": ip }),
|
||||
);
|
||||
}
|
||||
EdgeEvent::PortsAssigned { listen_ports } => {
|
||||
send_event(
|
||||
"portsAssigned",
|
||||
serde_json::json!({ "listenPorts": listen_ports }),
|
||||
);
|
||||
}
|
||||
EdgeEvent::PortsUpdated { listen_ports } => {
|
||||
send_event(
|
||||
"portsUpdated",
|
||||
serde_json::json!({ "listenPorts": listen_ports }),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
match tunnel_edge.start().await {
|
||||
Ok(()) => {
|
||||
*edge.lock().await = Some(tunnel_edge);
|
||||
send_response(&req.id, serde_json::json!({ "started": true }));
|
||||
}
|
||||
Err(e) => {
|
||||
send_error(&req.id, &format!("failed to start edge: {}", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"stopEdge" => {
|
||||
let mut e = edge.lock().await;
|
||||
if let Some(edge_instance) = e.take() {
|
||||
edge_instance.stop().await;
|
||||
send_response(&req.id, serde_json::json!({ "stopped": true }));
|
||||
} else {
|
||||
send_response(
|
||||
&req.id,
|
||||
serde_json::json!({ "stopped": true, "wasRunning": false }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
"getEdgeStatus" => {
|
||||
let e = edge.lock().await;
|
||||
if let Some(edge_instance) = e.as_ref() {
|
||||
let status = edge_instance.get_status().await;
|
||||
send_response(
|
||||
&req.id,
|
||||
serde_json::to_value(&status).unwrap_or_default(),
|
||||
);
|
||||
} else {
|
||||
send_response(
|
||||
&req.id,
|
||||
serde_json::json!({
|
||||
"running": false,
|
||||
"connected": false,
|
||||
"publicIp": null,
|
||||
"activeStreams": 0,
|
||||
"listenPorts": []
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_ => {
|
||||
send_error(&req.id, &format!("unknown method: {}", req.method));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_ipc_request_deserialize() {
|
||||
let json = r#"{"id": "1", "method": "ping", "params": {}}"#;
|
||||
let req: IpcRequest = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(req.id, "1");
|
||||
assert_eq!(req.method, "ping");
|
||||
assert!(req.params.is_object());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ipc_response_skip_error_when_none() {
|
||||
let resp = IpcResponse {
|
||||
id: "1".to_string(),
|
||||
success: true,
|
||||
result: Some(serde_json::json!({"pong": true})),
|
||||
error: None,
|
||||
};
|
||||
let json = serde_json::to_value(&resp).unwrap();
|
||||
assert_eq!(json["id"], "1");
|
||||
assert_eq!(json["success"], true);
|
||||
assert_eq!(json["result"]["pong"], true);
|
||||
assert!(json.get("error").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ipc_response_skip_result_when_none() {
|
||||
let resp = IpcResponse {
|
||||
id: "2".to_string(),
|
||||
success: false,
|
||||
result: None,
|
||||
error: Some("something failed".to_string()),
|
||||
};
|
||||
let json = serde_json::to_value(&resp).unwrap();
|
||||
assert_eq!(json["id"], "2");
|
||||
assert_eq!(json["success"], false);
|
||||
assert_eq!(json["error"], "something failed");
|
||||
assert!(json.get("result").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ipc_event_serialize() {
|
||||
let evt = IpcEvent {
|
||||
event: "ready".to_string(),
|
||||
data: serde_json::json!({"version": "2.0.0"}),
|
||||
};
|
||||
let json = serde_json::to_value(&evt).unwrap();
|
||||
assert_eq!(json["event"], "ready");
|
||||
assert_eq!(json["data"]["version"], "2.0.0");
|
||||
}
|
||||
}
|
||||
15
rust/crates/remoteingress-core/Cargo.toml
Normal file
15
rust/crates/remoteingress-core/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "remoteingress-core"
|
||||
version = "2.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
remoteingress-protocol = { path = "../remoteingress-protocol" }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
tokio-rustls = "0.26"
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "logging", "std", "tls12"] }
|
||||
rcgen = "0.13"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
log = "0.4"
|
||||
rustls-pemfile = "2"
|
||||
776
rust/crates/remoteingress-core/src/edge.rs
Normal file
776
rust/crates/remoteingress-core/src/edge.rs
Normal file
@@ -0,0 +1,776 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::sync::{mpsc, Mutex, RwLock};
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio_rustls::TlsConnector;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use remoteingress_protocol::*;
|
||||
|
||||
/// Edge configuration (hub-host + credentials only; ports come from hub).
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EdgeConfig {
|
||||
pub hub_host: String,
|
||||
pub hub_port: u16,
|
||||
pub edge_id: String,
|
||||
pub secret: String,
|
||||
}
|
||||
|
||||
/// Handshake config received from hub after authentication.
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct HandshakeConfig {
|
||||
listen_ports: Vec<u16>,
|
||||
#[serde(default = "default_stun_interval")]
|
||||
stun_interval_secs: u64,
|
||||
}
|
||||
|
||||
fn default_stun_interval() -> u64 {
|
||||
300
|
||||
}
|
||||
|
||||
/// Runtime config update received from hub via FRAME_CONFIG.
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ConfigUpdate {
|
||||
listen_ports: Vec<u16>,
|
||||
}
|
||||
|
||||
/// Events emitted by the edge.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(tag = "type")]
|
||||
pub enum EdgeEvent {
|
||||
TunnelConnected,
|
||||
TunnelDisconnected,
|
||||
#[serde(rename_all = "camelCase")]
|
||||
PublicIpDiscovered { ip: String },
|
||||
#[serde(rename_all = "camelCase")]
|
||||
PortsAssigned { listen_ports: Vec<u16> },
|
||||
#[serde(rename_all = "camelCase")]
|
||||
PortsUpdated { listen_ports: Vec<u16> },
|
||||
}
|
||||
|
||||
/// Edge status response.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EdgeStatus {
|
||||
pub running: bool,
|
||||
pub connected: bool,
|
||||
pub public_ip: Option<String>,
|
||||
pub active_streams: usize,
|
||||
pub listen_ports: Vec<u16>,
|
||||
}
|
||||
|
||||
/// The tunnel edge that listens for client connections and multiplexes them to the hub.
|
||||
pub struct TunnelEdge {
|
||||
config: RwLock<EdgeConfig>,
|
||||
event_tx: mpsc::UnboundedSender<EdgeEvent>,
|
||||
event_rx: Mutex<Option<mpsc::UnboundedReceiver<EdgeEvent>>>,
|
||||
shutdown_tx: Mutex<Option<mpsc::Sender<()>>>,
|
||||
running: RwLock<bool>,
|
||||
connected: Arc<RwLock<bool>>,
|
||||
public_ip: Arc<RwLock<Option<String>>>,
|
||||
active_streams: Arc<AtomicU32>,
|
||||
next_stream_id: Arc<AtomicU32>,
|
||||
listen_ports: Arc<RwLock<Vec<u16>>>,
|
||||
}
|
||||
|
||||
impl TunnelEdge {
|
||||
pub fn new(config: EdgeConfig) -> Self {
|
||||
let (event_tx, event_rx) = mpsc::unbounded_channel();
|
||||
Self {
|
||||
config: RwLock::new(config),
|
||||
event_tx,
|
||||
event_rx: Mutex::new(Some(event_rx)),
|
||||
shutdown_tx: Mutex::new(None),
|
||||
running: RwLock::new(false),
|
||||
connected: Arc::new(RwLock::new(false)),
|
||||
public_ip: Arc::new(RwLock::new(None)),
|
||||
active_streams: Arc::new(AtomicU32::new(0)),
|
||||
next_stream_id: Arc::new(AtomicU32::new(1)),
|
||||
listen_ports: Arc::new(RwLock::new(Vec::new())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Take the event receiver (can only be called once).
|
||||
pub async fn take_event_rx(&self) -> Option<mpsc::UnboundedReceiver<EdgeEvent>> {
|
||||
self.event_rx.lock().await.take()
|
||||
}
|
||||
|
||||
/// Get the current edge status.
|
||||
pub async fn get_status(&self) -> EdgeStatus {
|
||||
EdgeStatus {
|
||||
running: *self.running.read().await,
|
||||
connected: *self.connected.read().await,
|
||||
public_ip: self.public_ip.read().await.clone(),
|
||||
active_streams: self.active_streams.load(Ordering::Relaxed) as usize,
|
||||
listen_ports: self.listen_ports.read().await.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the edge: connect to hub, start listeners.
|
||||
pub async fn start(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let config = self.config.read().await.clone();
|
||||
let (shutdown_tx, shutdown_rx) = mpsc::channel::<()>(1);
|
||||
*self.shutdown_tx.lock().await = Some(shutdown_tx);
|
||||
*self.running.write().await = true;
|
||||
|
||||
let connected = self.connected.clone();
|
||||
let public_ip = self.public_ip.clone();
|
||||
let active_streams = self.active_streams.clone();
|
||||
let next_stream_id = self.next_stream_id.clone();
|
||||
let event_tx = self.event_tx.clone();
|
||||
let listen_ports = self.listen_ports.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
edge_main_loop(
|
||||
config,
|
||||
connected,
|
||||
public_ip,
|
||||
active_streams,
|
||||
next_stream_id,
|
||||
event_tx,
|
||||
listen_ports,
|
||||
shutdown_rx,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Stop the edge.
|
||||
pub async fn stop(&self) {
|
||||
if let Some(tx) = self.shutdown_tx.lock().await.take() {
|
||||
let _ = tx.send(()).await;
|
||||
}
|
||||
*self.running.write().await = false;
|
||||
*self.connected.write().await = false;
|
||||
self.listen_ports.write().await.clear();
|
||||
}
|
||||
}
|
||||
|
||||
async fn edge_main_loop(
|
||||
config: EdgeConfig,
|
||||
connected: Arc<RwLock<bool>>,
|
||||
public_ip: Arc<RwLock<Option<String>>>,
|
||||
active_streams: Arc<AtomicU32>,
|
||||
next_stream_id: Arc<AtomicU32>,
|
||||
event_tx: mpsc::UnboundedSender<EdgeEvent>,
|
||||
listen_ports: Arc<RwLock<Vec<u16>>>,
|
||||
mut shutdown_rx: mpsc::Receiver<()>,
|
||||
) {
|
||||
let mut backoff_ms: u64 = 1000;
|
||||
let max_backoff_ms: u64 = 30000;
|
||||
|
||||
loop {
|
||||
// Try to connect to hub
|
||||
let result = connect_to_hub_and_run(
|
||||
&config,
|
||||
&connected,
|
||||
&public_ip,
|
||||
&active_streams,
|
||||
&next_stream_id,
|
||||
&event_tx,
|
||||
&listen_ports,
|
||||
&mut shutdown_rx,
|
||||
)
|
||||
.await;
|
||||
|
||||
*connected.write().await = false;
|
||||
let _ = event_tx.send(EdgeEvent::TunnelDisconnected);
|
||||
active_streams.store(0, Ordering::Relaxed);
|
||||
listen_ports.write().await.clear();
|
||||
|
||||
match result {
|
||||
EdgeLoopResult::Shutdown => break,
|
||||
EdgeLoopResult::Reconnect => {
|
||||
log::info!("Reconnecting in {}ms...", backoff_ms);
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(std::time::Duration::from_millis(backoff_ms)) => {}
|
||||
_ = shutdown_rx.recv() => break,
|
||||
}
|
||||
backoff_ms = (backoff_ms * 2).min(max_backoff_ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum EdgeLoopResult {
|
||||
Shutdown,
|
||||
Reconnect,
|
||||
}
|
||||
|
||||
async fn connect_to_hub_and_run(
|
||||
config: &EdgeConfig,
|
||||
connected: &Arc<RwLock<bool>>,
|
||||
public_ip: &Arc<RwLock<Option<String>>>,
|
||||
active_streams: &Arc<AtomicU32>,
|
||||
next_stream_id: &Arc<AtomicU32>,
|
||||
event_tx: &mpsc::UnboundedSender<EdgeEvent>,
|
||||
listen_ports: &Arc<RwLock<Vec<u16>>>,
|
||||
shutdown_rx: &mut mpsc::Receiver<()>,
|
||||
) -> EdgeLoopResult {
|
||||
// Build TLS connector that skips cert verification (auth is via secret)
|
||||
let tls_config = rustls::ClientConfig::builder()
|
||||
.dangerous()
|
||||
.with_custom_certificate_verifier(Arc::new(NoCertVerifier))
|
||||
.with_no_client_auth();
|
||||
|
||||
let connector = TlsConnector::from(Arc::new(tls_config));
|
||||
|
||||
let addr = format!("{}:{}", config.hub_host, config.hub_port);
|
||||
let tcp = match TcpStream::connect(&addr).await {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
log::error!("Failed to connect to hub at {}: {}", addr, e);
|
||||
return EdgeLoopResult::Reconnect;
|
||||
}
|
||||
};
|
||||
|
||||
let server_name = rustls::pki_types::ServerName::try_from(config.hub_host.clone())
|
||||
.unwrap_or_else(|_| rustls::pki_types::ServerName::try_from("remoteingress-hub".to_string()).unwrap());
|
||||
|
||||
let tls_stream = match connector.connect(server_name, tcp).await {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
log::error!("TLS handshake failed: {}", e);
|
||||
return EdgeLoopResult::Reconnect;
|
||||
}
|
||||
};
|
||||
|
||||
let (read_half, mut write_half) = tokio::io::split(tls_stream);
|
||||
|
||||
// Send auth line
|
||||
let auth_line = format!("EDGE {} {}\n", config.edge_id, config.secret);
|
||||
if write_half.write_all(auth_line.as_bytes()).await.is_err() {
|
||||
return EdgeLoopResult::Reconnect;
|
||||
}
|
||||
|
||||
// Read handshake response line from hub (JSON with initial config)
|
||||
let mut buf_reader = BufReader::new(read_half);
|
||||
let mut handshake_line = String::new();
|
||||
match buf_reader.read_line(&mut handshake_line).await {
|
||||
Ok(0) => {
|
||||
log::error!("Hub rejected connection (EOF before handshake)");
|
||||
return EdgeLoopResult::Reconnect;
|
||||
}
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::error!("Failed to read handshake response: {}", e);
|
||||
return EdgeLoopResult::Reconnect;
|
||||
}
|
||||
}
|
||||
|
||||
let handshake: HandshakeConfig = match serde_json::from_str(handshake_line.trim()) {
|
||||
Ok(h) => h,
|
||||
Err(e) => {
|
||||
log::error!("Invalid handshake response: {}", e);
|
||||
return EdgeLoopResult::Reconnect;
|
||||
}
|
||||
};
|
||||
|
||||
log::info!(
|
||||
"Handshake from hub: ports {:?}, stun_interval {}s",
|
||||
handshake.listen_ports,
|
||||
handshake.stun_interval_secs
|
||||
);
|
||||
|
||||
*connected.write().await = true;
|
||||
let _ = event_tx.send(EdgeEvent::TunnelConnected);
|
||||
log::info!("Connected to hub at {}", addr);
|
||||
|
||||
// Store initial ports and emit event
|
||||
*listen_ports.write().await = handshake.listen_ports.clone();
|
||||
let _ = event_tx.send(EdgeEvent::PortsAssigned {
|
||||
listen_ports: handshake.listen_ports.clone(),
|
||||
});
|
||||
|
||||
// Start STUN discovery
|
||||
let stun_interval = handshake.stun_interval_secs;
|
||||
let public_ip_clone = public_ip.clone();
|
||||
let event_tx_clone = event_tx.clone();
|
||||
let stun_handle = tokio::spawn(async move {
|
||||
loop {
|
||||
if let Some(ip) = crate::stun::discover_public_ip().await {
|
||||
let mut pip = public_ip_clone.write().await;
|
||||
let changed = pip.as_ref() != Some(&ip);
|
||||
*pip = Some(ip.clone());
|
||||
if changed {
|
||||
let _ = event_tx_clone.send(EdgeEvent::PublicIpDiscovered { ip });
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_secs(stun_interval)).await;
|
||||
}
|
||||
});
|
||||
|
||||
// Client socket map: stream_id -> sender for writing data back to client
|
||||
let client_writers: Arc<Mutex<HashMap<u32, mpsc::Sender<Vec<u8>>>>> =
|
||||
Arc::new(Mutex::new(HashMap::new()));
|
||||
|
||||
// Shared tunnel writer
|
||||
let tunnel_writer = Arc::new(Mutex::new(write_half));
|
||||
|
||||
// Start TCP listeners for initial ports (hot-reloadable)
|
||||
let mut port_listeners: HashMap<u16, JoinHandle<()>> = HashMap::new();
|
||||
apply_port_config(
|
||||
&handshake.listen_ports,
|
||||
&mut port_listeners,
|
||||
&tunnel_writer,
|
||||
&client_writers,
|
||||
active_streams,
|
||||
next_stream_id,
|
||||
&config.edge_id,
|
||||
);
|
||||
|
||||
// Read frames from hub
|
||||
let mut frame_reader = FrameReader::new(buf_reader);
|
||||
let result = loop {
|
||||
tokio::select! {
|
||||
frame_result = frame_reader.next_frame() => {
|
||||
match frame_result {
|
||||
Ok(Some(frame)) => {
|
||||
match frame.frame_type {
|
||||
FRAME_DATA_BACK => {
|
||||
let writers = client_writers.lock().await;
|
||||
if let Some(tx) = writers.get(&frame.stream_id) {
|
||||
let _ = tx.send(frame.payload).await;
|
||||
}
|
||||
}
|
||||
FRAME_CLOSE_BACK => {
|
||||
let mut writers = client_writers.lock().await;
|
||||
writers.remove(&frame.stream_id);
|
||||
}
|
||||
FRAME_CONFIG => {
|
||||
if let Ok(update) = serde_json::from_slice::<ConfigUpdate>(&frame.payload) {
|
||||
log::info!("Config update from hub: ports {:?}", update.listen_ports);
|
||||
*listen_ports.write().await = update.listen_ports.clone();
|
||||
let _ = event_tx.send(EdgeEvent::PortsUpdated {
|
||||
listen_ports: update.listen_ports.clone(),
|
||||
});
|
||||
apply_port_config(
|
||||
&update.listen_ports,
|
||||
&mut port_listeners,
|
||||
&tunnel_writer,
|
||||
&client_writers,
|
||||
active_streams,
|
||||
next_stream_id,
|
||||
&config.edge_id,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
log::warn!("Unexpected frame type {} from hub", frame.frame_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
log::info!("Hub disconnected (EOF)");
|
||||
break EdgeLoopResult::Reconnect;
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Hub frame error: {}", e);
|
||||
break EdgeLoopResult::Reconnect;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = shutdown_rx.recv() => {
|
||||
break EdgeLoopResult::Shutdown;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Cleanup
|
||||
stun_handle.abort();
|
||||
for (_, h) in port_listeners.drain() {
|
||||
h.abort();
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Apply a new port configuration: spawn listeners for added ports, abort removed ports.
|
||||
fn apply_port_config(
|
||||
new_ports: &[u16],
|
||||
port_listeners: &mut HashMap<u16, JoinHandle<()>>,
|
||||
tunnel_writer: &Arc<Mutex<tokio::io::WriteHalf<tokio_rustls::client::TlsStream<TcpStream>>>>,
|
||||
client_writers: &Arc<Mutex<HashMap<u32, mpsc::Sender<Vec<u8>>>>>,
|
||||
active_streams: &Arc<AtomicU32>,
|
||||
next_stream_id: &Arc<AtomicU32>,
|
||||
edge_id: &str,
|
||||
) {
|
||||
let new_set: std::collections::HashSet<u16> = new_ports.iter().copied().collect();
|
||||
let old_set: std::collections::HashSet<u16> = port_listeners.keys().copied().collect();
|
||||
|
||||
// Remove ports no longer needed
|
||||
for &port in old_set.difference(&new_set) {
|
||||
if let Some(handle) = port_listeners.remove(&port) {
|
||||
log::info!("Stopping listener on port {}", port);
|
||||
handle.abort();
|
||||
}
|
||||
}
|
||||
|
||||
// Add new ports
|
||||
for &port in new_set.difference(&old_set) {
|
||||
let tunnel_writer = tunnel_writer.clone();
|
||||
let client_writers = client_writers.clone();
|
||||
let active_streams = active_streams.clone();
|
||||
let next_stream_id = next_stream_id.clone();
|
||||
let edge_id = edge_id.to_string();
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
let listener = match TcpListener::bind(("0.0.0.0", port)).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
log::error!("Failed to bind port {}: {}", port, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
log::info!("Listening on port {}", port);
|
||||
|
||||
loop {
|
||||
match listener.accept().await {
|
||||
Ok((client_stream, client_addr)) => {
|
||||
let stream_id = next_stream_id.fetch_add(1, Ordering::Relaxed);
|
||||
let tunnel_writer = tunnel_writer.clone();
|
||||
let client_writers = client_writers.clone();
|
||||
let active_streams = active_streams.clone();
|
||||
let edge_id = edge_id.clone();
|
||||
|
||||
active_streams.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
tokio::spawn(async move {
|
||||
handle_client_connection(
|
||||
client_stream,
|
||||
client_addr,
|
||||
stream_id,
|
||||
port,
|
||||
&edge_id,
|
||||
tunnel_writer,
|
||||
client_writers,
|
||||
)
|
||||
.await;
|
||||
active_streams.fetch_sub(1, Ordering::Relaxed);
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Accept error on port {}: {}", port, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
port_listeners.insert(port, handle);
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_client_connection(
|
||||
client_stream: TcpStream,
|
||||
client_addr: std::net::SocketAddr,
|
||||
stream_id: u32,
|
||||
dest_port: u16,
|
||||
edge_id: &str,
|
||||
tunnel_writer: Arc<Mutex<tokio::io::WriteHalf<tokio_rustls::client::TlsStream<TcpStream>>>>,
|
||||
client_writers: Arc<Mutex<HashMap<u32, mpsc::Sender<Vec<u8>>>>>,
|
||||
) {
|
||||
let client_ip = client_addr.ip().to_string();
|
||||
let client_port = client_addr.port();
|
||||
|
||||
// Determine edge IP (use 0.0.0.0 as placeholder — hub doesn't use it for routing)
|
||||
let edge_ip = "0.0.0.0";
|
||||
|
||||
// Send OPEN frame with PROXY v1 header
|
||||
let proxy_header = build_proxy_v1_header(&client_ip, edge_ip, client_port, dest_port);
|
||||
let open_frame = encode_frame(stream_id, FRAME_OPEN, proxy_header.as_bytes());
|
||||
{
|
||||
let mut w = tunnel_writer.lock().await;
|
||||
if w.write_all(&open_frame).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Set up channel for data coming back from hub
|
||||
let (back_tx, mut back_rx) = mpsc::channel::<Vec<u8>>(256);
|
||||
{
|
||||
let mut writers = client_writers.lock().await;
|
||||
writers.insert(stream_id, back_tx);
|
||||
}
|
||||
|
||||
let (mut client_read, mut client_write) = client_stream.into_split();
|
||||
|
||||
// Task: hub -> client
|
||||
let hub_to_client = tokio::spawn(async move {
|
||||
while let Some(data) = back_rx.recv().await {
|
||||
if client_write.write_all(&data).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let _ = client_write.shutdown().await;
|
||||
});
|
||||
|
||||
// Task: client -> hub
|
||||
let mut buf = vec![0u8; 32768];
|
||||
loop {
|
||||
match client_read.read(&mut buf).await {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
let data_frame = encode_frame(stream_id, FRAME_DATA, &buf[..n]);
|
||||
let mut w = tunnel_writer.lock().await;
|
||||
if w.write_all(&data_frame).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
// Send CLOSE frame
|
||||
let close_frame = encode_frame(stream_id, FRAME_CLOSE, &[]);
|
||||
{
|
||||
let mut w = tunnel_writer.lock().await;
|
||||
let _ = w.write_all(&close_frame).await;
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
{
|
||||
let mut writers = client_writers.lock().await;
|
||||
writers.remove(&stream_id);
|
||||
}
|
||||
hub_to_client.abort();
|
||||
let _ = edge_id; // used for logging context
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// --- Serde tests ---
|
||||
|
||||
#[test]
|
||||
fn test_edge_config_deserialize_camel_case() {
|
||||
let json = r#"{
|
||||
"hubHost": "hub.example.com",
|
||||
"hubPort": 8443,
|
||||
"edgeId": "edge-1",
|
||||
"secret": "my-secret"
|
||||
}"#;
|
||||
let config: EdgeConfig = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(config.hub_host, "hub.example.com");
|
||||
assert_eq!(config.hub_port, 8443);
|
||||
assert_eq!(config.edge_id, "edge-1");
|
||||
assert_eq!(config.secret, "my-secret");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_config_serialize_roundtrip() {
|
||||
let config = EdgeConfig {
|
||||
hub_host: "host.test".to_string(),
|
||||
hub_port: 9999,
|
||||
edge_id: "e1".to_string(),
|
||||
secret: "sec".to_string(),
|
||||
};
|
||||
let json = serde_json::to_string(&config).unwrap();
|
||||
let back: EdgeConfig = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(back.hub_host, config.hub_host);
|
||||
assert_eq!(back.hub_port, config.hub_port);
|
||||
assert_eq!(back.edge_id, config.edge_id);
|
||||
assert_eq!(back.secret, config.secret);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_handshake_config_deserialize_all_fields() {
|
||||
let json = r#"{"listenPorts": [80, 443], "stunIntervalSecs": 120}"#;
|
||||
let hc: HandshakeConfig = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(hc.listen_ports, vec![80, 443]);
|
||||
assert_eq!(hc.stun_interval_secs, 120);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_handshake_config_default_stun_interval() {
|
||||
let json = r#"{"listenPorts": [443]}"#;
|
||||
let hc: HandshakeConfig = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(hc.listen_ports, vec![443]);
|
||||
assert_eq!(hc.stun_interval_secs, 300);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_update_deserialize() {
|
||||
let json = r#"{"listenPorts": [8080, 9090]}"#;
|
||||
let update: ConfigUpdate = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(update.listen_ports, vec![8080, 9090]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_status_serialize() {
|
||||
let status = EdgeStatus {
|
||||
running: true,
|
||||
connected: true,
|
||||
public_ip: Some("1.2.3.4".to_string()),
|
||||
active_streams: 5,
|
||||
listen_ports: vec![443],
|
||||
};
|
||||
let json = serde_json::to_value(&status).unwrap();
|
||||
assert_eq!(json["running"], true);
|
||||
assert_eq!(json["connected"], true);
|
||||
assert_eq!(json["publicIp"], "1.2.3.4");
|
||||
assert_eq!(json["activeStreams"], 5);
|
||||
assert_eq!(json["listenPorts"], serde_json::json!([443]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_status_serialize_none_ip() {
|
||||
let status = EdgeStatus {
|
||||
running: false,
|
||||
connected: false,
|
||||
public_ip: None,
|
||||
active_streams: 0,
|
||||
listen_ports: vec![],
|
||||
};
|
||||
let json = serde_json::to_value(&status).unwrap();
|
||||
assert!(json["publicIp"].is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_event_tunnel_connected() {
|
||||
let event = EdgeEvent::TunnelConnected;
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "tunnelConnected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_event_tunnel_disconnected() {
|
||||
let event = EdgeEvent::TunnelDisconnected;
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "tunnelDisconnected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_event_public_ip_discovered() {
|
||||
let event = EdgeEvent::PublicIpDiscovered {
|
||||
ip: "203.0.113.1".to_string(),
|
||||
};
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "publicIpDiscovered");
|
||||
assert_eq!(json["ip"], "203.0.113.1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_event_ports_assigned() {
|
||||
let event = EdgeEvent::PortsAssigned {
|
||||
listen_ports: vec![443, 8080],
|
||||
};
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "portsAssigned");
|
||||
assert_eq!(json["listenPorts"], serde_json::json!([443, 8080]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_event_ports_updated() {
|
||||
let event = EdgeEvent::PortsUpdated {
|
||||
listen_ports: vec![9090],
|
||||
};
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "portsUpdated");
|
||||
assert_eq!(json["listenPorts"], serde_json::json!([9090]));
|
||||
}
|
||||
|
||||
// --- Async tests ---
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tunnel_edge_new_get_status() {
|
||||
let edge = TunnelEdge::new(EdgeConfig {
|
||||
hub_host: "localhost".to_string(),
|
||||
hub_port: 8443,
|
||||
edge_id: "test-edge".to_string(),
|
||||
secret: "test-secret".to_string(),
|
||||
});
|
||||
let status = edge.get_status().await;
|
||||
assert!(!status.running);
|
||||
assert!(!status.connected);
|
||||
assert!(status.public_ip.is_none());
|
||||
assert_eq!(status.active_streams, 0);
|
||||
assert!(status.listen_ports.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tunnel_edge_take_event_rx() {
|
||||
let edge = TunnelEdge::new(EdgeConfig {
|
||||
hub_host: "localhost".to_string(),
|
||||
hub_port: 8443,
|
||||
edge_id: "e".to_string(),
|
||||
secret: "s".to_string(),
|
||||
});
|
||||
let rx1 = edge.take_event_rx().await;
|
||||
assert!(rx1.is_some());
|
||||
let rx2 = edge.take_event_rx().await;
|
||||
assert!(rx2.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tunnel_edge_stop_without_start() {
|
||||
let edge = TunnelEdge::new(EdgeConfig {
|
||||
hub_host: "localhost".to_string(),
|
||||
hub_port: 8443,
|
||||
edge_id: "e".to_string(),
|
||||
secret: "s".to_string(),
|
||||
});
|
||||
edge.stop().await; // should not panic
|
||||
let status = edge.get_status().await;
|
||||
assert!(!status.running);
|
||||
}
|
||||
}
|
||||
|
||||
/// TLS certificate verifier that accepts any certificate (auth is via shared secret).
|
||||
#[derive(Debug)]
|
||||
struct NoCertVerifier;
|
||||
|
||||
impl rustls::client::danger::ServerCertVerifier for NoCertVerifier {
|
||||
fn verify_server_cert(
|
||||
&self,
|
||||
_end_entity: &rustls::pki_types::CertificateDer<'_>,
|
||||
_intermediates: &[rustls::pki_types::CertificateDer<'_>],
|
||||
_server_name: &rustls::pki_types::ServerName<'_>,
|
||||
_ocsp_response: &[u8],
|
||||
_now: rustls::pki_types::UnixTime,
|
||||
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
|
||||
Ok(rustls::client::danger::ServerCertVerified::assertion())
|
||||
}
|
||||
|
||||
fn verify_tls12_signature(
|
||||
&self,
|
||||
_message: &[u8],
|
||||
_cert: &rustls::pki_types::CertificateDer<'_>,
|
||||
_dss: &rustls::DigitallySignedStruct,
|
||||
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
|
||||
Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
|
||||
}
|
||||
|
||||
fn verify_tls13_signature(
|
||||
&self,
|
||||
_message: &[u8],
|
||||
_cert: &rustls::pki_types::CertificateDer<'_>,
|
||||
_dss: &rustls::DigitallySignedStruct,
|
||||
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
|
||||
Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
|
||||
}
|
||||
|
||||
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
|
||||
vec![
|
||||
rustls::SignatureScheme::RSA_PKCS1_SHA256,
|
||||
rustls::SignatureScheme::RSA_PKCS1_SHA384,
|
||||
rustls::SignatureScheme::RSA_PKCS1_SHA512,
|
||||
rustls::SignatureScheme::ECDSA_NISTP256_SHA256,
|
||||
rustls::SignatureScheme::ECDSA_NISTP384_SHA384,
|
||||
rustls::SignatureScheme::ECDSA_NISTP521_SHA512,
|
||||
rustls::SignatureScheme::RSA_PSS_SHA256,
|
||||
rustls::SignatureScheme::RSA_PSS_SHA384,
|
||||
rustls::SignatureScheme::RSA_PSS_SHA512,
|
||||
rustls::SignatureScheme::ED25519,
|
||||
rustls::SignatureScheme::ED448,
|
||||
]
|
||||
}
|
||||
}
|
||||
758
rust/crates/remoteingress-core/src/hub.rs
Normal file
758
rust/crates/remoteingress-core/src/hub.rs
Normal file
@@ -0,0 +1,758 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::sync::{mpsc, Mutex, RwLock};
|
||||
use tokio_rustls::TlsAcceptor;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use remoteingress_protocol::*;
|
||||
|
||||
/// Hub configuration.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct HubConfig {
|
||||
pub tunnel_port: u16,
|
||||
pub target_host: Option<String>,
|
||||
#[serde(skip)]
|
||||
pub tls_cert_pem: Option<String>,
|
||||
#[serde(skip)]
|
||||
pub tls_key_pem: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for HubConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
tunnel_port: 8443,
|
||||
target_host: Some("127.0.0.1".to_string()),
|
||||
tls_cert_pem: None,
|
||||
tls_key_pem: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An allowed edge identity.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AllowedEdge {
|
||||
pub id: String,
|
||||
pub secret: String,
|
||||
#[serde(default)]
|
||||
pub listen_ports: Vec<u16>,
|
||||
pub stun_interval_secs: Option<u64>,
|
||||
}
|
||||
|
||||
/// Handshake response sent to edge after authentication.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct HandshakeResponse {
|
||||
listen_ports: Vec<u16>,
|
||||
stun_interval_secs: u64,
|
||||
}
|
||||
|
||||
/// Configuration update pushed to a connected edge at runtime.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EdgeConfigUpdate {
|
||||
pub listen_ports: Vec<u16>,
|
||||
}
|
||||
|
||||
/// Runtime status of a connected edge.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ConnectedEdgeStatus {
|
||||
pub edge_id: String,
|
||||
pub connected_at: u64,
|
||||
pub active_streams: usize,
|
||||
}
|
||||
|
||||
/// Events emitted by the hub.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(tag = "type")]
|
||||
pub enum HubEvent {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
EdgeConnected { edge_id: String },
|
||||
#[serde(rename_all = "camelCase")]
|
||||
EdgeDisconnected { edge_id: String },
|
||||
#[serde(rename_all = "camelCase")]
|
||||
StreamOpened { edge_id: String, stream_id: u32 },
|
||||
#[serde(rename_all = "camelCase")]
|
||||
StreamClosed { edge_id: String, stream_id: u32 },
|
||||
}
|
||||
|
||||
/// Hub status response.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct HubStatus {
|
||||
pub running: bool,
|
||||
pub tunnel_port: u16,
|
||||
pub connected_edges: Vec<ConnectedEdgeStatus>,
|
||||
}
|
||||
|
||||
/// The tunnel hub that accepts edge connections and demuxes streams to SmartProxy.
|
||||
pub struct TunnelHub {
|
||||
config: RwLock<HubConfig>,
|
||||
allowed_edges: Arc<RwLock<HashMap<String, AllowedEdge>>>,
|
||||
connected_edges: Arc<Mutex<HashMap<String, ConnectedEdgeInfo>>>,
|
||||
event_tx: mpsc::UnboundedSender<HubEvent>,
|
||||
event_rx: Mutex<Option<mpsc::UnboundedReceiver<HubEvent>>>,
|
||||
shutdown_tx: Mutex<Option<mpsc::Sender<()>>>,
|
||||
running: RwLock<bool>,
|
||||
}
|
||||
|
||||
struct ConnectedEdgeInfo {
|
||||
connected_at: u64,
|
||||
active_streams: Arc<Mutex<HashMap<u32, mpsc::Sender<Vec<u8>>>>>,
|
||||
config_tx: mpsc::Sender<EdgeConfigUpdate>,
|
||||
}
|
||||
|
||||
impl TunnelHub {
|
||||
pub fn new(config: HubConfig) -> Self {
|
||||
let (event_tx, event_rx) = mpsc::unbounded_channel();
|
||||
Self {
|
||||
config: RwLock::new(config),
|
||||
allowed_edges: Arc::new(RwLock::new(HashMap::new())),
|
||||
connected_edges: Arc::new(Mutex::new(HashMap::new())),
|
||||
event_tx,
|
||||
event_rx: Mutex::new(Some(event_rx)),
|
||||
shutdown_tx: Mutex::new(None),
|
||||
running: RwLock::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
/// Take the event receiver (can only be called once).
|
||||
pub async fn take_event_rx(&self) -> Option<mpsc::UnboundedReceiver<HubEvent>> {
|
||||
self.event_rx.lock().await.take()
|
||||
}
|
||||
|
||||
/// Update the list of allowed edges.
|
||||
/// For any currently-connected edge whose ports changed, push a config update.
|
||||
pub async fn update_allowed_edges(&self, edges: Vec<AllowedEdge>) {
|
||||
let mut map = self.allowed_edges.write().await;
|
||||
|
||||
// Build new map
|
||||
let mut new_map = HashMap::new();
|
||||
for edge in &edges {
|
||||
new_map.insert(edge.id.clone(), edge.clone());
|
||||
}
|
||||
|
||||
// Push config updates to connected edges whose ports changed
|
||||
let connected = self.connected_edges.lock().await;
|
||||
for edge in &edges {
|
||||
if let Some(info) = connected.get(&edge.id) {
|
||||
// Check if ports changed compared to old config
|
||||
let ports_changed = match map.get(&edge.id) {
|
||||
Some(old) => old.listen_ports != edge.listen_ports,
|
||||
None => true, // newly allowed edge that's already connected
|
||||
};
|
||||
if ports_changed {
|
||||
let update = EdgeConfigUpdate {
|
||||
listen_ports: edge.listen_ports.clone(),
|
||||
};
|
||||
let _ = info.config_tx.try_send(update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*map = new_map;
|
||||
}
|
||||
|
||||
/// Get the current hub status.
|
||||
pub async fn get_status(&self) -> HubStatus {
|
||||
let running = *self.running.read().await;
|
||||
let config = self.config.read().await;
|
||||
let edges = self.connected_edges.lock().await;
|
||||
|
||||
let mut connected = Vec::new();
|
||||
for (id, info) in edges.iter() {
|
||||
let streams = info.active_streams.lock().await;
|
||||
connected.push(ConnectedEdgeStatus {
|
||||
edge_id: id.clone(),
|
||||
connected_at: info.connected_at,
|
||||
active_streams: streams.len(),
|
||||
});
|
||||
}
|
||||
|
||||
HubStatus {
|
||||
running,
|
||||
tunnel_port: config.tunnel_port,
|
||||
connected_edges: connected,
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the hub — listen for TLS connections from edges.
|
||||
pub async fn start(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let config = self.config.read().await.clone();
|
||||
let tls_config = build_tls_config(&config)?;
|
||||
let acceptor = TlsAcceptor::from(Arc::new(tls_config));
|
||||
|
||||
let listener = TcpListener::bind(("0.0.0.0", config.tunnel_port)).await?;
|
||||
log::info!("Hub listening on port {}", config.tunnel_port);
|
||||
|
||||
let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1);
|
||||
*self.shutdown_tx.lock().await = Some(shutdown_tx);
|
||||
*self.running.write().await = true;
|
||||
|
||||
let allowed = self.allowed_edges.clone();
|
||||
let connected = self.connected_edges.clone();
|
||||
let event_tx = self.event_tx.clone();
|
||||
let target_host = config.target_host.unwrap_or_else(|| "127.0.0.1".to_string());
|
||||
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
tokio::select! {
|
||||
result = listener.accept() => {
|
||||
match result {
|
||||
Ok((stream, addr)) => {
|
||||
log::info!("Edge connection from {}", addr);
|
||||
let acceptor = acceptor.clone();
|
||||
let allowed = allowed.clone();
|
||||
let connected = connected.clone();
|
||||
let event_tx = event_tx.clone();
|
||||
let target = target_host.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = handle_edge_connection(
|
||||
stream, acceptor, allowed, connected, event_tx, target,
|
||||
).await {
|
||||
log::error!("Edge connection error: {}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Accept error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = shutdown_rx.recv() => {
|
||||
log::info!("Hub shutting down");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Stop the hub.
|
||||
pub async fn stop(&self) {
|
||||
if let Some(tx) = self.shutdown_tx.lock().await.take() {
|
||||
let _ = tx.send(()).await;
|
||||
}
|
||||
*self.running.write().await = false;
|
||||
// Clear connected edges
|
||||
self.connected_edges.lock().await.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a single edge connection: authenticate, then enter frame loop.
|
||||
async fn handle_edge_connection(
|
||||
stream: TcpStream,
|
||||
acceptor: TlsAcceptor,
|
||||
allowed: Arc<RwLock<HashMap<String, AllowedEdge>>>,
|
||||
connected: Arc<Mutex<HashMap<String, ConnectedEdgeInfo>>>,
|
||||
event_tx: mpsc::UnboundedSender<HubEvent>,
|
||||
target_host: String,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let tls_stream = acceptor.accept(stream).await?;
|
||||
let (read_half, mut write_half) = tokio::io::split(tls_stream);
|
||||
let mut buf_reader = BufReader::new(read_half);
|
||||
|
||||
// Read auth line: "EDGE <edgeId> <secret>\n"
|
||||
let mut auth_line = String::new();
|
||||
buf_reader.read_line(&mut auth_line).await?;
|
||||
let auth_line = auth_line.trim();
|
||||
|
||||
let parts: Vec<&str> = auth_line.splitn(3, ' ').collect();
|
||||
if parts.len() != 3 || parts[0] != "EDGE" {
|
||||
return Err("invalid auth line".into());
|
||||
}
|
||||
|
||||
let edge_id = parts[1].to_string();
|
||||
let secret = parts[2];
|
||||
|
||||
// Verify credentials and extract edge config
|
||||
let (listen_ports, stun_interval_secs) = {
|
||||
let edges = allowed.read().await;
|
||||
match edges.get(&edge_id) {
|
||||
Some(edge) => {
|
||||
if !constant_time_eq(secret.as_bytes(), edge.secret.as_bytes()) {
|
||||
return Err(format!("invalid secret for edge {}", edge_id).into());
|
||||
}
|
||||
(edge.listen_ports.clone(), edge.stun_interval_secs.unwrap_or(300))
|
||||
}
|
||||
None => {
|
||||
return Err(format!("unknown edge {}", edge_id).into());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
log::info!("Edge {} authenticated", edge_id);
|
||||
let _ = event_tx.send(HubEvent::EdgeConnected {
|
||||
edge_id: edge_id.clone(),
|
||||
});
|
||||
|
||||
// Send handshake response with initial config before frame protocol begins
|
||||
let handshake = HandshakeResponse {
|
||||
listen_ports: listen_ports.clone(),
|
||||
stun_interval_secs,
|
||||
};
|
||||
let mut handshake_json = serde_json::to_string(&handshake)?;
|
||||
handshake_json.push('\n');
|
||||
write_half.write_all(handshake_json.as_bytes()).await?;
|
||||
|
||||
// Track this edge
|
||||
let streams: Arc<Mutex<HashMap<u32, mpsc::Sender<Vec<u8>>>>> =
|
||||
Arc::new(Mutex::new(HashMap::new()));
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs();
|
||||
|
||||
// Create config update channel
|
||||
let (config_tx, mut config_rx) = mpsc::channel::<EdgeConfigUpdate>(16);
|
||||
|
||||
{
|
||||
let mut edges = connected.lock().await;
|
||||
edges.insert(
|
||||
edge_id.clone(),
|
||||
ConnectedEdgeInfo {
|
||||
connected_at: now,
|
||||
active_streams: streams.clone(),
|
||||
config_tx,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Shared writer for sending frames back to edge
|
||||
let write_half = Arc::new(Mutex::new(write_half));
|
||||
|
||||
// Spawn task to forward config updates as FRAME_CONFIG frames
|
||||
let config_writer = write_half.clone();
|
||||
let config_edge_id = edge_id.clone();
|
||||
let config_handle = tokio::spawn(async move {
|
||||
while let Some(update) = config_rx.recv().await {
|
||||
if let Ok(payload) = serde_json::to_vec(&update) {
|
||||
let frame = encode_frame(0, FRAME_CONFIG, &payload);
|
||||
let mut w = config_writer.lock().await;
|
||||
if w.write_all(&frame).await.is_err() {
|
||||
log::error!("Failed to send config update to edge {}", config_edge_id);
|
||||
break;
|
||||
}
|
||||
log::info!("Sent config update to edge {}: ports {:?}", config_edge_id, update.listen_ports);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Frame reading loop
|
||||
let mut frame_reader = FrameReader::new(buf_reader);
|
||||
|
||||
loop {
|
||||
match frame_reader.next_frame().await {
|
||||
Ok(Some(frame)) => {
|
||||
match frame.frame_type {
|
||||
FRAME_OPEN => {
|
||||
// Payload is PROXY v1 header line
|
||||
let proxy_header = String::from_utf8_lossy(&frame.payload).to_string();
|
||||
|
||||
// Parse destination port from PROXY header
|
||||
let dest_port = parse_dest_port_from_proxy(&proxy_header).unwrap_or(443);
|
||||
|
||||
let stream_id = frame.stream_id;
|
||||
let edge_id_clone = edge_id.clone();
|
||||
let event_tx_clone = event_tx.clone();
|
||||
let streams_clone = streams.clone();
|
||||
let writer_clone = write_half.clone();
|
||||
let target = target_host.clone();
|
||||
|
||||
let _ = event_tx.send(HubEvent::StreamOpened {
|
||||
edge_id: edge_id.clone(),
|
||||
stream_id,
|
||||
});
|
||||
|
||||
// Create channel for data from edge to this stream
|
||||
let (data_tx, mut data_rx) = mpsc::channel::<Vec<u8>>(256);
|
||||
{
|
||||
let mut s = streams.lock().await;
|
||||
s.insert(stream_id, data_tx);
|
||||
}
|
||||
|
||||
// Spawn task: connect to SmartProxy, send PROXY header, pipe data
|
||||
tokio::spawn(async move {
|
||||
let result = async {
|
||||
let mut upstream =
|
||||
TcpStream::connect((target.as_str(), dest_port)).await?;
|
||||
upstream.write_all(proxy_header.as_bytes()).await?;
|
||||
|
||||
let (mut up_read, mut up_write) =
|
||||
upstream.into_split();
|
||||
|
||||
// Forward data from edge (via channel) to SmartProxy
|
||||
let writer_for_edge_data = tokio::spawn(async move {
|
||||
while let Some(data) = data_rx.recv().await {
|
||||
if up_write.write_all(&data).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let _ = up_write.shutdown().await;
|
||||
});
|
||||
|
||||
// Forward data from SmartProxy back to edge
|
||||
let mut buf = vec![0u8; 32768];
|
||||
loop {
|
||||
match up_read.read(&mut buf).await {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
let frame =
|
||||
encode_frame(stream_id, FRAME_DATA_BACK, &buf[..n]);
|
||||
let mut w = writer_clone.lock().await;
|
||||
if w.write_all(&frame).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
// Send CLOSE_BACK to edge
|
||||
let close_frame = encode_frame(stream_id, FRAME_CLOSE_BACK, &[]);
|
||||
let mut w = writer_clone.lock().await;
|
||||
let _ = w.write_all(&close_frame).await;
|
||||
|
||||
writer_for_edge_data.abort();
|
||||
Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
|
||||
}
|
||||
.await;
|
||||
|
||||
if let Err(e) = result {
|
||||
log::error!("Stream {} error: {}", stream_id, e);
|
||||
// Send CLOSE_BACK on error
|
||||
let close_frame = encode_frame(stream_id, FRAME_CLOSE_BACK, &[]);
|
||||
let mut w = writer_clone.lock().await;
|
||||
let _ = w.write_all(&close_frame).await;
|
||||
}
|
||||
|
||||
// Clean up stream
|
||||
{
|
||||
let mut s = streams_clone.lock().await;
|
||||
s.remove(&stream_id);
|
||||
}
|
||||
let _ = event_tx_clone.send(HubEvent::StreamClosed {
|
||||
edge_id: edge_id_clone,
|
||||
stream_id,
|
||||
});
|
||||
});
|
||||
}
|
||||
FRAME_DATA => {
|
||||
let s = streams.lock().await;
|
||||
if let Some(tx) = s.get(&frame.stream_id) {
|
||||
let _ = tx.send(frame.payload).await;
|
||||
}
|
||||
}
|
||||
FRAME_CLOSE => {
|
||||
let mut s = streams.lock().await;
|
||||
s.remove(&frame.stream_id);
|
||||
}
|
||||
_ => {
|
||||
log::warn!("Unexpected frame type {} from edge", frame.frame_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
log::info!("Edge {} disconnected (EOF)", edge_id);
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Edge {} frame error: {}", edge_id, e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
config_handle.abort();
|
||||
{
|
||||
let mut edges = connected.lock().await;
|
||||
edges.remove(&edge_id);
|
||||
}
|
||||
let _ = event_tx.send(HubEvent::EdgeDisconnected {
|
||||
edge_id: edge_id.clone(),
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parse destination port from PROXY v1 header.
|
||||
fn parse_dest_port_from_proxy(header: &str) -> Option<u16> {
|
||||
let parts: Vec<&str> = header.trim().split_whitespace().collect();
|
||||
if parts.len() >= 6 {
|
||||
parts[5].parse().ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Build TLS server config from PEM strings, or auto-generate self-signed.
|
||||
fn build_tls_config(
|
||||
config: &HubConfig,
|
||||
) -> Result<rustls::ServerConfig, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let (cert_pem, key_pem) = match (&config.tls_cert_pem, &config.tls_key_pem) {
|
||||
(Some(cert), Some(key)) => (cert.clone(), key.clone()),
|
||||
_ => {
|
||||
// Generate self-signed certificate
|
||||
let cert = rcgen::generate_simple_self_signed(vec!["remoteingress-hub".to_string()])?;
|
||||
let cert_pem = cert.cert.pem();
|
||||
let key_pem = cert.key_pair.serialize_pem();
|
||||
(cert_pem, key_pem)
|
||||
}
|
||||
};
|
||||
|
||||
let certs = rustls_pemfile_parse_certs(&cert_pem)?;
|
||||
let key = rustls_pemfile_parse_key(&key_pem)?;
|
||||
|
||||
let mut config = rustls::ServerConfig::builder()
|
||||
.with_no_client_auth()
|
||||
.with_single_cert(certs, key)?;
|
||||
|
||||
config.alpn_protocols = vec![b"remoteingress".to_vec()];
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn rustls_pemfile_parse_certs(
|
||||
pem: &str,
|
||||
) -> Result<Vec<rustls::pki_types::CertificateDer<'static>>, Box<dyn std::error::Error + Send + Sync>>
|
||||
{
|
||||
let mut reader = std::io::Cursor::new(pem.as_bytes());
|
||||
let certs = rustls_pemfile::certs(&mut reader).collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(certs)
|
||||
}
|
||||
|
||||
fn rustls_pemfile_parse_key(
|
||||
pem: &str,
|
||||
) -> Result<rustls::pki_types::PrivateKeyDer<'static>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let mut reader = std::io::Cursor::new(pem.as_bytes());
|
||||
let key = rustls_pemfile::private_key(&mut reader)?
|
||||
.ok_or("no private key found in PEM")?;
|
||||
Ok(key)
|
||||
}
|
||||
|
||||
/// Constant-time comparison of two byte slices.
|
||||
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
|
||||
if a.len() != b.len() {
|
||||
return false;
|
||||
}
|
||||
let mut diff = 0u8;
|
||||
for (x, y) in a.iter().zip(b.iter()) {
|
||||
diff |= x ^ y;
|
||||
}
|
||||
diff == 0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// --- constant_time_eq tests ---
|
||||
|
||||
#[test]
|
||||
fn test_constant_time_eq_equal() {
|
||||
assert!(constant_time_eq(b"hello", b"hello"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constant_time_eq_different_content() {
|
||||
assert!(!constant_time_eq(b"hello", b"world"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constant_time_eq_different_lengths() {
|
||||
assert!(!constant_time_eq(b"short", b"longer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constant_time_eq_both_empty() {
|
||||
assert!(constant_time_eq(b"", b""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constant_time_eq_one_empty() {
|
||||
assert!(!constant_time_eq(b"", b"notempty"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constant_time_eq_single_bit_difference() {
|
||||
// 'A' = 0x41, 'a' = 0x61 — differ by one bit
|
||||
assert!(!constant_time_eq(b"A", b"a"));
|
||||
}
|
||||
|
||||
// --- parse_dest_port_from_proxy tests ---
|
||||
|
||||
#[test]
|
||||
fn test_parse_dest_port_443() {
|
||||
let header = "PROXY TCP4 1.2.3.4 5.6.7.8 12345 443\r\n";
|
||||
assert_eq!(parse_dest_port_from_proxy(header), Some(443));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_dest_port_80() {
|
||||
let header = "PROXY TCP4 10.0.0.1 10.0.0.2 54321 80\r\n";
|
||||
assert_eq!(parse_dest_port_from_proxy(header), Some(80));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_dest_port_65535() {
|
||||
let header = "PROXY TCP4 10.0.0.1 10.0.0.2 1 65535\r\n";
|
||||
assert_eq!(parse_dest_port_from_proxy(header), Some(65535));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_dest_port_too_few_fields() {
|
||||
let header = "PROXY TCP4 1.2.3.4";
|
||||
assert_eq!(parse_dest_port_from_proxy(header), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_dest_port_empty_string() {
|
||||
assert_eq!(parse_dest_port_from_proxy(""), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_dest_port_non_numeric() {
|
||||
let header = "PROXY TCP4 1.2.3.4 5.6.7.8 12345 abc\r\n";
|
||||
assert_eq!(parse_dest_port_from_proxy(header), None);
|
||||
}
|
||||
|
||||
// --- Serde tests ---
|
||||
|
||||
#[test]
|
||||
fn test_allowed_edge_deserialize_all_fields() {
|
||||
let json = r#"{
|
||||
"id": "edge-1",
|
||||
"secret": "s3cret",
|
||||
"listenPorts": [443, 8080],
|
||||
"stunIntervalSecs": 120
|
||||
}"#;
|
||||
let edge: AllowedEdge = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(edge.id, "edge-1");
|
||||
assert_eq!(edge.secret, "s3cret");
|
||||
assert_eq!(edge.listen_ports, vec![443, 8080]);
|
||||
assert_eq!(edge.stun_interval_secs, Some(120));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_allowed_edge_deserialize_with_defaults() {
|
||||
let json = r#"{"id": "edge-2", "secret": "key"}"#;
|
||||
let edge: AllowedEdge = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(edge.id, "edge-2");
|
||||
assert_eq!(edge.secret, "key");
|
||||
assert!(edge.listen_ports.is_empty());
|
||||
assert_eq!(edge.stun_interval_secs, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_handshake_response_serializes_camel_case() {
|
||||
let resp = HandshakeResponse {
|
||||
listen_ports: vec![443, 8080],
|
||||
stun_interval_secs: 300,
|
||||
};
|
||||
let json = serde_json::to_value(&resp).unwrap();
|
||||
assert_eq!(json["listenPorts"], serde_json::json!([443, 8080]));
|
||||
assert_eq!(json["stunIntervalSecs"], 300);
|
||||
// Ensure snake_case keys are NOT present
|
||||
assert!(json.get("listen_ports").is_none());
|
||||
assert!(json.get("stun_interval_secs").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edge_config_update_serializes_camel_case() {
|
||||
let update = EdgeConfigUpdate {
|
||||
listen_ports: vec![80, 443],
|
||||
};
|
||||
let json = serde_json::to_value(&update).unwrap();
|
||||
assert_eq!(json["listenPorts"], serde_json::json!([80, 443]));
|
||||
assert!(json.get("listen_ports").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hub_config_default() {
|
||||
let config = HubConfig::default();
|
||||
assert_eq!(config.tunnel_port, 8443);
|
||||
assert_eq!(config.target_host, Some("127.0.0.1".to_string()));
|
||||
assert!(config.tls_cert_pem.is_none());
|
||||
assert!(config.tls_key_pem.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hub_event_edge_connected_serialize() {
|
||||
let event = HubEvent::EdgeConnected {
|
||||
edge_id: "edge-1".to_string(),
|
||||
};
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "edgeConnected");
|
||||
assert_eq!(json["edgeId"], "edge-1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hub_event_edge_disconnected_serialize() {
|
||||
let event = HubEvent::EdgeDisconnected {
|
||||
edge_id: "edge-2".to_string(),
|
||||
};
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "edgeDisconnected");
|
||||
assert_eq!(json["edgeId"], "edge-2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hub_event_stream_opened_serialize() {
|
||||
let event = HubEvent::StreamOpened {
|
||||
edge_id: "e".to_string(),
|
||||
stream_id: 42,
|
||||
};
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "streamOpened");
|
||||
assert_eq!(json["edgeId"], "e");
|
||||
assert_eq!(json["streamId"], 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hub_event_stream_closed_serialize() {
|
||||
let event = HubEvent::StreamClosed {
|
||||
edge_id: "e".to_string(),
|
||||
stream_id: 7,
|
||||
};
|
||||
let json = serde_json::to_value(&event).unwrap();
|
||||
assert_eq!(json["type"], "streamClosed");
|
||||
assert_eq!(json["edgeId"], "e");
|
||||
assert_eq!(json["streamId"], 7);
|
||||
}
|
||||
|
||||
// --- Async tests ---
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tunnel_hub_new_get_status() {
|
||||
let hub = TunnelHub::new(HubConfig::default());
|
||||
let status = hub.get_status().await;
|
||||
assert!(!status.running);
|
||||
assert!(status.connected_edges.is_empty());
|
||||
assert_eq!(status.tunnel_port, 8443);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tunnel_hub_take_event_rx() {
|
||||
let hub = TunnelHub::new(HubConfig::default());
|
||||
let rx1 = hub.take_event_rx().await;
|
||||
assert!(rx1.is_some());
|
||||
let rx2 = hub.take_event_rx().await;
|
||||
assert!(rx2.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tunnel_hub_stop_without_start() {
|
||||
let hub = TunnelHub::new(HubConfig::default());
|
||||
hub.stop().await; // should not panic
|
||||
let status = hub.get_status().await;
|
||||
assert!(!status.running);
|
||||
}
|
||||
}
|
||||
5
rust/crates/remoteingress-core/src/lib.rs
Normal file
5
rust/crates/remoteingress-core/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod hub;
|
||||
pub mod edge;
|
||||
pub mod stun;
|
||||
|
||||
pub use remoteingress_protocol as protocol;
|
||||
264
rust/crates/remoteingress-core/src/stun.rs
Normal file
264
rust/crates/remoteingress-core/src/stun.rs
Normal file
@@ -0,0 +1,264 @@
|
||||
use std::net::Ipv4Addr;
|
||||
use tokio::net::UdpSocket;
|
||||
use tokio::time::{timeout, Duration};
|
||||
|
||||
const STUN_SERVER: &str = "stun.cloudflare.com:3478";
|
||||
const STUN_TIMEOUT: Duration = Duration::from_secs(3);
|
||||
|
||||
// STUN constants
|
||||
const STUN_BINDING_REQUEST: u16 = 0x0001;
|
||||
const STUN_MAGIC_COOKIE: u32 = 0x2112A442;
|
||||
const ATTR_XOR_MAPPED_ADDRESS: u16 = 0x0020;
|
||||
const ATTR_MAPPED_ADDRESS: u16 = 0x0001;
|
||||
|
||||
/// Discover our public IP via STUN Binding Request (RFC 5389).
|
||||
/// Returns `None` on timeout or parse failure.
|
||||
pub async fn discover_public_ip() -> Option<String> {
|
||||
discover_public_ip_from(STUN_SERVER).await
|
||||
}
|
||||
|
||||
pub async fn discover_public_ip_from(server: &str) -> Option<String> {
|
||||
let result = timeout(STUN_TIMEOUT, async {
|
||||
let socket = UdpSocket::bind("0.0.0.0:0").await.ok()?;
|
||||
socket.connect(server).await.ok()?;
|
||||
|
||||
// Build STUN Binding Request (20 bytes)
|
||||
let mut request = [0u8; 20];
|
||||
// Message Type: Binding Request (0x0001)
|
||||
request[0..2].copy_from_slice(&STUN_BINDING_REQUEST.to_be_bytes());
|
||||
// Message Length: 0 (no attributes)
|
||||
request[2..4].copy_from_slice(&0u16.to_be_bytes());
|
||||
// Magic Cookie
|
||||
request[4..8].copy_from_slice(&STUN_MAGIC_COOKIE.to_be_bytes());
|
||||
// Transaction ID: 12 random bytes
|
||||
let txn_id: [u8; 12] = rand_bytes();
|
||||
request[8..20].copy_from_slice(&txn_id);
|
||||
|
||||
socket.send(&request).await.ok()?;
|
||||
|
||||
let mut buf = [0u8; 512];
|
||||
let n = socket.recv(&mut buf).await.ok()?;
|
||||
if n < 20 {
|
||||
return None;
|
||||
}
|
||||
|
||||
parse_stun_response(&buf[..n], &txn_id)
|
||||
})
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(ip) => ip,
|
||||
Err(_) => None, // timeout
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_stun_response(data: &[u8], _txn_id: &[u8; 12]) -> Option<String> {
|
||||
if data.len() < 20 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Verify it's a Binding Response (0x0101)
|
||||
let msg_type = u16::from_be_bytes([data[0], data[1]]);
|
||||
if msg_type != 0x0101 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let msg_len = u16::from_be_bytes([data[2], data[3]]) as usize;
|
||||
let magic = u32::from_be_bytes([data[4], data[5], data[6], data[7]]);
|
||||
|
||||
// Parse attributes
|
||||
let attrs = &data[20..std::cmp::min(20 + msg_len, data.len())];
|
||||
let mut offset = 0;
|
||||
|
||||
while offset + 4 <= attrs.len() {
|
||||
let attr_type = u16::from_be_bytes([attrs[offset], attrs[offset + 1]]);
|
||||
let attr_len = u16::from_be_bytes([attrs[offset + 2], attrs[offset + 3]]) as usize;
|
||||
offset += 4;
|
||||
|
||||
if offset + attr_len > attrs.len() {
|
||||
break;
|
||||
}
|
||||
|
||||
let attr_data = &attrs[offset..offset + attr_len];
|
||||
|
||||
match attr_type {
|
||||
ATTR_XOR_MAPPED_ADDRESS if attr_data.len() >= 8 => {
|
||||
let family = attr_data[1];
|
||||
if family == 0x01 {
|
||||
// IPv4
|
||||
let port_xored = u16::from_be_bytes([attr_data[2], attr_data[3]]);
|
||||
let _port = port_xored ^ (STUN_MAGIC_COOKIE >> 16) as u16;
|
||||
let ip_xored = u32::from_be_bytes([
|
||||
attr_data[4],
|
||||
attr_data[5],
|
||||
attr_data[6],
|
||||
attr_data[7],
|
||||
]);
|
||||
let ip = ip_xored ^ magic;
|
||||
return Some(Ipv4Addr::from(ip).to_string());
|
||||
}
|
||||
}
|
||||
ATTR_MAPPED_ADDRESS if attr_data.len() >= 8 => {
|
||||
let family = attr_data[1];
|
||||
if family == 0x01 {
|
||||
// IPv4 (non-XOR fallback)
|
||||
let ip = u32::from_be_bytes([
|
||||
attr_data[4],
|
||||
attr_data[5],
|
||||
attr_data[6],
|
||||
attr_data[7],
|
||||
]);
|
||||
return Some(Ipv4Addr::from(ip).to_string());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Pad to 4-byte boundary
|
||||
offset += (attr_len + 3) & !3;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Build a synthetic STUN Binding Response with given attributes.
|
||||
fn build_stun_response(attrs: &[(u16, &[u8])]) -> Vec<u8> {
|
||||
let mut attrs_bytes = Vec::new();
|
||||
for &(attr_type, attr_data) in attrs {
|
||||
attrs_bytes.extend_from_slice(&attr_type.to_be_bytes());
|
||||
attrs_bytes.extend_from_slice(&(attr_data.len() as u16).to_be_bytes());
|
||||
attrs_bytes.extend_from_slice(attr_data);
|
||||
// Pad to 4-byte boundary
|
||||
let pad = (4 - (attr_data.len() % 4)) % 4;
|
||||
attrs_bytes.extend(std::iter::repeat(0u8).take(pad));
|
||||
}
|
||||
|
||||
let mut response = Vec::new();
|
||||
// msg_type = 0x0101 (Binding Response)
|
||||
response.extend_from_slice(&0x0101u16.to_be_bytes());
|
||||
// message length
|
||||
response.extend_from_slice(&(attrs_bytes.len() as u16).to_be_bytes());
|
||||
// magic cookie
|
||||
response.extend_from_slice(&STUN_MAGIC_COOKIE.to_be_bytes());
|
||||
// transaction ID (12 bytes)
|
||||
response.extend_from_slice(&[0u8; 12]);
|
||||
// attributes
|
||||
response.extend_from_slice(&attrs_bytes);
|
||||
response
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_xor_mapped_address_ipv4() {
|
||||
// IP 203.0.113.1 = 0xCB007101, XOR'd with magic 0x2112A442 = 0xEA12D543
|
||||
let attr_data: [u8; 8] = [
|
||||
0x00, 0x01, // reserved + family (IPv4)
|
||||
0x11, 0x2B, // port XOR'd with 0x2112 (port 0x3039 = 12345)
|
||||
0xEA, 0x12, 0xD5, 0x43, // IP XOR'd
|
||||
];
|
||||
let data = build_stun_response(&[(ATTR_XOR_MAPPED_ADDRESS, &attr_data)]);
|
||||
let txn_id = [0u8; 12];
|
||||
let result = parse_stun_response(&data, &txn_id);
|
||||
assert_eq!(result, Some("203.0.113.1".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mapped_address_fallback_ipv4() {
|
||||
// IP 192.168.1.1 = 0xC0A80101 (no XOR)
|
||||
let attr_data: [u8; 8] = [
|
||||
0x00, 0x01, // reserved + family (IPv4)
|
||||
0x00, 0x50, // port 80
|
||||
0xC0, 0xA8, 0x01, 0x01, // IP
|
||||
];
|
||||
let data = build_stun_response(&[(ATTR_MAPPED_ADDRESS, &attr_data)]);
|
||||
let txn_id = [0u8; 12];
|
||||
let result = parse_stun_response(&data, &txn_id);
|
||||
assert_eq!(result, Some("192.168.1.1".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_response_too_short() {
|
||||
let data = vec![0u8; 19]; // < 20 bytes
|
||||
let txn_id = [0u8; 12];
|
||||
assert_eq!(parse_stun_response(&data, &txn_id), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrong_msg_type() {
|
||||
// Build with correct helper then overwrite msg_type to 0x0001 (Binding Request)
|
||||
let mut data = build_stun_response(&[]);
|
||||
data[0] = 0x00;
|
||||
data[1] = 0x01;
|
||||
let txn_id = [0u8; 12];
|
||||
assert_eq!(parse_stun_response(&data, &txn_id), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_mapped_address_attributes() {
|
||||
// Valid response with no attributes
|
||||
let data = build_stun_response(&[]);
|
||||
let txn_id = [0u8; 12];
|
||||
assert_eq!(parse_stun_response(&data, &txn_id), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_xor_preferred_over_mapped() {
|
||||
// XOR gives 203.0.113.1, MAPPED gives 192.168.1.1
|
||||
let xor_data: [u8; 8] = [
|
||||
0x00, 0x01,
|
||||
0x11, 0x2B,
|
||||
0xEA, 0x12, 0xD5, 0x43,
|
||||
];
|
||||
let mapped_data: [u8; 8] = [
|
||||
0x00, 0x01,
|
||||
0x00, 0x50,
|
||||
0xC0, 0xA8, 0x01, 0x01,
|
||||
];
|
||||
// XOR listed first — should be preferred
|
||||
let data = build_stun_response(&[
|
||||
(ATTR_XOR_MAPPED_ADDRESS, &xor_data),
|
||||
(ATTR_MAPPED_ADDRESS, &mapped_data),
|
||||
]);
|
||||
let txn_id = [0u8; 12];
|
||||
let result = parse_stun_response(&data, &txn_id);
|
||||
assert_eq!(result, Some("203.0.113.1".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_truncated_attribute_data() {
|
||||
// Attribute claims 8 bytes but only 4 are present
|
||||
let mut data = build_stun_response(&[]);
|
||||
// Manually append a truncated XOR_MAPPED_ADDRESS attribute
|
||||
let attr_type = ATTR_XOR_MAPPED_ADDRESS.to_be_bytes();
|
||||
let attr_len = 8u16.to_be_bytes(); // claims 8 bytes
|
||||
let truncated = [0x00, 0x01, 0x11, 0x2B]; // only 4 bytes
|
||||
// Update message length
|
||||
let new_msg_len = (attr_type.len() + attr_len.len() + truncated.len()) as u16;
|
||||
data[2..4].copy_from_slice(&new_msg_len.to_be_bytes());
|
||||
data.extend_from_slice(&attr_type);
|
||||
data.extend_from_slice(&attr_len);
|
||||
data.extend_from_slice(&truncated);
|
||||
|
||||
let txn_id = [0u8; 12];
|
||||
// Should return None, not panic
|
||||
assert_eq!(parse_stun_response(&data, &txn_id), None);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate 12 random bytes for transaction ID.
|
||||
fn rand_bytes() -> [u8; 12] {
|
||||
let mut bytes = [0u8; 12];
|
||||
// Use a simple approach: mix timestamp + counter
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default();
|
||||
let nanos = now.as_nanos();
|
||||
bytes[0..8].copy_from_slice(&(nanos as u64).to_le_bytes());
|
||||
// Fill remaining with process-id based data
|
||||
let pid = std::process::id();
|
||||
bytes[8..12].copy_from_slice(&pid.to_le_bytes());
|
||||
bytes
|
||||
}
|
||||
7
rust/crates/remoteingress-protocol/Cargo.toml
Normal file
7
rust/crates/remoteingress-protocol/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "remoteingress-protocol"
|
||||
version = "2.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1", features = ["io-util"] }
|
||||
296
rust/crates/remoteingress-protocol/src/lib.rs
Normal file
296
rust/crates/remoteingress-protocol/src/lib.rs
Normal file
@@ -0,0 +1,296 @@
|
||||
use tokio::io::{AsyncRead, AsyncReadExt};
|
||||
|
||||
// Frame type constants
|
||||
pub const FRAME_OPEN: u8 = 0x01;
|
||||
pub const FRAME_DATA: u8 = 0x02;
|
||||
pub const FRAME_CLOSE: u8 = 0x03;
|
||||
pub const FRAME_DATA_BACK: u8 = 0x04;
|
||||
pub const FRAME_CLOSE_BACK: u8 = 0x05;
|
||||
pub const FRAME_CONFIG: u8 = 0x06; // Hub -> Edge: configuration update
|
||||
|
||||
// Frame header size: 4 (stream_id) + 1 (type) + 4 (length) = 9 bytes
|
||||
pub const FRAME_HEADER_SIZE: usize = 9;
|
||||
|
||||
// Maximum payload size (16 MB)
|
||||
pub const MAX_PAYLOAD_SIZE: u32 = 16 * 1024 * 1024;
|
||||
|
||||
/// A single multiplexed frame.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Frame {
|
||||
pub stream_id: u32,
|
||||
pub frame_type: u8,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Encode a frame into bytes: [stream_id:4][type:1][length:4][payload]
|
||||
pub fn encode_frame(stream_id: u32, frame_type: u8, payload: &[u8]) -> Vec<u8> {
|
||||
let len = payload.len() as u32;
|
||||
let mut buf = Vec::with_capacity(FRAME_HEADER_SIZE + payload.len());
|
||||
buf.extend_from_slice(&stream_id.to_be_bytes());
|
||||
buf.push(frame_type);
|
||||
buf.extend_from_slice(&len.to_be_bytes());
|
||||
buf.extend_from_slice(payload);
|
||||
buf
|
||||
}
|
||||
|
||||
/// Build a PROXY protocol v1 header line.
|
||||
/// Format: `PROXY TCP4 <client_ip> <edge_ip> <client_port> <dest_port>\r\n`
|
||||
pub fn build_proxy_v1_header(
|
||||
client_ip: &str,
|
||||
edge_ip: &str,
|
||||
client_port: u16,
|
||||
dest_port: u16,
|
||||
) -> String {
|
||||
format!(
|
||||
"PROXY TCP4 {} {} {} {}\r\n",
|
||||
client_ip, edge_ip, client_port, dest_port
|
||||
)
|
||||
}
|
||||
|
||||
/// Stateful async frame reader that yields `Frame` values from an `AsyncRead`.
|
||||
pub struct FrameReader<R> {
|
||||
reader: R,
|
||||
header_buf: [u8; FRAME_HEADER_SIZE],
|
||||
}
|
||||
|
||||
impl<R: AsyncRead + Unpin> FrameReader<R> {
|
||||
pub fn new(reader: R) -> Self {
|
||||
Self {
|
||||
reader,
|
||||
header_buf: [0u8; FRAME_HEADER_SIZE],
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the next frame. Returns `None` on EOF, `Err` on protocol violation.
|
||||
pub async fn next_frame(&mut self) -> Result<Option<Frame>, std::io::Error> {
|
||||
// Read header
|
||||
match self.reader.read_exact(&mut self.header_buf).await {
|
||||
Ok(_) => {}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(None),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
|
||||
let stream_id = u32::from_be_bytes([
|
||||
self.header_buf[0],
|
||||
self.header_buf[1],
|
||||
self.header_buf[2],
|
||||
self.header_buf[3],
|
||||
]);
|
||||
let frame_type = self.header_buf[4];
|
||||
let length = u32::from_be_bytes([
|
||||
self.header_buf[5],
|
||||
self.header_buf[6],
|
||||
self.header_buf[7],
|
||||
self.header_buf[8],
|
||||
]);
|
||||
|
||||
if length > MAX_PAYLOAD_SIZE {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!("frame payload too large: {} bytes", length),
|
||||
));
|
||||
}
|
||||
|
||||
let mut payload = vec![0u8; length as usize];
|
||||
if length > 0 {
|
||||
self.reader.read_exact(&mut payload).await?;
|
||||
}
|
||||
|
||||
Ok(Some(Frame {
|
||||
stream_id,
|
||||
frame_type,
|
||||
payload,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Consume the reader and return the inner stream.
|
||||
pub fn into_inner(self) -> R {
|
||||
self.reader
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_encode_frame() {
|
||||
let data = b"hello";
|
||||
let encoded = encode_frame(42, FRAME_DATA, data);
|
||||
assert_eq!(encoded.len(), FRAME_HEADER_SIZE + data.len());
|
||||
// stream_id = 42 in BE
|
||||
assert_eq!(&encoded[0..4], &42u32.to_be_bytes());
|
||||
// frame type
|
||||
assert_eq!(encoded[4], FRAME_DATA);
|
||||
// length
|
||||
assert_eq!(&encoded[5..9], &5u32.to_be_bytes());
|
||||
// payload
|
||||
assert_eq!(&encoded[9..], b"hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_empty_frame() {
|
||||
let encoded = encode_frame(1, FRAME_CLOSE, &[]);
|
||||
assert_eq!(encoded.len(), FRAME_HEADER_SIZE);
|
||||
assert_eq!(&encoded[5..9], &0u32.to_be_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_proxy_v1_header() {
|
||||
let header = build_proxy_v1_header("1.2.3.4", "5.6.7.8", 12345, 443);
|
||||
assert_eq!(header, "PROXY TCP4 1.2.3.4 5.6.7.8 12345 443\r\n");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_frame_reader() {
|
||||
let frame1 = encode_frame(1, FRAME_OPEN, b"PROXY TCP4 1.2.3.4 5.6.7.8 1234 443\r\n");
|
||||
let frame2 = encode_frame(1, FRAME_DATA, b"GET / HTTP/1.1\r\n");
|
||||
let frame3 = encode_frame(1, FRAME_CLOSE, &[]);
|
||||
|
||||
let mut data = Vec::new();
|
||||
data.extend_from_slice(&frame1);
|
||||
data.extend_from_slice(&frame2);
|
||||
data.extend_from_slice(&frame3);
|
||||
|
||||
let cursor = std::io::Cursor::new(data);
|
||||
let mut reader = FrameReader::new(cursor);
|
||||
|
||||
let f1 = reader.next_frame().await.unwrap().unwrap();
|
||||
assert_eq!(f1.stream_id, 1);
|
||||
assert_eq!(f1.frame_type, FRAME_OPEN);
|
||||
assert!(f1.payload.starts_with(b"PROXY"));
|
||||
|
||||
let f2 = reader.next_frame().await.unwrap().unwrap();
|
||||
assert_eq!(f2.frame_type, FRAME_DATA);
|
||||
|
||||
let f3 = reader.next_frame().await.unwrap().unwrap();
|
||||
assert_eq!(f3.frame_type, FRAME_CLOSE);
|
||||
assert!(f3.payload.is_empty());
|
||||
|
||||
// EOF
|
||||
assert!(reader.next_frame().await.unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_frame_config_type() {
|
||||
let payload = b"{\"listenPorts\":[443]}";
|
||||
let encoded = encode_frame(0, FRAME_CONFIG, payload);
|
||||
assert_eq!(encoded[4], FRAME_CONFIG);
|
||||
assert_eq!(&encoded[0..4], &0u32.to_be_bytes());
|
||||
assert_eq!(&encoded[9..], payload.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_frame_data_back_type() {
|
||||
let payload = b"response data";
|
||||
let encoded = encode_frame(7, FRAME_DATA_BACK, payload);
|
||||
assert_eq!(encoded[4], FRAME_DATA_BACK);
|
||||
assert_eq!(&encoded[0..4], &7u32.to_be_bytes());
|
||||
assert_eq!(&encoded[5..9], &(payload.len() as u32).to_be_bytes());
|
||||
assert_eq!(&encoded[9..], payload.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_frame_close_back_type() {
|
||||
let encoded = encode_frame(99, FRAME_CLOSE_BACK, &[]);
|
||||
assert_eq!(encoded[4], FRAME_CLOSE_BACK);
|
||||
assert_eq!(&encoded[0..4], &99u32.to_be_bytes());
|
||||
assert_eq!(&encoded[5..9], &0u32.to_be_bytes());
|
||||
assert_eq!(encoded.len(), FRAME_HEADER_SIZE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_frame_large_stream_id() {
|
||||
let encoded = encode_frame(u32::MAX, FRAME_DATA, b"x");
|
||||
assert_eq!(&encoded[0..4], &u32::MAX.to_be_bytes());
|
||||
assert_eq!(encoded[4], FRAME_DATA);
|
||||
assert_eq!(&encoded[5..9], &1u32.to_be_bytes());
|
||||
assert_eq!(encoded[9], b'x');
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_frame_reader_max_payload_rejection() {
|
||||
let mut data = Vec::new();
|
||||
data.extend_from_slice(&1u32.to_be_bytes());
|
||||
data.push(FRAME_DATA);
|
||||
data.extend_from_slice(&(MAX_PAYLOAD_SIZE + 1).to_be_bytes());
|
||||
|
||||
let cursor = std::io::Cursor::new(data);
|
||||
let mut reader = FrameReader::new(cursor);
|
||||
|
||||
let result = reader.next_frame().await;
|
||||
assert!(result.is_err());
|
||||
let err = result.unwrap_err();
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_frame_reader_eof_mid_header() {
|
||||
// Only 5 bytes — not enough for a 9-byte header
|
||||
let data = vec![0u8; 5];
|
||||
let cursor = std::io::Cursor::new(data);
|
||||
let mut reader = FrameReader::new(cursor);
|
||||
|
||||
// Should return Ok(None) on partial header EOF
|
||||
let result = reader.next_frame().await;
|
||||
assert!(result.unwrap().is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_frame_reader_eof_mid_payload() {
|
||||
// Full header claiming 100 bytes of payload, but only 10 bytes present
|
||||
let mut data = Vec::new();
|
||||
data.extend_from_slice(&1u32.to_be_bytes());
|
||||
data.push(FRAME_DATA);
|
||||
data.extend_from_slice(&100u32.to_be_bytes());
|
||||
data.extend_from_slice(&[0xAB; 10]);
|
||||
|
||||
let cursor = std::io::Cursor::new(data);
|
||||
let mut reader = FrameReader::new(cursor);
|
||||
|
||||
let result = reader.next_frame().await;
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_frame_reader_all_frame_types() {
|
||||
let types = [
|
||||
FRAME_OPEN,
|
||||
FRAME_DATA,
|
||||
FRAME_CLOSE,
|
||||
FRAME_DATA_BACK,
|
||||
FRAME_CLOSE_BACK,
|
||||
FRAME_CONFIG,
|
||||
];
|
||||
|
||||
let mut data = Vec::new();
|
||||
for (i, &ft) in types.iter().enumerate() {
|
||||
let payload = format!("payload_{}", i);
|
||||
data.extend_from_slice(&encode_frame(i as u32, ft, payload.as_bytes()));
|
||||
}
|
||||
|
||||
let cursor = std::io::Cursor::new(data);
|
||||
let mut reader = FrameReader::new(cursor);
|
||||
|
||||
for (i, &ft) in types.iter().enumerate() {
|
||||
let frame = reader.next_frame().await.unwrap().unwrap();
|
||||
assert_eq!(frame.stream_id, i as u32);
|
||||
assert_eq!(frame.frame_type, ft);
|
||||
assert_eq!(frame.payload, format!("payload_{}", i).as_bytes());
|
||||
}
|
||||
|
||||
assert!(reader.next_frame().await.unwrap().is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_frame_reader_zero_length_payload() {
|
||||
let data = encode_frame(42, FRAME_CLOSE, &[]);
|
||||
let cursor = std::io::Cursor::new(data);
|
||||
let mut reader = FrameReader::new(cursor);
|
||||
|
||||
let frame = reader.next_frame().await.unwrap().unwrap();
|
||||
assert_eq!(frame.stream_id, 42);
|
||||
assert_eq!(frame.frame_type, FRAME_CLOSE);
|
||||
assert!(frame.payload.is_empty());
|
||||
}
|
||||
}
|
||||
35
test/test.classes.node.ts
Normal file
35
test/test.classes.node.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import { EventEmitter } from 'events';
|
||||
import { RemoteIngressHub, RemoteIngressEdge } from '../ts/index.js';
|
||||
|
||||
tap.test('RemoteIngressHub constructor does not throw', async () => {
|
||||
const hub = new RemoteIngressHub();
|
||||
expect(hub).toBeTruthy();
|
||||
});
|
||||
|
||||
tap.test('RemoteIngressHub is instanceof EventEmitter', async () => {
|
||||
const hub = new RemoteIngressHub();
|
||||
expect(hub).toBeInstanceOf(EventEmitter);
|
||||
});
|
||||
|
||||
tap.test('RemoteIngressHub.running is false before start', async () => {
|
||||
const hub = new RemoteIngressHub();
|
||||
expect(hub.running).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('RemoteIngressEdge constructor does not throw', async () => {
|
||||
const edge = new RemoteIngressEdge();
|
||||
expect(edge).toBeTruthy();
|
||||
});
|
||||
|
||||
tap.test('RemoteIngressEdge is instanceof EventEmitter', async () => {
|
||||
const edge = new RemoteIngressEdge();
|
||||
expect(edge).toBeInstanceOf(EventEmitter);
|
||||
});
|
||||
|
||||
tap.test('RemoteIngressEdge.running is false before start', async () => {
|
||||
const edge = new RemoteIngressEdge();
|
||||
expect(edge.running).toBeFalse();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
152
test/test.token.node.ts
Normal file
152
test/test.token.node.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import { encodeConnectionToken, decodeConnectionToken, type IConnectionTokenData } from '../ts/classes.token.js';
|
||||
|
||||
tap.test('token roundtrip with unicode chars in secret', async () => {
|
||||
const data: IConnectionTokenData = {
|
||||
hubHost: 'hub.example.com',
|
||||
hubPort: 8443,
|
||||
edgeId: 'edge-1',
|
||||
secret: 'sécret-with-ünïcödé-日本語',
|
||||
};
|
||||
const token = encodeConnectionToken(data);
|
||||
const decoded = decodeConnectionToken(token);
|
||||
expect(decoded.secret).toEqual(data.secret);
|
||||
});
|
||||
|
||||
tap.test('token roundtrip with empty edgeId', async () => {
|
||||
const data: IConnectionTokenData = {
|
||||
hubHost: 'hub.test',
|
||||
hubPort: 443,
|
||||
edgeId: '',
|
||||
secret: 'key',
|
||||
};
|
||||
const token = encodeConnectionToken(data);
|
||||
const decoded = decodeConnectionToken(token);
|
||||
expect(decoded.edgeId).toEqual('');
|
||||
});
|
||||
|
||||
tap.test('token roundtrip with port 0', async () => {
|
||||
const data: IConnectionTokenData = {
|
||||
hubHost: 'h',
|
||||
hubPort: 0,
|
||||
edgeId: 'e',
|
||||
secret: 's',
|
||||
};
|
||||
const token = encodeConnectionToken(data);
|
||||
const decoded = decodeConnectionToken(token);
|
||||
expect(decoded.hubPort).toEqual(0);
|
||||
});
|
||||
|
||||
tap.test('token roundtrip with port 65535', async () => {
|
||||
const data: IConnectionTokenData = {
|
||||
hubHost: 'h',
|
||||
hubPort: 65535,
|
||||
edgeId: 'e',
|
||||
secret: 's',
|
||||
};
|
||||
const token = encodeConnectionToken(data);
|
||||
const decoded = decodeConnectionToken(token);
|
||||
expect(decoded.hubPort).toEqual(65535);
|
||||
});
|
||||
|
||||
tap.test('token roundtrip with very long secret (10k chars)', async () => {
|
||||
const longSecret = 'x'.repeat(10000);
|
||||
const data: IConnectionTokenData = {
|
||||
hubHost: 'host',
|
||||
hubPort: 1234,
|
||||
edgeId: 'edge',
|
||||
secret: longSecret,
|
||||
};
|
||||
const token = encodeConnectionToken(data);
|
||||
const decoded = decodeConnectionToken(token);
|
||||
expect(decoded.secret).toEqual(longSecret);
|
||||
expect(decoded.secret.length).toEqual(10000);
|
||||
});
|
||||
|
||||
tap.test('token string is URL-safe', async () => {
|
||||
const data: IConnectionTokenData = {
|
||||
hubHost: 'hub.example.com',
|
||||
hubPort: 8443,
|
||||
edgeId: 'edge-001',
|
||||
secret: 'super+secret/key==with+special/chars',
|
||||
};
|
||||
const token = encodeConnectionToken(data);
|
||||
expect(token).toMatch(/^[A-Za-z0-9_-]+$/);
|
||||
});
|
||||
|
||||
tap.test('decode empty string throws', async () => {
|
||||
let error: Error | undefined;
|
||||
try {
|
||||
decodeConnectionToken('');
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
});
|
||||
|
||||
tap.test('decode valid base64 but wrong JSON shape throws missing required fields', async () => {
|
||||
// Encode { "a": 1, "b": 2 } — valid JSON but wrong shape
|
||||
const token = Buffer.from(JSON.stringify({ a: 1, b: 2 }), 'utf-8')
|
||||
.toString('base64')
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=+$/, '');
|
||||
|
||||
let error: Error | undefined;
|
||||
try {
|
||||
decodeConnectionToken(token);
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(error!.message).toInclude('missing required fields');
|
||||
});
|
||||
|
||||
tap.test('decode valid JSON but wrong field types throws missing required fields', async () => {
|
||||
// h is number instead of string, p is string instead of number
|
||||
const token = Buffer.from(JSON.stringify({ h: 123, p: 'notnum', e: 'e', s: 's' }), 'utf-8')
|
||||
.toString('base64')
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=+$/, '');
|
||||
|
||||
let error: Error | undefined;
|
||||
try {
|
||||
decodeConnectionToken(token);
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(error!.message).toInclude('missing required fields');
|
||||
});
|
||||
|
||||
tap.test('decode with extra fields succeeds', async () => {
|
||||
const token = Buffer.from(
|
||||
JSON.stringify({ h: 'host', p: 443, e: 'edge', s: 'secret', extra: 'ignored' }),
|
||||
'utf-8',
|
||||
)
|
||||
.toString('base64')
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=+$/, '');
|
||||
|
||||
const decoded = decodeConnectionToken(token);
|
||||
expect(decoded.hubHost).toEqual('host');
|
||||
expect(decoded.hubPort).toEqual(443);
|
||||
expect(decoded.edgeId).toEqual('edge');
|
||||
expect(decoded.secret).toEqual('secret');
|
||||
});
|
||||
|
||||
tap.test('encode is deterministic', async () => {
|
||||
const data: IConnectionTokenData = {
|
||||
hubHost: 'hub.test',
|
||||
hubPort: 8443,
|
||||
edgeId: 'edge-1',
|
||||
secret: 'deterministic-key',
|
||||
};
|
||||
const token1 = encodeConnectionToken(data);
|
||||
const token2 = encodeConnectionToken(data);
|
||||
expect(token1).toEqual(token2);
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
65
test/test.ts
65
test/test.ts
@@ -1,8 +1,61 @@
|
||||
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
||||
import * as remoteingress from '../ts/index.js'
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as remoteingress from '../ts/index.js';
|
||||
|
||||
tap.test('first test', async () => {
|
||||
console.log(remoteingress)
|
||||
})
|
||||
tap.test('should export RemoteIngressHub', async () => {
|
||||
expect(remoteingress.RemoteIngressHub).toBeTypeOf('function');
|
||||
});
|
||||
|
||||
tap.start()
|
||||
tap.test('should export RemoteIngressEdge', async () => {
|
||||
expect(remoteingress.RemoteIngressEdge).toBeTypeOf('function');
|
||||
});
|
||||
|
||||
tap.test('should export encodeConnectionToken and decodeConnectionToken', async () => {
|
||||
expect(remoteingress.encodeConnectionToken).toBeTypeOf('function');
|
||||
expect(remoteingress.decodeConnectionToken).toBeTypeOf('function');
|
||||
});
|
||||
|
||||
tap.test('should roundtrip encode → decode a connection token', async () => {
|
||||
const data: remoteingress.IConnectionTokenData = {
|
||||
hubHost: 'hub.example.com',
|
||||
hubPort: 8443,
|
||||
edgeId: 'edge-001',
|
||||
secret: 'super-secret-key',
|
||||
};
|
||||
const token = remoteingress.encodeConnectionToken(data);
|
||||
const decoded = remoteingress.decodeConnectionToken(token);
|
||||
expect(decoded.hubHost).toEqual(data.hubHost);
|
||||
expect(decoded.hubPort).toEqual(data.hubPort);
|
||||
expect(decoded.edgeId).toEqual(data.edgeId);
|
||||
expect(decoded.secret).toEqual(data.secret);
|
||||
});
|
||||
|
||||
tap.test('should throw on malformed token', async () => {
|
||||
let error: Error | undefined;
|
||||
try {
|
||||
remoteingress.decodeConnectionToken('not-valid-json!!!');
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(error!.message).toInclude('Invalid connection token');
|
||||
});
|
||||
|
||||
tap.test('should throw on token with missing fields', async () => {
|
||||
// Encode a partial object (missing 'p' and 's')
|
||||
const partial = Buffer.from(JSON.stringify({ h: 'host', e: 'edge' }), 'utf-8')
|
||||
.toString('base64')
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=+$/, '');
|
||||
|
||||
let error: Error | undefined;
|
||||
try {
|
||||
remoteingress.decodeConnectionToken(partial);
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(error!.message).toInclude('missing required fields');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@serve.zone/remoteingress',
|
||||
version: '1.0.2',
|
||||
description: 'a remoteingress service for serve.zone'
|
||||
version: '3.3.0',
|
||||
description: 'Edge ingress tunnel for DcRouter - accepts incoming TCP connections at network edge and tunnels them to DcRouter SmartProxy preserving client IP via PROXY protocol v1.'
|
||||
}
|
||||
|
||||
173
ts/classes.remoteingressedge.ts
Normal file
173
ts/classes.remoteingressedge.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import { EventEmitter } from 'events';
|
||||
import { decodeConnectionToken } from './classes.token.js';
|
||||
|
||||
// Command map for the edge side of remoteingress-bin
|
||||
type TEdgeCommands = {
|
||||
ping: {
|
||||
params: Record<string, never>;
|
||||
result: { pong: boolean };
|
||||
};
|
||||
startEdge: {
|
||||
params: {
|
||||
hubHost: string;
|
||||
hubPort: number;
|
||||
edgeId: string;
|
||||
secret: string;
|
||||
};
|
||||
result: { started: boolean };
|
||||
};
|
||||
stopEdge: {
|
||||
params: Record<string, never>;
|
||||
result: { stopped: boolean; wasRunning?: boolean };
|
||||
};
|
||||
getEdgeStatus: {
|
||||
params: Record<string, never>;
|
||||
result: {
|
||||
running: boolean;
|
||||
connected: boolean;
|
||||
publicIp: string | null;
|
||||
activeStreams: number;
|
||||
listenPorts: number[];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export interface IEdgeConfig {
|
||||
hubHost: string;
|
||||
hubPort?: number;
|
||||
edgeId: string;
|
||||
secret: string;
|
||||
}
|
||||
|
||||
export class RemoteIngressEdge extends EventEmitter {
|
||||
private bridge: InstanceType<typeof plugins.smartrust.RustBridge<TEdgeCommands>>;
|
||||
private started = false;
|
||||
private statusInterval: ReturnType<typeof setInterval> | undefined;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
const packageDir = plugins.path.resolve(
|
||||
plugins.path.dirname(new URL(import.meta.url).pathname),
|
||||
'..',
|
||||
);
|
||||
|
||||
this.bridge = new plugins.smartrust.RustBridge<TEdgeCommands>({
|
||||
binaryName: 'remoteingress-bin',
|
||||
cliArgs: ['--management'],
|
||||
requestTimeoutMs: 30_000,
|
||||
readyTimeoutMs: 10_000,
|
||||
localPaths: [
|
||||
// Platform-suffixed binary in dist_rust (production)
|
||||
plugins.path.join(packageDir, 'dist_rust', `remoteingress-bin_${process.platform === 'win32' ? 'windows' : 'linux'}_${process.arch === 'x64' ? 'amd64' : process.arch}`),
|
||||
// Exact binaryName fallback in dist_rust
|
||||
plugins.path.join(packageDir, 'dist_rust', 'remoteingress-bin'),
|
||||
// Development build paths (cargo output uses exact name)
|
||||
plugins.path.join(packageDir, 'rust', 'target', 'release', 'remoteingress-bin'),
|
||||
plugins.path.join(packageDir, 'rust', 'target', 'debug', 'remoteingress-bin'),
|
||||
],
|
||||
searchSystemPath: false,
|
||||
});
|
||||
|
||||
// Forward events from Rust binary
|
||||
this.bridge.on('management:tunnelConnected', () => {
|
||||
this.emit('tunnelConnected');
|
||||
});
|
||||
this.bridge.on('management:tunnelDisconnected', () => {
|
||||
this.emit('tunnelDisconnected');
|
||||
});
|
||||
this.bridge.on('management:publicIpDiscovered', (data: { ip: string }) => {
|
||||
this.emit('publicIpDiscovered', data);
|
||||
});
|
||||
this.bridge.on('management:portsAssigned', (data: { listenPorts: number[] }) => {
|
||||
console.log(`[RemoteIngressEdge] Ports assigned by hub: ${data.listenPorts.join(', ')}`);
|
||||
this.emit('portsAssigned', data);
|
||||
});
|
||||
this.bridge.on('management:portsUpdated', (data: { listenPorts: number[] }) => {
|
||||
console.log(`[RemoteIngressEdge] Ports updated by hub: ${data.listenPorts.join(', ')}`);
|
||||
this.emit('portsUpdated', data);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the edge — spawns the Rust binary and connects to the hub.
|
||||
* Accepts either a connection token or an explicit IEdgeConfig.
|
||||
*/
|
||||
public async start(config: { token: string } | IEdgeConfig): Promise<void> {
|
||||
let edgeConfig: IEdgeConfig;
|
||||
|
||||
if ('token' in config) {
|
||||
const decoded = decodeConnectionToken(config.token);
|
||||
edgeConfig = {
|
||||
hubHost: decoded.hubHost,
|
||||
hubPort: decoded.hubPort,
|
||||
edgeId: decoded.edgeId,
|
||||
secret: decoded.secret,
|
||||
};
|
||||
} else {
|
||||
edgeConfig = config;
|
||||
}
|
||||
|
||||
const spawned = await this.bridge.spawn();
|
||||
if (!spawned) {
|
||||
throw new Error('Failed to spawn remoteingress-bin');
|
||||
}
|
||||
|
||||
await this.bridge.sendCommand('startEdge', {
|
||||
hubHost: edgeConfig.hubHost,
|
||||
hubPort: edgeConfig.hubPort ?? 8443,
|
||||
edgeId: edgeConfig.edgeId,
|
||||
secret: edgeConfig.secret,
|
||||
});
|
||||
|
||||
this.started = true;
|
||||
|
||||
// Start periodic status logging
|
||||
this.statusInterval = setInterval(async () => {
|
||||
try {
|
||||
const status = await this.getStatus();
|
||||
console.log(
|
||||
`[RemoteIngressEdge] Status: connected=${status.connected}, ` +
|
||||
`streams=${status.activeStreams}, ports=[${status.listenPorts.join(',')}], ` +
|
||||
`publicIp=${status.publicIp ?? 'unknown'}`
|
||||
);
|
||||
} catch {
|
||||
// Bridge may be shutting down
|
||||
}
|
||||
}, 60_000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the edge and kill the Rust process.
|
||||
*/
|
||||
public async stop(): Promise<void> {
|
||||
if (this.statusInterval) {
|
||||
clearInterval(this.statusInterval);
|
||||
this.statusInterval = undefined;
|
||||
}
|
||||
if (this.started) {
|
||||
try {
|
||||
await this.bridge.sendCommand('stopEdge', {} as Record<string, never>);
|
||||
} catch {
|
||||
// Process may already be dead
|
||||
}
|
||||
this.bridge.kill();
|
||||
this.started = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current edge status.
|
||||
*/
|
||||
public async getStatus() {
|
||||
return this.bridge.sendCommand('getEdgeStatus', {} as Record<string, never>);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the bridge is running.
|
||||
*/
|
||||
public get running(): boolean {
|
||||
return this.bridge.running;
|
||||
}
|
||||
}
|
||||
142
ts/classes.remoteingresshub.ts
Normal file
142
ts/classes.remoteingresshub.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
// Command map for the hub side of remoteingress-bin
|
||||
type THubCommands = {
|
||||
ping: {
|
||||
params: Record<string, never>;
|
||||
result: { pong: boolean };
|
||||
};
|
||||
startHub: {
|
||||
params: {
|
||||
tunnelPort: number;
|
||||
targetHost?: string;
|
||||
};
|
||||
result: { started: boolean };
|
||||
};
|
||||
stopHub: {
|
||||
params: Record<string, never>;
|
||||
result: { stopped: boolean; wasRunning?: boolean };
|
||||
};
|
||||
updateAllowedEdges: {
|
||||
params: {
|
||||
edges: Array<{ id: string; secret: string; listenPorts?: number[]; stunIntervalSecs?: number }>;
|
||||
};
|
||||
result: { updated: boolean };
|
||||
};
|
||||
getHubStatus: {
|
||||
params: Record<string, never>;
|
||||
result: {
|
||||
running: boolean;
|
||||
tunnelPort: number;
|
||||
connectedEdges: Array<{
|
||||
edgeId: string;
|
||||
connectedAt: number;
|
||||
activeStreams: number;
|
||||
}>;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export interface IHubConfig {
|
||||
tunnelPort?: number;
|
||||
targetHost?: string;
|
||||
}
|
||||
|
||||
export class RemoteIngressHub extends EventEmitter {
|
||||
private bridge: InstanceType<typeof plugins.smartrust.RustBridge<THubCommands>>;
|
||||
private started = false;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
const packageDir = plugins.path.resolve(
|
||||
plugins.path.dirname(new URL(import.meta.url).pathname),
|
||||
'..',
|
||||
);
|
||||
|
||||
this.bridge = new plugins.smartrust.RustBridge<THubCommands>({
|
||||
binaryName: 'remoteingress-bin',
|
||||
cliArgs: ['--management'],
|
||||
requestTimeoutMs: 30_000,
|
||||
readyTimeoutMs: 10_000,
|
||||
localPaths: [
|
||||
// Platform-suffixed binary in dist_rust (production)
|
||||
plugins.path.join(packageDir, 'dist_rust', `remoteingress-bin_${process.platform === 'win32' ? 'windows' : 'linux'}_${process.arch === 'x64' ? 'amd64' : process.arch}`),
|
||||
// Exact binaryName fallback in dist_rust
|
||||
plugins.path.join(packageDir, 'dist_rust', 'remoteingress-bin'),
|
||||
// Development build paths (cargo output uses exact name)
|
||||
plugins.path.join(packageDir, 'rust', 'target', 'release', 'remoteingress-bin'),
|
||||
plugins.path.join(packageDir, 'rust', 'target', 'debug', 'remoteingress-bin'),
|
||||
],
|
||||
searchSystemPath: false,
|
||||
});
|
||||
|
||||
// Forward events from Rust binary
|
||||
this.bridge.on('management:edgeConnected', (data: { edgeId: string }) => {
|
||||
this.emit('edgeConnected', data);
|
||||
});
|
||||
this.bridge.on('management:edgeDisconnected', (data: { edgeId: string }) => {
|
||||
this.emit('edgeDisconnected', data);
|
||||
});
|
||||
this.bridge.on('management:streamOpened', (data: { edgeId: string; streamId: number }) => {
|
||||
this.emit('streamOpened', data);
|
||||
});
|
||||
this.bridge.on('management:streamClosed', (data: { edgeId: string; streamId: number }) => {
|
||||
this.emit('streamClosed', data);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the hub — spawns the Rust binary and starts the tunnel server.
|
||||
*/
|
||||
public async start(config: IHubConfig = {}): Promise<void> {
|
||||
const spawned = await this.bridge.spawn();
|
||||
if (!spawned) {
|
||||
throw new Error('Failed to spawn remoteingress-bin');
|
||||
}
|
||||
|
||||
await this.bridge.sendCommand('startHub', {
|
||||
tunnelPort: config.tunnelPort ?? 8443,
|
||||
targetHost: config.targetHost ?? '127.0.0.1',
|
||||
});
|
||||
|
||||
this.started = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the hub and kill the Rust process.
|
||||
*/
|
||||
public async stop(): Promise<void> {
|
||||
if (this.started) {
|
||||
try {
|
||||
await this.bridge.sendCommand('stopHub', {} as Record<string, never>);
|
||||
} catch {
|
||||
// Process may already be dead
|
||||
}
|
||||
this.bridge.kill();
|
||||
this.started = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the list of allowed edges that can connect to this hub.
|
||||
*/
|
||||
public async updateAllowedEdges(edges: Array<{ id: string; secret: string; listenPorts?: number[]; stunIntervalSecs?: number }>): Promise<void> {
|
||||
await this.bridge.sendCommand('updateAllowedEdges', { edges });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current hub status.
|
||||
*/
|
||||
public async getStatus() {
|
||||
return this.bridge.sendCommand('getHubStatus', {} as Record<string, never>);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the bridge is running.
|
||||
*/
|
||||
public get running(): boolean {
|
||||
return this.bridge.running;
|
||||
}
|
||||
}
|
||||
66
ts/classes.token.ts
Normal file
66
ts/classes.token.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* Connection token utilities for RemoteIngress edge connections.
|
||||
* A token is a base64url-encoded compact JSON object carrying hub connection details.
|
||||
*/
|
||||
|
||||
export interface IConnectionTokenData {
|
||||
hubHost: string;
|
||||
hubPort: number;
|
||||
edgeId: string;
|
||||
secret: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode connection data into a single opaque token string (base64url).
|
||||
*/
|
||||
export function encodeConnectionToken(data: IConnectionTokenData): string {
|
||||
const compact = JSON.stringify({
|
||||
h: data.hubHost,
|
||||
p: data.hubPort,
|
||||
e: data.edgeId,
|
||||
s: data.secret,
|
||||
});
|
||||
// base64url: standard base64 with + → -, / → _, trailing = stripped
|
||||
return Buffer.from(compact, 'utf-8')
|
||||
.toString('base64')
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=+$/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a connection token back into its constituent fields.
|
||||
* Throws on malformed or incomplete tokens.
|
||||
*/
|
||||
export function decodeConnectionToken(token: string): IConnectionTokenData {
|
||||
let parsed: { h?: unknown; p?: unknown; e?: unknown; s?: unknown };
|
||||
try {
|
||||
// Restore standard base64 from base64url
|
||||
let base64 = token.replace(/-/g, '+').replace(/_/g, '/');
|
||||
// Re-add padding
|
||||
const remainder = base64.length % 4;
|
||||
if (remainder === 2) base64 += '==';
|
||||
else if (remainder === 3) base64 += '=';
|
||||
|
||||
const json = Buffer.from(base64, 'base64').toString('utf-8');
|
||||
parsed = JSON.parse(json);
|
||||
} catch {
|
||||
throw new Error('Invalid connection token');
|
||||
}
|
||||
|
||||
if (
|
||||
typeof parsed.h !== 'string' ||
|
||||
typeof parsed.p !== 'number' ||
|
||||
typeof parsed.e !== 'string' ||
|
||||
typeof parsed.s !== 'string'
|
||||
) {
|
||||
throw new Error('Invalid connection token: missing required fields');
|
||||
}
|
||||
|
||||
return {
|
||||
hubHost: parsed.h,
|
||||
hubPort: parsed.p,
|
||||
edgeId: parsed.e,
|
||||
secret: parsed.s,
|
||||
};
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import * as tls from 'tls';
|
||||
|
||||
export class ConnectorPrivate {
|
||||
private targetHost: string;
|
||||
private targetPort: number;
|
||||
|
||||
constructor(targetHost: string, targetPort: number) {
|
||||
this.targetHost = targetHost;
|
||||
this.targetPort = targetPort;
|
||||
this.connectToPublicRemoteConnector();
|
||||
}
|
||||
|
||||
private connectToPublicRemoteConnector(): void {
|
||||
const options = {
|
||||
// If your server requires client certificate authentication, you can specify key and cert here as well
|
||||
};
|
||||
|
||||
const tunnel = tls.connect({ port: 4000, ...options }, () => {
|
||||
console.log('Connected to PublicRemoteConnector on port 4000');
|
||||
});
|
||||
|
||||
tunnel.on('data', (data: Buffer) => {
|
||||
// Similar logic for forwarding data to and from the target
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import * as tls from 'tls';
|
||||
import * as fs from 'fs';
|
||||
|
||||
export class ConnectorPublic {
|
||||
private tunnel: tls.TLSSocket | null = null;
|
||||
|
||||
constructor() {
|
||||
this.createTunnel();
|
||||
this.listenOnPorts();
|
||||
}
|
||||
|
||||
private createTunnel(): void {
|
||||
const options = {
|
||||
key: fs.readFileSync('path/to/key.pem'),
|
||||
cert: fs.readFileSync('path/to/cert.pem')
|
||||
};
|
||||
|
||||
const server = tls.createServer(options, (socket: tls.TLSSocket) => {
|
||||
this.tunnel = socket;
|
||||
console.log('Tunnel established with LocalConnector');
|
||||
});
|
||||
|
||||
server.listen(4000, () => {
|
||||
console.log('PublicRemoteConnector listening for tunnel on port 4000');
|
||||
});
|
||||
}
|
||||
|
||||
private listenOnPorts(): void {
|
||||
// Implement similar logic for listening on ports 80 and 443
|
||||
// Keep in mind you may need to adjust how you handle secure and non-secure traffic
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
import * as plugins from './remoteingress.plugins.js';
|
||||
|
||||
export let demoExport = 'Hi there! :) This is an exported string';
|
||||
export * from './classes.remoteingresshub.js';
|
||||
export * from './classes.remoteingressedge.js';
|
||||
export * from './classes.token.js';
|
||||
|
||||
7
ts/plugins.ts
Normal file
7
ts/plugins.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
// node native scope
|
||||
import * as path from 'path';
|
||||
export { path };
|
||||
|
||||
// @push.rocks scope
|
||||
import * as smartrust from '@push.rocks/smartrust';
|
||||
export { smartrust };
|
||||
@@ -1,4 +0,0 @@
|
||||
const removeme = {};
|
||||
export {
|
||||
removeme
|
||||
}
|
||||
Reference in New Issue
Block a user