Skip to content

Commit

Permalink
discovery: allow running when compiled without zeroconf backend...
Browse files Browse the repository at this point in the history
...but exit with an error if there's no way to authenticate
  • Loading branch information
wisp3rwind committed Oct 6, 2024
1 parent 33964da commit 012dac9
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,11 @@ struct Setup {
credentials: Option<Credentials>,
enable_oauth: bool,
oauth_port: Option<u16>,
enable_discovery: bool,
zeroconf_port: u16,
player_event_program: Option<String>,
emit_sink_events: bool,
zeroconf_ip: Vec<std::net::IpAddr>,
zeroconf_backend: librespot::discovery::ServiceBuilder,
zeroconf_backend: Option<librespot::discovery::ServiceBuilder>,
}

fn get_setup() -> Setup {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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::<u16>() {
Ok(value) if value != 0 => value,
Expand Down Expand Up @@ -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<std::net::IpAddr> = if opt_present(ZEROCONF_INTERFACE) {
if let Some(zeroconf_ip) = opt_str(ZEROCONF_INTERFACE) {
zeroconf_ip
Expand All @@ -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()
Expand All @@ -1317,7 +1346,8 @@ fn get_setup() -> Setup {
);

exit(1);
});
})
});

let connect_config = {
let connect_default_config = ConnectConfig::default();
Expand Down Expand Up @@ -1760,7 +1790,6 @@ fn get_setup() -> Setup {
credentials,
enable_oauth,
oauth_port,
enable_discovery,
zeroconf_port,
player_event_program,
emit_sink_events,
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down

0 comments on commit 012dac9

Please sign in to comment.