Skip to content

Commit f6cab50

Browse files
authored
Support custom wintun.dll paths (#73)
1 parent 719aae3 commit f6cab50

5 files changed

Lines changed: 16 additions & 8 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ target/
66
**/*.rs.bk
77
Cargo.lock
88
wintun.dll
9-
.history

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ nix = { version = "0.29", features = ["ioctl"] }
3030

3131
[target.'cfg(target_os = "windows")'.dependencies]
3232
wintun = { version = "0.4", features = ["panic_on_unsent_packets"] }
33-
libloading = "0.8.3"
33+
libloading = "0.8"
3434

3535
[target.'cfg(any(target_os = "macos", target_os = "freebsd"))'.dependencies]
3636
ipnet = "2"

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,4 @@ pub extern "C" fn start_tun(fd: std::os::raw::c_int) {
135135
Windows
136136
-----
137137
You need to copy the [wintun.dll](https://wintun.net/) file which matches your architecture to
138-
the same directory as your executable or can set `WINTUN_LIBARAY_PATH` to the dll file and run your program as administrator.
139-
138+
the same directory as your executable and run your program as administrator.

src/platform/windows/device.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ pub struct Device {
3131
impl Device {
3232
/// Create a new `Device` for the given `Configuration`.
3333
pub fn new(config: &Configuration) -> Result<Self> {
34+
let wintun_path = match &config.platform_config.wintun_path {
35+
Some(path) => path.clone(),
36+
None => "wintun.dll".to_string(),
37+
};
3438
let wintun = unsafe {
35-
let wintun_libray_path =
36-
std::env::var("WINTUN_LIBARAY_PATH").unwrap_or("wintun.dll".to_string());
37-
let wintun = libloading::Library::new(wintun_libray_path)?;
39+
let wintun = libloading::Library::new(wintun_path)?;
3840
wintun::load_from_library(wintun)?
3941
};
4042
let tun_name = config.tun_name.as_deref().unwrap_or("wintun");

src/platform/windows/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@ use crate::configuration::Configuration;
2222
use crate::error::Result;
2323

2424
/// Windows-only interface configuration.
25-
#[derive(Copy, Clone, Default, Debug)]
25+
#[derive(Clone, Default, Debug)]
2626
pub struct PlatformConfig {
2727
pub(crate) device_guid: Option<u128>,
28+
pub(crate) wintun_path: Option<String>,
2829
}
2930

3031
impl PlatformConfig {
3132
pub fn device_guid(&mut self, device_guid: Option<u128>) {
3233
log::trace!("Windows configuration device GUID");
3334
self.device_guid = device_guid;
3435
}
36+
37+
/// Use a custom path to the wintun.dll instead of looking in the working directory.
38+
/// Security note: It is up to the caller to ensure that the library can be safely loaded from
39+
/// the indicated path.
40+
pub fn custom_wintun_path(&mut self, wintun_path: Option<String>) {
41+
self.wintun_path = wintun_path;
42+
}
3543
}
3644

3745
/// Create a TUN device with the given name.

0 commit comments

Comments
 (0)