pub(crate) mod native;
pub(crate) mod tunnel;
use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
use crate::error::Error;
use crate::secrets::SecretValue;
pub use native::{NativeApp, NativeConsole, NativeNeed, NativeOpen};
pub use tunnel::Tunnel;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ConsoleProtocol {
Vnc,
Rdp,
None,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[non_exhaustive]
pub struct ConsoleOffer {
pub protocol: ConsoleProtocol,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unavailable: Option<String>,
}
impl ConsoleOffer {
pub(crate) fn of(protocol: ConsoleProtocol, note: Option<&str>) -> ConsoleOffer {
ConsoleOffer {
protocol,
note: note.map(str::to_owned),
unavailable: None,
}
}
pub(crate) fn none(why: &str) -> ConsoleOffer {
ConsoleOffer::of(ConsoleProtocol::None, Some(why))
}
pub(crate) fn unavailable(mut self, why: Option<String>) -> ConsoleOffer {
self.unavailable = why;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Endpoint {
Loopback(SocketAddr),
ProxmoxVnc(ProxmoxVnc),
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ProxmoxVnc {
pub websocket: SecretValue,
pub port: u16,
pub ticket: SecretValue,
pub authorization: (String, SecretValue),
pub insecure: bool,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct Console {
pub machine: String,
pub protocol: ConsoleProtocol,
pub endpoint: Endpoint,
pub user: Option<String>,
pub password: Option<SecretValue>,
pub note: Option<String>,
pub tunnel: Option<Tunnel>,
pub(crate) through: Option<tunnel::To>,
}
impl Console {
#[must_use]
pub fn new(machine: &str, protocol: ConsoleProtocol, endpoint: Endpoint) -> Console {
Console {
machine: machine.to_owned(),
protocol,
endpoint,
user: None,
password: None,
note: None,
tunnel: None,
through: None,
}
}
pub(crate) fn tunnelled(machine: &str, protocol: ConsoleProtocol, to: tunnel::To) -> Console {
let unopened = Endpoint::Loopback(SocketAddr::from(([127, 0, 0, 1], 0)));
let mut console = Console::new(machine, protocol, unopened);
console.through = Some(to);
console
}
pub(crate) fn with_note(mut self, note: Option<&str>) -> Console {
self.note = note.map(str::to_owned);
self
}
}
pub(crate) fn no_console(machine: &str, why: impl Into<String>) -> Error {
Error::NoConsole {
machine: machine.to_owned(),
why: why.into(),
}
}
pub(crate) fn on_loopback(machine: &str, at: SocketAddr) -> crate::Result<SocketAddr> {
if at.ip().is_loopback() {
Ok(at)
} else {
Err(no_console(
machine,
format!("its console listens on {at}, not on this computer's loopback: the runner refuses it"),
))
}
}
pub(crate) fn why_not<T>(found: crate::Result<T>) -> Option<String> {
match found {
Ok(_) => None,
Err(Error::NoConsole { why, .. }) => Some(why),
Err(e) => Some(e.to_string()),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_console_off_the_loopback_is_refused() {
assert!(on_loopback("web", "127.0.0.1:5900".parse().unwrap()).is_ok());
assert!(on_loopback("web", "[::1]:5900".parse().unwrap()).is_ok());
for lan in ["0.0.0.0:5900", "192.168.1.20:5900", "[::]:5900"] {
let err = on_loopback("web", lan.parse().unwrap()).unwrap_err();
assert_eq!(err.code(), crate::ErrorCode::NoConsole, "{lan}");
assert!(err.to_string().contains("not on this computer's loopback"), "{err}");
}
}
#[test]
fn an_offer_says_its_protocol_and_why_it_cant_be_shown() {
let offer = ConsoleOffer::of(ConsoleProtocol::Rdp, Some("needs the extension pack")).unavailable(Some("off".into()));
let json = serde_json::to_value(&offer).unwrap();
assert_eq!(
json,
serde_json::json!({ "protocol": "rdp", "note": "needs the extension pack", "unavailable": "off" })
);
assert_eq!(serde_json::to_value(ConsoleOffer::none("x")).unwrap()["protocol"], "none");
}
}