Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ serde = { workspace = true }
serde_json = { workspace = true }
libc = "0.2"


# The CONNECT-UDP bins (#6109) ride on the serve-gated modules;
# required-features keeps the connect-only variant (§2's
# mechanical-incapability build) from trying to build them.
[[bin]]
name = "fleet-connect-udp"
path = "src/bin/fleet-connect-udp.rs"
required-features = ["serve"]

[[bin]]
name = "fleet-udp-forward"
path = "src/bin/fleet-udp-forward.rs"
required-features = ["serve"]

[features]
# The broker half. The connect variant compiles this crate without
# it: a dropped-on-a-box artifact carries the client and nothing
Expand Down
42 changes: 42 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/src/bin/fleet-connect-udp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! fleet-connect-udp — the standalone CONNECT-UDP endpoint
//! (RFC 9298, #6109), for hosts where the session broker
//! (`fleet proxy serve`) doesn't run: the laptop serves its
//! fleet roles as `fleet serve <role>` units, so the endpoint
//! gets its own handler process there.
//!
//! Dark by construction, twice over: it refuses to start
//! unless the fleet config it loads opts in with
//! `[proxy] connect_udp = true` (fleet-config), and the
//! endpoint binds whatever `connect_udp_listen` says — the
//! default, and the only sanctioned posture pre-cutover, is
//! loopback. Which config loads is fleet-config's own
//! resolution ($FLEET_CONFIG, else ~/.config/fleet/fleet.toml),
//! so a host can stage this endpoint behind a dedicated config
//! file without touching the shared one.

use std::process::ExitCode;

fn main() -> ExitCode {
let config = match fleet_config::Config::load() {
Ok(c) => c,
Err(e) => {
eprintln!("fleet-connect-udp: cannot load fleet config: {e}");
return ExitCode::FAILURE;
}
};
if !config.connect_udp_enabled() {
eprintln!(
"fleet-connect-udp: [proxy] connect_udp is not enabled in the loaded \
fleet config — the endpoint stays dark. Enable it deliberately, \
loopback only, in the config this process loads."
);
return ExitCode::from(fleet_proxy::EXIT_UNAVAILABLE as u8);
}
match fleet_proxy::connect_udp::run(&config) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("fleet-connect-udp: {e}");
ExitCode::FAILURE
}
}
}
117 changes: 117 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/src/bin/fleet-udp-forward.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! fleet-udp-forward — the CONNECT-UDP client shim (#6109):
//! local UDP in, CONNECT-UDP sessions out, replies back. The
//! piece a real consumer (Moonlight) needs in front of it,
//! since it speaks plain UDP and nothing else.
//!
//! Two ways to name the forwards:
//!
//! * `--target <host> --ports <set>` — port-preserving over a
//! set (`47998-48010` is the Moonlight shape): local port P
//! forwards to <host>:P.
//! * `--map <lport>=<host>:<rport>` (repeatable) — explicit
//! one-off mappings.
//!
//! Everything binds `--bind` (default 127.0.0.1) and talks to
//! the CONNECT-UDP endpoint at `--proxy` (default
//! 127.0.0.1:3131). Config-gated in the only way a shim can
//! be: nothing runs it unless a host deliberately starts it.

use std::net::{IpAddr, SocketAddr};
use std::process::ExitCode;

use fleet_proxy::EXIT_USAGE;
use fleet_proxy::udp_forward::{Forward, parse_map, parse_port_set};

const DEFAULT_PROXY: &str = "127.0.0.1:3131";
const USAGE: &str = "usage: fleet-udp-forward [--proxy <addr>] [--bind <ip>] \
(--map <lport>=<host>:<rport>... | --target <host> --ports <set>)";

fn fail(msg: &str) -> ExitCode {
eprintln!("fleet-udp-forward: {msg}");
eprintln!("{USAGE}");
ExitCode::from(EXIT_USAGE as u8)
}

fn take(args: &[String], i: &mut usize, flag: &str) -> Result<String, ExitCode> {
*i += 1;
args.get(*i)
.cloned()
.ok_or_else(|| fail(&format!("{flag} needs a value")))
}

fn main() -> ExitCode {
let argv: Vec<String> = std::env::args().skip(1).collect();
let mut proxy = DEFAULT_PROXY.to_string();
let mut bind: IpAddr = IpAddr::from([127, 0, 0, 1]);
let mut target: Option<String> = None;
let mut ports: Option<String> = None;
let mut maps: Vec<String> = Vec::new();
let mut i = 0;
while i < argv.len() {
match argv[i].as_str() {
"--proxy" => match take(&argv, &mut i, "--proxy") {
Ok(v) => proxy = v,
Err(c) => return c,
},
"--bind" => match take(&argv, &mut i, "--bind") {
Ok(v) => match v.parse() {
Ok(ip) => bind = ip,
Err(_) => return fail(&format!("--bind {v:?} is not an IP address")),
},
Err(c) => return c,
},
"--target" => match take(&argv, &mut i, "--target") {
Ok(v) => target = Some(v),
Err(c) => return c,
},
"--ports" => match take(&argv, &mut i, "--ports") {
Ok(v) => ports = Some(v),
Err(c) => return c,
},
"--map" => match take(&argv, &mut i, "--map") {
Ok(v) => maps.push(v),
Err(c) => return c,
},
"-h" | "--help" => {
println!("{USAGE}");
return ExitCode::SUCCESS;
}
other => return fail(&format!("unknown argument {other:?}")),
}
i += 1;
}

let mut forwards: Vec<Forward> = Vec::new();
for m in &maps {
match parse_map(m, bind) {
Ok(f) => forwards.push(f),
Err(e) => return fail(&e),
}
}
match (target, ports) {
(Some(host), Some(spec)) => match parse_port_set(&spec) {
Ok(ps) => {
for p in ps {
forwards.push(Forward {
listen: SocketAddr::new(bind, p),
target_host: host.clone(),
target_port: p,
});
}
}
Err(e) => return fail(&e),
},
(None, None) => {}
_ => return fail("--target and --ports must be given together"),
}
if forwards.is_empty() {
return fail("nothing to forward: give --map and/or --target + --ports");
}
match fleet_proxy::udp_forward::run_forwards(&proxy, &forwards) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("fleet-udp-forward: {e}");
ExitCode::FAILURE
}
}
}
7 changes: 7 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/src/connect_udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
//! * A session with no datagrams in either direction for
//! [`RELAY_IDLE_TIMEOUT`] closes.
//!
//! The follow-up surface beside this module: `udp_forward`
//! (the client shim wrapping plain UDP into these sessions)
//! and two small bins — `fleet-connect-udp`, the standalone
//! endpoint for hosts where the broker doesn't run (the
//! laptop's dark deployment), and `fleet-udp-forward`, the
//! shim. Neither bin is wired into any service by the code.
//!
//! There is deliberately **no target allowlist**: the listener
//! binds loopback by default and is off unless configured, the
//! same trust posture as the broker itself (a local capability,
Expand Down
3 changes: 3 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub mod serve;
#[cfg(feature = "serve")]
pub mod connect_udp;

#[cfg(feature = "serve")]
pub mod udp_forward;

/// Exit code for "the transport is down" — repo-queue's established
/// 3 (§3's collision table: 3 means repair the transport, 69 means
/// wrong build). The proxy client surface honors the same split.
Expand Down
Loading