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

Add sponsor ID to dynamic config

Rod Hynes 7 лет назад
Родитель
Сommit
5aad5afa6e

+ 4 - 4
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.h

@@ -203,7 +203,7 @@ This may mean that it had connectivity and now doesn't, or went from Wi-Fi to
 WWAN or vice versa or VPN state changed
 Swift: @code func onInternetReachabilityChanged(_ currentReachability: Reachability) @endcode
 */
-- (void)onInternetReachabilityChanged:(Reachability*_Nonnull)currentReachability;
+- (void)onInternetReachabilityChanged:(Reachability * _Nonnull)currentReachability;
 
 /*!
  Called when tunnel-core determines which server egress regions are available
@@ -333,10 +333,10 @@ Swift: @code func onInternetReachabilityChanged(_ currentReachability: Reachabil
 
 
 /*!
- Reconnect a previously started PsiphonTunnel with authorizations set to the specified list.
- Has no effect if there is no running PsiphonTunnel. authorizations may be nil.
+ Reconnect a previously started PsiphonTunnel with the specified config changes.
+ reconnectWithConfig has no effect if there is no running PsiphonTunnel.
  */
-- (void)reconnectWithAuthorizations:(NSArray<NSString *> *_Nullable)authorizations;
+- (void)reconnectWithConfig:(NSString * _Nullable) newSponsorID :(NSArray<NSString *> *_Nullable)newAuthorizations;
 
 /*!
  Force stops the tunnel and reconnects with the current session ID.

+ 11 - 5
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m

@@ -151,14 +151,20 @@
 }
 
 // See comment in header
-- (void)reconnectWithAuthorizations:(NSArray<NSString *> *_Nullable)authorizations {
+- (void)reconnectWithConfig:(NSString * _Nullable) newSponsorID :(NSArray<NSString *> *_Nullable)newAuthorizations {
+
+    NSString *sponsorID = @"";
+    if (newSponsorID != nil) {
+        sponsorID = newSponsorID;
+    }
 
     NSString *authorizationsList = @"";
-    if (authorizations != nil) {
-        authorizationsList = [authorizations componentsJoinedByString: @" "];
+    if (newAuthorizations != nil) {
+        authorizationsList = [newAuthorizations componentsJoinedByString: @" "];
     }
 
-    GoPsiReconnectTunnel(authorizationsList);
+    GoPsiSetDynamicConfig(sponsorID, authorizationsList);
+    GoPsiReconnectTunnel();
 }
 
 // See comment in header
@@ -1311,7 +1317,7 @@
     // Restart if the state has changed, unless the previous state was NotReachable, because
     // the tunnel should be waiting for connectivity in that case.
     if (networkStatus != previousNetworkStatus && previousNetworkStatus != NotReachable) {
-        GoPsiReconnectTunnel(nil);
+        GoPsiReconnectTunnel();
     }
 }
 

+ 19 - 10
MobileLibrary/psi/psi.go

@@ -184,25 +184,34 @@ func Stop() {
 
 // ReconnectTunnel initiates a reconnect of the current tunnel, if one is
 // running.
-//
-// ReconnectTunnel takes an optional parameter which overrides the
-// Authorizations field set in the config passed to Start. When not "", the
-// input newAuthorizationsList is a space-delimited list of base64
-// Authoriztions. newAuthorizationsList is parsed and applied before the
-// current tunnel is reconnected.
-func ReconnectTunnel(newAuthorizationsList string) {
+func ReconnectTunnel() {
 
 	controllerMutex.Lock()
 	defer controllerMutex.Unlock()
 
 	if controller != nil {
-		if newAuthorizationsList != "" {
-			controller.SetAuthorizations(strings.Split(newAuthorizationsList, " "))
-		}
 		controller.TerminateNextActiveTunnel()
 	}
 }
 
+// SetDynamicConfig overrides the sponsor ID and authorizations fields set in
+// the config passed to Start. SetDynamicConfig has no effect if no Controller
+// is started.
+//
+// The input newAuthorizationsList is a space-delimited list of base64
+// authorizations. This is a workaround for gobind type limitations.
+func SetDynamicConfig(newSponsorID, newAuthorizationsList string) {
+
+	controllerMutex.Lock()
+	defer controllerMutex.Unlock()
+
+	if controller != nil {
+		controller.SetDynamicConfig(
+			newSponsorID,
+			strings.Split(newAuthorizationsList, " "))
+	}
+}
+
 // Encrypt and upload feedback.
 func SendFeedback(configJson, diagnosticsJson, b64EncodedPublicKey, uploadServer, uploadPath, uploadServerHeaders string) error {
 	return psiphon.SendFeedback(configJson, diagnosticsJson, b64EncodedPublicKey, uploadServer, uploadPath, uploadServerHeaders)

+ 26 - 14
psiphon/config.go

@@ -481,8 +481,9 @@ type Config struct {
 	// calling clientParameters.Set directly will fail to add config values.
 	clientParameters *parameters.ClientParameters
 
-	authorizationsMutex sync.Mutex
-	authorizations      []string
+	dynamicConfigMutex sync.Mutex
+	sponsorID          string
+	authorizations     []string
 
 	deviceBinder    DeviceBinder
 	networkIDGetter NetworkIDGetter
@@ -672,9 +673,9 @@ func (config *Config) Commit() error {
 		return common.ContextError(err)
 	}
 
-	// client authorizations default to config.Authorizations
+	// Set defaults for dynamic config fields.
 
-	config.SetAuthorizations(config.Authorizations)
+	config.SetDynamicConfig(config.SponsorId, config.Authorizations)
 
 	// Initialize config.deviceBinder and config.config.networkIDGetter. These
 	// wrap config.DeviceBinder and config.NetworkIDGetter/NetworkID with
@@ -753,22 +754,33 @@ func (config *Config) SetClientParameters(tag string, skipOnError bool, applyPar
 	return nil
 }
 
+// SetDynamicConfig sets the current client sponsor ID and authorizations.
+// Invalid values for sponsor ID are ignored. The caller must not modify the
+// input authorizations slice.
+func (config *Config) SetDynamicConfig(sponsorID string, authorizations []string) {
+	config.dynamicConfigMutex.Lock()
+	defer config.dynamicConfigMutex.Unlock()
+	if sponsorID != "" {
+		config.sponsorID = sponsorID
+	}
+	config.authorizations = authorizations
+}
+
+// GetSponsorID returns the current client sponsor ID.
+func (config *Config) GetSponsorID() string {
+	config.dynamicConfigMutex.Lock()
+	defer config.dynamicConfigMutex.Unlock()
+	return config.sponsorID
+}
+
 // GetAuthorizations returns the current client authorizations.
 // The caller must not modify the returned slice.
 func (config *Config) GetAuthorizations() []string {
-	config.authorizationsMutex.Lock()
-	defer config.authorizationsMutex.Unlock()
+	config.dynamicConfigMutex.Lock()
+	defer config.dynamicConfigMutex.Unlock()
 	return config.authorizations
 }
 
-// SetAuthorizations sets the current client authorizations.
-// The caller must not modify the input slice.
-func (config *Config) SetAuthorizations(authorizations []string) {
-	config.authorizationsMutex.Lock()
-	defer config.authorizationsMutex.Unlock()
-	config.authorizations = authorizations
-}
-
 func (config *Config) makeConfigParameters() map[string]interface{} {
 
 	// Build set of config values to apply to parameters.

+ 5 - 5
psiphon/controller.go

@@ -282,11 +282,11 @@ func (controller *Controller) SignalComponentFailure() {
 	controller.stopRunning()
 }
 
-// SetAuthorizations overrides the Authorizations field of the Controller
-// config with the input value. The new value will be used in the next tunnel
-// connection.
-func (controller *Controller) SetAuthorizations(authorizations []string) {
-	controller.config.SetAuthorizations(authorizations)
+// SetDynamicConfig overrides the sponsor ID and authorizations fields of the
+// Controller config with the input values. The new values will be used in the
+// next tunnel connection.
+func (controller *Controller) SetDynamicConfig(sponsorID string, authorizations []string) {
+	controller.config.SetDynamicConfig(sponsorID, authorizations)
 }
 
 // TerminateNextActiveTunnel terminates the active tunnel, which will initiate

+ 1 - 1
psiphon/serverApi.go

@@ -692,7 +692,7 @@ func getBaseAPIParameters(
 	params["client_session_id"] = sessionID
 	params["server_secret"] = serverEntry.WebServerSecret
 	params["propagation_channel_id"] = config.PropagationChannelId
-	params["sponsor_id"] = config.SponsorId
+	params["sponsor_id"] = config.GetSponsorID()
 	params["client_version"] = config.ClientVersion
 	params["relay_protocol"] = protocol
 	params["client_platform"] = config.ClientPlatform