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

Add device_region to homepageQueryParameterSubstitution

Rod Hynes 3 лет назад
Родитель
Сommit
e8b9f0e99d
3 измененных файлов с 30 добавлено и 14 удалено
  1. 10 1
      psiphon/server/api.go
  2. 17 12
      psiphon/server/psinet/psinet.go
  3. 3 1
      psiphon/server/tunnelServer.go

+ 10 - 1
psiphon/server/api.go

@@ -241,6 +241,11 @@ func handshakeAPIRequestHandler(
 		}
 	}
 
+	deviceRegion, ok := getOptionalStringRequestParam(params, "device_region")
+	if !ok {
+		deviceRegion = GEOIP_UNKNOWN_VALUE
+	}
+
 	// Note: no guarantee that PsinetDatabase won't reload between database calls
 	db := support.PsinetDatabase
 
@@ -261,6 +266,7 @@ func handshakeAPIRequestHandler(
 			domainBytesChecksum:     domainBytesChecksum,
 			establishedTunnelsCount: establishedTunnelsCount,
 			splitTunnelLookup:       splitTunnelLookup,
+			deviceRegion:            deviceRegion,
 		},
 		authorizations)
 	if err != nil {
@@ -367,9 +373,12 @@ func handshakeAPIRequestHandler(
 	// the JSON response, return an empty array instead of null for legacy
 	// clients.
 
+	homepages := db.GetRandomizedHomepages(
+		sponsorID, geoIPData.Country, geoIPData.ASN, deviceRegion, isMobile)
+
 	handshakeResponse := protocol.HandshakeResponse{
 		SSHSessionID:             sessionID,
-		Homepages:                db.GetRandomizedHomepages(sponsorID, geoIPData.Country, geoIPData.ASN, isMobile),
+		Homepages:                homepages,
 		UpgradeClientVersion:     db.GetUpgradeClientVersion(clientVersion, normalizedPlatform),
 		PageViewRegexes:          make([]map[string]string, 0),
 		HttpsRequestRegexes:      httpsRequestRegexes,

+ 17 - 12
psiphon/server/psinet/psinet.go

@@ -137,9 +137,9 @@ func NewDatabase(filename string) (*Database, error) {
 // GetRandomizedHomepages returns a randomly ordered list of home pages
 // for the specified sponsor, region, and platform.
 func (db *Database) GetRandomizedHomepages(
-	sponsorID, clientRegion, clientASN string, isMobilePlatform bool) []string {
+	sponsorID, clientRegion, clientASN, deviceRegion string, isMobilePlatform bool) []string {
 
-	homepages := db.GetHomepages(sponsorID, clientRegion, clientASN, isMobilePlatform)
+	homepages := db.GetHomepages(sponsorID, clientRegion, clientASN, deviceRegion, isMobilePlatform)
 	if len(homepages) > 1 {
 		shuffledHomepages := make([]string, len(homepages))
 		perm := rand.Perm(len(homepages))
@@ -154,7 +154,7 @@ func (db *Database) GetRandomizedHomepages(
 // GetHomepages returns a list of home pages for the specified sponsor,
 // region, and platform.
 func (db *Database) GetHomepages(
-	sponsorID, clientRegion, clientASN string, isMobilePlatform bool) []string {
+	sponsorID, clientRegion, clientASN, deviceRegion string, isMobilePlatform bool) []string {
 
 	db.ReloadableFile.RLock()
 	defer db.ReloadableFile.RUnlock()
@@ -187,7 +187,8 @@ func (db *Database) GetHomepages(
 	if ok {
 		for _, homePage := range homePagesByRegion {
 			sponsorHomePages = append(
-				sponsorHomePages, homepageQueryParameterSubstitution(homePage.URL, clientRegion, clientASN))
+				sponsorHomePages, homepageQueryParameterSubstitution(
+					homePage.URL, clientRegion, clientASN, deviceRegion))
 		}
 	}
 
@@ -198,7 +199,8 @@ func (db *Database) GetHomepages(
 			for _, homePage := range defaultHomePages {
 				// client_region query parameter substitution
 				sponsorHomePages = append(
-					sponsorHomePages, homepageQueryParameterSubstitution(homePage.URL, clientRegion, clientASN))
+					sponsorHomePages, homepageQueryParameterSubstitution(
+						homePage.URL, clientRegion, clientASN, deviceRegion))
 			}
 		}
 	}
@@ -207,17 +209,18 @@ func (db *Database) GetHomepages(
 }
 
 func homepageQueryParameterSubstitution(
-	url, clientRegion, clientASN string) string {
+	url, clientRegion, clientASN, deviceRegion string) string {
 
-	return strings.Replace(
-		strings.Replace(url, "client_region=XX", "client_region="+clientRegion, 1),
-		"client_asn=XX", "client_asn="+clientASN, 1)
+	url = strings.Replace(url, "client_region=XX", "client_region="+clientRegion, 1)
+	url = strings.Replace(url, "client_asn=XX", "client_asn="+clientASN, 1)
+	url = strings.Replace(url, "device_region=XX", "device_region="+deviceRegion, 1)
+	return url
 }
 
 // GetAlertActionURLs returns a list of alert action URLs for the specified
 // alert reason and sponsor.
 func (db *Database) GetAlertActionURLs(
-	alertReason, sponsorID, clientRegion, clientASN string) []string {
+	alertReason, sponsorID, clientRegion, clientASN, deviceRegion string) []string {
 
 	db.ReloadableFile.RLock()
 	defer db.ReloadableFile.RUnlock()
@@ -231,14 +234,16 @@ func (db *Database) GetAlertActionURLs(
 	if sponsor != nil {
 		for _, URL := range sponsor.AlertActionURLs[alertReason] {
 			actionURLs = append(
-				actionURLs, homepageQueryParameterSubstitution(URL, clientRegion, clientASN))
+				actionURLs, homepageQueryParameterSubstitution(
+					URL, clientRegion, clientASN, deviceRegion))
 		}
 	}
 
 	if len(actionURLs) == 0 {
 		for _, URL := range db.DefaultAlertActionURLs[alertReason] {
 			actionURLs = append(
-				actionURLs, homepageQueryParameterSubstitution(URL, clientRegion, clientASN))
+				actionURLs, homepageQueryParameterSubstitution(
+					URL, clientRegion, clientASN, deviceRegion))
 		}
 	}
 

+ 3 - 1
psiphon/server/tunnelServer.go

@@ -1560,6 +1560,7 @@ type handshakeState struct {
 	domainBytesChecksum     []byte
 	establishedTunnelsCount int
 	splitTunnelLookup       *splitTunnelLookup
+	deviceRegion            string
 }
 
 type destinationBytesMetrics struct {
@@ -3194,7 +3195,8 @@ func (sshClient *sshClient) getAlertActionURLs(alertReason string) []string {
 		alertReason,
 		sponsorID,
 		sshClient.geoIPData.Country,
-		sshClient.geoIPData.ASN)
+		sshClient.geoIPData.ASN,
+		sshClient.handshakeState.deviceRegion)
 }
 
 func (sshClient *sshClient) rejectNewChannel(newChannel ssh.NewChannel, logMessage string) {