All your machines, one app. For you and your AI agents: agent sessions, desktops, files, logs and GitHub across your Macs, Linux and Windows machines, with an MCP server.
Nobody signed in: say so at once, and open the machine with Remote Desktop (#27)
Adib Hanna03518cc01695
diff --git a/AGENTS.md b/AGENTS.mdindex aad48ec..8b41bff 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -125,7 +125,7 @@ The CLI equivalents are `muxin session ls|start|read|question|answer|send|do|att- `machine_connection` says how Muxin reaches a machine: `direct` (QUIC), `ssh` (one live ssh connection) or `none`, with a measured round trip and the `network` it goes over ("Local network", "Tailscale", ...). `direct_connection(machine_id, enabled)` pairs for direct connections (it installs a user service on the machine; only when asked).- Tailscale, when it's on this computer, keeps machines reachable away from home: Muxin uses the local network when the machine is on it and Tailscale otherwise, and switches by itself when the network changes. A machine's `tailnet` (in `get_machine` and `machine_connection`) says whether Tailscale sees it online and whether traffic goes peer-to-peer (`path: "direct"`) or through a slower relay (`"relay"`). An `unreachable.kind` of `"tailscale-off"` means Tailscale is off or signed out on this computer: tell the user to turn it on; Muxin never changes Tailscale's settings.- `machine_ports(machine_id)` lists what's listening on a machine: each port with its program or Docker container, and for web servers (`web: true`) their status and page title. To show the user a server running there, they open it from the machine's page in the app (Servers › Open, at `localhost` on their computer) or run `muxin forward <machine> <port>`; you can check it from the machine itself with `workspace_exec(command: "curl -sS localhost:<port>")`.-- `desktop_screenshot(machine_id)` returns a picture of the machine's screen (Linux with X11, Macs, and Windows with someone signed in). `desktop_input(machine_id, events)` clicks, types, scrolls and presses keys there, in that picture's pixels: look first, act, then look again. It's the user's real screen: only when asked, and never type passwords.+- `desktop_screenshot(machine_id)` returns a picture of the machine's screen (Linux with X11, Macs, and Windows with someone signed in). When nobody is signed in there (common on servers), it says so at once: there's no screen to show. Tell the user. They can sign in at the machine, or open its own Remote Desktop or Screen Sharing (the machine's menu in the app, or `muxin desktop rdp|vnc <machine>`), which shows the sign-in screen. `desktop_input(machine_id, events)` clicks, types, scrolls and presses keys there, in that picture's pixels: look first, act, then look again. It's the user's real screen: only when asked, and never type passwords.- A machine can have several monitors. `desktop_displays(machine_id)` lists them (each with an `id`; Muxin's virtual ones have `virtual: true`); pass `display` to `desktop_screenshot` and `desktop_input` to use one. `desktop_add_display(machine_id, width?, height?, name?)` adds a virtual monitor to the desktop (Linux and Macs), as if another screen were plugged in, and `desktop_remove_display(machine_id, display)` takes it away. `desktop_set_display(machine_id, display, width?, height?, scale?)` changes a monitor's resolution (one of its `modes`) or the desktop's scale on it (`scales`; on Linux X11 one scale for all monitors): a 4K monitor at scale 2 reads like 1080p and stays sharp. They change the user's real desktop: only when asked.## Cloud machinesdiff --git a/README.md b/README.mdindex 80920a9..1073be9 100644--- a/README.md+++ b/README.md@@ -416,6 +416,14 @@ Muxin first reaches a machine with `ssh`, installs its small agent there, and fr**Desktop.** On a Linux machine with an X11 desktop, a Mac, or a Windows PC someone is signed in to, **Show desktop** (on its page, or its right-click menu) shows its screen live and sends your mouse and keyboard to it. On Linux ⌘ works as Ctrl; on a Mac it stays ⌘. An open desktop is listed under its machine in the sidebar (and keeps streaming while you look elsewhere); close it there.+**Nobody signed in.** Muxin shows the screen of the account it connects as, so that account has to be signed in there. Servers often have nobody signed in, and a Remote Desktop window closed without signing out leaves a session with no screen. Muxin says so right away and offers **Open with Remote Desktop** (Windows), **Open with Screen Sharing** (Mac) or **Open with RDP or VNC** (Linux); the machine's right-click menu has it too. It opens the machine's own remote desktop in your app for it: Windows App or Remote Desktop Connection, Screen Sharing, Remmina or FreeRDP. That shows the sign-in screen, so you can sign in. It goes through Muxin's connection to the machine, so it works away from home too and nothing has to be opened in a firewall. Here it's at `localhost:3390` (VNC: `localhost:5901`), listed under Servers. The machine needs it turned on:++- **Windows:** Remote Desktop, in Settings › System › Remote Desktop (on Windows Server: Server Manager › Local Server). Windows Home can't be reached this way. Windows Server allows two admin sessions at once without extra licenses.+- **Mac:** Screen Sharing, in System Settings › General › Sharing.+- **Linux:** xrdp, GNOME's Remote Desktop, or a VNC server such as x11vnc.++From a terminal: `muxin desktop rdp <machine>` or `muxin desktop vnc <machine>`.+**Monitors, real and virtual.** A machine with more than one monitor gets a picker above its desktop: show one monitor at a time (on Linux, or all of them). **Add monitor** (or **Add virtual monitor…** in the machine's right-click menu) plugs in a monitor that isn't there, the way [DeskPad](https://github.com/Stengo/DeskPad) does on a Mac: pick a name and a size (720p to 4K), and it joins the desktop like a real one, to the right of the others. Windows move onto it, the machine's own tools (System Settings, RustDesk) see it, and while the machine's desktop is open, Muxin lists it under the Desktop item in the sidebar to open it directly. It starts at the main monitor's size and offers every common one (720p to 4K). Above a monitor, pick its resolution, and on Linux desktops that offer it, its scale: 200% makes things twice as big and just as sharp, so a 4K monitor reads like 1080p (the stream is always the screen's own size, so it stays crisp). Right-click a monitor (in the picker or the sidebar) to rename or remove it, and in the all-screens view, click a monitor's name to open it. On Cinnamon each virtual monitor gets copies of the main monitor's panels (menu, clock, dock with the same pinned apps), so it's usable on its own. It stays until you remove it (**Remove monitor**) or the machine restarts. Handy on a machine with no screen (a dummy HDMI plug, a server), or to give an agent a monitor of its own. On a Mac it's a CGVirtualDisplay. On Linux (X11) it's a software dummy plug: Muxin plugs a made-up monitor (at the size you picked) into a video output with nothing connected, and the desktop sets it up like a real one. That takes root, so when sudo wants a password Muxin asks for yours, only then, sends it to sudo over its encrypted connection, and keeps it in memory only until you quit. Nothing is installed.```sh@@ -573,7 +581,7 @@ muxin schedule log beelink s-ab12cd**Windows machines** are managed like the others, over OpenSSH (Settings › System › Optional features › OpenSSH Server, then key-based login; an administrator account for services and runners). Muxin installs its agent in `%USERPROFILE%\.muxen\bin` and works with PowerShell or cmd as sshd's shell:- **Sessions** keep running after Muxin closes (ConPTY, in the agent's session server).-- **Desktop and clipboard:** the screen, mouse and keyboard of whoever is signed in; Muxin starts its helper in their session through a one-off scheduled task (no window flashes).+- **Desktop and clipboard:** the screen, mouse and keyboard of the account Muxin connects as, while it's signed in; Muxin starts its helper in its session through a one-off scheduled task (no window flashes). With nobody signed in, use **Open with Remote Desktop** (see Desktop above).- **Pages:** Processes and GPUs, Services (Windows services; "at boot" is the startup type), Servers, Logs (the System, Application and Security event logs, a service's events, files), Usage, Copy files, git, Run (PowerShell), workspaces (sync copies through the agent; it doesn't delete).- **Checks** report admin rights, Docker, the network card (for Wake on LAN), pending reboots and failed services.- **Direct connections** run `listen` as a scheduled task at sign-in, with a Windows Firewall rule for the agent.diff --git a/agent/src/desktop.rs b/agent/src/desktop.rsindex 8e65b0d..bfe8932 100644--- a/agent/src/desktop.rs+++ b/agent/src/desktop.rs@@ -70,6 +70,45 @@ pub fn session_env() -> Option<Vec<(String, String)>> {Some(Vec::new())}+/// Why this user's screen isn't there to show, when that's so: nobody is+/// signed in at the screen, or someone else is. Asked before starting+/// anything in the desktop session, so the answer is quick.+#[cfg(target_os = "macos")]+pub fn no_session() -> Option<String> {+ use std::os::unix::fs::MetadataExt;+ // The console belongs to whoever's session is on the screen; root's at the login window.+ let owner = std::fs::metadata("/dev/console").ok()?.uid();+ let me = unsafe { libc::getuid() };+ if owner == me {+ return None;+ }+ if owner == 0 {+ return Some("Nobody is signed in to this Mac, so there's no screen to show. Sign in first, at the Mac or with Screen Sharing.".into());+ }+ let name = |uid: u32| unsafe {+ let pw = libc::getpwuid(uid);+ if pw.is_null() || (*pw).pw_name.is_null() {+ return format!("user {uid}");+ }+ std::ffi::CStr::from_ptr((*pw).pw_name).to_string_lossy().into_owned()+ };+ Some(format!("{} is using this Mac's screen, not {}. Muxin shows the screen of the account it connects as.", name(owner), name(me)))+}++#[cfg(windows)]+pub fn no_session() -> Option<String> {+ if crate::win_screen::in_desktop_session() {+ return None;+ }+ crate::win_screen::no_desktop_session()+}++#[cfg(target_os = "linux")]+pub fn no_session() -> Option<String> {+ // `session_env` says it: no DISPLAY to borrow.+ None+}+/// `muxen-agent screen [fps] [quality] [max_width] [tiles|frames] [display]`:/// streams a screen on stdout and applies input from stdin until either/// closes. `display` is one from `displays` (see displays.rs); without it, thediff --git a/agent/src/link.rs b/agent/src/link.rsindex 392e167..37c37a1 100644--- a/agent/src/link.rs+++ b/agent/src/link.rs@@ -396,9 +396,14 @@ fn desktop(req: &Value, out: &Out, attached: &Attached) -> Value {if channel == CONTROL {return json!({ "ok": false, "error": "desktop needs a channel" });}+ // Nobody's screen to show: `no_session` lets the app offer the machine's+ // own remote desktop (RDP, VNC) instead, which can sign someone in.let Some(env) = crate::desktop::session_env() else {- return json!({ "ok": false, "error": "No graphical session found here (nobody logged in to a desktop?)." });+ return json!({ "ok": false, "error": "No graphical session found here (nobody logged in to a desktop?).", "no_session": true });};+ if let Some(why) = crate::desktop::no_session() {+ return json!({ "ok": false, "error": why, "no_session": true });+ }let arg = |k: &str, d: u64| req[k].as_u64().unwrap_or(d).to_string();let mode = if req["tiles"].as_bool() == Some(true) { "tiles" } else { "frames" };let display = req["display"].as_str().unwrap_or("").to_string();diff --git a/agent/src/win_screen.rs b/agent/src/win_screen.rsindex 96f66b0..1bfa7c9 100644--- a/agent/src/win_screen.rs+++ b/agent/src/win_screen.rs@@ -463,6 +463,70 @@ pub fn in_desktop_session() -> bool {unsafe { ProcessIdToSessionId(std::process::id(), &mut session) != 0 && session != 0 && session == WTSGetActiveConsoleSessionId() }}+/// A signed-in Windows session: its user's name, and whether it's on a+/// screen (connected) rather than disconnected.+struct SignedIn {+ user: String,+ connected: bool,+}++/// Who is signed in to a desktop here, at the screen or through Remote+/// Desktop, from Windows' list of sessions. None when Windows won't say.+fn signed_in_users() -> Option<Vec<SignedIn>> {+ use windows_sys::Win32::System::RemoteDesktop::*;+ let query = |id: u32, class: WTS_INFO_CLASS| -> String {+ let mut buf: windows_sys::core::PWSTR = std::ptr::null_mut();+ let mut bytes = 0u32;+ if unsafe { WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, id, class, &mut buf, &mut bytes) } == 0 || buf.is_null() {+ return String::new();+ }+ let units = unsafe { std::slice::from_raw_parts(buf, bytes as usize / 2) };+ let text = String::from_utf16_lossy(units.split(|u| *u == 0).next().unwrap_or_default());+ unsafe { WTSFreeMemory(buf as _) };+ text+ };+ let mut list: *mut WTS_SESSION_INFOW = std::ptr::null_mut();+ let mut count = 0u32;+ if unsafe { WTSEnumerateSessionsW(WTS_CURRENT_SERVER_HANDLE, 0, 1, &mut list, &mut count) } == 0 || list.is_null() {+ return None;+ }+ let sessions = unsafe { std::slice::from_raw_parts(list, count as usize) };+ // Session 0 is services'; the sign-in screen's session is only connected.+ let users: Vec<SignedIn> = sessions+ .iter()+ .filter(|s| s.SessionId != 0 && (s.State == WTSActive || s.State == WTSDisconnected))+ .map(|s| SignedIn { user: query(s.SessionId, WTSUserName), connected: s.State == WTSActive })+ .collect();+ unsafe { WTSFreeMemory(list as _) };+ // A signed-in session whose user Windows won't name: don't guess.+ (!users.iter().any(|u| u.user.is_empty())).then_some(users)+}++/// Why there's no desktop session with a screen to start in, when Windows+/// says so: nobody signed in, only someone else (a scheduled task only runs+/// in its own user's session), or this user's session is disconnected (its+/// Remote Desktop window closed: Windows draws nothing for it).+pub fn no_desktop_session() -> Option<String> {+ let users = signed_in_users()?;+ // By name, as the task names its user (`$env:USERNAME`); sshd's+ // USERDOMAIN says WORKGROUP rather than the computer's name.+ let me = std::env::var("USERNAME").unwrap_or_default();+ if me.is_empty() {+ return None;+ }+ let own: Vec<&SignedIn> = users.iter().filter(|u| u.user.eq_ignore_ascii_case(&me)).collect();+ if own.iter().any(|u| u.connected) {+ return None;+ }+ if !own.is_empty() {+ return Some(format!("{me} is signed in to Windows here, but that session is disconnected (its Remote Desktop window was closed), so there's no screen to show. Open it with Remote Desktop again."));+ }+ Some(match users.first() {+ Some(other) => format!("{} is signed in to Windows here, not {me}. Muxin shows the screen of the account it connects as.", other.user),+ None => "Nobody is signed in to Windows here, so there's no screen to show. Sign in first, at the machine or with Remote Desktop.".into(),+ })+}+/// Where the desktop session's helper listens: "port token".fn gui_port_file() -> std::path::PathBuf {crate::home_dir().join("gui.port")@@ -487,6 +551,10 @@ pub fn start_in_desktop(args: &[String]) -> Result<TcpStream, String> {if let Ok(s) = ask_gui_server(args) {return Ok(s);}+ // The task would never start: say so now, not after waiting for it.+ if let Some(why) = no_desktop_session() {+ return Err(why);+ }let exe = std::env::current_exe().map_err(|e| e.to_string())?;let task = format!("Muxen desktop {}", std::process::id());let q = crate::win::quote;@@ -508,7 +576,7 @@ pub fn start_in_desktop(args: &[String]) -> Result<TcpStream, String> {match ask_gui_server(args) {Ok(s) => break Ok(s),Err(_) if Instant::now() < deadline => std::thread::sleep(Duration::from_millis(150)),- Err(_) => break Err("Nobody is signed in to Windows' desktop here, so there's no screen to show.".to_string()),+ Err(_) => break Err("Windows didn't start Muxin's helper in your desktop session, so there's no screen to show.".to_string()),}},};diff --git a/src/api.rs b/src/api.rsindex b5d3136..024b912 100644--- a/src/api.rs+++ b/src/api.rs@@ -3756,6 +3756,22 @@ pub async fn forward_port(engine: &Arc<Engine>, req: ForwardRequest) -> ApiResulengine.forward(&m.id, req.port, None, req.local_port).await.map_err(|e| ApiError::new("invalid", e))}+#[derive(Debug, Deserialize)]+#[serde(deny_unknown_fields)]+pub struct RemoteDesktopRequest {+ pub machine_id: String,+ /// "rdp" or "vnc" (default: the one the machine has; RDP first, VNC on a Mac).+ #[serde(default)]+ pub protocol: Option<crate::fleet::remote_desktop::Protocol>,+}++/// Opens a machine's own remote desktop (RDP or VNC) in this computer's app+/// for it, through a port opened here for as long as this process runs.+pub async fn open_remote_desktop(engine: &Arc<Engine>, req: RemoteDesktopRequest) -> ApiResult<crate::fleet::remote_desktop::RemoteDesktop> {+ let m = find_machine(&engine.current(), &req.machine_id)?.clone();+ engine.open_remote_desktop(&m.id, req.protocol).await.map_err(|e| ApiError::new("invalid", e))+}+#[derive(Debug, Deserialize)]#[serde(deny_unknown_fields)]pub struct ComposeProjectRequest {diff --git a/src/fleet/mod.rs b/src/fleet/mod.rsindex e7788ab..38ef6d4 100644--- a/src/fleet/mod.rs+++ b/src/fleet/mod.rs@@ -16,6 +16,7 @@ pub mod files;pub mod model;pub mod ports;pub mod procs;+pub mod remote_desktop;pub mod report;pub mod sessions;pub mod tar_filter;diff --git a/src/fleet/remote_desktop.rs b/src/fleet/remote_desktop.rsnew file mode 100644index 0000000..18cd286--- /dev/null+++ b/src/fleet/remote_desktop.rs@@ -0,0 +1,270 @@+//! A machine's own remote desktop (RDP or VNC), in this computer's app for+//! it: the machine's port opened here over its link (see ports.rs, so it+//! works away from home and needs nothing opened in a firewall), then+//! Windows App or Remote Desktop Connection, Screen Sharing, Remmina or+//! FreeRDP started on it. It shows the sign-in screen too, so it works when+//! nobody is signed in there and Muxin's own desktop view has no screen.++use std::path::PathBuf;+use std::process::{Command, Stdio};+use std::sync::Arc;++use serde::{Deserialize, Serialize};++use super::Engine;++#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "lowercase")]+pub enum Protocol {+ Rdp,+ Vnc,+}++impl Protocol {+ pub fn name(self) -> &'static str {+ match self {+ Protocol::Rdp => "Remote Desktop",+ Protocol::Vnc => "VNC",+ }+ }++ /// Where it opens here: not its usual port, which Remote Desktop+ /// Connection and Screen Sharing keep for this computer itself.+ fn local_port(self) -> u16 {+ match self {+ Protocol::Rdp => 3390,+ Protocol::Vnc => 5901,+ }+ }+}++const RDP_PORT: u16 = 3389;+/// VNC servers listen on 5900 + their display number.+const VNC_PORTS: std::ops::RangeInclusive<u16> = 5900..=5909;++/// A machine's remote desktop, open here.+#[derive(Debug, Clone, Serialize)]+pub struct RemoteDesktop {+ pub machine_id: String,+ pub protocol: Protocol,+ /// Where it is here: "localhost:3390".+ pub address: String,+ /// The app it opened in; None when this computer has none for it.+ pub app: Option<String>,+ /// The port there, for closing it (`Engine::stop_forward`).+ pub port: u16,+}++impl RemoteDesktop {+ /// What happened, in a sentence.+ pub fn summary(&self, machine: &str) -> String {+ match &self.app {+ Some(app) => format!("Opened {machine} in {app}, at {} here.", self.address),+ None => format!("{machine}'s {} is at {} here. Open it in your {} app.", self.protocol.name(), self.address, self.protocol.name()),+ }+ }+}++/// The system a machine runs, from its platform ("windows-x64").+#[derive(Debug, Clone, Copy, PartialEq)]+enum System {+ Windows,+ Mac,+ Linux,+}++impl System {+ fn of(platform: &str) -> System {+ if platform.starts_with("windows") {+ System::Windows+ } else if platform.starts_with("darwin") {+ System::Mac+ } else {+ System::Linux+ }+ }+}++/// "Open with Remote Desktop", "Open with Screen Sharing": the menu item and button.+#[cfg_attr(not(feature = "gui"), allow(dead_code))]+pub fn open_label(platform: &str) -> &'static str {+ match System::of(platform) {+ System::Windows => "Open with Remote Desktop",+ System::Mac => "Open with Screen Sharing",+ System::Linux => "Open with RDP or VNC",+ }+}++/// Which protocol and port to use, from what listens there: the one asked+/// for, else the system's own, else the other one. Err says how to turn one on.+fn choose(system: System, name: &str, wanted: Option<Protocol>, listening: &[u16]) -> Result<(Protocol, u16), String> {+ let rdp = listening.contains(&RDP_PORT).then_some((Protocol::Rdp, RDP_PORT));+ let vnc = VNC_PORTS.clone().find(|p| listening.contains(p)).map(|p| (Protocol::Vnc, p));+ let found = match (wanted, system) {+ (Some(Protocol::Rdp), _) => rdp,+ (Some(Protocol::Vnc), _) => vnc,+ (None, System::Mac) => vnc.or(rdp),+ (None, _) => rdp.or(vnc),+ };+ found.ok_or_else(|| match (wanted, system) {+ (Some(Protocol::Rdp), System::Mac) => format!("{name} is a Mac: it takes VNC (Screen Sharing), not Remote Desktop."),+ (Some(Protocol::Vnc), System::Windows) => format!("No VNC server runs on {name}. Windows has none built in: install one such as TightVNC, or use Remote Desktop."),+ (_, System::Windows) => format!("Remote Desktop is off on {name}. Turn it on there in Settings › System › Remote Desktop (on Windows Server: Server Manager › Local Server). Windows Home can't be reached this way."),+ (_, System::Mac) => format!("Screen Sharing is off on {name}. Turn it on there in System Settings › General › Sharing."),+ (Some(Protocol::Rdp), System::Linux) => format!("Nothing on {name} takes Remote Desktop. Install xrdp there, or turn on GNOME's Remote Desktop."),+ (Some(Protocol::Vnc), System::Linux) => format!("No VNC server runs on {name}. Start one there, such as x11vnc or TigerVNC."),+ (None, System::Linux) => format!("Nothing on {name} takes Remote Desktop or VNC. Install xrdp, turn on GNOME's Remote Desktop, or run a VNC server such as x11vnc."),+ })+}++/// The account to sign in as: the one Muxin connects as (the ssh user, as+/// the machine's settings or the ssh config say).+async fn user_of(spec: &crate::config::MachineSpec) -> Option<String> {+ if let Some(u) = spec.user.clone().or_else(|| spec.host.split_once('@').map(|(u, _)| u.to_string())).filter(|u| !u.is_empty()) {+ return Some(u);+ }+ let out = tokio::time::timeout(std::time::Duration::from_secs(3), tokio::process::Command::new("ssh").arg("-G").args(crate::probe::ssh_args(spec)).output()).await.ok()?.ok()?;+ String::from_utf8_lossy(&out.stdout).lines().find_map(|l| l.strip_prefix("user ").map(|u| u.trim().to_string())).filter(|u| !u.is_empty())+}++/// A Remote Desktop file for `address`, as Windows App and Remote Desktop Connection read it.+fn rdp_file(address: &str, user: Option<&str>) -> String {+ let mut out = format!("full address:s:{address}\r\n");+ if let Some(u) = user {+ out.push_str(&format!("username:s:{u}\r\n"));+ }+ out+}++/// Writes the Remote Desktop file for a machine, in the temp folder.+fn write_rdp_file(machine_id: &str, address: &str, user: Option<&str>) -> Option<PathBuf> {+ let path = std::env::temp_dir().join(format!("muxin-{machine_id}.rdp"));+ std::fs::write(&path, rdp_file(address, user)).ok()?;+ Some(path)+}++/// The first of `programs` this computer has, on PATH.+fn on_path<'a>(programs: &[&'a str]) -> Option<(&'a str, PathBuf)> {+ let dirs: Vec<PathBuf> = std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect()).unwrap_or_default();+ programs.iter().find_map(|p| dirs.iter().map(|d| d.join(p)).find(|f| f.is_file()).map(|f| (*p, f)))+}++/// Starts this computer's app for it on `address`; the app's name, or None+/// when there's none here.+fn launch(protocol: Protocol, machine_id: &str, port: u16, user: Option<&str>) -> Option<String> {+ let address = format!("localhost:{port}");+ let spawn = |cmd: &mut Command| cmd.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).spawn().is_ok();+ if cfg!(target_os = "macos") {+ let opened = |what: &str| Command::new("open").arg(what).stdout(Stdio::null()).stderr(Stdio::null()).status().is_ok_and(|s| s.success());+ return match protocol {+ Protocol::Rdp => {+ let file = write_rdp_file(machine_id, &address, user)?;+ opened(&file.display().to_string()).then(|| {+ let home = dirs::home_dir().unwrap_or_default();+ ["Windows App", "Microsoft Remote Desktop"]+ .into_iter()+ .find(|a| [PathBuf::from("/Applications"), home.join("Applications")].iter().any(|d| d.join(format!("{a}.app")).exists()))+ .unwrap_or("your Remote Desktop app")+ .to_string()+ })+ }+ // Screen Sharing, built in.+ Protocol::Vnc => {+ let url = match user {+ Some(u) => format!("vnc://{u}@{address}"),+ None => format!("vnc://{address}"),+ };+ opened(&url).then(|| "Screen Sharing".to_string())+ }+ };+ }+ if cfg!(windows) {+ return match protocol {+ // Remote Desktop Connection, built in.+ Protocol::Rdp => {+ let file = write_rdp_file(machine_id, &address, user)?;+ spawn(Command::new("mstsc.exe").arg(&file)).then(|| "Remote Desktop Connection".to_string())+ }+ // A VNC viewer that took vnc:// links, else TigerVNC's or TightVNC's on PATH.+ Protocol::Vnc => {+ let registered = Command::new("reg").args(["query", r"HKCR\vnc\shell\open\command"]).stdout(Stdio::null()).stderr(Stdio::null()).status().is_ok_and(|s| s.success());+ if registered && spawn(Command::new("rundll32.exe").args(["url.dll,FileProtocolHandler", &format!("vnc://{address}")])) {+ return Some("your VNC viewer".into());+ }+ let (name, path) = on_path(&["vncviewer.exe", "tvnviewer.exe"])?;+ spawn(Command::new(path).arg(format!("localhost::{port}"))).then(|| name.trim_end_matches(".exe").to_string())+ }+ };+ }+ let who = user.map(|u| format!("{u}@")).unwrap_or_default();+ let candidates: Vec<(&str, Vec<String>)> = match protocol {+ Protocol::Rdp => {+ let mut freerdp = vec![format!("/v:{address}")];+ freerdp.extend(user.map(|u| format!("/u:{u}")));+ vec![("remmina", vec!["-c".into(), format!("rdp://{who}{address}")]), ("xfreerdp3", freerdp.clone()), ("sdl-freerdp3", freerdp.clone()), ("xfreerdp", freerdp)]+ }+ Protocol::Vnc => vec![("remmina", vec!["-c".into(), format!("vnc://{who}{address}")]), ("vncviewer", vec![format!("localhost::{port}")])],+ };+ candidates.into_iter().find_map(|(name, args)| {+ let (_, path) = on_path(&[name])?;+ spawn(Command::new(path).args(&args)).then(|| name.to_string())+ })+}++impl Engine {+ /// Opens a machine's own remote desktop in this computer's app for it:+ /// `protocol`, or the one it has (RDP first, VNC on a Mac). Its port+ /// stays open here (Servers lists it) until closed or Muxin quits.+ pub async fn open_remote_desktop(self: &Arc<Self>, machine_id: &str, protocol: Option<Protocol>) -> Result<RemoteDesktop, String> {+ let (spec, _) = self.machine(machine_id).ok_or_else(|| format!("No machine called {machine_id}."))?;+ if spec.is_local() {+ return Err(format!("{} is this computer.", spec.name));+ }+ spec.refuse_if_disconnected()?;+ let platform = self.current().machines.iter().find(|m| m.id == spec.id).map(|m| m.platform.clone()).unwrap_or_default();+ let listening = self.listening(&spec.id).await?;+ let (protocol, port) = choose(System::of(&platform), &spec.name, protocol, &listening)?;+ let forward = self.forward(&spec.id, port, None, Some(protocol.local_port())).await?;+ let user = user_of(&spec).await;+ let (id, local) = (spec.id.clone(), forward.local_port);+ let app = tokio::task::spawn_blocking(move || launch(protocol, &id, local, user.as_deref())).await.unwrap_or(None);+ Ok(RemoteDesktop { machine_id: spec.id, protocol, address: format!("localhost:{local}"), app, port })+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn picks_what_listens() {+ assert_eq!(choose(System::Windows, "srv", None, &[22, 3389, 5985]), Ok((Protocol::Rdp, 3389)));+ assert_eq!(choose(System::Mac, "mini", None, &[22, 5900]), Ok((Protocol::Vnc, 5900)));+ // Linux: RDP when both, else the VNC display that's up.+ assert_eq!(choose(System::Linux, "box", None, &[3389, 5900]), Ok((Protocol::Rdp, 3389)));+ assert_eq!(choose(System::Linux, "box", None, &[5901]), Ok((Protocol::Vnc, 5901)));+ // Windows with a VNC server and Remote Desktop off.+ assert_eq!(choose(System::Windows, "srv", None, &[5900]), Ok((Protocol::Vnc, 5900)));+ assert_eq!(choose(System::Windows, "srv", Some(Protocol::Vnc), &[3389, 5900]), Ok((Protocol::Vnc, 5900)));+ }++ #[test]+ fn says_how_to_turn_it_on() {+ assert!(choose(System::Windows, "srv", None, &[22]).unwrap_err().starts_with("Remote Desktop is off on srv."));+ assert!(choose(System::Mac, "mini", None, &[22]).unwrap_err().starts_with("Screen Sharing is off on mini."));+ assert!(choose(System::Mac, "mini", Some(Protocol::Rdp), &[5900]).unwrap_err().contains("is a Mac"));+ assert!(choose(System::Linux, "box", None, &[]).unwrap_err().contains("xrdp"));+ }++ #[test]+ fn writes_rdp_files() {+ assert_eq!(rdp_file("localhost:3390", Some("adib")), "full address:s:localhost:3390\r\nusername:s:adib\r\n");+ assert_eq!(rdp_file("localhost:3391", None), "full address:s:localhost:3391\r\n");+ }++ #[test]+ fn labels() {+ assert_eq!(open_label("windows-x64"), "Open with Remote Desktop");+ assert_eq!(open_label("darwin-arm64"), "Open with Screen Sharing");+ }+}diff --git a/src/main.rs b/src/main.rsindex 7d51c10..793452d 100644--- a/src/main.rs+++ b/src/main.rs@@ -657,6 +657,10 @@ enum DesktopCommand {Remove(DesktopRemoveArgs),/// Change a monitor's resolution or scaleSet(DesktopSetArgs),+ /// Open the machine's own Remote Desktop (RDP) in your app for it, through Muxin, until Ctrl-C+ Rdp(MachineArgs),+ /// Open the machine's own VNC (a Mac's Screen Sharing) in your app for it, through Muxin, until Ctrl-C+ Vnc(MachineArgs),}#[derive(Args)]@@ -2197,9 +2201,30 @@ fn desktop(cmd: DesktopCommand, runtime: &tokio::runtime::Runtime) -> u8 {let req = api::RemoveDisplayRequest { machine_id: a.machine, display: a.display };run(runtime, &a.agent, async move |e| api::desktop_remove_display(&e, req).await, |v| format!("Removed display {}.\n", v["removed"].as_str().unwrap_or("")))}+ DesktopCommand::Rdp(a) => remote_desktop(a, crate::fleet::remote_desktop::Protocol::Rdp, runtime),+ DesktopCommand::Vnc(a) => remote_desktop(a, crate::fleet::remote_desktop::Protocol::Vnc, runtime),}}+/// `muxin desktop rdp|vnc`: the machine's own remote desktop in this+/// computer's app for it, its port open here until Ctrl-C.+fn remote_desktop(a: MachineArgs, protocol: crate::fleet::remote_desktop::Protocol, runtime: &tokio::runtime::Runtime) -> u8 {+ let req = api::RemoteDesktopRequest { machine_id: a.machine_id, protocol: Some(protocol) };+ run(+ runtime,+ &a.agent,+ async move |e| {+ let opened = api::open_remote_desktop(&e, req).await?;+ let name = e.machine(&opened.machine_id).map(|(spec, _)| spec.name).unwrap_or_else(|| opened.machine_id.clone());+ eprintln!("{} Ctrl-C closes it.", opened.summary(&name));+ let _ = tokio::signal::ctrl_c().await;+ e.stop_forward(&opened.machine_id, opened.port);+ Ok(opened)+ },+ |_| String::new(),+ )+}+fn services(a: ServicesArgs, runtime: &tokio::runtime::Runtime) -> u8 {let machine = a.machine.clone();match (a.action.clone(), a.name.clone()) {diff --git a/src/mcp.rs b/src/mcp.rsindex c24054b..2eb609f 100644--- a/src/mcp.rs+++ b/src/mcp.rs@@ -966,7 +966,7 @@ pub fn tools() -> Vec<Value> {}),json!({"name": "desktop_screenshot",- "description": "A picture of a machine's screen (Linux with an X11 desktop, or a Mac on a direct connection), or of one of its displays. Coordinates for desktop_input are in its pixels.",+ "description": "A picture of a machine's screen (Linux with an X11 desktop, a Mac, or Windows), or of one of its displays. The account Muxin connects as must be signed in there; when nobody is, it says so (the user can open the machine's own Remote Desktop or Screen Sharing from its menu in the app). Coordinates for desktop_input are in its pixels.","inputSchema": schema(json!({"machine_id": { "type": "string", "description": MACHINE_ID },"display": { "type": "string", "description": DISPLAY_ID }diff --git a/src/ui/desktop.rs b/src/ui/desktop.rsindex b2c07d0..1ef3d7f 100644--- a/src/ui/desktop.rs+++ b/src/ui/desktop.rs@@ -48,6 +48,8 @@ enum Msg {/// The stream ended: why, and whether it may well work again in a moment/// (the connection dropped, as opposed to the machine saying no).Ended(String, bool),+ /// Nobody's screen is there to show (nobody signed in): why.+ NoScreen(String),/// The monitor asked for is gone: showing the default one instead.Gone,}@@ -125,6 +127,14 @@ pub struct DesktopView {offline: Option<String>,/// The machine is a Mac (⌘ stays ⌘).mac: bool,+ /// Its platform ("windows-x64"), for what its own remote desktop is called.+ platform: String,+ /// Nobody's screen is there (nobody signed in): its own remote desktop+ /// (RDP, VNC) is offered instead, which can sign someone in.+ no_screen: bool,+ /// Opening that, and what came of it (text, failed).+ opening: bool,+ opened: Option<(String, bool)>,/// What the machine says about the stream (e.g. view only).note: Option<String>,/// A monitor's size or scale being changed, and why the last change didn't happen.@@ -170,6 +180,10 @@ impl DesktopView {frames: 0,offline: None,mac: false,+ platform: String::new(),+ no_screen: false,+ opening: false,+ opened: None,note: None,resuming: false,retries: 0,@@ -179,7 +193,8 @@ impl DesktopView {_tasks: Vec::new(),_subscriptions: Vec::new(),};- view.mac = view.engine.current().machines.iter().any(|m| m.id == view.machine && m.platform.starts_with("darwin"));+ view.platform = view.engine.current().machines.iter().find(|m| m.id == view.machine).map(|m| m.platform.clone()).unwrap_or_default();+ view.mac = view.platform.starts_with("darwin");view.start(window, cx);// Keys and buttons held when the view loses the keyboard (another// app, ⌘Q, a click elsewhere): let go on the machine too.@@ -291,10 +306,38 @@ impl DesktopView {.detach();}+ /// Opens the machine's own remote desktop (RDP, VNC) in this computer's+ /// app for it: what there is when nobody's screen is.+ fn open_remote(&mut self, cx: &mut Context<Self>) {+ self.opening = true;+ self.opened = None;+ let (engine, id) = (self.engine.clone(), self.machine.clone());+ let name = engine.current().machines.iter().find(|m| m.id == id).map(|m| m.name.clone()).unwrap_or_else(|| id.clone());+ let job = engine.runtime().spawn({+ let engine = engine.clone();+ async move { engine.open_remote_desktop(&id, None).await }+ });+ cx.spawn(async move |this, cx| {+ let result = job.await.unwrap_or_else(|e| Err(e.to_string()));+ let _ = this.update(cx, |this, cx| {+ this.opening = false;+ this.opened = Some(match result {+ Ok(r) => (r.summary(&name), false),+ Err(e) => (e, true),+ });+ cx.notify();+ });+ })+ .detach();+ cx.notify();+ }+/// (Re)starts the stream from the machine.fn start(&mut self, window: &mut Window, cx: &mut Context<Self>) {self.remote = None;self.state = State::Connecting;+ self.no_screen = false;+ self.opened = None;// Each stream has its own flag: an old stream's undelivered draw request// mustn't hold up the new one.self.pending = Arc::new(std::sync::atomic::AtomicBool::new(false));@@ -345,7 +388,8 @@ impl DesktopView {}Ok(r) => {link.close_channel(channel);- let _ = tx.unbounded_send(Msg::Ended(r["error"].as_str().unwrap_or("The machine can't show its desktop.").to_string(), false));+ let why = r["error"].as_str().unwrap_or("The machine can't show its desktop.").to_string();+ let _ = tx.unbounded_send(if r["no_session"].as_bool() == Some(true) { Msg::NoScreen(why) } else { Msg::Ended(why, false) });return;}Err(e) => {@@ -405,6 +449,12 @@ impl DesktopView {}}Msg::Gone => this.display = None,+ Msg::NoScreen(why) => {+ this.remote = None;+ this.resuming = false;+ this.no_screen = true;+ this.state = State::Ended(why);+ }Msg::Ended(why, may_return) => {this.remote = None;// The connection dropped (another network, a@@ -729,11 +779,29 @@ impl Render for DesktopView {.gap(px(10.)).child(kit::color_tile("monitor", 44.)).child(div().max_w(px(460.)).text_center().text_size(px(13.)).text_color(theme.text_muted).child(why.clone()))- .child(Button::new("desktop-retry", "Try again").icon("refresh-cw").on_click(cx.listener(|this, _, window, cx| {- this.retries = 0;- this.start(window, cx);- cx.notify();- }))),+ .child(+ div()+ .flex()+ .gap(px(8.))+ // Nobody's screen: the machine's own remote desktop can sign someone in.+ .when(self.no_screen, |d| {+ d.child(+ Button::new("desktop-remote", crate::fleet::remote_desktop::open_label(&self.platform))+ .icon("external-link")+ .style(ButtonStyle::Primary)+ .disabled(self.opening)+ .on_click(cx.listener(|this, _, _, cx| this.open_remote(cx))),+ )+ })+ .child(Button::new("desktop-retry", "Try again").icon("refresh-cw").on_click(cx.listener(|this, _, window, cx| {+ this.retries = 0;+ this.start(window, cx);+ cx.notify();+ }))),+ )+ .children(self.opened.clone().map(|(text, failed)| {+ div().max_w(px(460.)).text_center().text_size(px(12.5)).text_color(if failed { theme.error } else { theme.text_muted }).child(text)+ })),),(State::Connecting, None) if self.resuming && self.frame.is_some() => Some(div().flex().items_center().gap(px(10.)).child(spinner("desktop-resume", 16., theme.text_muted)).child(diff --git a/src/ui/menu.rs b/src/ui/menu.rsindex 568311c..6770415 100644--- a/src/ui/menu.rs+++ b/src/ui/menu.rs@@ -41,6 +41,8 @@ pub struct ContextMenu {enum Action {NewTerminal(String),ShowDesktop(String),+ /// The machine's own remote desktop (RDP, VNC), in this computer's app for it.+ OpenRemoteDesktop(String),ShowLogs(String),ShowServices(String),CopyFiles(String),@@ -191,6 +193,9 @@ impl Root {if super::format::has_desktop(m) && !m.local {items.push(needs_up(act("Show desktop", "monitor", Action::ShowDesktop(id.clone()))));}+ if super::format::has_desktop(m) && !m.local && !s.demo {+ items.push(needs_up(act(crate::fleet::remote_desktop::open_label(&m.platform), "external-link", Action::OpenRemoteDesktop(id.clone()))));+ }if super::format::has_desktop(m) && !s.demo && !m.platform.starts_with("windows") {items.push(needs_up(act("Add virtual monitor…", "monitor", Action::AddDisplay(id.clone()))));}@@ -454,6 +459,7 @@ impl Root {}Action::NewAgent(machine, agent) => self.show_new_session_with(Some(machine), Some(agent), window, cx),Action::ShowDesktop(machine) => self.navigate(Route::Desktop(machine), window, cx),+ Action::OpenRemoteDesktop(machine) => self.open_remote_desktop(&machine, cx),Action::ShowLogs(machine) => self.navigate(Route::Logs(machine), window, cx),Action::ShowServices(machine) => self.navigate(Route::Services(machine), window, cx),Action::CopyFiles(machine) => self.show_copy_files(Some(machine), None, window, cx),diff --git a/src/ui/root.rs b/src/ui/root.rsindex 339894a..541a224 100644--- a/src/ui/root.rs+++ b/src/ui/root.rs@@ -1327,6 +1327,24 @@ impl Root {self.track(task);}+ /// Opens a machine's own remote desktop (RDP, VNC) in this computer's app for it.+ pub(super) fn open_remote_desktop(&mut self, machine_id: &str, cx: &mut Context<Self>) {+ let (engine, machine) = (self.engine.clone(), machine_id.to_string());+ let name = self.snapshot.machines.iter().find(|m| m.id == machine).map(|m| m.name.clone()).unwrap_or_else(|| machine.clone());+ let job = engine.runtime().spawn({+ let engine = engine.clone();+ async move { engine.open_remote_desktop(&machine, None).await }+ });+ let task = cx.spawn(async move |this, cx| {+ let result = job.await.unwrap_or_else(|e| Err(e.to_string()));+ let _ = this.update(cx, |this, cx| match result {+ Ok(r) => this.notify_user(r.summary(&name), false, cx),+ Err(e) => this.notify_user(e, true, cx),+ });+ });+ self.track(task);+ }+/// Connects to a machine again, or disconnects from it (its sessions go on there).pub(super) fn set_machine_connected(&mut self, machine_id: &str, connected: bool, cx: &mut Context<Self>) {if let Err(e) = self.engine.set_connected(machine_id, connected) {