Skip to main content

isothrum_client/proto/
console.rs

1//! A graphical console on the wire: where it answers, and how to sign in.
2
3use std::fmt;
4use std::net::SocketAddr;
5
6use isothrum_runner::ConsoleProtocol;
7use serde::{Deserialize, Serialize};
8
9/// A machine's graphical console as the service reached it ([`isothrum_runner::Console`] on the
10/// wire): where it answers, and how to sign in. Its secrets travel only over the owner-only
11/// endpoint; its debug form never shows one.
12#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
13#[non_exhaustive]
14pub struct ReachedConsole {
15    /// The machine.
16    pub machine: String,
17    /// The protocol it speaks.
18    pub protocol: ConsoleProtocol,
19    /// Where it answers.
20    pub endpoint: ConsoleEndpoint,
21    /// The account to sign in as, when it signs in to the machine's own OS.
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    pub user: Option<String>,
24    /// Its password. Never print or log it.
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub password: Option<String>,
27    /// What to know of it.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub note: Option<String>,
30    /// Whether an SSH tunnel of the service carries it: open while the connection that asked is.
31    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
32    pub tunnelled: bool,
33}
34
35/// Where a graphical console answers ([`isothrum_runner::Endpoint`] on the wire).
36#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
37#[serde(tag = "at", rename_all = "snake_case")]
38#[non_exhaustive]
39pub enum ConsoleEndpoint {
40    /// A TCP port of the loopback, speaking the console's protocol.
41    Loopback {
42        /// The address.
43        address: SocketAddr,
44    },
45    /// Proxmox VE's VNC websocket: connect to `websocket` with the header `header: authorization`
46    /// (its certificate unverified when `insecure`), then speak VNC with `ticket` as the password.
47    ProxmoxVnc {
48        /// The websocket's URL (it holds the ticket).
49        websocket: String,
50        /// The VNC port opened on the node.
51        port: u16,
52        /// The ticket.
53        ticket: String,
54        /// The signing-in header's name.
55        header: String,
56        /// Its value.
57        authorization: String,
58        /// The server's certificate isn't verified.
59        insecure: bool,
60    },
61}
62
63impl ReachedConsole {
64    /// `console` on the wire; none for an endpoint this protocol doesn't carry.
65    #[must_use]
66    pub fn of(console: &isothrum_runner::Console) -> Option<ReachedConsole> {
67        let endpoint = match &console.endpoint {
68            isothrum_runner::Endpoint::Loopback(address) => ConsoleEndpoint::Loopback { address: *address },
69            isothrum_runner::Endpoint::ProxmoxVnc(vnc) => ConsoleEndpoint::ProxmoxVnc {
70                websocket: vnc.websocket.expose().to_owned(),
71                port: vnc.port,
72                ticket: vnc.ticket.expose().to_owned(),
73                header: vnc.authorization.0.clone(),
74                authorization: vnc.authorization.1.expose().to_owned(),
75                insecure: vnc.insecure,
76            },
77            _ => return None,
78        };
79        Some(ReachedConsole {
80            machine: console.machine.clone(),
81            protocol: console.protocol,
82            endpoint,
83            user: console.user.clone(),
84            password: console.password.as_ref().map(|p| p.expose().to_owned()),
85            note: console.note.clone(),
86            tunnelled: console.tunnel.is_some(),
87        })
88    }
89}
90
91impl fmt::Debug for ReachedConsole {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        f.debug_struct("ReachedConsole")
94            .field("machine", &self.machine)
95            .field("protocol", &self.protocol)
96            .field("endpoint", &self.endpoint)
97            .field("user", &self.user)
98            .field("password", &self.password.as_ref().map(|_| "***"))
99            .field("note", &self.note)
100            .field("tunnelled", &self.tunnelled)
101            .finish()
102    }
103}
104
105impl fmt::Debug for ConsoleEndpoint {
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        match self {
108            ConsoleEndpoint::Loopback { address } => f.debug_struct("Loopback").field("address", address).finish(),
109            ConsoleEndpoint::ProxmoxVnc { port, header, insecure, .. } => f
110                .debug_struct("ProxmoxVnc")
111                .field("port", port)
112                .field("header", header)
113                .field("insecure", insecure)
114                .finish_non_exhaustive(),
115        }
116    }
117}