@@ -45,7 +45,7 @@ impl Drop for LoggingContext {
45
45
46
46
pub struct WgGoTunnel {
47
47
interface_name : String ,
48
- handle : Option < i32 > , // TODO(sebastian): Remove option
48
+ tunnel_handle : i32 ,
49
49
// holding on to the tunnel device and the log file ensures that the associated file handles
50
50
// live long enough and get closed when the tunnel is stopped
51
51
_tunnel_device : Tun ,
@@ -55,6 +55,7 @@ pub struct WgGoTunnel {
55
55
tun_provider : Arc < Mutex < TunProvider > > ,
56
56
daita_handle : Option < daita:: Session > ,
57
57
resource_dir : PathBuf ,
58
+ config : Config ,
58
59
}
59
60
60
61
impl WgGoTunnel {
@@ -95,18 +96,17 @@ impl WgGoTunnel {
95
96
Self :: bypass_tunnel_sockets ( & mut tunnel_device, handle)
96
97
. map_err ( TunnelError :: BypassError ) ?;
97
98
98
- let wg_go_tunnel = WgGoTunnel {
99
+ Ok ( WgGoTunnel {
99
100
interface_name,
100
- handle : Some ( handle) ,
101
+ tunnel_handle : handle,
101
102
_tunnel_device : tunnel_device,
102
103
_logging_context : logging_context,
103
104
#[ cfg( target_os = "android" ) ]
104
105
tun_provider : tun_provider_clone,
105
106
resource_dir : resource_dir. to_owned ( ) ,
106
107
daita_handle : None ,
107
- } ;
108
-
109
- Ok ( wg_go_tunnel)
108
+ config : config. clone ( ) ,
109
+ } )
110
110
}
111
111
112
112
fn create_tunnel_config (
@@ -156,11 +156,9 @@ impl WgGoTunnel {
156
156
}
157
157
158
158
fn stop_tunnel ( & mut self ) -> Result < ( ) > {
159
- if let Some ( handle) = self . handle . take ( ) {
160
- let status = unsafe { wgTurnOff ( handle) } ;
161
- if status < 0 {
162
- return Err ( TunnelError :: StopWireguardError { status } ) ;
163
- }
159
+ let status = unsafe { wgTurnOff ( self . tunnel_handle ) } ;
160
+ if status < 0 {
161
+ return Err ( TunnelError :: StopWireguardError { status } ) ;
164
162
}
165
163
Ok ( ( ) )
166
164
}
@@ -215,7 +213,7 @@ impl Tunnel for WgGoTunnel {
215
213
216
214
fn get_tunnel_stats ( & self ) -> Result < StatsMap > {
217
215
let config_str = unsafe {
218
- let ptr = wgGetConfig ( self . handle . unwrap ( ) ) ;
216
+ let ptr = wgGetConfig ( self . tunnel_handle ) ;
219
217
if ptr. is_null ( ) {
220
218
log:: error!( "Failed to get config !" ) ;
221
219
return Err ( TunnelError :: GetConfigError ) ;
@@ -250,7 +248,7 @@ impl Tunnel for WgGoTunnel {
250
248
config : Config ,
251
249
) -> Pin < Box < dyn Future < Output = std:: result:: Result < ( ) , super :: TunnelError > > + Send > > {
252
250
let wg_config_str = config. to_userspace_format ( ) ;
253
- let handle = self . handle . unwrap ( ) ;
251
+ let handle = self . tunnel_handle ;
254
252
#[ cfg( target_os = "android" ) ]
255
253
let tun_provider = self . tun_provider . clone ( ) ;
256
254
Box :: pin ( async move {
@@ -297,8 +295,8 @@ impl Tunnel for WgGoTunnel {
297
295
} ) ?;
298
296
299
297
log:: info!( "Initializing DAITA for wireguard device" ) ;
300
- let tunnel_handle = self . handle . expect ( "Tunnel should be active" ) ;
301
- let session = daita:: Session :: from_adapter ( tunnel_handle, machines)
298
+ let peer_public_key = & self . config . entry_peer . public_key ;
299
+ let session = daita:: Session :: from_adapter ( self . tunnel_handle , peer_public_key , machines)
302
300
. expect ( "Wireguard-go should fetch current tunnel from ID" ) ;
303
301
self . daita_handle = Some ( session) ;
304
302
0 commit comments