-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.rs
91 lines (75 loc) · 2.95 KB
/
server.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::str::FromStr;
use defguard_wireguard_rs::{
host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration, Kernel, Userspace, WGApi,
WireguardInterfaceApi,
};
use x25519_dalek::{EphemeralSecret, PublicKey};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create new api object for interface management
let ifname: String = if cfg!(target_os = "linux") || cfg!(target_os = "freebsd") {
"wg0".into()
} else {
"utun3".into()
};
#[cfg(not(target_os = "macos"))]
let wgapi = WGApi::<Kernel>::new(ifname.clone())?;
#[cfg(target_os = "macos")]
let wgapi = WGApi::<Userspace>::new(ifname.clone())?;
// create host interface
wgapi.create_interface()?;
// read current interface status
let host = wgapi.read_interface_data()?;
println!("WireGuard interface before configuration: {host:#?}");
// store peer keys to remove peers later
let mut peer_keys = Vec::new();
// prepare initial WireGuard interface configuration with one client
let secret = EphemeralSecret::random();
let key = PublicKey::from(&secret);
let peer_key: Key = key.as_ref().try_into().unwrap();
peer_keys.push(peer_key.clone());
let mut peer = Peer::new(peer_key);
let addr = IpAddrMask::from_str("10.20.30.2/32").unwrap();
peer.allowed_ips.push(addr);
let interface_config = InterfaceConfiguration {
name: ifname.clone(),
prvkey: "AAECAwQFBgcICQoLDA0OD/Dh0sO0pZaHeGlaSzwtHg8=".to_string(),
address: "10.6.0.30".to_string(),
port: 12345,
peers: vec![peer],
mtu: None,
};
println!("Prepared interface configuration: {interface_config:?}");
// apply initial interface configuration
#[cfg(not(windows))]
wgapi.configure_interface(&interface_config)?;
#[cfg(windows)]
wgapi.configure_interface(&interface_config, &[])?;
// read current interface status
let host = wgapi.read_interface_data()?;
println!("WireGuard interface after configuration: {host:#?}");
// add more WireGuard clients
for peer_id in 3..13 {
let secret = EphemeralSecret::random();
let key = PublicKey::from(&secret);
let peer_key: Key = key.as_ref().try_into().unwrap();
peer_keys.push(peer_key.clone());
let mut peer = Peer::new(peer_key);
let addr = IpAddrMask::from_str(&format!("10.20.30.{peer_id}/32")).unwrap();
peer.allowed_ips.push(addr);
// add peer to WireGuard interface
wgapi.configure_peer(&peer)?;
}
// read current interface status
let host = wgapi.read_interface_data()?;
println!("WireGuard interface with peers: {host:#?}");
// remove all peers
for peer_key in peer_keys {
wgapi.remove_peer(&peer_key)?;
}
// read current interface status
let host = wgapi.read_interface_data()?;
println!("WireGuard interface without peers: {host:#?}");
// remove interface
wgapi.remove_interface()?;
Ok(())
}