Skip to content

Commit

Permalink
feat: allow domain name as endpoint address (#54)
Browse files Browse the repository at this point in the history
* Add dns resolution for peer endpoint
* Handle both domain and IP endpoint
* chore: bump version
* Markdown links in function docstrings
* Leave IP resolution to `to_socket_addr()` function
  • Loading branch information
j-chmielewski authored Apr 25, 2024
1 parent 4a0429a commit 799775e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "defguard_wireguard_rs"
version = "0.4.2"
version = "0.4.3"
edition = "2021"
description = "A unified multi-platform high-level API for managing WireGuard interfaces"
license = "Apache-2.0"
Expand Down
8 changes: 7 additions & 1 deletion src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use netlink_packet_wireguard::{
};
use serde::{Deserialize, Serialize};

use crate::{key::Key, net::IpAddrMask};
use crate::{error::WireguardInterfaceError, key::Key, net::IpAddrMask, utils::resolve};

/// WireGuard peer representation.
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -53,6 +53,12 @@ impl Peer {
self.allowed_ips = allowed_ips;
}

/// Resolves endpoint address to [SocketAddr] and sets the field
pub fn set_endpoint(&mut self, endpoint: &str) -> Result<(), WireguardInterfaceError> {
self.endpoint = Some(resolve(endpoint)?);
Ok(())
}

#[must_use]
pub fn as_uapi_update(&self) -> String {
let mut output = format!("public_key={}\n", self.public_key.to_lower_hex());
Expand Down
2 changes: 1 addition & 1 deletion src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ where
});
let responses = netlink_request_genl::<GenlCtrl>(genlmsg, NLM_F_REQUEST | NLM_F_ACK)?;

match responses.get(0) {
match responses.first() {
Some(NetlinkMessage {
payload:
NetlinkPayload::InnerMessage(GenlMessage {
Expand Down
15 changes: 14 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use crate::netlink;
use crate::{check_command_output_status, Peer, WireguardInterfaceError};
use std::{collections::HashSet, process::Command};
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
use std::{io::Write, net::IpAddr, process::Stdio};
use std::{
io::Write,
net::{IpAddr, SocketAddr, ToSocketAddrs},
process::Stdio,
};
#[cfg(target_os = "macos")]
use std::{
io::{BufRead, BufReader, Cursor, Error as IoError},
Expand Down Expand Up @@ -340,3 +344,12 @@ pub(crate) fn clean_fwmark_rules(fwmark: u32) -> Result<(), WireguardInterfaceEr
netlink::delete_main_table_rule(IpVersion::IPv6, 0)?;
Ok(())
}

/// Resolves domain name to [SocketAddr]
pub fn resolve(addr: &str) -> Result<SocketAddr, WireguardInterfaceError> {
let error = || WireguardInterfaceError::PeerConfigurationError;
addr.to_socket_addrs()
.map_err(|_| error())?
.next()
.ok_or_else(error)
}

0 comments on commit 799775e

Please sign in to comment.