@@ -31,6 +31,7 @@ use objc2_network_extension::{
3131use serde:: { Deserialize , Serialize } ;
3232use sqlx:: SqliteExecutor ;
3333use tauri:: { AppHandle , Emitter , Manager } ;
34+ use tracing:: Level ;
3435
3536use crate :: {
3637 active_connections:: find_connection,
@@ -41,6 +42,7 @@ use crate::{
4142 } ,
4243 error:: Error ,
4344 events:: EventKey ,
45+ log_watcher:: service_log_watcher:: spawn_log_watcher_task,
4446 utils:: { DEFAULT_ROUTE_IPV4 , DEFAULT_ROUTE_IPV6 } ,
4547 ConnectionType ,
4648} ;
@@ -62,6 +64,8 @@ static VPN_STATE_UPDATE_COMMS: LazyLock<(
6264 ( Mutex :: new ( tx) , Mutex :: new ( Some ( rx) ) )
6365} ) ;
6466
67+ const SYSTEM_SYNC_DELAY_MS : u64 = 500 ;
68+
6569/// Thread responsible for handling VPN status update requests.
6670/// This is an async function.
6771/// It has access to the `AppHandle` to be able to emit events.
@@ -78,6 +82,7 @@ pub async fn connection_state_update_thread(app_handle: &AppHandle) {
7882 debug ! ( "Waiting for status update message from channel..." ) ;
7983
8084 debug ! ( "Status update message received, synchronizing state..." ) ;
85+ tokio:: time:: sleep ( Duration :: from_millis ( SYSTEM_SYNC_DELAY_MS ) ) . await ;
8186 sync_connections_with_system ( app_handle) . await ;
8287
8388 debug ! ( "Processed status update message." ) ;
@@ -128,6 +133,26 @@ pub async fn sync_connections_with_system(app_handle: &AppHandle) {
128133 app_handle
129134 . emit ( EventKey :: ConnectionChanged . into ( ) , ( ) )
130135 . unwrap ( ) ;
136+
137+ debug ! (
138+ "Spawning log watcher for location {} (started from system settings)" ,
139+ location. name
140+ ) ;
141+ if let Err ( e) = spawn_log_watcher_task (
142+ app_handle. clone ( ) ,
143+ location. id ,
144+ location. name . clone ( ) ,
145+ ConnectionType :: Location ,
146+ Level :: DEBUG ,
147+ None ,
148+ )
149+ . await
150+ {
151+ warn ! (
152+ "Failed to spawn log watcher for location {}: {e}" ,
153+ location. name
154+ ) ;
155+ }
131156 }
132157 }
133158 Some ( NEVPNStatus :: Disconnected ) => {
@@ -197,6 +222,27 @@ pub async fn sync_connections_with_system(app_handle: &AppHandle) {
197222 app_handle
198223 . emit ( EventKey :: ConnectionChanged . into ( ) , ( ) )
199224 . unwrap ( ) ;
225+
226+ // Spawn log watcher for this tunnel (VPN was started from system settings)
227+ debug ! (
228+ "Spawning log watcher for tunnel {} (started from system settings)" ,
229+ tunnel. name
230+ ) ;
231+ if let Err ( e) = spawn_log_watcher_task (
232+ app_handle. clone ( ) ,
233+ tunnel. id ,
234+ tunnel. name . clone ( ) ,
235+ ConnectionType :: Tunnel ,
236+ Level :: DEBUG ,
237+ None ,
238+ )
239+ . await
240+ {
241+ warn ! (
242+ "Failed to spawn log watcher for tunnel {}: {e}" ,
243+ tunnel. name
244+ ) ;
245+ }
200246 }
201247 }
202248 Some ( NEVPNStatus :: Disconnected ) => {
@@ -676,7 +722,7 @@ impl TunnelConfiguration {
676722 info ! ( "VPN started" ) ;
677723 }
678724 } else {
679- error ! (
725+ debug ! (
680726 "Couldn't find configuration from system settings for {}" ,
681727 self . name
682728 ) ;
@@ -691,7 +737,7 @@ pub(crate) fn remove_config_for_location(location: &Location<Id>) {
691737 provider_manager. removeFromPreferencesWithCompletionHandler ( None ) ;
692738 }
693739 } else {
694- error ! (
740+ debug ! (
695741 "Couldn't find configuration in system settings for location {}" ,
696742 location. name
697743 ) ;
@@ -705,7 +751,7 @@ pub(crate) fn remove_config_for_tunnel(tunnel: &Tunnel<Id>) {
705751 provider_manager. removeFromPreferencesWithCompletionHandler ( None ) ;
706752 }
707753 } else {
708- error ! (
754+ debug ! (
709755 "Couldn't find configuration in system settings for tunnel {}" ,
710756 tunnel. name
711757 ) ;
@@ -716,7 +762,7 @@ pub(crate) fn remove_config_for_tunnel(tunnel: &Tunnel<Id>) {
716762pub ( crate ) fn stop_tunnel_for_location ( location : & Location < Id > ) -> bool {
717763 manager_for_key_and_value ( "locationId" , location. id ) . map_or_else (
718764 || {
719- error ! (
765+ debug ! (
720766 "Couldn't find configuration in system settings for location {}" ,
721767 location. name
722768 ) ;
@@ -726,6 +772,7 @@ pub(crate) fn stop_tunnel_for_location(location: &Location<Id>) -> bool {
726772 unsafe {
727773 provider_manager. connection ( ) . stopVPNTunnel ( ) ;
728774 }
775+
729776 info ! ( "VPN stopped" ) ;
730777 true
731778 } ,
@@ -736,7 +783,7 @@ pub(crate) fn stop_tunnel_for_location(location: &Location<Id>) -> bool {
736783pub ( crate ) fn stop_tunnel_for_tunnel ( tunnel : & Tunnel < Id > ) -> bool {
737784 manager_for_key_and_value ( "tunnelId" , tunnel. id ) . map_or_else (
738785 || {
739- error ! (
786+ debug ! (
740787 "Couldn't find configuration in system settings for location {}" ,
741788 tunnel. name
742789 ) ;
@@ -746,6 +793,7 @@ pub(crate) fn stop_tunnel_for_tunnel(tunnel: &Tunnel<Id>) -> bool {
746793 unsafe {
747794 provider_manager. connection ( ) . stopVPNTunnel ( ) ;
748795 }
796+
749797 info ! ( "VPN stopped" ) ;
750798 true
751799 } ,
@@ -756,7 +804,7 @@ pub(crate) fn stop_tunnel_for_tunnel(tunnel: &Tunnel<Id>) -> bool {
756804pub ( crate ) fn get_location_status ( location : & Location < Id > ) -> Option < NEVPNStatus > {
757805 manager_for_key_and_value ( "locationId" , location. id ) . map_or_else (
758806 || {
759- error ! (
807+ debug ! (
760808 "Couldn't find configuration in system settings for location {}" ,
761809 location. name
762810 ) ;
@@ -773,7 +821,7 @@ pub(crate) fn get_location_status(location: &Location<Id>) -> Option<NEVPNStatus
773821pub ( crate ) fn get_tunnel_status ( tunnel : & Tunnel < Id > ) -> Option < NEVPNStatus > {
774822 manager_for_key_and_value ( "tunnelId" , tunnel. id ) . map_or_else (
775823 || {
776- error ! (
824+ debug ! (
777825 "Couldn't find configuration in system settings for tunnel {}" ,
778826 tunnel. name
779827 ) ;
0 commit comments