7 个版本 (4 个重大变更)
新版本 0.5.0 | 2024 年 8 月 10 日 |
---|---|
0.4.0 | 2024 年 4 月 11 日 |
0.3.0 | 2021 年 11 月 23 日 |
0.2.2 | 2021 年 11 月 15 日 |
0.1.0 | 2021 年 9 月 30 日 |
#7 in #wireguard
每月下载量 95
2MB
2K SLoC
包含 (Windows DLL, 2MB) wireguard.dll, (Windows DLL, 1.5MB) wireguard.dll, (Windows DLL, 1MB) wireguard.dll, (Windows DLL, 670KB) wireguard.dll
wireguard-nt
为 WireGuard NT C 库提供安全的 Rust 惯用绑定:[https://git.zx2c4.com/wireguard-nt/about](https://git.zx2c4.com/wireguard-nt/about)
使用纯 Rust 类型和方法包装 WireGuard NT 库的功能,使其易于使用。
使用方法
将此库添加到您的 Cargo.toml
依赖中
[dependencies]
wireguard-nt = "0.4"
在您的代码中加载 wireguard.dll 签名驱动程序文件,从 [https://git.zx2c4.com/wireguard-nt/about](https://git.zx2c4.com/wireguard-nt/about) 下载
然后调用 Adapter::create
或 Adapter::open
以获取 wireguard 适配器。首先使用 Adapter::set_config
设置其配置。
示例
//Must be run as Administrator because we create network adapters
//Load the wireguard dll file so that we can call the underlying C functions
//Unsafe because we are loading an arbitrary dll file
let wireguard = unsafe { wireguard_nt::load_from_path("path/to/wireguard.dll") }.expect("Failed to load wireguard dll");
//Try to open an adapter with the name "Demo"
let adapter = match wireguard_nt::Adapter::open(wireguard, "Demo") {
Ok(a) => a,
Err((_, wireguard)) => {
//If loading failed (most likely it didn't exist), create a new one
match wireguard_nt::Adapter::create(wireguard, "WireGuard", "Demo", None) {
Ok(a) => a,
Err((e, _)) => panic!("Failed to create adapter: {:?}", e),
}
}
};
let interface = wireguard_nt::SetInterface {
//Let the OS pick a port for us
listen_port: None,
//Generated from the private key if not specified
public_key: None,
//Fill in private keys in real code
private_key: None,
//Add a peer
peers: vec![wireguard_nt::SetPeer {
//Provide a public key so that we can communicate with them
public_key: None,
//Disable additional AES encryption
preshared_key: None,
//Send a keepalive packet every 21 seconds
keep_alive: Some(21),
//Route all traffic through the WireGuard interface
allowed_ips: vec!["0.0.0.0/0".parse().unwrap()],
//The peer's ip address
endpoint: "1.2.3.4".parse().unwrap(),
}],
};
//Set the config our adapter will use
//This lets it know about the peers and keys
adapter.set_config(&interface).unwrap();
let internal_ip = "10.4.0.2".parse().unwrap();
let internal_prefix_length = 24;
let internal_ipnet = ipnet::Ipv4Net::new(internal_ip, internal_prefix_length).unwrap();
//Set up the routing table with the allowed ips for our peers,
//and assign an ip to the interface
adapter.set_default_route(&[internal_ipnet.into()], &interface).unwrap();
//drop(adapter)
//The adapter closes its resources when dropped
查看 examples/demo_server.rs
,它连接到 wireguard 演示服务器
版本兼容性
支持 Wireguard NT 0.10 及以上版本。版本小于 0.10 的变更会导致互操作性困难。如果这对您的影响,请提交问题。
许可证:MIT
依赖项
~14–22MB
~287K SLoC