From 012dac9b49709f75dc26a639b243ea4835c1874a Mon Sep 17 00:00:00 2001 From: wisp3rwind <17089248+wisp3rwind@users.noreply.github.com> Date: Sun, 6 Oct 2024 14:07:58 +0200 Subject: [PATCH] discovery: allow running when compiled without zeroconf backend... ...but exit with an error if there's no way to authenticate --- src/main.rs | 59 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index caba0ffd1..6e006dcc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -212,12 +212,11 @@ struct Setup { credentials: Option, enable_oauth: bool, oauth_port: Option, - enable_discovery: bool, zeroconf_port: u16, player_event_program: Option, emit_sink_events: bool, zeroconf_ip: Vec, - zeroconf_backend: librespot::discovery::ServiceBuilder, + zeroconf_backend: Option, } fn get_setup() -> Setup { @@ -1193,9 +1192,18 @@ fn get_setup() -> Setup { } }; - let enable_discovery = !opt_present(DISABLE_DISCOVERY); + let no_discovery_reason = if !cfg!(any(feature = "with-libmdns", feature = "with-dns-sd", feature = "with-avahi")) { + Some("librespot compiled without zeroconf backend".to_owned()) + } else if opt_present(DISABLE_DISCOVERY) { + Some(format!( + "the `--{}` / `-{}` flag set", + DISABLE_DISCOVERY, DISABLE_DISCOVERY_SHORT, + )) + } else { + None + }; - if credentials.is_none() && !enable_discovery && !enable_oauth { + if credentials.is_none() && no_discovery_reason.is_some() && !enable_oauth { error!("Credentials are required if discovery and oauth login are disabled."); exit(1); } @@ -1228,14 +1236,16 @@ fn get_setup() -> Setup { Some(5588) }; - if !enable_discovery && opt_present(ZEROCONF_PORT) { - warn!( - "With the `--{}` / `-{}` flag set `--{}` / `-{}` has no effect.", - DISABLE_DISCOVERY, DISABLE_DISCOVERY_SHORT, ZEROCONF_PORT, ZEROCONF_PORT_SHORT - ); + if let Some(reason) = no_discovery_reason.as_deref() { + if opt_present(ZEROCONF_PORT) { + warn!( + "With {} `--{}` / `-{}` has no effect.", + reason, ZEROCONF_PORT, ZEROCONF_PORT_SHORT + ); + } } - let zeroconf_port = if enable_discovery { + let zeroconf_port = if no_discovery_reason.is_none() { opt_str(ZEROCONF_PORT) .map(|port| match port.parse::() { Ok(value) if value != 0 => value, @@ -1271,6 +1281,15 @@ fn get_setup() -> Setup { None => SessionConfig::default().autoplay, }; + if let Some(reason) = no_discovery_reason.as_deref() { + if opt_present(ZEROCONF_INTERFACE) { + warn!( + "With {} `--{}` / `-{}` has no effect.", + reason, ZEROCONF_INTERFACE, ZEROCONF_INTERFACE_SHORT + ); + } + } + let zeroconf_ip: Vec = if opt_present(ZEROCONF_INTERFACE) { if let Some(zeroconf_ip) = opt_str(ZEROCONF_INTERFACE) { zeroconf_ip @@ -1296,8 +1315,18 @@ fn get_setup() -> Setup { vec![] }; + if let Some(reason) = no_discovery_reason.as_deref() { + if opt_present(ZEROCONF_BACKEND) { + warn!( + "With {} `--{}` / `-{}` has no effect.", + reason, ZEROCONF_BACKEND, ZEROCONF_BACKEND_SHORT + ); + } + } + let zeroconf_backend_name = opt_str(ZEROCONF_BACKEND); - let zeroconf_backend = librespot::discovery::find(zeroconf_backend_name.as_deref()) + let zeroconf_backend = no_discovery_reason.is_none().then(|| { + librespot::discovery::find(zeroconf_backend_name.as_deref()) .unwrap_or_else(|_| { let available_backends: Vec<_> = librespot::discovery::BACKENDS .iter() @@ -1317,7 +1346,8 @@ fn get_setup() -> Setup { ); exit(1); - }); + }) + }); let connect_config = { let connect_default_config = ConnectConfig::default(); @@ -1760,7 +1790,6 @@ fn get_setup() -> Setup { credentials, enable_oauth, oauth_port, - enable_discovery, zeroconf_port, player_event_program, emit_sink_events, @@ -1794,7 +1823,7 @@ async fn main() { let mut sys = System::new(); - if setup.enable_discovery { + if let Some(zeroconf_backend) = setup.zeroconf_backend { // When started at boot as a service discovery may fail due to it // trying to bind to interfaces before the network is actually up. // This could be prevented in systemd by starting the service after @@ -1814,7 +1843,7 @@ async fn main() { .is_group(setup.connect_config.is_group) .port(setup.zeroconf_port) .zeroconf_ip(setup.zeroconf_ip.clone()) - .zeroconf_backend(setup.zeroconf_backend) + .zeroconf_backend(zeroconf_backend) .launch() { Ok(d) => break Some(d),