Skip to content

Commit

Permalink
Only bring the interface up when peers are added
Browse files Browse the repository at this point in the history
  • Loading branch information
Jauler committed Nov 11, 2024
1 parent bee067e commit e5ffad8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/telio-wg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ cc.workspace = true
sha2.workspace = true
winapi = { workspace = true, features = ["nldef"] }

wireguard-nt = { git = "https://github.com/NordSecurity/wireguard-nt-rust-wrapper", tag = "v1.0.5" }
wireguard-nt = { git = "https://github.com/NordSecurity/wireguard-nt-rust-wrapper", branch = "unplugged_adapter_on_windows_experiment" }

wg-go-rust-wrapper = { path = "../../wireguard-go-rust-wrapper" }

Expand Down
83 changes: 57 additions & 26 deletions crates/telio-wg/src/adapter/windows_native_wg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,21 @@ impl WindowsNativeWg {
))));
}

let mut os_error = IOError::from_raw_os_error(0);
for _ in 0..5 {
if wg_dev.adapter.up() {
telio_log_debug!("Adapter state set to up");
return Ok(wg_dev);
}
os_error = IOError::last_os_error();
telio_log_warn!("Failed to set adapter state to up, last error: {os_error:?}");
sleep(Duration::from_millis(200));
}
Err(AdapterError::WindowsNativeWg(Error::Fail(format!(
"Failed to set adapter's state to up, last error: {os_error:?}",
))))
Ok(wg_dev)

//let mut os_error = IOError::from_raw_os_error(0);
//for _ in 0..5 {
// if wg_dev.adapter.up() {
// telio_log_debug!("Adapter state set to up");
// return Ok(wg_dev);
// }
// os_error = IOError::last_os_error();
// telio_log_warn!("Failed to set adapter state to up, last error: {os_error:?}");
// sleep(Duration::from_millis(200));
//}
//Err(AdapterError::WindowsNativeWg(Error::Fail(format!(
// "Failed to set adapter's state to up, last error: {os_error:?}",
//))))
}

fn get_config_uapi(&self) -> Response {
Expand Down Expand Up @@ -208,21 +210,50 @@ impl Adapter for WindowsNativeWg {
async fn send_uapi_cmd(&self, cmd: &Cmd) -> Result<Response, AdapterError> {
match cmd {
Get => Ok(self.get_config_uapi()),
Set(set_cfg) => match self.adapter.set_config_uapi(set_cfg) {
Ok(()) => {
// Remember last successfully set configuration
if let Ok(mut interface_watcher) =
(self as &WindowsNativeWg).watcher.clone().lock()
{
interface_watcher.set_last_known_configuration(set_cfg);
Set(set_cfg) => {
// If we have any peers added -> bring the adapter up
if set_cfg.peers.len() > 0 && !self.adapter.is_up() {
if self.adapter.up() {
telio_log_info!("Adapter brought up succesfully");
} else {
let os_error = IOError::last_os_error();
telio_log_warn!("Failed to set adapter state to up, last error: {os_error:?}");
return Err(os_error.into());
}

Ok(self.get_config_uapi())
}
Err(_err) => Ok(Response {
errno: 1,
interface: None,
}),

let (resp, peer_cnt) = match self.adapter.set_config_uapi(set_cfg) {
Ok(()) => {
// Remember last successfully set configuration
if let Ok(mut interface_watcher) =
(self as &WindowsNativeWg).watcher.clone().lock()
{
interface_watcher.set_last_known_configuration(set_cfg);
}

let resp = self.get_config_uapi();
let peer_cnt = resp.interface.as_ref().map(|i| i.peers.len());
(Ok(resp), peer_cnt)
}
Err(_err) => (Ok(Response {
errno: 1,
interface: None,
}),
None)
};

// If all of the peers has been removed -> bring the adapter down
if peer_cnt.map(|p| p == 0).unwrap_or(false) && self.adapter.is_up() {
if self.adapter.down() {
telio_log_info!("Adapter brought down succesfully");
} else {
let os_error = IOError::last_os_error();
telio_log_warn!("Failed to set adapter state to down, last error: {os_error:?}");
return Err(os_error.into());
}
};

resp
},
}
}
Expand Down
38 changes: 19 additions & 19 deletions crates/telio-wg/src/wg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,25 +629,25 @@ impl State {
// but will properly resume work after that. In order to determine a non-recoverable failure
// such as a malicious removal, we need to count the successive failed calls.
// If a certain threshold is reached, cleanup the network config and notify the app about connection loss.
if 0 == ret.errno {
self.uapi_fail_counter = 0;
} else {
self.uapi_fail_counter += 1;
}

if self.uapi_fail_counter >= MAX_UAPI_FAIL_COUNT && ret.interface.is_none() {
if let Some(libtelio_event) = &self.libtelio_event {
let err_event = LibtelioEvent::builder::<LibtelioError>()
.set(EventMsg::from("Interface gone"))
.set(ErrorCode::Unknown)
.set(ErrorLevel::Critical)
.build();
if let Some(err_event) = err_event {
let _ = libtelio_event.send(Box::new(err_event));
}
}
return Err(Error::InternalError("Interface gone"));
}
//if 0 == ret.errno {
// self.uapi_fail_counter = 0;
//} else {
// self.uapi_fail_counter += 1;
//}

//if self.uapi_fail_counter >= MAX_UAPI_FAIL_COUNT && ret.interface.is_none() {
// if let Some(libtelio_event) = &self.libtelio_event {
// let err_event = LibtelioEvent::builder::<LibtelioError>()
// .set(EventMsg::from("Interface gone"))
// .set(ErrorCode::Unknown)
// .set(ErrorLevel::Critical)
// .build();
// if let Some(err_event) = err_event {
// let _ = libtelio_event.send(Box::new(err_event));
// }
// }
// return Err(Error::InternalError("Interface gone"));
//}

Ok(ret)
}
Expand Down

0 comments on commit e5ffad8

Please sign in to comment.