cli: allow client process to control singleton process (#177141)

Other connected clients will now print:

```
Connected to an existing tunnel process running on this machine. You can press:

- Ctrl+C to detach
- "x" to stop the tunnel and exit
- "r" to restart the tunnel
```

These are then sent to the server to have that take effect. This is
mostly some refactors in the singleton_server to make the lifecycle work.
This commit is contained in:
Connor Peet
2023-03-14 17:55:28 -07:00
committed by GitHub
parent 994b2d03c0
commit 0e7d14d32d
12 changed files with 266 additions and 133 deletions

View File

@@ -103,9 +103,17 @@ enum ServerSignal {
Respawn,
}
pub struct ServerTermination {
pub enum Next {
/// Whether the server should be respawned in a new binary (see ServerSignal.Respawn).
pub respawn: bool,
Respawn,
/// Whether the tunnel should be restarted
Restart,
/// Whether the process should exit
Exit,
}
pub struct ServerTermination {
pub next: Next,
pub tunnel: ActiveTunnel,
}
@@ -142,7 +150,7 @@ fn print_listening(log: &log::Logger, tunnel_name: &str) {
}
}
let message = &format!("\nOpen this link in your browser {}\n", addr);
let message = &format!("\r\nOpen this link in your browser {}\r\n", addr);
log.result(message);
}
@@ -166,11 +174,14 @@ pub async fn serve(
loop {
tokio::select! {
Ok(r) = shutdown_rx.wait() => {
info!(log, "Shutting down: {}", r);
Ok(reason) = shutdown_rx.wait() => {
info!(log, "Shutting down: {}", reason);
drop(signal_exit);
return Ok(ServerTermination {
respawn: false,
next: match reason {
ShutdownSignal::RpcRestartRequested => Next::Restart,
_ => Next::Exit,
},
tunnel,
});
},
@@ -178,7 +189,7 @@ pub async fn serve(
if let Some(ServerSignal::Respawn) = c {
drop(signal_exit);
return Ok(ServerTermination {
respawn: true,
next: Next::Respawn,
tunnel,
});
}
@@ -192,7 +203,7 @@ pub async fn serve(
None => {
warning!(log, "ssh tunnel disposed, tearing down");
return Ok(ServerTermination {
respawn: false,
next: Next::Restart,
tunnel,
});
}