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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use std::path::PathBuf;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(
name = "erbridge",
version,
about = "Low-latency TCP/UDP port forwarder with a reverse (NAT-traversal) mode"
)]
pub struct Cli {
/// TOML config file (see config.toml.example).
#[arg(long, short = 'c', global = true)]
pub config: Option<PathBuf>,
/// Disable the interactive TUI and write structured JSON logs instead
/// (use when running unattended, e.g. as a background/Windows service).
#[arg(long, global = true)]
pub headless: bool,
/// JSON log file path when --headless is set. Defaults to `erbridge.log`.
#[arg(long, global = true)]
pub log_file: Option<PathBuf>,
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand)]
pub enum Command {
/// Direct forwarding: external listen port(s) -> internal target(s).
Forward {
/// Ad-hoc mapping, repeatable: LISTEN->TARGET[/tcp|udp|both], where
/// proto can add a `+tls` or `+noise` modifier (or bare `tls`/`noise`
/// for `tcp+tls`/`tcp+noise`) to wrap that mapping's listen side in
/// that transport. LISTEN may be a bare port (binds 0.0.0.0) or
/// host:port.
#[arg(long = "map", value_name = "LISTEN->TARGET[/proto]")]
maps: Vec<String>,
/// Token for every `+tls`/`+noise` mapping above that doesn't set its
/// own (applies to all of them). Mandatory for `+noise`; for `+tls`,
/// omitting it lets any TLS client connect, same as today.
#[arg(long)]
token: Option<String>,
},
/// Companion to a `forward` mapping secured with `+tls`/`+noise`
/// (optionally `--token`): listens locally in plaintext, and for every
/// connection dials the secured mapping using that same transport,
/// presents the token if configured, then relays bytes -- so a plain
/// local client doesn't need its own TLS/Noise support to reach it.
Client {
/// Ad-hoc mapping, repeatable: LOCAL_LISTEN->SERVER_ADDR[/tls|/noise].
/// LOCAL_LISTEN may be a bare port (binds 127.0.0.1) or host:port;
/// the modifier names the far side mapping's transport and defaults
/// to `tls` when omitted.
#[arg(long = "map", value_name = "LOCAL_LISTEN->SERVER_ADDR[/proto]")]
maps: Vec<String>,
/// Token to present to the secured mapping (applies to all of them
/// above); must match its `--token`/`token`.
#[arg(long)]
token: Option<String>,
},
/// Reverse tunnel role A: wait for `connect` (B) to dial in, then expose
/// external ports that get relayed through B to B's local targets.
Serve {
/// Control address B dials, e.g. 0.0.0.0:9000.
#[arg(long)]
listen: Option<String>,
/// Shared secret B must present to be accepted.
#[arg(long)]
token: Option<String>,
/// Ad-hoc tunnel, repeatable: NAME=EXTERNAL_ADDR.
#[arg(long = "tunnel", value_name = "NAME=EXTERNAL_ADDR")]
tunnels: Vec<String>,
/// Multi-tenant mode: the tenant file (see `serve-admin`). Each tenant
/// has its own token and is reached by its own TLS SNI host. Replaces
/// --token and --tunnel; needs --sni-listen.
#[arg(long, value_name = "FILE")]
tenants: Option<PathBuf>,
/// Multi-tenant mode: address that accepts TLS connections and routes
/// them to tenants by SNI, e.g. 0.0.0.0:443.
#[arg(long, value_name = "ADDR")]
sni_listen: Option<String>,
},
/// Reverse tunnel role B: dial `serve` (A) and relay each stream it
/// opens to a locally-configured target.
Connect {
/// A's control address to dial, e.g. 1.2.3.4:9000.
#[arg(long)]
server: Option<String>,
/// Shared secret to present to A.
#[arg(long)]
token: Option<String>,
/// Ad-hoc tunnel, repeatable: NAME=TARGET_ADDR.
#[arg(long = "tunnel", value_name = "NAME=TARGET_ADDR")]
tunnels: Vec<String>,
/// Credentials file written by `enroll`: supplies the token and the
/// tunnel name, so only --target is needed besides --server.
#[arg(long, value_name = "FILE")]
credentials: Option<PathBuf>,
/// Local target for the tunnel named in --credentials.
#[arg(long, value_name = "ADDR", requires = "credentials")]
target: Option<String>,
},
/// Trade a single-use enrollment code for the credentials of a new tenant
/// on a multi-tenant `serve`.
Enroll {
/// The server's control address, e.g. relay.example.com:9000.
#[arg(long)]
server: String,
/// The code from `serve-admin issue`.
#[arg(long)]
code: String,
/// Write the credentials here (mode 0600, never overwrites) instead of
/// printing them.
#[arg(long, value_name = "FILE")]
out: Option<PathBuf>,
},
/// Manage the tenant file of a multi-tenant `serve`. Safe to run while
/// `serve` is up; changes apply within a second.
ServeAdmin {
/// The tenant file (or set `serve.tenants_file` in the config).
#[arg(long, value_name = "FILE", global = true)]
tenants: Option<PathBuf>,
#[command(subcommand)]
action: AdminAction,
},
}
#[derive(Subcommand, Debug, Clone)]
pub enum AdminAction {
/// Print a new single-use enrollment code for a host.
Issue {
/// The TLS SNI host the new tenant will be reached at.
#[arg(long)]
sni: String,
/// Concurrent connections the tenant may have (default 64).
#[arg(long)]
max_conns: Option<usize>,
/// Seconds until the code stops working (default: never).
#[arg(long, value_name = "SECS")]
expires_in: Option<u64>,
},
/// Remove a tenant; its token stops working and its session ends.
Revoke { name: String },
/// Suspend a tenant without forgetting it.
Disable { name: String },
/// Resume a suspended tenant.
Enable { name: String },
/// List tenants and the number of unused codes.
List,
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(args: &[&str]) -> Result<Cli, clap::Error> {
Cli::try_parse_from(std::iter::once("erbridge").chain(args.iter().copied()))
}
#[test]
fn serve_accepts_the_multi_tenant_flags() {
let cli = parse(&[
"serve",
"--listen",
"0.0.0.0:9000",
"--tenants",
"t.toml",
"--sni-listen",
"0.0.0.0:443",
])
.unwrap();
let Command::Serve {
tenants,
sni_listen,
token,
..
} = cli.command
else {
panic!("not serve");
};
assert_eq!(tenants.unwrap().to_str(), Some("t.toml"));
assert_eq!(sni_listen.as_deref(), Some("0.0.0.0:443"));
assert!(token.is_none());
}
#[test]
fn connect_takes_credentials_and_a_target() {
let cli = parse(&[
"connect",
"--server",
"h:9000",
"--credentials",
"c.toml",
"--target",
"127.0.0.1:80",
])
.unwrap();
let Command::Connect {
credentials,
target,
..
} = cli.command
else {
panic!("not connect");
};
assert_eq!(credentials.unwrap().to_str(), Some("c.toml"));
assert_eq!(target.as_deref(), Some("127.0.0.1:80"));
// --target alone makes no sense.
assert!(parse(&["connect", "--server", "h:1", "--target", "x:1"]).is_err());
}
#[test]
fn enroll_needs_a_server_and_a_code() {
assert!(parse(&["enroll", "--server", "h:9000", "--code", "abc"]).is_ok());
assert!(parse(&["enroll", "--server", "h:9000"]).is_err());
assert!(parse(&["enroll", "--code", "abc"]).is_err());
}
#[test]
fn serve_admin_subcommands_parse() {
let cli = parse(&[
"serve-admin",
"--tenants",
"t.toml",
"issue",
"--sni",
"a.example.com",
"--max-conns",
"5",
"--expires-in",
"3600",
])
.unwrap();
let Command::ServeAdmin { tenants, action } = cli.command else {
panic!("not serve-admin");
};
assert_eq!(tenants.unwrap().to_str(), Some("t.toml"));
assert!(matches!(
action,
AdminAction::Issue {
max_conns: Some(5),
expires_in: Some(3600),
..
}
));
for ok in [
&["serve-admin", "revoke", "t-1"][..],
&["serve-admin", "disable", "t-1"],
&["serve-admin", "enable", "t-1"],
&["serve-admin", "list"],
] {
assert!(parse(ok).is_ok(), "{ok:?}");
}
assert!(
parse(&["serve-admin", "issue"]).is_err(),
"issue needs --sni"
);
}
}