Skip to content

Commit 00323ca

Browse files
authored
macOS: set MTU on config sync (#715)
1 parent 507a268 commit 00323ca

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src-tauri/src/app_config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub struct AppConfig {
7272
/// In seconds. How much time after last network activity the connection is automatically dropped.
7373
pub peer_alive_period: u32,
7474
/// Maximal transmission unit. 0 means default value.
75-
pub mtu: u32,
75+
mtu: u32,
7676
}
7777

7878
// Important: keep in sync with client store default in frontend
@@ -138,7 +138,7 @@ impl AppConfig {
138138
/// ambiguity when applying updates coming from the frontend. An incoming MTU value of 0 is
139139
/// interpreted as a request to fall back to the default.
140140
#[must_use]
141-
pub fn get_mtu(&self) -> Option<u32> {
141+
pub fn mtu(&self) -> Option<u32> {
142142
match self.mtu {
143143
0 => None,
144144
v => Some(v),

src-tauri/src/apple.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,12 @@ pub(crate) fn tunnel_stats(id: Id, connection_type: &ConnectionType) -> Option<S
925925
}
926926

927927
/// Synchronize locations and tunnels with system settings.
928-
pub async fn sync_locations_and_tunnels() -> Result<(), sqlx::Error> {
928+
pub async fn sync_locations_and_tunnels(mtu: Option<u32>) -> Result<(), sqlx::Error> {
929929
// Update location settings.
930930
let all_locations = Location::all(&*DB_POOL, false).await?;
931931
for location in &all_locations {
932-
// For syncing, set `preshred_key` and `mtu` to `None`.
933-
let Ok(tunnel_config) = location.tunnel_configurarion(&*DB_POOL, None, None).await else {
932+
// For syncing, set `preshred_key` to `None`.
933+
let Ok(tunnel_config) = location.tunnel_configurarion(&*DB_POOL, None, mtu).await else {
934934
error!(
935935
"Failed to convert location {} to tunnel configuration.",
936936
location.name
@@ -943,8 +943,7 @@ pub async fn sync_locations_and_tunnels() -> Result<(), sqlx::Error> {
943943
// Update tunnel settings.
944944
let all_tunnels = Tunnel::all(&*DB_POOL).await?;
945945
for tunnel in &all_tunnels {
946-
// For syncing, set `mtu` to `None`.
947-
let Ok(tunnel_config) = tunnel.tunnel_configurarion(None) else {
946+
let Ok(tunnel_config) = tunnel.tunnel_configurarion(mtu) else {
948947
error!(
949948
"Failed to convert tunnel {} to tunnel configuration.",
950949
tunnel.name

src-tauri/src/bin/defguard-client.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,15 @@ async fn startup(app_handle: &AppHandle) {
9090
let semaphore = Arc::new(AtomicBool::new(false));
9191
let semaphore_clone = Arc::clone(&semaphore);
9292

93+
// Retrieve MTU from `AppConfig`.
94+
let app_state = app_handle.state::<AppState>();
95+
let mtu = app_state
96+
.app_config
97+
.lock()
98+
.expect("failed to lock app state")
99+
.mtu();
93100
let handle = tauri::async_runtime::spawn(async move {
94-
if let Err(err) = defguard_client::apple::sync_locations_and_tunnels().await {
101+
if let Err(err) = defguard_client::apple::sync_locations_and_tunnels(mtu).await {
95102
error!("Failed to sync locations and tunnels: {err}");
96103
}
97104
semaphore_clone.store(true, Ordering::Release);

src-tauri/src/utils.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,11 @@ pub(crate) async fn handle_connection_for_location(
676676
) -> Result<(), Error> {
677677
debug!("Setting up the connection for location {}", location.name);
678678
let state = handle.state::<AppState>();
679-
let mtu = state.app_config.lock().unwrap().get_mtu();
679+
let mtu = state
680+
.app_config
681+
.lock()
682+
.expect("failed to lock app state")
683+
.mtu();
680684
let interface_name =
681685
setup_interface(location, &location.name, preshared_key, mtu, &DB_POOL).await?;
682686
state
@@ -709,7 +713,11 @@ pub(crate) async fn handle_connection_for_tunnel(
709713
) -> Result<(), Error> {
710714
debug!("Setting up the connection for tunnel: {}", tunnel.name);
711715
let state = handle.state::<AppState>();
712-
let mtu = state.app_config.lock().unwrap().get_mtu();
716+
let mtu = state
717+
.app_config
718+
.lock()
719+
.expect("failed to lock app state")
720+
.mtu();
713721
let interface_name = setup_interface_tunnel(tunnel, &tunnel.name, mtu).await?;
714722
state
715723
.add_connection(tunnel.id, &interface_name, ConnectionType::Tunnel)

0 commit comments

Comments
 (0)