git.delta.rocks / remowt / refs/commits / eadb0bb3c19e

difftreelog

feat auto prompter

ymurrvruYaroslav Bolyukin4 days agoparent: #751b8f1.patch.diff
in: trunk

9 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -308,9 +308,9 @@
 
 [[package]]
 name = "bifrostlink"
-version = "0.2.3"
+version = "0.2.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "704657867d2107831c57edd363726440c86675464b5fc113702854e17062cafc"
+checksum = "ad2d0e30a2aa432b78f41f9676572f88201d4dc73bc2b7bc90704d2e02b7d062"
 dependencies = [
  "async-trait",
  "async_fn_traits",
@@ -327,9 +327,9 @@
 
 [[package]]
 name = "bifrostlink-macros"
-version = "0.2.3"
+version = "0.2.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "158308eb569b467c0116680f79d0ecc389f4d540f6d5a0c9279bfe79b1cd5bdb"
+checksum = "e2121559c45cbe89c4f8d1d741360d5b028577254f6beca053dc02332da85b43"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -338,9 +338,9 @@
 
 [[package]]
 name = "bifrostlink-ports"
-version = "0.2.3"
+version = "0.2.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7612993f0bd8bc6a71867461266567212a35a716b2a5aef5f9967ab08c891782"
+checksum = "e9395c4ccca497b0c50583e6de57aca921c046ae0c10f56030cd2c5a20db05f8"
 dependencies = [
  "bifrostlink",
  "bytes",
@@ -1835,7 +1835,7 @@
 
 [[package]]
 name = "polkit-backend"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
  "anyhow",
  "clap",
@@ -2055,7 +2055,7 @@
 
 [[package]]
 name = "remowt-agent"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
  "anyhow",
  "bifrostlink",
@@ -2083,7 +2083,7 @@
 
 [[package]]
 name = "remowt-client"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
  "anyhow",
  "bifrostlink",
@@ -2106,7 +2106,7 @@
 
 [[package]]
 name = "remowt-endpoints"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
  "anyhow",
  "bifrostlink",
@@ -2124,7 +2124,7 @@
 
 [[package]]
 name = "remowt-link-shared"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
  "bifrostlink",
  "bytes",
@@ -2138,7 +2138,7 @@
 
 [[package]]
 name = "remowt-plugin"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
  "anyhow",
  "bifrostlink",
@@ -2152,7 +2152,7 @@
 
 [[package]]
 name = "remowt-polkit-shared"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
  "nix",
  "serde",
@@ -2161,7 +2161,7 @@
 
 [[package]]
 name = "remowt-ssh"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
  "anyhow",
  "async-trait",
@@ -2189,8 +2189,9 @@
 
 [[package]]
 name = "remowt-ui-prompt"
-version = "0.1.3"
+version = "0.1.4"
 dependencies = [
+ "anyhow",
  "bifrostlink",
  "bifrostlink-macros",
  "serde",
modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,7 +3,7 @@
 resolver = "2"
 
 [workspace.package]
-version = "0.1.3"
+version = "0.1.4"
 license = "MIT"
 edition = "2021"
 repository = "https://git.delta.rocks/r/remowt"
@@ -52,7 +52,6 @@
 thiserror = "2.0.18"
 
 [profile.release]
-strip = true
 panic = "abort"
 opt-level = "z"
 lto = true
modifiedcmds/remowt-agent/src/askpass.rsdiffbeforeafterboth
--- a/cmds/remowt-agent/src/askpass.rs
+++ b/cmds/remowt-agent/src/askpass.rs
@@ -2,24 +2,25 @@
 use std::io::Write as _;
 
 use anyhow::Context as _;
-use remowt_ui_prompt::bifrost::PromptEndpointsClient;
-use remowt_ui_prompt::dbus::{DbusPrompterInterface, DbusPrompterProxy};
-use remowt_ui_prompt::Source;
+use remowt_ui_prompt::dbus::{DbusPrompterInterface, DbusPrompterProxy, BUS_NAME, PROMPTER_PATH};
+use remowt_ui_prompt::{Prompter, Source};
+use tracing::debug;
 use zbus::Connection;
-
-use remowt_link_shared::BifConfig;
-
-const BUS_NAME: &str = "lach.RemowtAskpass";
-const PROMPTER_PATH: &str = "/lach/Askpass";
 
-pub async fn serve(
-	conn: &Connection,
-	prompter: PromptEndpointsClient<BifConfig>,
-) -> anyhow::Result<()> {
+pub async fn serve<P>(conn: &Connection, prompter: P) -> anyhow::Result<()>
+where
+	P: Prompter + 'static,
+{
 	conn.object_server()
 		.at(PROMPTER_PATH, DbusPrompterInterface(prompter))
 		.await?;
-	conn.request_name(BUS_NAME).await?;
+	match conn.request_name(BUS_NAME).await {
+		Ok(()) => {}
+		Err(zbus::Error::NameTaken) => {
+			debug!("{BUS_NAME} already owned, chaining to upstream");
+		}
+		Err(e) => return Err(e.into()),
+	}
 	Ok(())
 }
 
modifiedcmds/remowt-agent/src/main.rsdiffbeforeafterboth
--- a/cmds/remowt-agent/src/main.rs
+++ b/cmds/remowt-agent/src/main.rs
@@ -11,7 +11,9 @@
 use bifrostlink_ports::stdio::from_stdio;
 use bifrostlink_ports::unix_socket::from_socket;
 use clap::Parser;
-use remowt_endpoints::{fs::Fs, nix_daemon::NixDaemon, pty::Pty, subprocess::Subprocess, systemd::Systemd};
+use remowt_endpoints::{
+	fs::Fs, nix_daemon::NixDaemon, pty::Pty, subprocess::Subprocess, systemd::Systemd,
+};
 use remowt_link_shared::{editor::EditorEndpointsClient, Address, BifConfig};
 use remowt_polkit_shared::{emphasize, BackendRequest, Identity, PidDisplay};
 use remowt_ui_prompt::bifrost::PromptEndpointsClient;
@@ -250,13 +252,16 @@
 	}
 }
 async fn main_real() -> anyhow::Result<()> {
-	let conn = Connection::system().await?;
+	let system_conn = Connection::system().await?;
 	let helper = SocketHelper {
 		fallback: SuidHelper,
 	};
-	register_auth_agent(&conn, Agent::new(helper, RofiPrompter)).await?;
+	register_auth_agent(&system_conn, Agent::new(helper, RofiPrompter)).await?;
+
+	let session_conn = Connection::session().await?;
+	askpass::serve(&session_conn, RofiPrompter).await?;
 
-	let _conn = conn;
+	let _keep_alive = (system_conn, session_conn);
 	pending().await
 }
 async fn main_real_agent(
modifiedcmds/remowt-ssh/src/main.rsdiffbeforeafterboth
--- a/cmds/remowt-ssh/src/main.rs
+++ b/cmds/remowt-ssh/src/main.rs
@@ -13,8 +13,8 @@
 use remowt_client::editor::SshEditor;
 use remowt_client::{AgentBundle, Remowt};
 use remowt_link_shared::editor::serve_editor;
+use remowt_ui_prompt::auto::AutoPrompter;
 use remowt_ui_prompt::bifrost::serve_prompts;
-use remowt_ui_prompt::rofi::RofiPrompter;
 use remowt_ui_prompt::{PrependSourcePrompter, Source};
 use tokio::io::unix::AsyncFd;
 use tokio::io::{AsyncRead, ReadBuf};
@@ -61,7 +61,7 @@
 	serve_prompts(
 		&mut rpc,
 		PrependSourcePrompter {
-			prompter: RofiPrompter,
+			prompter: AutoPrompter::new().await,
 			source: match opts {
 				Opts::Ssh { host, .. } => vec![Source(Cow::Owned(format!("ssh host: {}", host)))],
 				Opts::Local { .. } => vec![],
modifiedcrates/remowt-ui-prompt/Cargo.tomldiffbeforeafterboth
--- a/crates/remowt-ui-prompt/Cargo.toml
+++ b/crates/remowt-ui-prompt/Cargo.toml
@@ -6,6 +6,7 @@
 license.workspace = true
 
 [dependencies]
+anyhow.workspace = true
 bifrostlink.workspace = true
 bifrostlink-macros.workspace = true
 serde.workspace = true
addedcrates/remowt-ui-prompt/src/auto.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/remowt-ui-prompt/src/auto.rs
@@ -0,0 +1,83 @@
+use anyhow::bail;
+use tracing::debug;
+use zbus::fdo::DBusProxy;
+use zbus::names::BusName;
+
+use crate::dbus::{DbusPrompterProxy, BUS_NAME, PROMPTER_PATH};
+use crate::rofi::RofiPrompter;
+use crate::{Prompter, Result, Source};
+
+pub struct AutoPrompter {
+	dbus: Option<DbusPrompterProxy<'static>>,
+	fallback: RofiPrompter,
+}
+
+impl AutoPrompter {
+	pub async fn new() -> Self {
+		let dbus = match Self::try_dbus().await {
+			Ok(p) => Some(p),
+			Err(e) => {
+				debug!("dbus prompter unavailable, falling back to rofi: {e}");
+				None
+			}
+		};
+		Self {
+			dbus,
+			fallback: RofiPrompter,
+		}
+	}
+
+	async fn try_dbus() -> anyhow::Result<DbusPrompterProxy<'static>> {
+		let conn = zbus::Connection::session().await?;
+		let dbus = DBusProxy::new(&conn).await?;
+		let name = BusName::try_from(BUS_NAME)?;
+		if !dbus.name_has_owner(name).await? {
+			bail!("{BUS_NAME} not registered on session bus");
+		}
+		let proxy = DbusPrompterProxy::builder(&conn)
+			.destination(BUS_NAME)?
+			.path(PROMPTER_PATH)?
+			.build()
+			.await?;
+		Ok(proxy)
+	}
+}
+
+impl Prompter for AutoPrompter {
+	async fn prompt_enum(
+		&self,
+		prompt: &str,
+		description: &str,
+		variants: &[&str],
+		source: &[Source],
+	) -> Result<u32> {
+		if let Some(dbus) = &self.dbus {
+			return Prompter::prompt_enum(dbus, prompt, description, variants, source).await;
+		}
+		self.fallback
+			.prompt_enum(prompt, description, variants, source)
+			.await
+	}
+
+	async fn prompt_text(
+		&self,
+		echo: bool,
+		prompt: &str,
+		description: &str,
+		source: &[Source],
+	) -> Result<String> {
+		if let Some(dbus) = &self.dbus {
+			return Prompter::prompt_text(dbus, echo, prompt, description, source).await;
+		}
+		self.fallback
+			.prompt_text(echo, prompt, description, source)
+			.await
+	}
+
+	async fn display_text(&self, error: bool, description: &str, source: &[Source]) -> Result<()> {
+		if let Some(dbus) = &self.dbus {
+			return Prompter::display_text(dbus, error, description, source).await;
+		}
+		self.fallback.display_text(error, description, source).await
+	}
+}
modifiedcrates/remowt-ui-prompt/src/dbus.rsdiffbeforeafterboth
--- a/crates/remowt-ui-prompt/src/dbus.rs
+++ b/crates/remowt-ui-prompt/src/dbus.rs
@@ -5,17 +5,25 @@
 use crate::{BlockingPrompter, Result};
 use crate::{Error, Prompter};
 
+pub const BUS_NAME: &str = "lach.RemowtAskpass";
+pub const PROMPTER_PATH: &str = "/lach/Askpass";
+
 pub struct DbusPrompterInterface<P>(pub P);
 
 #[interface(name = "lach.PolkitInputHandler")]
 impl<P: Prompter + Send + Sync + 'static> DbusPrompterInterface<P> {
-	async fn prompt_radio(
+	async fn prompt_enum(
 		&self,
 		prompt: &str,
 		description: &str,
+		variants: Vec<String>,
 		source: Vec<Source>,
-	) -> fdo::Result<bool> {
-		Ok(self.0.prompt_radio(prompt, description, &source).await?)
+	) -> fdo::Result<u32> {
+		let variants: Vec<&str> = variants.iter().map(|v| v.as_str()).collect();
+		Ok(self
+			.0
+			.prompt_enum(prompt, description, &variants, &source)
+			.await?)
 	}
 	async fn prompt_text(
 		&self,
modifiedcrates/remowt-ui-prompt/src/lib.rsdiffbeforeafterboth
after · crates/remowt-ui-prompt/src/lib.rs
1use core::fmt;2use std::borrow::Cow;3use std::future::Future;4use std::result;56pub mod auto;7pub mod bifrost;8pub mod dbus;9pub mod rofi;1011#[derive(thiserror::Error, Debug, serde::Serialize, serde::Deserialize)]12pub enum Error {13	#[error("user has cancelled input")]14	Cancel,15	#[error("input error: {0}")]16	InputError(String),17	#[error("unknown remote error: {0}")]18	Remote(String),19}2021pub type Result<T, E = Error> = result::Result<T, E>;2223#[cfg_attr(feature = "dbus", derive(zbus::zvariant::Type))]24#[derive(serde::Serialize, serde::Deserialize, Clone)]25pub struct Source(pub Cow<'static, str>);26impl fmt::Display for Source {27	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {28		write!(f, "<u>{}</u>", self.0)29	}30}3132pub trait Prompter: Send + Sync {33	fn prompt_radio(34		&self,35		prompt: &str,36		description: &str,37		source: &[Source],38	) -> impl Future<Output = Result<bool>> + Send {39		let fut = self.prompt_enum(prompt, description, &["No", "Yes"], source);40		async { fut.await.map(|v| v == 1) }41	}42	fn prompt_enum(43		&self,44		prompt: &str,45		description: &str,46		variants: &[&str],47		source: &[Source],48	) -> impl Future<Output = Result<u32>> + Send;49	fn prompt_text(50		&self,51		echo: bool,52		prompt: &str,53		description: &str,54		source: &[Source],55	) -> impl Future<Output = Result<String>> + Send;56	fn display_text(57		&self,58		error: bool,59		description: &str,60		source: &[Source],61	) -> impl Future<Output = Result<()>> + Send;62}63pub trait BlockingPrompter {64	fn prompt_radio(&self, prompt: &str, description: &str, source: &[Source]) -> Result<bool> {65		self.prompt_enum(prompt, description, &["No", "Yes"], source)66			.map(|v| v == 1)67	}68	fn prompt_enum(69		&self,70		prompt: &str,71		description: &str,72		variants: &[&str],73		source: &[Source],74	) -> Result<u32>;75	fn prompt_text(76		&self,77		echo: bool,78		prompt: &str,79		description: &str,80		source: &[Source],81	) -> Result<String>;82	fn display_text(&self, error: bool, description: &str, source: &[Source]) -> Result<()>;83}84impl<P> Prompter for &P85where86	P: Prompter,87{88	fn prompt_radio(89		&self,90		prompt: &str,91		description: &str,92		source: &[Source],93	) -> impl Future<Output = Result<bool>> + Send {94		(*self).prompt_radio(prompt, description, source)95	}9697	fn prompt_enum(98		&self,99		prompt: &str,100		description: &str,101		variants: &[&str],102		source: &[Source],103	) -> impl Future<Output = Result<u32>> + Send {104		(*self).prompt_enum(prompt, description, variants, source)105	}106107	fn prompt_text(108		&self,109		echo: bool,110		prompt: &str,111		description: &str,112		source: &[Source],113	) -> impl Future<Output = Result<String>> + Send {114		(*self).prompt_text(echo, prompt, description, source)115	}116117	fn display_text(118		&self,119		error: bool,120		description: &str,121		source: &[Source],122	) -> impl Future<Output = Result<()>> + Send {123		(*self).display_text(error, description, source)124	}125}126127pub struct PrependSourcePrompter<P> {128	pub prompter: P,129	pub source: Vec<Source>,130	pub description: String,131}132impl<P> PrependSourcePrompter<P> {133	fn source(&self, input: &[Source]) -> Vec<Source> {134		let mut out = self.source.clone();135		out.extend(input.iter().cloned());136		out137	}138	fn description(&self, input: &str) -> String {139		if self.description.is_empty() {140			input.to_owned()141		} else if input.is_empty() {142			self.description.to_owned()143		} else {144			format!("{input}\n\n{}", self.description)145		}146	}147}148impl<P> Prompter for PrependSourcePrompter<P>149where150	P: Prompter + Sync,151{152	async fn prompt_radio(153		&self,154		prompt: &str,155		description: &str,156		source: &[Source],157	) -> Result<bool> {158		self.prompter159			.prompt_radio(prompt, &self.description(description), &self.source(source))160			.await161	}162163	async fn prompt_enum(164		&self,165		prompt: &str,166		description: &str,167		variants: &[&str],168		source: &[Source],169	) -> Result<u32> {170		self.prompter171			.prompt_enum(172				prompt,173				&self.description(description),174				variants,175				&self.source(source),176			)177			.await178	}179180	async fn prompt_text(181		&self,182		echo: bool,183		prompt: &str,184		description: &str,185		source: &[Source],186	) -> Result<String> {187		self.prompter188			.prompt_text(189				echo,190				prompt,191				&self.description(description),192				&self.source(source),193			)194			.await195	}196197	async fn display_text(&self, error: bool, description: &str, source: &[Source]) -> Result<()> {198		self.prompter199			.display_text(error, &self.description(description), &self.source(source))200			.await201	}202}