wgctrl-rs: modprobe wireguard if it's available but not loaded

Fixes #5
pull/36/head
Jake McGinty 2021-04-07 18:41:38 +09:00
parent 4ec2f4099b
commit 41fd9014c0
1 changed files with 13 additions and 0 deletions

View File

@ -10,10 +10,14 @@ use std::{
net::{IpAddr, SocketAddr},
os::raw::c_char,
path::Path,
process::Command,
ptr, str,
sync::Once,
time::{Duration, SystemTime},
};
static MODPROBE: Once = Once::new();
impl<'a> From<&'a wgctrl_sys::wg_allowedip> for AllowedIp {
fn from(raw: &wgctrl_sys::wg_allowedip) -> AllowedIp {
let addr = match i32::from(raw.family) {
@ -303,6 +307,15 @@ fn encode_name(name: &str) -> [c_char; 16] {
}
pub fn exists() -> bool {
// Try to load the wireguard module is loaded if it was not before.
MODPROBE.call_once(|| {
Command::new("/sbin/modprobe")
.arg("wireguard")
.output()
.ok();
});
// Check that the wireguard module is loaded.
Path::new("/sys/module/wireguard").is_dir()
}