Skip to content

Commit

Permalink
Log command errors (#74)
Browse files Browse the repository at this point in the history
* better error messages

* peer config error

* liines -> lines

* fix macros

* remove redundant errors
  • Loading branch information
t-aleksander authored Oct 2, 2024
1 parent f68b3b3 commit 713f108
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub enum WireguardInterfaceError {
ExecutableNotFound(String),
#[error("Unix socket error: {0}")]
UnixSockerError(String),
#[error("Peer configuration error")]
PeerConfigurationError,
#[error("Peer configuration error: {0}")]
PeerConfigurationError(String),
#[error("Interface data read error: {0}")]
ReadInterfaceError(String),
#[error("Netlink error: {0}")]
Expand All @@ -30,8 +30,8 @@ pub enum WireguardInterfaceError {
UserspaceNotSupported,
#[error("Kernel support is not available on this platform")]
KernelNotSupported,
#[error("DNS error")]
DnsError,
#[error("DNS error: {0}")]
DnsError(String),
#[error("Service installation failed: `{message}`")]
ServiceInstallationFailed {
err: std::io::Error,
Expand Down
55 changes: 35 additions & 20 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,30 @@ pub(crate) fn configure_dns(
debug!("Executing command resolvconf with args: {args:?}");
cmd.args(args);

// Execute resolvconf command and pipe filtered DNS entries
if let Ok(mut child) = cmd.stdin(Stdio::piped()).spawn() {
if let Some(mut stdin) = child.stdin.take() {
for entry in dns {
debug!("Adding nameserver entry: {entry}");
writeln!(stdin, "nameserver {entry}")?;
match cmd.stdin(Stdio::piped()).spawn() {
Ok(mut child) => {
if let Some(mut stdin) = child.stdin.take() {
for entry in dns {
debug!("Adding nameserver entry: {entry}");
writeln!(stdin, "nameserver {entry}")?;
}
for domain in search_domains {
debug!("Adding search domain entry: {domain}");
writeln!(stdin, "search {domain}")?;
}
}
for domain in search_domains {
debug!("Adding search domain entry: {domain}");
writeln!(stdin, "search {domain}")?;

let status = child.wait().expect("Failed to wait for command");
if status.success() {
Ok(())
} else {
Err(WireguardInterfaceError::DnsError(format!("Failed to execute resolvconf command while setting DNS servers and search domains: {status}")))
}
}

let status = child.wait().expect("Failed to wait for command");
if status.success() {
return Ok(());
Err(e) => {
Err(WireguardInterfaceError::DnsError(format!("Failed to execute resolvconf command while setting DNS servers and search domains: {e}")))
}
}

Err(WireguardInterfaceError::DnsError)
}

#[cfg(target_os = "macos")]
Expand All @@ -81,7 +85,10 @@ fn network_services() -> Result<Vec<String>, IoError> {

Ok(lines)
} else {
Err(IoError::other("command failed"))
Err(IoError::other(format!(
"network setup command failed: {}",
output.status
)))
}
}

Expand All @@ -103,7 +110,9 @@ pub(crate) fn configure_dns(

let status = cmd.status()?;
if !status.success() {
warn!("Command `networksetup` failed while setting DNS servers for {service}");
return Err(WireguardInterfaceError::DnsError(format!(
"Command `networksetup` failed while setting DNS servers for {service}: {status}"
)));
}

// Set search domains, if empty, clear all search domains.
Expand All @@ -116,8 +125,10 @@ pub(crate) fn configure_dns(
} else {
cmd.args(search_domains.iter());
}
if !cmd.status()?.success() {
warn!("Command `networksetup` failed while setting search domains for {service}");

let status = cmd.status()?;
if !status.success() {
return Err(WireguardInterfaceError::DnsError(format!("Command `networksetup` failed while setting search domains for {service}: {status}")));
}
}

Expand Down Expand Up @@ -322,7 +333,11 @@ pub(crate) fn clean_fwmark_rules(fwmark: u32) -> Result<(), WireguardInterfaceEr

/// Resolves domain name to [`SocketAddr`].
pub fn resolve(addr: &str) -> Result<SocketAddr, WireguardInterfaceError> {
let error = || WireguardInterfaceError::PeerConfigurationError;
let error = || {
WireguardInterfaceError::PeerConfigurationError(format!(
"Failed to resolve address: {addr}"
))
};
addr.to_socket_addrs()
.map_err(|_| error())?
.next()
Expand Down
17 changes: 13 additions & 4 deletions src/wgapi_userspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,15 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
socket.write_all(b"set=1\n")?;
socket.write_all(peer.as_uapi_update().as_bytes())?;
socket.write_all(b"\n")?;
let errno = Self::parse_errno(socket);

if Self::parse_errno(socket) == 0 {
if errno == 0 {
Ok(())
} else {
Err(WireguardInterfaceError::PeerConfigurationError)
Err(WireguardInterfaceError::PeerConfigurationError(format!(
"Failed to configure peer {peer:?} on interface {}, errno: {errno}",
self.ifname
)))
}
}

Expand All @@ -283,10 +287,15 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
)?;
socket.write_all(b"\n")?;

if Self::parse_errno(socket) == 0 {
let errno = Self::parse_errno(socket);

if errno == 0 {
Ok(())
} else {
Err(WireguardInterfaceError::PeerConfigurationError)
Err(WireguardInterfaceError::PeerConfigurationError(format!(
"Failed to remove peer with public key {peer_pubkey} from interface {}, errno: {errno}",
self.ifname
)))
}
}

Expand Down

0 comments on commit 713f108

Please sign in to comment.