Browse Source

Support optional client_asn query parameter in home page URLs

Rod Hynes 6 years ago
parent
commit
6369f8fa5b
3 changed files with 32 additions and 17 deletions
  1. 1 1
      psiphon/server/api.go
  2. 19 5
      psiphon/server/psinet/psinet.go
  3. 12 11
      psiphon/server/psinet/psinet_test.go

+ 1 - 1
psiphon/server/api.go

@@ -312,7 +312,7 @@ func handshakeAPIRequestHandler(
 
 	handshakeResponse := protocol.HandshakeResponse{
 		SSHSessionID:           sessionID,
-		Homepages:              db.GetRandomizedHomepages(sponsorID, geoIPData.Country, isMobile),
+		Homepages:              db.GetRandomizedHomepages(sponsorID, geoIPData.Country, geoIPData.ASN, isMobile),
 		UpgradeClientVersion:   db.GetUpgradeClientVersion(clientVersion, normalizedPlatform),
 		PageViewRegexes:        make([]map[string]string, 0),
 		HttpsRequestRegexes:    httpsRequestRegexes,

+ 19 - 5
psiphon/server/psinet/psinet.go

@@ -117,8 +117,10 @@ 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 string, isMobilePlatform bool) []string {
-	homepages := db.GetHomepages(sponsorID, clientRegion, isMobilePlatform)
+func (db *Database) GetRandomizedHomepages(
+	sponsorID, clientRegion, clientASN string, isMobilePlatform bool) []string {
+
+	homepages := db.GetHomepages(sponsorID, clientRegion, clientASN, isMobilePlatform)
 	if len(homepages) > 1 {
 		shuffledHomepages := make([]string, len(homepages))
 		perm := rand.Perm(len(homepages))
@@ -132,7 +134,9 @@ func (db *Database) GetRandomizedHomepages(sponsorID, clientRegion string, isMob
 
 // GetHomepages returns a list of home pages for the specified sponsor,
 // region, and platform.
-func (db *Database) GetHomepages(sponsorID, clientRegion string, isMobilePlatform bool) []string {
+func (db *Database) GetHomepages(
+	sponsorID, clientRegion, clientASN string, isMobilePlatform bool) []string {
+
 	db.ReloadableFile.RLock()
 	defer db.ReloadableFile.RUnlock()
 
@@ -163,7 +167,8 @@ func (db *Database) GetHomepages(sponsorID, clientRegion string, isMobilePlatfor
 	homePagesByRegion, ok := homePages[clientRegion]
 	if ok {
 		for _, homePage := range homePagesByRegion {
-			sponsorHomePages = append(sponsorHomePages, strings.Replace(homePage.URL, "client_region=XX", "client_region="+clientRegion, 1))
+			sponsorHomePages = append(
+				sponsorHomePages, homepageQueryParameterSubstitution(homePage.URL, clientRegion, clientASN))
 		}
 	}
 
@@ -173,7 +178,8 @@ func (db *Database) GetHomepages(sponsorID, clientRegion string, isMobilePlatfor
 		if ok {
 			for _, homePage := range defaultHomePages {
 				// client_region query parameter substitution
-				sponsorHomePages = append(sponsorHomePages, strings.Replace(homePage.URL, "client_region=XX", "client_region="+clientRegion, 1))
+				sponsorHomePages = append(
+					sponsorHomePages, homepageQueryParameterSubstitution(homePage.URL, clientRegion, clientASN))
 			}
 		}
 	}
@@ -181,6 +187,14 @@ func (db *Database) GetHomepages(sponsorID, clientRegion string, isMobilePlatfor
 	return sponsorHomePages
 }
 
+func homepageQueryParameterSubstitution(
+	url, clientRegion, clientASN string) string {
+
+	return strings.Replace(
+		strings.Replace(url, "client_region=XX", "client_region="+clientRegion, 1),
+		"client_asn=XX", "client_asn="+clientASN, 1)
+}
+
 // GetUpgradeClientVersion returns a new client version when an upgrade is
 // indicated for the specified client current version. The result is "" when
 // no upgrade is available. Caller should normalize clientPlatform.

+ 12 - 11
psiphon/server/psinet/psinet_test.go

@@ -55,11 +55,11 @@ func TestDatabase(t *testing.T) {
                 "mobile_home_pages": {
                     "CLIENT-REGION" : [{
                         "region" : "CLIENT-REGION",
-                        "url" : "MOBILE-HOME-PAGE-URL?client_region=XX"
+                        "url" : "MOBILE-HOME-PAGE-URL?client_region=XX&client_asn=XX"
                      }],
                     "None" : [{
                         "region" : "None",
-                        "url" : "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=XX"
+                        "url" : "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=XX&client_asn=XX"
                      }]
                 },
                 "https_request_regexes" : [{
@@ -109,22 +109,23 @@ func TestDatabase(t *testing.T) {
 	homePageTestCases := []struct {
 		sponsorID    string
 		clientRegion string
+		clientASN    string
 		isMobile     bool
 		expectedURL  string
 	}{
-		{"SPONSOR-ID", "CLIENT-REGION", false, "HOME-PAGE-URL?client_region=CLIENT-REGION"},
-		{"SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", false, "DEFAULT-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION"},
-		{"SPONSOR-ID", "CLIENT-REGION", true, "MOBILE-HOME-PAGE-URL?client_region=CLIENT-REGION"},
-		{"SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", true, "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION"},
-		{"UNCONFIGURED-SPONSOR-ID", "CLIENT-REGION", false, "HOME-PAGE-URL?client_region=CLIENT-REGION"},
-		{"UNCONFIGURED-SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", false, "DEFAULT-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION"},
-		{"UNCONFIGURED-SPONSOR-ID", "CLIENT-REGION", true, "MOBILE-HOME-PAGE-URL?client_region=CLIENT-REGION"},
-		{"UNCONFIGURED-SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", true, "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION"},
+		{"SPONSOR-ID", "CLIENT-REGION", "65535", false, "HOME-PAGE-URL?client_region=CLIENT-REGION"},
+		{"SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", "65535", false, "DEFAULT-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION"},
+		{"SPONSOR-ID", "CLIENT-REGION", "65535", true, "MOBILE-HOME-PAGE-URL?client_region=CLIENT-REGION&client_asn=65535"},
+		{"SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", "65535", true, "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION&client_asn=65535"},
+		{"UNCONFIGURED-SPONSOR-ID", "CLIENT-REGION", "65535", false, "HOME-PAGE-URL?client_region=CLIENT-REGION"},
+		{"UNCONFIGURED-SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", "65535", false, "DEFAULT-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION"},
+		{"UNCONFIGURED-SPONSOR-ID", "CLIENT-REGION", "65535", true, "MOBILE-HOME-PAGE-URL?client_region=CLIENT-REGION&client_asn=65535"},
+		{"UNCONFIGURED-SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", "65535", true, "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION&client_asn=65535"},
 	}
 
 	for _, testCase := range homePageTestCases {
 		t.Run(fmt.Sprintf("%+v", testCase), func(t *testing.T) {
-			homepages := db.GetHomepages(testCase.sponsorID, testCase.clientRegion, testCase.isMobile)
+			homepages := db.GetHomepages(testCase.sponsorID, testCase.clientRegion, testCase.clientASN, testCase.isMobile)
 			if len(homepages) != 1 || homepages[0] != testCase.expectedURL {
 				t.Fatalf("unexpected home page: %+v", homepages)
 			}