diff --git a/Cargo.lock b/Cargo.lock index 0568c28..1429158 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -891,9 +891,9 @@ checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" [[package]] name = "smallvec" -version = "1.15.2" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" +checksum = "b9be42f50aa861c555654aa3a37f52f4b1074bacf4e48fe0ef7fa584e80f1f0f" [[package]] name = "socket2" diff --git a/examples/client.rs b/examples/client.rs index aeab172..beb7baf 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -1,4 +1,7 @@ -use std::{net::SocketAddr, str::FromStr}; +use std::{ + net::{IpAddr, Ipv4Addr, SocketAddr}, + str::FromStr, +}; use defguard_wireguard_rs::{ InterfaceConfiguration, WGApi, WireguardInterfaceApi, key::Key, net::IpAddrMask, peer::Peer, @@ -7,6 +10,8 @@ use x25519_dalek::{EphemeralSecret, PublicKey}; #[cfg(not(target_os = "netbsd"))] fn main() -> Result<(), Box> { + env_logger::init(); + // Create new API object for interface let ifname: String = if cfg!(target_os = "linux") || cfg!(target_os = "freebsd") { "wg0".into() @@ -31,7 +36,7 @@ fn main() -> Result<(), Box> { log::info!("endpoint"); // Your WireGuard server endpoint which client connects to - let endpoint: SocketAddr = "10.10.10.10:55001".parse().unwrap(); + let endpoint = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 10, 10, 10)), 55001); // Peer endpoint and interval peer.endpoint = Some(endpoint); peer.persistent_keepalive_interval = Some(25); diff --git a/src/dns.rs b/src/dns.rs index e8ad061..ed551d4 100644 --- a/src/dns.rs +++ b/src/dns.rs @@ -13,6 +13,7 @@ use std::io::{Cursor, Error as IoError}; use std::net::IpAddr; #[cfg(any(target_os = "freebsd", target_os = "linux", target_os = "netbsd"))] use std::{ + fmt::Write as _, fs::{File, read_dir}, io::Write, path::Path, @@ -282,7 +283,7 @@ impl<'a> DnsConfig<'a> { fn resolvconf_stdin(&self) -> String { let mut stdin = String::new(); for server in self.servers { - stdin.push_str(&format!("nameserver {server}\n")); + let _ = writeln!(stdin, "nameserver {server}"); } // Routing-only domains have to be declared as search domains here, as that is the only way // of telling resolvconf which domains this interface resolves. @@ -295,7 +296,7 @@ impl<'a> DnsConfig<'a> { if !domains.is_empty() { // resolv.conf(5) holds a single search list, and a second `search` line overrides the // first one rather than extending it. - stdin.push_str(&format!("search {}\n", domains.join(" "))); + let _ = writeln!(stdin, "search {}", domains.join(" ")); } stdin } @@ -478,16 +479,15 @@ fn systemd_resolved_available() -> bool { debug!("{RESOLVED_RUNTIME_DIR} does not exist, assuming systemd-resolved is not running"); return false; } - match get_command_path(RESOLVECTL) { - Ok(Some(_)) => true, - _ => { - warn!( - "systemd-resolved appears to be running, but the `{RESOLVECTL}` command could \ - not be found in PATH. Falling back to `{RESOLVCONF}`, which cannot configure \ - split DNS on this host." - ); - false - } + if let Ok(Some(_)) = get_command_path(RESOLVECTL) { + true + } else { + warn!( + "systemd-resolved appears to be running, but the `{RESOLVECTL}` command could not be \ + found in PATH. Falling back to `{RESOLVCONF}`, which cannot configure split DNS on \ + this host." + ); + false } } diff --git a/src/netlink.rs b/src/netlink.rs index 5e97f29..6eb308a 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -484,11 +484,10 @@ pub(crate) fn set_link_up(if_name: &str) -> NetlinkResult<()> { Ok(()) } -#[cfg(test)] /// Get default route for a given address family. -pub(crate) fn get_gateway(address_family: AddressFamily) -> NetlinkResult> { +pub(crate) fn get_gateway(ip_version: IpVersion) -> NetlinkResult> { let header = RouteHeader { - address_family, + address_family: ip_version.address_family(), table: RouteHeader::RT_TABLE_MAIN, // protocol: RouteProtocol::Boot, // doesn't filter // scope: RouteScope::Universe, // doesn't filter @@ -522,13 +521,70 @@ pub(crate) fn get_gateway(address_family: AddressFamily) -> NetlinkResult