diff options
| author | diogo464 <[email protected]> | 2026-02-15 22:05:57 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2026-02-15 22:05:57 +0000 |
| commit | 56ac8740b79e291eabe6427d722921533b3a9837 (patch) | |
| tree | c244662e382263efec95d6ac445cfc9f987e4758 /src | |
| parent | 75ccbd675c22fb3275c5763518c3b97819db4c53 (diff) | |
updated dependencies
Diffstat (limited to 'src')
| -rw-r--r-- | src/key.rs | 3 | ||||
| -rw-r--r-- | src/lib.rs | 97 | ||||
| -rw-r--r-- | src/setup.rs | 108 | ||||
| -rw-r--r-- | src/view.rs | 59 |
4 files changed, 168 insertions, 99 deletions
| @@ -1,7 +1,8 @@ | |||
| 1 | use base64::Engine; | 1 | use base64::Engine; |
| 2 | use netlink_packet_wireguard::constants::WG_KEY_LEN; | ||
| 3 | use rand::{rngs::OsRng, RngCore}; | 2 | use rand::{rngs::OsRng, RngCore}; |
| 4 | 3 | ||
| 4 | const WG_KEY_LEN: usize = netlink_packet_wireguard::WireguardAttribute::WG_KEY_LEN; | ||
| 5 | |||
| 5 | // Code from: https://git.zx2c4.com/wireguard-tools/tree/contrib/embeddable-wg-library/wireguard.c | 6 | // Code from: https://git.zx2c4.com/wireguard-tools/tree/contrib/embeddable-wg-library/wireguard.c |
| 6 | 7 | ||
| 7 | type Fe = [i64; 16]; | 8 | type Fe = [i64; 16]; |
| @@ -3,18 +3,18 @@ mod key; | |||
| 3 | mod setup; | 3 | mod setup; |
| 4 | mod view; | 4 | mod view; |
| 5 | 5 | ||
| 6 | use std::borrow::Cow; | 6 | use std::{ |
| 7 | borrow::Cow, | ||
| 8 | net::{Ipv4Addr, Ipv6Addr}, | ||
| 9 | }; | ||
| 7 | 10 | ||
| 8 | use futures::{StreamExt, TryStreamExt}; | 11 | use futures::{StreamExt, TryStreamExt}; |
| 9 | use genetlink::{GenetlinkError, GenetlinkHandle}; | 12 | use genetlink::{GenetlinkError, GenetlinkHandle}; |
| 10 | use netlink_packet_core::{NetlinkMessage, NetlinkPayload, NLM_F_ACK, NLM_F_DUMP, NLM_F_REQUEST}; | 13 | use netlink_packet_core::{NetlinkMessage, NetlinkPayload, NLM_F_ACK, NLM_F_DUMP, NLM_F_REQUEST}; |
| 11 | use netlink_packet_generic::GenlMessage; | 14 | use netlink_packet_generic::GenlMessage; |
| 12 | use netlink_packet_route::{ | 15 | use netlink_packet_route::{link::LinkAttribute, route::RouteScope}; |
| 13 | link::{InfoKind, LinkAttribute, LinkInfo}, | 16 | use netlink_packet_wireguard::{WireguardAttribute, WireguardCmd, WireguardMessage}; |
| 14 | route::RouteScope, | 17 | use rtnetlink::{Handle, LinkMessageBuilder, LinkSetRequest, LinkWireguard, RouteMessageBuilder}; |
| 15 | }; | ||
| 16 | use netlink_packet_wireguard::{nlas::WgDeviceAttrs, Wireguard, WireguardCmd}; | ||
| 17 | use rtnetlink::Handle; | ||
| 18 | 18 | ||
| 19 | pub use conf::*; | 19 | pub use conf::*; |
| 20 | pub use key::*; | 20 | pub use key::*; |
| @@ -149,9 +149,9 @@ impl WireGuard { | |||
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | pub async fn view_device(&mut self, device_name: &str) -> Result<DeviceView> { | 151 | pub async fn view_device(&mut self, device_name: &str) -> Result<DeviceView> { |
| 152 | let genlmsg: GenlMessage<Wireguard> = GenlMessage::from_payload(Wireguard { | 152 | let genlmsg: GenlMessage<WireguardMessage> = GenlMessage::from_payload(WireguardMessage { |
| 153 | cmd: WireguardCmd::GetDevice, | 153 | cmd: WireguardCmd::GetDevice, |
| 154 | nlas: vec![WgDeviceAttrs::IfName(device_name.to_string())], | 154 | attributes: vec![WireguardAttribute::IfName(device_name.to_string())], |
| 155 | }); | 155 | }); |
| 156 | let mut nlmsg = NetlinkMessage::from(genlmsg); | 156 | let mut nlmsg = NetlinkMessage::from(genlmsg); |
| 157 | nlmsg.header.flags = NLM_F_REQUEST | NLM_F_DUMP; | 157 | nlmsg.header.flags = NLM_F_REQUEST | NLM_F_DUMP; |
| @@ -213,7 +213,7 @@ impl WireGuard { | |||
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | let message = descriptor.into_wireguard(device_name.to_string()); | 215 | let message = descriptor.into_wireguard(device_name.to_string()); |
| 216 | let genlmsg: GenlMessage<Wireguard> = GenlMessage::from_payload(message); | 216 | let genlmsg: GenlMessage<WireguardMessage> = GenlMessage::from_payload(message); |
| 217 | let mut nlmsg = NetlinkMessage::from(genlmsg); | 217 | let mut nlmsg = NetlinkMessage::from(genlmsg); |
| 218 | nlmsg.header.flags = NLM_F_REQUEST | NLM_F_ACK; | 218 | nlmsg.header.flags = NLM_F_REQUEST | NLM_F_ACK; |
| 219 | 219 | ||
| @@ -225,16 +225,12 @@ impl WireGuard { | |||
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | async fn link_create(&self, name: &str) -> Result<()> { | 227 | async fn link_create(&self, name: &str) -> Result<()> { |
| 228 | let mut msg = self.rt_handle.link().add().replace(); | 228 | self.rt_handle |
| 229 | msg.message_mut() | 229 | .link() |
| 230 | .attributes | 230 | .add(LinkMessageBuilder::<LinkWireguard>::new(name).build()) |
| 231 | .push(LinkAttribute::LinkInfo(vec![LinkInfo::Kind( | 231 | .replace() |
| 232 | InfoKind::Wireguard, | 232 | .execute() |
| 233 | )])); | 233 | .await?; |
| 234 | msg.message_mut() | ||
| 235 | .attributes | ||
| 236 | .push(LinkAttribute::IfName(name.to_string())); | ||
| 237 | msg.execute().await?; | ||
| 238 | Ok(()) | 234 | Ok(()) |
| 239 | } | 235 | } |
| 240 | 236 | ||
| @@ -245,13 +241,31 @@ impl WireGuard { | |||
| 245 | 241 | ||
| 246 | async fn link_up(&self, ifindex: u32) -> Result<()> { | 242 | async fn link_up(&self, ifindex: u32) -> Result<()> { |
| 247 | tracing::trace!("Bringing up interface {}", ifindex); | 243 | tracing::trace!("Bringing up interface {}", ifindex); |
| 248 | self.rt_handle.link().set(ifindex).up().execute().await?; | 244 | self.rt_handle |
| 245 | .link() | ||
| 246 | .set( | ||
| 247 | LinkMessageBuilder::<LinkSetRequest>::default() | ||
| 248 | .index(ifindex) | ||
| 249 | .up() | ||
| 250 | .build(), | ||
| 251 | ) | ||
| 252 | .execute() | ||
| 253 | .await?; | ||
| 249 | Ok(()) | 254 | Ok(()) |
| 250 | } | 255 | } |
| 251 | 256 | ||
| 252 | async fn link_down(&self, ifindex: u32) -> Result<()> { | 257 | async fn link_down(&self, ifindex: u32) -> Result<()> { |
| 253 | tracing::trace!("Bringing down interface {}", ifindex); | 258 | tracing::trace!("Bringing down interface {}", ifindex); |
| 254 | self.rt_handle.link().set(ifindex).down().execute().await?; | 259 | self.rt_handle |
| 260 | .link() | ||
| 261 | .set( | ||
| 262 | LinkMessageBuilder::<LinkSetRequest>::default() | ||
| 263 | .index(ifindex) | ||
| 264 | .down() | ||
| 265 | .build(), | ||
| 266 | ) | ||
| 267 | .execute() | ||
| 268 | .await?; | ||
| 255 | Ok(()) | 269 | Ok(()) |
| 256 | } | 270 | } |
| 257 | 271 | ||
| @@ -314,30 +328,37 @@ impl WireGuard { | |||
| 314 | #[allow(unused)] | 328 | #[allow(unused)] |
| 315 | async fn route_add(&self, ifindex: u32, net: ipnet::IpNet) -> Result<()> { | 329 | async fn route_add(&self, ifindex: u32, net: ipnet::IpNet) -> Result<()> { |
| 316 | tracing::trace!("Adding route {} to {}", net, ifindex); | 330 | tracing::trace!("Adding route {} to {}", net, ifindex); |
| 317 | let request = self | ||
| 318 | .rt_handle | ||
| 319 | .route() | ||
| 320 | .add() | ||
| 321 | .scope(RouteScope::Link) | ||
| 322 | .output_interface(ifindex) | ||
| 323 | .replace(); | ||
| 324 | 331 | ||
| 325 | match net.addr() { | 332 | match net.addr() { |
| 326 | std::net::IpAddr::V4(ip) => { | 333 | std::net::IpAddr::V4(ip) => { |
| 327 | request | 334 | self.rt_handle |
| 328 | .v4() | 335 | .route() |
| 329 | .destination_prefix(ip, net.prefix_len()) | 336 | .add( |
| 337 | RouteMessageBuilder::<Ipv4Addr>::default() | ||
| 338 | .scope(RouteScope::Link) | ||
| 339 | .output_interface(ifindex) | ||
| 340 | .destination_prefix(ip, net.prefix_len()) | ||
| 341 | .build(), | ||
| 342 | ) | ||
| 343 | .replace() | ||
| 330 | .execute() | 344 | .execute() |
| 331 | .await | 345 | .await?; |
| 332 | } | 346 | } |
| 333 | std::net::IpAddr::V6(ip) => { | 347 | std::net::IpAddr::V6(ip) => { |
| 334 | request | 348 | self.rt_handle |
| 335 | .v6() | 349 | .route() |
| 336 | .destination_prefix(ip, net.prefix_len()) | 350 | .add( |
| 351 | RouteMessageBuilder::<Ipv6Addr>::default() | ||
| 352 | .scope(RouteScope::Link) | ||
| 353 | .output_interface(ifindex) | ||
| 354 | .destination_prefix(ip, net.prefix_len()) | ||
| 355 | .build(), | ||
| 356 | ) | ||
| 357 | .replace() | ||
| 337 | .execute() | 358 | .execute() |
| 338 | .await | 359 | .await?; |
| 339 | } | 360 | } |
| 340 | }?; | 361 | }; |
| 341 | 362 | ||
| 342 | Ok(()) | 363 | Ok(()) |
| 343 | } | 364 | } |
diff --git a/src/setup.rs b/src/setup.rs index e7d454c..c36772f 100644 --- a/src/setup.rs +++ b/src/setup.rs | |||
| @@ -2,13 +2,46 @@ use std::net::{IpAddr, SocketAddr}; | |||
| 2 | 2 | ||
| 3 | use ipnet::IpNet; | 3 | use ipnet::IpNet; |
| 4 | use netlink_packet_wireguard::{ | 4 | use netlink_packet_wireguard::{ |
| 5 | constants::{AF_INET, AF_INET6, WGDEVICE_F_REPLACE_PEERS, WGPEER_F_REPLACE_ALLOWEDIPS}, | 5 | WireguardAddressFamily, WireguardAllowedIp, WireguardAllowedIpAttr, WireguardAttribute, |
| 6 | nlas::{WgAllowedIp, WgAllowedIpAttrs, WgDeviceAttrs, WgPeer, WgPeerAttrs}, | 6 | WireguardCmd, WireguardMessage, WireguardPeer, WireguardPeerAttribute, |
| 7 | Wireguard, WireguardCmd, | ||
| 8 | }; | 7 | }; |
| 9 | 8 | ||
| 10 | use super::Key; | 9 | use super::Key; |
| 11 | 10 | ||
| 11 | #[allow(unused)] | ||
| 12 | mod constants { | ||
| 13 | // this is copy pasted from the netlink_packet_wireguard's constants module because for some reason | ||
| 14 | // they stopped exposing constants in commit 3067a394fc7bc28fadbed5359c44cce95aac0f13 | ||
| 15 | pub const WGDEVICE_F_REPLACE_PEERS: u32 = 1 << 0; | ||
| 16 | |||
| 17 | pub const WGPEER_F_REMOVE_ME: u32 = 1 << 0; | ||
| 18 | pub const WGPEER_F_REPLACE_ALLOWEDIPS: u32 = 1 << 1; | ||
| 19 | pub const WGPEER_F_UPDATE_ONLY: u32 = 1 << 2; | ||
| 20 | |||
| 21 | pub const WGPEER_A_UNSPEC: u16 = 0; | ||
| 22 | pub const WGPEER_A_PUBLIC_KEY: u16 = 1; | ||
| 23 | pub const WGPEER_A_PRESHARED_KEY: u16 = 2; | ||
| 24 | pub const WGPEER_A_FLAGS: u16 = 3; | ||
| 25 | pub const WGPEER_A_ENDPOINT: u16 = 4; | ||
| 26 | pub const WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL: u16 = 5; | ||
| 27 | pub const WGPEER_A_LAST_HANDSHAKE_TIME: u16 = 6; | ||
| 28 | pub const WGPEER_A_RX_BYTES: u16 = 7; | ||
| 29 | pub const WGPEER_A_TX_BYTES: u16 = 8; | ||
| 30 | pub const WGPEER_A_ALLOWEDIPS: u16 = 9; | ||
| 31 | pub const WGPEER_A_PROTOCOL_VERSION: u16 = 10; | ||
| 32 | |||
| 33 | pub const WGALLOWEDIP_A_UNSPEC: u16 = 0; | ||
| 34 | pub const WGALLOWEDIP_A_FAMILY: u16 = 1; | ||
| 35 | pub const WGALLOWEDIP_A_IPADDR: u16 = 2; | ||
| 36 | pub const WGALLOWEDIP_A_CIDR_MASK: u16 = 3; | ||
| 37 | |||
| 38 | pub const AF_INET6: u16 = 10; | ||
| 39 | pub const AF_INET: u16 = 2; | ||
| 40 | } | ||
| 41 | |||
| 42 | #[allow(unused)] | ||
| 43 | pub(crate) use constants::*; | ||
| 44 | |||
| 12 | #[derive(Debug)] | 45 | #[derive(Debug)] |
| 13 | pub struct PeerDescriptor { | 46 | pub struct PeerDescriptor { |
| 14 | pub(super) public_key: Key, | 47 | pub(super) public_key: Key, |
| @@ -87,20 +120,25 @@ impl PeerDescriptor { | |||
| 87 | self | 120 | self |
| 88 | } | 121 | } |
| 89 | 122 | ||
| 90 | pub(super) fn into_wireguard(self) -> WgPeer { | 123 | pub(super) fn into_wireguard(self) -> WireguardPeer { |
| 91 | let mut nlas = Vec::new(); | 124 | let mut attributes = Vec::new(); |
| 92 | nlas.push(WgPeerAttrs::PublicKey(self.public_key.into_array())); | 125 | attributes.push(WireguardPeerAttribute::PublicKey( |
| 93 | nlas.extend( | 126 | self.public_key.into_array(), |
| 127 | )); | ||
| 128 | attributes.extend( | ||
| 94 | self.preshared_key | 129 | self.preshared_key |
| 95 | .map(|key| WgPeerAttrs::PresharedKey(key.into_array())), | 130 | .map(|key| WireguardPeerAttribute::PresharedKey(key.into_array())), |
| 96 | ); | 131 | ); |
| 97 | nlas.extend(self.endpoint.map(WgPeerAttrs::Endpoint)); | 132 | attributes.extend(self.endpoint.map(WireguardPeerAttribute::Endpoint)); |
| 98 | nlas.extend(self.keepalive.map(WgPeerAttrs::PersistentKeepalive)); | 133 | attributes.extend( |
| 99 | nlas.extend(self.allowed_ips.map(|allowed_ips| { | 134 | self.keepalive |
| 100 | WgPeerAttrs::AllowedIps(allowed_ips.into_iter().map(ipnet_to_wg).collect()) | 135 | .map(WireguardPeerAttribute::PersistentKeepalive), |
| 136 | ); | ||
| 137 | attributes.extend(self.allowed_ips.map(|allowed_ips| { | ||
| 138 | WireguardPeerAttribute::AllowedIps(allowed_ips.into_iter().map(ipnet_to_wg).collect()) | ||
| 101 | })); | 139 | })); |
| 102 | nlas.push(WgPeerAttrs::Flags(WGPEER_F_REPLACE_ALLOWEDIPS)); | 140 | attributes.push(WireguardPeerAttribute::Flags(WGPEER_F_REPLACE_ALLOWEDIPS)); |
| 103 | WgPeer(nlas) | 141 | WireguardPeer(attributes) |
| 104 | } | 142 | } |
| 105 | } | 143 | } |
| 106 | 144 | ||
| @@ -174,39 +212,43 @@ impl DeviceDescriptor { | |||
| 174 | self | 212 | self |
| 175 | } | 213 | } |
| 176 | 214 | ||
| 177 | pub(super) fn into_wireguard(self, device_name: String) -> Wireguard { | 215 | pub(super) fn into_wireguard(self, device_name: String) -> WireguardMessage { |
| 178 | let mut nlas = Vec::new(); | 216 | let mut attributes = Vec::new(); |
| 179 | nlas.push(WgDeviceAttrs::IfName(device_name)); | 217 | attributes.push(WireguardAttribute::IfName(device_name)); |
| 180 | nlas.extend( | 218 | attributes.extend( |
| 181 | self.private_key | 219 | self.private_key |
| 182 | .map(|key| WgDeviceAttrs::PrivateKey(key.into_array())), | 220 | .map(|key| WireguardAttribute::PrivateKey(key.into_array())), |
| 183 | ); | 221 | ); |
| 184 | nlas.extend(self.listen_port.map(WgDeviceAttrs::ListenPort)); | 222 | attributes.extend(self.listen_port.map(WireguardAttribute::ListenPort)); |
| 185 | nlas.extend(self.fwmark.map(WgDeviceAttrs::Fwmark)); | 223 | attributes.extend(self.fwmark.map(WireguardAttribute::Fwmark)); |
| 186 | nlas.extend(self.peers.map(|peers| { | 224 | attributes.extend(self.peers.map(|peers| { |
| 187 | WgDeviceAttrs::Peers( | 225 | WireguardAttribute::Peers( |
| 188 | peers | 226 | peers |
| 189 | .into_iter() | 227 | .into_iter() |
| 190 | .map(PeerDescriptor::into_wireguard) | 228 | .map(PeerDescriptor::into_wireguard) |
| 191 | .collect(), | 229 | .collect(), |
| 192 | ) | 230 | ) |
| 193 | })); | 231 | })); |
| 194 | nlas.push(WgDeviceAttrs::Flags(WGDEVICE_F_REPLACE_PEERS)); | 232 | attributes.push(WireguardAttribute::Flags(WGDEVICE_F_REPLACE_PEERS)); |
| 195 | 233 | ||
| 196 | Wireguard { | 234 | WireguardMessage { |
| 197 | cmd: WireguardCmd::SetDevice, | 235 | cmd: WireguardCmd::SetDevice, |
| 198 | nlas, | 236 | attributes, |
| 199 | } | 237 | } |
| 200 | } | 238 | } |
| 201 | } | 239 | } |
| 202 | 240 | ||
| 203 | fn ipnet_to_wg(net: IpNet) -> WgAllowedIp { | 241 | fn ipnet_to_wg(net: IpNet) -> WireguardAllowedIp { |
| 204 | let mut nlas = Vec::default(); | 242 | let mut attributes = Vec::default(); |
| 205 | nlas.push(WgAllowedIpAttrs::Cidr(net.prefix_len())); | 243 | attributes.push(WireguardAllowedIpAttr::Cidr(net.prefix_len())); |
| 206 | nlas.push(WgAllowedIpAttrs::IpAddr(net.addr())); | 244 | attributes.push(WireguardAllowedIpAttr::IpAddr(net.addr())); |
| 207 | match net.addr() { | 245 | match net.addr() { |
| 208 | IpAddr::V4(_) => nlas.push(WgAllowedIpAttrs::Family(AF_INET)), | 246 | IpAddr::V4(_) => { |
| 209 | IpAddr::V6(_) => nlas.push(WgAllowedIpAttrs::Family(AF_INET6)), | 247 | attributes.push(WireguardAllowedIpAttr::Family(WireguardAddressFamily::Ipv4)) |
| 248 | } | ||
| 249 | IpAddr::V6(_) => { | ||
| 250 | attributes.push(WireguardAllowedIpAttr::Family(WireguardAddressFamily::Ipv6)) | ||
| 251 | } | ||
| 210 | } | 252 | } |
| 211 | WgAllowedIp(nlas) | 253 | WireguardAllowedIp(attributes) |
| 212 | } | 254 | } |
diff --git a/src/view.rs b/src/view.rs index 2858811..69f718a 100644 --- a/src/view.rs +++ b/src/view.rs | |||
| @@ -1,9 +1,12 @@ | |||
| 1 | use std::{net::SocketAddr, time::SystemTime}; | 1 | use std::{ |
| 2 | net::SocketAddr, | ||
| 3 | time::{Duration, SystemTime}, | ||
| 4 | }; | ||
| 2 | 5 | ||
| 3 | use ipnet::IpNet; | 6 | use ipnet::IpNet; |
| 4 | use netlink_packet_wireguard::{ | 7 | use netlink_packet_wireguard::{ |
| 5 | nlas::{WgAllowedIp, WgAllowedIpAttrs, WgDeviceAttrs, WgPeer, WgPeerAttrs}, | 8 | WireguardAllowedIp, WireguardAllowedIpAttr, WireguardAttribute, WireguardMessage, |
| 6 | Wireguard, | 9 | WireguardPeer, WireguardPeerAttribute, |
| 7 | }; | 10 | }; |
| 8 | 11 | ||
| 9 | use super::{Error, Key, Result}; | 12 | use super::{Error, Key, Result}; |
| @@ -31,7 +34,7 @@ pub struct PeerView { | |||
| 31 | pub allowed_ips: Vec<IpNet>, | 34 | pub allowed_ips: Vec<IpNet>, |
| 32 | } | 35 | } |
| 33 | 36 | ||
| 34 | pub(super) fn device_view_from_payload(wg: Wireguard) -> Result<DeviceView> { | 37 | pub(super) fn device_view_from_payload(wg: WireguardMessage) -> Result<DeviceView> { |
| 35 | let mut if_index = None; | 38 | let mut if_index = None; |
| 36 | let mut if_name = None; | 39 | let mut if_name = None; |
| 37 | let mut private_key = None; | 40 | let mut private_key = None; |
| @@ -40,15 +43,15 @@ pub(super) fn device_view_from_payload(wg: Wireguard) -> Result<DeviceView> { | |||
| 40 | let mut fwmark = None; | 43 | let mut fwmark = None; |
| 41 | let mut peers = None; | 44 | let mut peers = None; |
| 42 | 45 | ||
| 43 | for nla in wg.nlas { | 46 | for attr in wg.attributes { |
| 44 | match nla { | 47 | match attr { |
| 45 | WgDeviceAttrs::IfIndex(v) => if_index = Some(v), | 48 | WireguardAttribute::IfIndex(v) => if_index = Some(v), |
| 46 | WgDeviceAttrs::IfName(v) => if_name = Some(v), | 49 | WireguardAttribute::IfName(v) => if_name = Some(v), |
| 47 | WgDeviceAttrs::PrivateKey(v) => private_key = Some(Key::from(v)), | 50 | WireguardAttribute::PrivateKey(v) => private_key = Some(Key::from(v)), |
| 48 | WgDeviceAttrs::PublicKey(v) => public_key = Some(Key::from(v)), | 51 | WireguardAttribute::PublicKey(v) => public_key = Some(Key::from(v)), |
| 49 | WgDeviceAttrs::ListenPort(v) => listen_port = Some(v), | 52 | WireguardAttribute::ListenPort(v) => listen_port = Some(v), |
| 50 | WgDeviceAttrs::Fwmark(v) => fwmark = Some(v), | 53 | WireguardAttribute::Fwmark(v) => fwmark = Some(v), |
| 51 | WgDeviceAttrs::Peers(v) => peers = Some(peers_from_wg_peers(v)?), | 54 | WireguardAttribute::Peers(v) => peers = Some(peers_from_wg_peers(v)?), |
| 52 | _ => {} | 55 | _ => {} |
| 53 | } | 56 | } |
| 54 | } | 57 | } |
| @@ -64,7 +67,7 @@ pub(super) fn device_view_from_payload(wg: Wireguard) -> Result<DeviceView> { | |||
| 64 | }) | 67 | }) |
| 65 | } | 68 | } |
| 66 | 69 | ||
| 67 | fn peers_from_wg_peers(wg_peers: Vec<WgPeer>) -> Result<Vec<PeerView>> { | 70 | fn peers_from_wg_peers(wg_peers: Vec<WireguardPeer>) -> Result<Vec<PeerView>> { |
| 68 | let mut peers = Vec::with_capacity(wg_peers.len()); | 71 | let mut peers = Vec::with_capacity(wg_peers.len()); |
| 69 | for wg_peer in wg_peers { | 72 | for wg_peer in wg_peers { |
| 70 | peers.push(peer_from_wg_peer(wg_peer)?); | 73 | peers.push(peer_from_wg_peer(wg_peer)?); |
| @@ -72,7 +75,7 @@ fn peers_from_wg_peers(wg_peers: Vec<WgPeer>) -> Result<Vec<PeerView>> { | |||
| 72 | Ok(peers) | 75 | Ok(peers) |
| 73 | } | 76 | } |
| 74 | 77 | ||
| 75 | fn peer_from_wg_peer(wg_peer: WgPeer) -> Result<PeerView> { | 78 | fn peer_from_wg_peer(wg_peer: WireguardPeer) -> Result<PeerView> { |
| 76 | let mut public_key = None; | 79 | let mut public_key = None; |
| 77 | let mut preshared_key = None; | 80 | let mut preshared_key = None; |
| 78 | let mut endpoint = None; | 81 | let mut endpoint = None; |
| @@ -84,14 +87,14 @@ fn peer_from_wg_peer(wg_peer: WgPeer) -> Result<PeerView> { | |||
| 84 | 87 | ||
| 85 | for attr in wg_peer.iter() { | 88 | for attr in wg_peer.iter() { |
| 86 | match attr { | 89 | match attr { |
| 87 | WgPeerAttrs::PublicKey(v) => public_key = Some(Key::from(v)), | 90 | WireguardPeerAttribute::PublicKey(v) => public_key = Some(Key::from(v)), |
| 88 | WgPeerAttrs::PresharedKey(v) => preshared_key = Some(Key::from(v)), | 91 | WireguardPeerAttribute::PresharedKey(v) => preshared_key = Some(Key::from(v)), |
| 89 | WgPeerAttrs::Endpoint(v) => endpoint = Some(*v), | 92 | WireguardPeerAttribute::Endpoint(v) => endpoint = Some(*v), |
| 90 | WgPeerAttrs::PersistentKeepalive(v) => persistent_keepalive = Some(*v), | 93 | WireguardPeerAttribute::PersistentKeepalive(v) => persistent_keepalive = Some(*v), |
| 91 | WgPeerAttrs::LastHandshake(v) => last_handshake = Some(*v), | 94 | WireguardPeerAttribute::LastHandshake(v) => last_handshake = Some(*v), |
| 92 | WgPeerAttrs::RxBytes(v) => rx_bytes = Some(*v), | 95 | WireguardPeerAttribute::RxBytes(v) => rx_bytes = Some(*v), |
| 93 | WgPeerAttrs::TxBytes(v) => tx_bytes = Some(*v), | 96 | WireguardPeerAttribute::TxBytes(v) => tx_bytes = Some(*v), |
| 94 | WgPeerAttrs::AllowedIps(v) => { | 97 | WireguardPeerAttribute::AllowedIps(v) => { |
| 95 | for ip in v { | 98 | for ip in v { |
| 96 | allowed_ips.push(ipnet_from_wg(ip)?); | 99 | allowed_ips.push(ipnet_from_wg(ip)?); |
| 97 | } | 100 | } |
| @@ -105,20 +108,22 @@ fn peer_from_wg_peer(wg_peer: WgPeer) -> Result<PeerView> { | |||
| 105 | preshared_key, | 108 | preshared_key, |
| 106 | endpoint, | 109 | endpoint, |
| 107 | persistent_keepalive, | 110 | persistent_keepalive, |
| 108 | last_handshake: last_handshake.ok_or_else(|| Error::message("missing last_handshake"))?, | 111 | last_handshake: last_handshake |
| 112 | .map(|ts| SystemTime::now() - Duration::new(ts.seconds as u64, ts.nano_seconds as u32)) | ||
| 113 | .ok_or_else(|| Error::message("missing last_handshake"))?, | ||
| 109 | rx_bytes: rx_bytes.ok_or_else(|| Error::message("missing rx_bytes"))?, | 114 | rx_bytes: rx_bytes.ok_or_else(|| Error::message("missing rx_bytes"))?, |
| 110 | tx_bytes: tx_bytes.ok_or_else(|| Error::message("missing tx_bytes"))?, | 115 | tx_bytes: tx_bytes.ok_or_else(|| Error::message("missing tx_bytes"))?, |
| 111 | allowed_ips, | 116 | allowed_ips, |
| 112 | }) | 117 | }) |
| 113 | } | 118 | } |
| 114 | 119 | ||
| 115 | fn ipnet_from_wg(wg: &WgAllowedIp) -> Result<IpNet> { | 120 | fn ipnet_from_wg(wg: &WireguardAllowedIp) -> Result<IpNet> { |
| 116 | let mut ip = None; | 121 | let mut ip = None; |
| 117 | let mut prefix = None; | 122 | let mut prefix = None; |
| 118 | for attr in wg.iter() { | 123 | for attr in wg.iter() { |
| 119 | match attr { | 124 | match attr { |
| 120 | WgAllowedIpAttrs::IpAddr(v) => ip = Some(*v), | 125 | WireguardAllowedIpAttr::IpAddr(v) => ip = Some(*v), |
| 121 | WgAllowedIpAttrs::Cidr(v) => prefix = Some(*v), | 126 | WireguardAllowedIpAttr::Cidr(v) => prefix = Some(*v), |
| 122 | _ => {} | 127 | _ => {} |
| 123 | } | 128 | } |
| 124 | } | 129 | } |
