| 306 | } |
| 307 | |
| 308 | pub fn setup_wireguard(config: &WgConfig) -> Result<()> { |
| 309 | config.validate().context("Invalid wireguard config")?; |
| 310 | |
| 311 | info!("Setting up wireguard interface"); |
| 312 | |
| 313 | let ifname = &config.interface; |
| 314 | |
| 315 | // Check if interface exists by trying to run ip link show |
| 316 | if cmd!(ip link show $ifname > /dev/null).is_ok() { |
| 317 | info!("WireGuard interface {ifname} already exists"); |
| 318 | return Ok(()); |
| 319 | } |
| 320 | |
| 321 | let addr = format!("{}", config.ip); |
| 322 | // Interface doesn't exist, create and configure it |
| 323 | cmd! { |
| 324 | ip link add $ifname type wireguard; |
| 325 | ip address add $addr dev $ifname; |
| 326 | ip link set $ifname up; |
| 327 | }?; |
| 328 | |
| 329 | info!("Created and configured WireGuard interface {ifname}"); |
| 330 | |
| 331 | Ok(()) |
| 332 | } |
| 333 | |
| 334 | #[cfg(test)] |
| 335 | mod tests { |