Embed wintun inside of the Windows binary

Burrow writes the driver to a temporary file and then loads it.
This commit is contained in:
Conrad Kramer 2023-06-05 03:53:51 -04:00
parent a502e2132c
commit 9dc10544b9
3 changed files with 19 additions and 10 deletions

2
Cargo.lock generated
View file

@ -1299,12 +1299,14 @@ dependencies = [
"anyhow", "anyhow",
"bindgen", "bindgen",
"fehler", "fehler",
"lazy_static",
"libc", "libc",
"libloading", "libloading",
"nix", "nix",
"reqwest", "reqwest",
"socket2", "socket2",
"ssri", "ssri",
"tempfile",
"tokio", "tokio",
"widestring", "widestring",
"windows", "windows",

View file

@ -11,7 +11,9 @@ socket2 = "0.4"
tokio = { version = "1.28", features = [] } tokio = { version = "1.28", features = [] }
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
lazy_static = "1.4"
libloading = "0.7" libloading = "0.7"
tempfile = "3.5"
widestring = "1.0" widestring = "1.0"
windows = { version = "0.48", features = ["Win32_Foundation", "Win32_NetworkManagement_IpHelper"] } windows = { version = "0.48", features = ["Win32_Foundation", "Win32_NetworkManagement_IpHelper"] }

View file

@ -8,7 +8,6 @@ mod queue;
pub use queue::TunQueue; pub use queue::TunQueue;
pub struct TunInterface { pub struct TunInterface {
wintun: sys::wintun,
handle: sys::WINTUN_ADAPTER_HANDLE, handle: sys::WINTUN_ADAPTER_HANDLE,
name: String, name: String,
} }
@ -17,14 +16,12 @@ impl TunInterface {
#[throws] #[throws]
pub fn new() -> TunInterface { pub fn new() -> TunInterface {
let name = U16CString::from(u16cstr!("Burrow")); let name = U16CString::from(u16cstr!("Burrow"));
let wintun = sys::wintun::default();
let handle = let handle =
unsafe { wintun.WintunCreateAdapter(name.as_ptr(), name.as_ptr(), ptr::null()) }; unsafe { sys::WINTUN.WintunCreateAdapter(name.as_ptr(), name.as_ptr(), ptr::null()) };
if handle.is_null() { if handle.is_null() {
unsafe { GetLastError() }.ok()? unsafe { GetLastError() }.ok()?
} }
TunInterface { TunInterface {
wintun,
handle, handle,
name: String::from("Burrow"), name: String::from("Burrow"),
} }
@ -37,17 +34,25 @@ impl TunInterface {
impl Drop for TunInterface { impl Drop for TunInterface {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { self.wintun.WintunCloseAdapter(self.handle) } unsafe { sys::WINTUN.WintunCloseAdapter(self.handle) }
} }
} }
pub(crate) mod sys { pub(crate) mod sys {
#![allow(dead_code, non_camel_case_types, non_snake_case)] #![allow(clippy::all, dead_code, non_camel_case_types, non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/wintun.rs")); include!(concat!(env!("OUT_DIR"), "/wintun.rs"));
impl Default for wintun { const WINTUN_BINARY: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wintun.dll"));
fn default() -> Self {
unsafe { wintun::new(format!("{}/wintun.dll", env!("OUT_DIR"))).unwrap() } lazy_static::lazy_static! {
} pub static ref WINTUN: wintun = {
use std::io::Write;
let mut temp_file = tempfile::NamedTempFile::new().unwrap();
temp_file.write_all(&WINTUN_BINARY).unwrap();
let (_, path) = temp_file.keep().unwrap();
unsafe { wintun::new(&path) }.unwrap()
};
} }
} }