@@ -163,7 +163,10 @@ type DeviceLocal struct {
163163 windowStatusChangeListeners * connect.CallbackList [WindowStatusChangeListener ]
164164 jwtRefreshListeners * connect.CallbackList [JwtRefreshListener ]
165165
166- localUserNatUnsub func ()
166+ localUserNatSub func ()
167+
168+ ingressSecurityPolicyGenerator func (* connect.SecurityPolicyStatsCollector ) connect.SecurityPolicy
169+ egressSecurityPolicyGenerator func (* connect.SecurityPolicyStatsCollector ) connect.SecurityPolicy
167170
168171 viewControllerManager
169172}
@@ -371,18 +374,28 @@ func newDeviceLocalWithOverrides(
371374 }
372375 deviceLocal .viewControllerManager = * newViewControllerManager (ctx , deviceLocal )
373376
377+ var logout func () error
378+ if networkSpace .asyncLocalState != nil {
379+ logout = networkSpace .asyncLocalState .localState .Logout
380+ } else {
381+ // do nothing
382+ logout = func () error {
383+ return nil
384+ }
385+ }
386+
374387 deviceLocal .tokenManager = newDeviceTokenManager (
375388 ctx ,
376389 api ,
377390 deviceLocal .SetByJwt ,
378391 // TODO the logout event should be propagated to the user
379- networkSpace . asyncLocalState . localState . Logout ,
392+ logout ,
380393 )
381394
382395 // set up with nil destination
383396 if provider != nil {
384- localUserNatUnsub := provider .LocalUserNat ().AddReceivePacketCallback (deviceLocal .receive )
385- deviceLocal .localUserNatUnsub = localUserNatUnsub
397+ localUserNatSub := provider .LocalUserNat ().AddReceivePacketCallback (deviceLocal .receive )
398+ deviceLocal .localUserNatSub = localUserNatSub
386399 }
387400
388401 if enableRpc {
@@ -399,6 +412,20 @@ func (self *DeviceLocal) Ctx() context.Context {
399412 return self .ctx
400413}
401414
415+ // gomobile:ignore
416+ func (self * DeviceLocal ) SetIngressSecurityPolicyGenerator (g func (* connect.SecurityPolicyStatsCollector ) connect.SecurityPolicy ) {
417+ self .stateLock .Lock ()
418+ defer self .stateLock .Unlock ()
419+ self .ingressSecurityPolicyGenerator = g
420+ }
421+
422+ // gomobile:ignore
423+ func (self * DeviceLocal ) SetEgressSecurityPolicyGenerator (g func (* connect.SecurityPolicyStatsCollector ) connect.SecurityPolicy ) {
424+ self .stateLock .Lock ()
425+ defer self .stateLock .Unlock ()
426+ self .egressSecurityPolicyGenerator = g
427+ }
428+
402429func (self * DeviceLocal ) RefreshToken (attempt int ) error {
403430 self .tokenManager .RefreshToken ()
404431 return nil
@@ -461,8 +488,10 @@ func (self *DeviceLocal) client() *connect.Client {
461488func (self * DeviceLocal ) SetByJwt (byJwt string ) {
462489 self .GetApi ().SetByJwt (byJwt )
463490
464- self .networkSpace .asyncLocalState .localState .SetByClientJwt (byJwt )
465- self .networkSpace .asyncLocalState .localState .SetByJwt (byJwt )
491+ if self .networkSpace .asyncLocalState != nil {
492+ self .networkSpace .asyncLocalState .localState .SetByClientJwt (byJwt )
493+ self .networkSpace .asyncLocalState .localState .SetByJwt (byJwt )
494+ }
466495
467496 if self .provider != nil {
468497 self .provider .SetByJwt (byJwt )
@@ -1284,6 +1313,12 @@ func (self *DeviceLocal) SetDestination(location *ConnectLocation, specs *Provid
12841313 }
12851314 settings := connect .DefaultMultiClientSettings ()
12861315 settings .DefaultPerformanceProfile = toConnectPerformanceProfile (self .performanceProfile )
1316+ if self .ingressSecurityPolicyGenerator != nil {
1317+ settings .IngressSecurityPolicyGenerator = self .ingressSecurityPolicyGenerator
1318+ }
1319+ if self .egressSecurityPolicyGenerator != nil {
1320+ settings .EgressSecurityPolicyGenerator = self .egressSecurityPolicyGenerator
1321+ }
12871322 multi := connect .NewRemoteUserNatMultiClient (
12881323 self .ctx ,
12891324 generator ,
@@ -1372,7 +1407,6 @@ func (self *DeviceLocal) SetConnectLocation(location *ConnectLocation) {
13721407 ClientId : location .ConnectLocationId .ClientId ,
13731408 BestAvailable : location .ConnectLocationId .BestAvailable ,
13741409 })
1375-
13761410 self .SetDestination (location , specs )
13771411 }
13781412}
@@ -1515,7 +1549,10 @@ func (self *DeviceLocal) Close() {
15151549 self .remoteUserNatClient = nil
15161550 }
15171551 // self.localUserNat.RemoveReceivePacketCallback(self.receive)
1518- self .localUserNatUnsub ()
1552+ if self .localUserNatSub != nil {
1553+ self .localUserNatSub ()
1554+ self .localUserNatSub = nil
1555+ }
15191556 if self .remoteUserNatProviderLocalUserNat != nil {
15201557 self .remoteUserNatProviderLocalUserNat .Close ()
15211558 self .remoteUserNatProviderLocalUserNat = nil
0 commit comments