Skip to content

Commit

Permalink
change some logs (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-aleksander authored Oct 16, 2024
1 parent a7840aa commit 6b78ce5
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ pub(crate) fn check_external_dependencies() -> Result<(), WireguardInterfaceErro
let missing = COMMANDS.iter().find(|cmd| {
env::split_paths(&paths)
.find(|dir| {
debug!("Trying to find {cmd} in {dir:?}");
trace!("Trying to find {cmd} in {dir:?}");
match dir.join(cmd).try_exists() {
Ok(true) => {
debug!("{cmd} found in {dir:?}");
true
}
Ok(false) => {
debug!("{cmd} not found in {dir:?}");
trace!("{cmd} not found in {dir:?}");
false
}
Err(err) => {
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ pub enum WireguardInterfaceError {
err: std::io::Error,
message: String,
},
#[error("Socket is closed: {0}")]
SocketClosed(String),
}
20 changes: 11 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn configure_dns(

let status = child.wait().expect("Failed to wait for command");
if status.success() {
info!("DNS servers and search domains set successfully for interface {ifname}");
debug!("DNS servers and search domains set successfully for interface {ifname}");
Ok(())
} else {
Err(WireguardInterfaceError::DnsError(format!("Failed to execute resolvconf command while setting DNS servers and search domains: {status}")))
Expand All @@ -74,7 +74,6 @@ pub(crate) fn configure_dns(
#[cfg(target_os = "macos")]
/// Obtain list of network services
fn network_services() -> Result<Vec<String>, IoError> {
debug!("Getting list of network services");
let output = Command::new("networksetup")
.arg("-listallnetworkservices")
.output()?;
Expand All @@ -87,7 +86,7 @@ fn network_services() -> Result<Vec<String>, IoError> {
.lines()
.filter_map(|line| line.ok().filter(|line| !line.contains('*')))
.collect();
debug!("Network services: {lines:?}");
debug!("Found following network services: {lines:?}");
Ok(lines)
} else {
Err(IoError::other(format!(
Expand All @@ -104,8 +103,11 @@ pub(crate) fn configure_dns(
) -> Result<(), WireguardInterfaceError> {
debug!("Configuring DNS servers and search domains, DNS: {dns:?}, search domains: {search_domains:?}");

debug!("Setting DNS servers and search domains for all network services");
for service in network_services()? {
debug!("Setting DNS entries for {service}");
debug!(
"Setting DNS entries (search domains and DNS servers) for network service {service}"
);
let mut cmd = Command::new("networksetup");
cmd.arg("-setdnsservers").arg(&service);
if dns.is_empty() {
Expand All @@ -121,7 +123,7 @@ pub(crate) fn configure_dns(
"Command `networksetup` failed while setting DNS servers for {service}: {status}"
)));
}
info!("DNS servers set successfully for {service}");
debug!("DNS servers set successfully for {service}");

// Set search domains, if empty, clear all search domains.
debug!("Setting search domains for {service}");
Expand All @@ -139,10 +141,10 @@ pub(crate) fn configure_dns(
return Err(WireguardInterfaceError::DnsError(format!("Command `networksetup` failed while setting search domains for {service}: {status}")));
}

info!("Search domains set successfully for {service}");
debug!("Search domains set successfully for {service}");
}

info!("DNS servers and search domains set successfully");
debug!("The following DNS servers and search domains were set successfully: DNS: {dns:?}, search domains: {search_domains:?}");
Ok(())
}

Expand Down Expand Up @@ -244,7 +246,7 @@ pub(crate) fn add_peer_routing(
debug!("Route added for allowed IP: {allowed_ip}");
}
}
info!("Peers routing added successfully");
debug!("Peers routing added successfully");
Ok(())
}

Expand Down Expand Up @@ -410,7 +412,7 @@ pub(crate) fn add_peer_routing(
}
}

info!("Peers routing added successfully");
debug!("Peers routing added successfully");
Ok(())
}

Expand Down
14 changes: 9 additions & 5 deletions src/wgapi_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ impl WireguardInterfaceApi for WGApi<Kernel> {
fn create_interface(&self) -> Result<(), WireguardInterfaceError> {
debug!("Creating interface {}", &self.ifname);
bsd::create_interface(&self.ifname)?;
info!("Interface {} created successfully", &self.ifname);
debug!("Interface {} created successfully", &self.ifname);
Ok(())
}

fn assign_address(&self, address: &IpAddrMask) -> Result<(), WireguardInterfaceError> {
debug!("Assigning address {address} to interface {}", self.ifname);
bsd::assign_address(&self.ifname, address)?;
info!(
debug!(
"Address {address} assigned to interface {} successfully",
self.ifname
);
Expand Down Expand Up @@ -89,7 +89,11 @@ impl WireguardInterfaceApi for WGApi<Kernel> {
}

info!(
"Interface {} configured successfully with config: {config:?}",
"Interface {} has been successfully configured. It has been assigned the following address: {}",
self.ifname, address
);
debug!(
"Interface {} configured with config: {config:?}",
self.ifname
);
Ok(())
Expand All @@ -110,7 +114,7 @@ impl WireguardInterfaceApi for WGApi<Kernel> {
fn configure_peer(&self, peer: &Peer) -> Result<(), WireguardInterfaceError> {
debug!("Configuring peer {peer:?} on interface {}", self.ifname);
bsd::set_peer(&self.ifname, peer)?;
info!(
debug!(
"Peer {peer:?} configured successfully on interface {}",
self.ifname
);
Expand All @@ -123,7 +127,7 @@ impl WireguardInterfaceApi for WGApi<Kernel> {
self.ifname
);
bsd::delete_peer(&self.ifname, peer_pubkey)?;
info!(
debug!(
"Peer with public key {peer_pubkey} removed successfully from interface {}",
self.ifname
);
Expand Down
14 changes: 9 additions & 5 deletions src/wgapi_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ impl WireguardInterfaceApi for WGApi<Kernel> {
fn create_interface(&self) -> Result<(), WireguardInterfaceError> {
debug!("Creating interface {}", self.ifname);
netlink::create_interface(&self.ifname)?;
info!("Interface {} created successfully", self.ifname);
debug!("Interface {} created successfully", self.ifname);
Ok(())
}

fn assign_address(&self, address: &IpAddrMask) -> Result<(), WireguardInterfaceError> {
debug!("Assigning address {address} to interface {}", self.ifname);
netlink::address_interface(&self.ifname, address)?;
info!(
debug!(
"Address {address} assigned to interface {} successfully",
self.ifname
);
Expand Down Expand Up @@ -76,7 +76,11 @@ impl WireguardInterfaceApi for WGApi<Kernel> {
}

info!(
"Interface {} configured successfully with config: {config:?}",
"Interface {} has been successfully configured. It has been assigned the following address: {}",
self.ifname, address
);
debug!(
"Interface {} configured with config: {config:?}",
self.ifname
);

Expand Down Expand Up @@ -127,7 +131,7 @@ impl WireguardInterfaceApi for WGApi<Kernel> {
fn configure_peer(&self, peer: &Peer) -> Result<(), WireguardInterfaceError> {
debug!("Configuring peer {peer:?} on interface {}", self.ifname);
netlink::set_peer(&self.ifname, peer)?;
info!("Peer {peer:?} configured on interface {}", self.ifname);
debug!("Peer {peer:?} configured on interface {}", self.ifname);
Ok(())
}

Expand All @@ -137,7 +141,7 @@ impl WireguardInterfaceApi for WGApi<Kernel> {
self.ifname
);
netlink::delete_peer(&self.ifname, peer_pubkey)?;
info!(
debug!(
"Peer with public key {peer_pubkey} removed from interface {}",
self.ifname
);
Expand Down
61 changes: 34 additions & 27 deletions src/wgapi_userspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ impl WGApi<Userspace> {

/// Create UNIX socket to communicate with `wireguard-go`.
fn socket(&self) -> io::Result<UnixStream> {
debug!("Connecting to a socket (interface {})", self.ifname);
let path = self.socket_path();
let socket = UnixStream::connect(path)?;
socket.set_read_timeout(Some(Duration::new(3, 0)))?;
debug!("Connected to a socket (interface {})", self.ifname);
Ok(socket)
}

// FIXME: currently other errors are ignored and result in 0 being returned.
fn parse_errno(buf: impl Read) -> u32 {
debug!("Parsing errno from buffer");
let reader = BufReader::new(buf);
for line_result in reader.lines() {
let line = match line_result {
Expand All @@ -87,7 +84,6 @@ impl WGApi<Userspace> {

/// Read host information using user-space API.
pub fn read_host(&self) -> io::Result<Host> {
debug!("Reading host interface info");
let mut socket = self.socket()?;
socket.write_all(b"get=1\n\n")?;
Host::parse_uapi(socket)
Expand Down Expand Up @@ -118,7 +114,7 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
.arg(&self.ifname)
.output()?;
check_command_output_status(output)?;
info!("Userspace interface {} created successfully", self.ifname);
debug!("Userspace interface {} created successfully", self.ifname);
Ok(())
}

Expand Down Expand Up @@ -146,10 +142,7 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
warn!("Received empty DNS server list. Skipping DNS configuration...");
return Ok(());
}
debug!(
"Configuring DNS for interface {}, using address: {dns:?}",
self.ifname
);
debug!("Beginning DNS configuration for interface {}", self.ifname);
// Setting DNS is not supported for macOS.
#[cfg(target_os = "macos")]
{
Expand All @@ -159,10 +152,7 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
{
configure_dns(&self.ifname, dns, search_domains)?;
}
info!(
"DNS configured for interface {}, using address: {dns:?}",
self.ifname
);
debug!("Finished configuring DNS for interface {}", self.ifname);
Ok(())
}

Expand All @@ -173,7 +163,7 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
bsd::assign_address(&self.ifname, address)?;
#[cfg(target_os = "linux")]
netlink::address_interface(&self.ifname, address)?;
info!("Address {address} assigned to interface {}", self.ifname);
debug!("Address {address} assigned to interface {}", self.ifname);

Ok(())
}
Expand All @@ -193,11 +183,14 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
self.assign_address(&address)?;

// configure interface
debug!("Setting host configuration for interface {}", self.ifname);
debug!(
"Applying the interface configuration to interface {}",
self.ifname
);
let host = config.try_into()?;
self.write_host(&host)?;
debug!("Host configuration set for interface {}.", self.ifname);
trace!("Host configuration: {host:?}");
debug!("Interface configuration set for interface {}.", self.ifname);
trace!("Interface configuration: {host:?}");

// Set maximum transfer unit (MTU).
if let Some(mtu) = config.mtu {
Expand All @@ -213,7 +206,11 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
}

info!(
"Interface {} configured successfully with config: {config:?}",
"Interface {} has been successfully configured. It has been assigned the following address: {}",
self.ifname, address
);
debug!(
"Interface {} configured with config: {config:?}",
self.ifname
);

Expand Down Expand Up @@ -277,10 +274,16 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
fn remove_interface(&self) -> Result<(), WireguardInterfaceError> {
debug!("Removing interface {}", self.ifname);
// `wireguard-go` should by design shut down if the socket is removed
debug!("Shutting down socket for interface {}", self.ifname);
debug!(
"Shutting down socket for interface {}, checking if the socket to remove exists...",
self.ifname
);
match self.socket() {
Ok(socket) => {
debug!("Removing socket for interface {}", self.ifname);
debug!(
"Socket exists, removing the socket for interface {}",
self.ifname
);
socket.shutdown(Shutdown::Both).map_err(|err| {
WireguardInterfaceError::UnixSockerError(format!(
"Failed to shutdown socket for interface {}: {err}",
Expand All @@ -301,13 +304,14 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
}
}

debug!("Clearing DNS entries, interface {}", self.ifname);
#[cfg(target_os = "macos")]
{
debug!("Clearing DNS entries by applying an empty DNS list to all network services, interface {}", self.ifname);
configure_dns(&[], &[])?;
}
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
{
debug!("Clearing DNS entries for interface {}", self.ifname);
clear_dns(&self.ifname)?;
}
debug!("DNS entries cleared, interface {}", self.ifname);
Expand Down Expand Up @@ -364,22 +368,25 @@ impl WireguardInterfaceApi for WGApi<Userspace> {
}

fn read_interface_data(&self) -> Result<Host, WireguardInterfaceError> {
debug!("Reading host info for interface {}", self.ifname);
debug!(
"Reading interface configuration and statistics for interface {}",
self.ifname
);
match self.read_host() {
Ok(host) => {
debug!("Host info read for interface {}", self.ifname);
trace!("Host info: {host:?}");
debug!("Interface configuration and statistics read successfully for interface {}", self.ifname);
trace!("Network information: {host:?}");
Ok(host)
}
Err(err) => match err {
err if err.kind() == ErrorKind::NotFound => {
return Err(WireguardInterfaceError::ReadInterfaceError(format!(
"Failed to read interface {} data, the socket may have been closed before we've attempted to read. If the socket has been closed intentionally, this message can be ignored. Error details: {err}",
return Err(WireguardInterfaceError::SocketClosed(format!(
"Failed to read network information for interface {} data, the socket may have been closed before we've attempted to read. If the socket has been closed intentionally, this message can be ignored. Error details: {err}",
self.ifname
)))
}
_ => Err(WireguardInterfaceError::ReadInterfaceError(format!(
"Failed to read interface {} data, error: {err}",
"Failed to read network information for interface {} data, error: {err}",
self.ifname
))),
},
Expand Down
Loading

0 comments on commit 6b78ce5

Please sign in to comment.