Skip to content

Commit

Permalink
feat: skip Dnsmasq and Caddy if can't read /etc/resolver (#132)
Browse files Browse the repository at this point in the history
This should fix cases where starting Linkup on a system that does not
have `/etc/resolver` setup would break.
Now it will skip both `caddy` and `dnsmasq` if the folder is not found.
  • Loading branch information
augustoccesar authored Jan 3, 2025
1 parent 10406b2 commit b00f3e2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
15 changes: 12 additions & 3 deletions linkup-cli/src/commands/local_dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,18 @@ fn uninstall_resolvers(resolve_domains: &[String]) -> Result<()> {
Ok(())
}

pub fn list_resolvers() -> Result<Vec<String>> {
let resolvers = fs::read_dir("/etc/resolver/")?
.map(|f| f.unwrap().file_name().into_string().unwrap())
pub fn list_resolvers() -> std::result::Result<Vec<String>, std::io::Error> {
let resolvers_dir = match fs::read_dir("/etc/resolver/") {
Ok(read_dir) => read_dir,
Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => return Ok(vec![]),
_ => return Err(err),
},
};

let resolvers = resolvers_dir
.filter_map(|entry| entry.ok())
.filter_map(|entry| entry.file_name().into_string().ok())
.collect();

Ok(resolvers)
Expand Down
34 changes: 24 additions & 10 deletions linkup-cli/src/services/caddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ impl Caddy {
output_str.contains("redis")
}

fn should_start(&self, domains: &[String]) -> bool {
let resolvers = local_dns::list_resolvers().unwrap();
fn should_start(&self, domains: &[String]) -> Result<bool, Error> {
let resolvers = local_dns::list_resolvers()?;

domains.iter().any(|domain| resolvers.contains(domain))
Ok(domains.iter().any(|domain| resolvers.contains(domain)))
}

pub fn running_pid(&self) -> Option<String> {
Expand All @@ -180,14 +180,28 @@ impl BackgroundService<Error> for Caddy {
) -> Result<(), Error> {
let domains = &state.domain_strings();

if !self.should_start(domains) {
self.notify_update_with_details(
&status_sender,
super::RunStatus::Skipped,
"Local DNS not installed",
);
match self.should_start(domains) {
Ok(true) => (),
Ok(false) => {
self.notify_update_with_details(
&status_sender,
super::RunStatus::Skipped,
"Local DNS not installed",
);

return Ok(());
return Ok(());
}
Err(err) => {
self.notify_update_with_details(
&status_sender,
super::RunStatus::Skipped,
"Failed to read resolvers folder",
);

log::warn!("Failed to read resolvers folder: {}", err);

return Ok(());
}
}

self.notify_update(&status_sender, super::RunStatus::Starting);
Expand Down
36 changes: 25 additions & 11 deletions linkup-cli/src/services/dnsmasq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ pid-file={}\n",
signal::get_running_pid(&self.pid_file_path)
}

fn should_start(&self, domains: &[String]) -> bool {
let resolvers = local_dns::list_resolvers().unwrap();
fn should_start(&self, domains: &[String]) -> Result<bool, Error> {
let resolvers = local_dns::list_resolvers()?;

domains.iter().any(|domain| resolvers.contains(domain))
Ok(domains.iter().any(|domain| resolvers.contains(domain)))
}
}

Expand All @@ -107,14 +107,28 @@ impl BackgroundService<Error> for Dnsmasq {
) -> Result<(), Error> {
let domains = &state.domain_strings();

if !self.should_start(domains) {
self.notify_update_with_details(
&status_sender,
super::RunStatus::Skipped,
"Local DNS not installed",
);

return Ok(());
match self.should_start(domains) {
Ok(true) => (),
Ok(false) => {
self.notify_update_with_details(
&status_sender,
super::RunStatus::Skipped,
"Local DNS not installed",
);

return Ok(());
}
Err(err) => {
self.notify_update_with_details(
&status_sender,
super::RunStatus::Skipped,
"Failed to read resolvers folder",
);

log::warn!("Failed to read resolvers folder: {}", err);

return Ok(());
}
}

self.notify_update(&status_sender, super::RunStatus::Starting);
Expand Down

0 comments on commit b00f3e2

Please sign in to comment.