Просмотр исходного кода

Reset broker clients on NetworkChanged event

Rod Hynes 1 год назад
Родитель
Сommit
57351289ae
2 измененных файлов с 24 добавлено и 2 удалено
  1. 5 1
      psiphon/controller.go
  2. 19 1
      psiphon/inproxy.go

+ 5 - 1
psiphon/controller.go

@@ -461,8 +461,12 @@ func (controller *Controller) SetDynamicConfig(sponsorID string, authorizations
 // proxy connections.
 func (controller *Controller) NetworkChanged() {
 
-	// Tunnels don't yet use the current network context.
+	// Explicitly reset components that don't use the current network context.
 	controller.TerminateNextActiveTunnel()
+	if controller.inproxyProxyBrokerClientManager != nil {
+		controller.inproxyProxyBrokerClientManager.NetworkChanged()
+	}
+	controller.inproxyClientBrokerClientManager.NetworkChanged()
 
 	controller.currentNetworkMutex.Lock()
 	defer controller.currentNetworkMutex.Unlock()

+ 19 - 1
psiphon/inproxy.go

@@ -114,6 +114,22 @@ func (b *InproxyBrokerClientManager) TacticsApplied() error {
 	return errors.Trace(b.reset(resetBrokerClientReasonTacticsApplied))
 }
 
+// NetworkChanged is called when the active network changes, to trigger a
+// broker client reset.
+func (b *InproxyBrokerClientManager) NetworkChanged() error {
+
+	b.mutex.Lock()
+	defer b.mutex.Unlock()
+
+	// Don't reset when not yet initialized; b.brokerClientInstance is
+	// initialized only on demand.
+	if b.brokerClientInstance == nil {
+		return nil
+	}
+
+	return errors.Trace(b.reset(resetBrokerClientReasonNetworkChanged))
+}
+
 // GetBrokerClient returns the current, shared broker client and its
 // corresponding dial parametrers (for metrics logging). If there is no
 // current broker client, if the network ID differs from the network ID
@@ -195,6 +211,7 @@ type resetBrokerClientReason int
 const (
 	resetBrokerClientReasonInit resetBrokerClientReason = iota + 1
 	resetBrokerClientReasonTacticsApplied
+	resetBrokerClientReasonNetworkChanged
 	resetBrokerClientReasonRoundTripperFailed
 	resetBrokerClientReasonRoundNoMatch
 )
@@ -220,7 +237,8 @@ func (b *InproxyBrokerClientManager) reset(reason resetBrokerClientReason) error
 
 	switch reason {
 	case resetBrokerClientReasonInit,
-		resetBrokerClientReasonTacticsApplied:
+		resetBrokerClientReasonTacticsApplied,
+		resetBrokerClientReasonNetworkChanged:
 		b.brokerSelectCount = 0
 
 	case resetBrokerClientReasonRoundTripperFailed,