-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclient.rs
59 lines (50 loc) · 1.9 KB
/
client.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
use std::{net::SocketAddr, 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
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 interface
wgapi.create_interface()?;
// Peer configuration
let secret = EphemeralSecret::random();
let key = PublicKey::from(&secret);
// Peer secret key
let peer_key: Key = key.as_ref().try_into().unwrap();
let mut peer = Peer::new(peer_key.clone());
log::info!("endpoint");
// Your WireGuard server endpoint which client connects to
let endpoint: SocketAddr = "10.10.10.10:55001".parse().unwrap();
// Peer endpoint and interval
peer.endpoint = Some(endpoint);
peer.persistent_keepalive_interval = Some(25);
peer.allowed_ips.push(IpAddrMask::from_str("10.6.0.0/24")?);
peer.allowed_ips
.push(IpAddrMask::from_str("192.168.22.0/24")?);
// interface configuration
let interface_config = InterfaceConfiguration {
name: ifname.clone(),
prvkey: "AAECAwQFBgcICQoLDA0OD/Dh0sO0pZaHeGlaSzwtHg8=".to_string(),
addresses: vec!["10.6.0.30".parse().unwrap()],
port: 12345,
peers: vec![peer],
mtu: None,
};
#[cfg(not(windows))]
wgapi.configure_interface(&interface_config)?;
#[cfg(windows)]
wgapi.configure_interface(&interface_config, &[], &[])?;
wgapi.configure_peer_routing(&interface_config.peers)?;
Ok(())
}