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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use clap::{Parser, Subcommand};
mod daemon;
#[derive(Parser)]
#[command(name = "wsproxy")]
#[command(about = "WebSocket proxy for TCP connections", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Run the proxy server (WebSocket -> TCP)
Server {
/// Path to configuration file (TOML format) with hot-reload support
#[arg(short, long, conflicts_with_all = ["listen", "route", "default_target", "tls_cert", "tls_key", "tls_self_signed"])]
config: Option<String>,
/// Address to listen for WebSocket connections (e.g., "0.0.0.0:8080")
#[arg(short, long, required_unless_present = "config")]
listen: Option<String>,
/// Route mapping in the format "path=target" (e.g., "/ssh=127.0.0.1:22")
/// Can be specified multiple times
#[arg(short, long, value_name = "PATH=TARGET")]
route: Vec<String>,
/// Default target for paths that don't match any route (e.g., "127.0.0.1:22")
#[arg(short, long)]
default_target: Option<String>,
/// Path to TLS certificate file (PEM format) for WSS support
#[arg(long, requires = "tls_key", conflicts_with = "tls_self_signed")]
tls_cert: Option<String>,
/// Path to TLS private key file (PEM format) for WSS support
#[arg(long, requires = "tls_cert", conflicts_with = "tls_self_signed")]
tls_key: Option<String>,
/// Generate a self-signed TLS certificate for WSS support
#[arg(long, conflicts_with_all = ["tls_cert", "tls_key"])]
tls_self_signed: bool,
},
/// Run the proxy client (TCP -> WebSocket)
Client {
/// Address to listen for TCP connections (e.g., "127.0.0.1:2222")
#[arg(short, long)]
listen: String,
/// WebSocket server URL to connect to (e.g., "ws://server:8080/ssh" or "wss://server:8080/ssh")
#[arg(short, long)]
server: String,
/// Skip TLS certificate verification (insecure, for self-signed certificates)
#[arg(short = 'k', long)]
insecure: bool,
/// Path to CA certificate file (PEM format) for verifying self-signed server certificates
#[arg(long)]
tls_ca_cert: Option<String>,
},
/// Run a single tunnel connection (stdin/stdout -> WebSocket)
/// Useful for SSH ProxyCommand
Tunnel {
/// WebSocket server URL to connect to (e.g., "ws://server:8080/ssh" or "wss://server:8080/ssh")
#[arg(short, long)]
server: String,
/// Skip TLS certificate verification (insecure, for self-signed certificates)
#[arg(short = 'k', long)]
insecure: bool,
/// Path to CA certificate file (PEM format) for verifying self-signed server certificates
#[arg(long)]
tls_ca_cert: Option<String>,
},
/// Manage daemon processes with automatic restart
Daemon {
#[command(subcommand)]
action: DaemonAction,
},
}
#[derive(Subcommand)]
enum DaemonAction {
/// Run the proxy server as a daemon with automatic restart
Server {
/// Path to configuration file (TOML format) with hot-reload support
#[arg(short, long, conflicts_with_all = ["listen", "route", "default_target", "tls_cert", "tls_key", "tls_self_signed"])]
config: Option<String>,
/// Address to listen for WebSocket connections (e.g., "0.0.0.0:8080")
#[arg(short, long, required_unless_present = "config")]
listen: Option<String>,
/// Route mapping in the format "path=target" (e.g., "/ssh=127.0.0.1:22")
/// Can be specified multiple times
#[arg(short, long, value_name = "PATH=TARGET")]
route: Vec<String>,
/// Default target for paths that don't match any route (e.g., "127.0.0.1:22")
#[arg(short, long)]
default_target: Option<String>,
/// Path to TLS certificate file (PEM format) for WSS support
#[arg(long, requires = "tls_key", conflicts_with = "tls_self_signed")]
tls_cert: Option<String>,
/// Path to TLS private key file (PEM format) for WSS support
#[arg(long, requires = "tls_cert", conflicts_with = "tls_self_signed")]
tls_key: Option<String>,
/// Generate a self-signed TLS certificate for WSS support
#[arg(long, conflicts_with_all = ["tls_cert", "tls_key"])]
tls_self_signed: bool,
},
/// Run the proxy client as a daemon with automatic restart
Client {
/// Address to listen for TCP connections (e.g., "127.0.0.1:2222")
#[arg(short, long)]
listen: String,
/// WebSocket server URL to connect to (e.g., "ws://server:8080/ssh" or "wss://server:8080/ssh")
#[arg(short, long)]
server: String,
/// Skip TLS certificate verification (insecure, for self-signed certificates)
#[arg(short = 'k', long)]
insecure: bool,
/// Path to CA certificate file (PEM format) for verifying self-signed server certificates
#[arg(long)]
tls_ca_cert: Option<String>,
},
/// List all running daemons
List,
/// Kill a daemon by ID
Kill {
/// The daemon ID to kill (from `daemon list`)
id: u32,
},
}
fn main() {
// Check if we're running as the daemon child process
if daemon::is_daemon_child() {
daemon::run_restart_loop();
}
if let Err(e) = run() {
eprintln!("Error: {e:?}");
std::process::exit(1);
}
}
#[tokio::main]
async fn run() -> wsproxy::Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Server {
config,
listen,
route,
default_target,
tls_cert,
tls_key,
tls_self_signed,
} => {
// Config file mode with hot-reload
if let Some(config_path) = config {
if daemon::should_monitor_stdin() {
tokio::select! {
result = wsproxy::server::run_with_config(&config_path) => {
result?;
}
_ = daemon::wait_for_stdin_close() => {
eprintln!("Parent daemon died, shutting down server");
std::process::exit(0);
}
}
} else {
wsproxy::server::run_with_config(&config_path).await?;
}
} else {
// CLI mode
let listen = listen.expect("listen is required when not using config");
let tls = match (tls_cert, tls_key, tls_self_signed) {
(Some(cert), Some(key), false) => wsproxy::server::TlsMode::Files {
cert: cert.leak(),
key: key.leak(),
},
(None, None, true) => wsproxy::server::TlsMode::SelfSigned,
_ => wsproxy::server::TlsMode::None,
};
if daemon::should_monitor_stdin() {
tokio::select! {
result = wsproxy::server::run(&listen, &route, default_target.as_deref(), tls) => {
result?;
}
_ = daemon::wait_for_stdin_close() => {
eprintln!("Parent daemon died, shutting down server");
std::process::exit(0);
}
}
} else {
wsproxy::server::run(&listen, &route, default_target.as_deref(), tls).await?;
}
}
}
Commands::Client {
listen,
server,
insecure,
tls_ca_cert,
} => {
let tls_options = wsproxy::client::TlsOptions {
insecure,
ca_cert_path: tls_ca_cert,
};
// Check if we should monitor stdin for parent death (daemon mode)
if daemon::should_monitor_stdin() {
tokio::select! {
result = wsproxy::client::run(&listen, &server, &tls_options) => {
result?;
}
_ = daemon::wait_for_stdin_close() => {
eprintln!("Parent daemon died, shutting down client");
std::process::exit(0);
}
}
} else {
wsproxy::client::run(&listen, &server, &tls_options).await?;
}
}
Commands::Tunnel {
server,
insecure,
tls_ca_cert,
} => {
let tls_options = wsproxy::client::TlsOptions {
insecure,
ca_cert_path: tls_ca_cert,
};
wsproxy::client::tunnel(&server, &tls_options).await?;
}
Commands::Daemon { action } => match action {
DaemonAction::Server {
config,
listen,
route,
default_target,
tls_cert,
tls_key,
tls_self_signed,
} => {
daemon::spawn_server(
config,
listen,
route,
default_target,
tls_cert,
tls_key,
tls_self_signed,
)?;
}
DaemonAction::Client {
listen,
server,
insecure,
tls_ca_cert,
} => {
daemon::spawn_client(listen, server, insecure, tls_ca_cert)?;
}
DaemonAction::List => {
let daemons = daemon::list()?;
if daemons.is_empty() {
println!("No daemons running");
} else {
println!("{:<4} {:<8} ARGUMENTS", "ID", "PID");
println!("{}", "-".repeat(50));
for d in daemons {
println!("{:<4} {:<8} {}", d.id, d.pid, d.args.join(" "));
}
}
}
DaemonAction::Kill { id } => {
if daemon::kill(id)? {
println!("Daemon {} killed", id);
} else {
eprintln!("Daemon {} not found or could not be killed", id);
std::process::exit(1);
}
}
},
}
Ok(())
}