wireguard-control: gracefully handle multiple Peers NLAs

Fixes #187
pull/186/head
Jake McGinty 2022-01-18 15:12:22 +09:00
parent ce23bbdf63
commit b09d12225b
1 changed files with 7 additions and 5 deletions

View File

@ -141,10 +141,12 @@ impl<'a> TryFrom<&'a [WgDeviceAttrs]> for Device {
let private_key = get_nla_value!(nlas, WgDeviceAttrs, PrivateKey).map(|key| Key(*key));
let listen_port = get_nla_value!(nlas, WgDeviceAttrs, ListenPort).cloned();
let fwmark = get_nla_value!(nlas, WgDeviceAttrs, Fwmark).cloned();
let peers = get_nla_value!(nlas, WgDeviceAttrs, Peers)
.cloned()
.unwrap_or_default()
.into_iter()
let peers = nlas.iter()
.filter_map(|nla| match nla {
WgDeviceAttrs::Peers(peers) => Some(peers.clone()),
_ => None
})
.flatten()
.map(PeerInfo::try_from)
.collect::<Result<Vec<_>, _>>()?;
Ok(Device {
@ -385,7 +387,7 @@ pub fn get_by_name(name: &InterfaceName) -> Result<Device, io::Error> {
log::debug!(
"get_by_name: parsed wireguard device {} with {} peer(s)",
device.name,
device.peers.len()
device.peers.len(),
);
Ok(device)
}