1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! CLI arguments for `rho daemon`.
use std::{net::SocketAddr, time::Duration};
use clap::Subcommand;
use crate::app::automation_protocol::parse_duration;
/// The shortest `rho daemon run --unload-idle-after`. Shorter, a session
/// could unload between one client's consecutive requests, as a reopen
/// takes up to 41 ms measured (`UNLOAD_IDLE_AFTER` has the receipt), and
/// the sweep, which runs once per period, would spin.
const MIN_UNLOAD_IDLE_AFTER: Duration = Duration::from_millis(100);
fn parse_unload_idle_after(value: &str) -> Result<Duration, String> {
let duration = parse_duration(value)?;
if duration < MIN_UNLOAD_IDLE_AFTER {
return Err(format!(
"invalid duration '{value}': must be at least {}",
humantime::format_duration(MIN_UNLOAD_IDLE_AFTER)
));
}
Ok(duration)
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum DaemonCommand {
/// Run the daemon in the foreground until stopped. The default.
Run {
/// Unload a session once it has sat idle this long: no turn under
/// way and no client attached. Unloading closes its MCP servers and
/// lets other rho processes open it; the next prompt or attach
/// reopens it. A session that saved nothing is discarded instead.
/// Defaults to 60s; at least 100ms.
#[arg(long, value_name = "DURATION", value_parser = parse_unload_idle_after)]
unload_idle_after: Option<Duration>,
/// The lock and socket descriptors a daemon that re-executes itself
/// in place hands the new binary. Only the daemon passes it.
#[arg(long, value_name = "LOCK:SOCKET", hide = true)]
exec_handoff: Option<String>,
},
/// Start the daemon in the background unless it is already running,
/// and wait until it answers.
///
/// Exits 0 once a daemon runs, including one that already did. Waits
/// for a daemon that is stopping to exit before starting a new one.
Start {
/// Print one JSON document instead of text: the daemon's pid and
/// version, or, for a daemon that refused this rho over its
/// protocol version, the refusal, which says which side to change
/// and what restarting the daemon would cost.
#[arg(long)]
json: bool,
},
/// Show the running daemon's pid, version, uptime, socket, and any
/// upgrade it waits to run.
///
/// Never starts a daemon. Exits with status 1 when none is running.
Status {
/// Print one JSON document instead of text.
#[arg(long)]
json: bool,
},
/// Stop the running daemon and wait for it to exit.
///
/// Exits with status 1 when none is running.
Stop,
/// Restart the running daemon in place and wait until it answers again.
///
/// The daemon keeps its pid, socket, and lock, so clients reconnect to
/// it rather than start another, and re-executes its executable, which
/// runs an installed upgrade at once. Turns under way are interrupted;
/// a TUI session's turn with no time limit continues once the TUI
/// attaches to its session again. Starts the daemon when none is
/// running.
Restart,
/// Expose the local daemon's `rho-protocol` over HTTP WebSockets (`GET /ws`)
/// so clients on hosts that cannot use SSH can reach it.
///
/// Binds a loopback or private/overlay network address; every upgrade
/// requires a bearer token from `rho daemon token rotate`.
Serve(DaemonServeArgs),
/// Manage the bearer tokens `rho daemon serve --http` accepts.
Token {
#[command(subcommand)]
command: DaemonTokenCommand,
},
/// Set this machine up for rho-ui to reach over the WebSocket bridge, or
/// undo it.
Bridge {
#[command(subcommand)]
command: DaemonBridgeCommand,
},
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum DaemonBridgeCommand {
/// Run `rho daemon serve --http` as a service that survives reboots
/// (systemd on Linux, launchd on macOS) and print a pairing string for
/// rho-ui, once.
///
/// Listens on this machine's Tailscale or NetBird address, port 8766,
/// unless --http or --url says otherwise. Running it again rotates the
/// token and prints a new pairing string; the old one stops working.
Setup(BridgeSetupArgs),
/// Pair another device with the bridge setup installed: give it token
/// NAME and print a pairing string for it, once. Leaves the service as
/// it is.
///
/// Use one name per device. Pairing a name again rotates its token, so
/// the device it paired before stops reaching the bridge.
Pair {
/// The device's token name, which `rho daemon token list` shows and
/// `rho daemon token revoke` unpairs.
#[arg(value_name = "NAME")]
name: String,
},
/// Stop and delete the bridge's service, and revoke its token.
Remove {
/// The token to revoke.
#[arg(long, value_name = "NAME", default_value = BRIDGE_TOKEN)]
token: String,
},
}
/// The token `rho daemon bridge` pairs and revokes unless told another.
const BRIDGE_TOKEN: &str = "rho-ui";
#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
pub struct BridgeSetupArgs {
/// Listen on this address instead of the overlay's: loopback or this
/// machine's private address, such as 192.168.1.20:8766.
#[arg(long, value_name = "ADDR")]
pub http: Option<SocketAddr>,
/// Pair through a TLS proxy at this wss:// address, which forwards to the
/// bridge on loopback (127.0.0.1:8766 unless --http names another).
#[arg(long, value_name = "URL")]
pub url: Option<String>,
/// Also accept this `Host` header. May be repeated.
#[arg(long, value_name = "HOST")]
pub allow_host: Vec<String>,
/// The token the pairing string carries, which `rho daemon token list`
/// shows. Pair more devices with `rho daemon bridge pair`.
#[arg(long, value_name = "NAME", default_value = BRIDGE_TOKEN)]
pub token: String,
}
#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
pub struct DaemonServeArgs {
/// Serve `rho-protocol` over HTTP WebSocket (`GET /ws`) on this address:
/// a loopback address, such as 127.0.0.1:8766, or this machine's address
/// on a private network, such as a NetBird or Tailscale peer's
/// 100.x.y.z:8766, where traffic travels as plain HTTP/WS.
#[arg(long, value_name = "ADDR")]
pub http: SocketAddr,
/// Also accept this `Host` header, such as a tunnel's public hostname,
/// on any port unless it names one (rho.example.com:443). May be
/// repeated.
#[arg(long, value_name = "HOST")]
pub allow_host: Vec<String>,
/// Accept WebSocket upgrades from this browser origin, such as
/// https://app.example.com. Any other `Origin` is refused. Browsers
/// cannot send the bearer token themselves, so they need a proxy that
/// adds `Authorization`. May be repeated.
#[arg(long, value_name = "ORIGIN")]
pub allow_origin: Vec<String>,
/// Internal: the listening socket a server that upgraded in place
/// handed this one.
#[arg(long, value_name = "FD", hide = true)]
pub listener_fd: Option<i32>,
/// Internal: on Windows, the server that upgraded in place to this one,
/// whose exit this one waits for before it listens.
#[arg(long, value_name = "PID", hide = true)]
pub predecessor: Option<u32>,
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum DaemonTokenCommand {
/// Create token NAME, or give it a new secret, and print the secret once.
/// The old secret stops working at once: a running bridge closes its
/// open WebSockets within 5 seconds.
///
/// A daemon HTTP token grants full user-level daemon protocol access
/// (opening sessions in any workspace, answering approvals, and editing
/// config). Treat it like SSH access.
Rotate {
/// The token's name, which `list` and logs show.
#[arg(value_name = "NAME")]
name: String,
},
/// List daemon HTTP tokens by name.
List {
/// Print one JSON document instead of text rows.
#[arg(long)]
json: bool,
},
/// Delete token NAME; new connections presenting it are refused at once.
/// A running bridge closes its open WebSockets within 5 seconds.
Revoke {
#[arg(value_name = "NAME")]
name: String,
},
}