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

Add DeviceLocation config parameter and logging.

Eugene Fryntov 2 лет назад
Родитель
Сommit
4db5ab330c
3 измененных файлов с 32 добавлено и 2 удалено
  1. 6 0
      psiphon/config.go
  2. 20 2
      psiphon/server/api.go
  3. 6 0
      psiphon/serverApi.go

+ 6 - 0
psiphon/config.go

@@ -446,6 +446,12 @@ type Config struct {
 	// out, the tunnel is considered to have failed.
 	DisablePeriodicSshKeepAlive bool
 
+	// DeviceLocation is the optional, reported location the host device is
+	// running in. This input value should be a string representing location
+	// geohash. The device location is reported to the server in the connected
+	// request and recorded for Psiphon stats.
+	DeviceLocation string
+
 	// DeviceRegion is the optional, reported region the host device is
 	// running in. This input value should be a ISO 3166-1 alpha-2 country
 	// code. The device region is reported to the server in the connected

+ 20 - 2
psiphon/server/api.go

@@ -549,8 +549,8 @@ var remoteServerListStatParams = append(
 // the remote_server_list_stats entries. Use the values from the outer status
 // request as an approximation (these values reflect the client at persistent
 // stat shipping time, which may differ from the client at persistent stat
-// recording time). Note that all but client_build_rev and device_region are
-// required fields.
+// recording time). Note that all but client_build_rev, device_region, and
+// device_location are required fields.
 var remoteServerListStatBackwardsCompatibilityParamNames = []string{
 	"session_id",
 	"propagation_channel_id",
@@ -559,6 +559,7 @@ var remoteServerListStatBackwardsCompatibilityParamNames = []string{
 	"client_platform",
 	"client_build_rev",
 	"device_region",
+	"device_location",
 }
 
 var failedTunnelStatParams = append(
@@ -881,6 +882,7 @@ var baseParams = []requestParamSpec{
 	{"client_features", isAnyString, requestParamOptional | requestParamArray},
 	{"client_build_rev", isHexDigits, requestParamOptional},
 	{"device_region", isAnyString, requestParamOptional},
+	{"device_location", isGeoHashString, requestParamOptional},
 }
 
 // baseSessionParams adds to baseParams the required session_id parameter. For
@@ -1556,3 +1558,19 @@ func isISO8601Date(_ *Config, value string) bool {
 func isLastConnected(_ *Config, value string) bool {
 	return value == "None" || isISO8601Date(nil, value)
 }
+
+const geohashAlphabet = "0123456789bcdefghjkmnpqrstuvwxyz"
+
+func isGeoHashString(_ *Config, value string) bool {
+	// Verify that the string is between 1 and 12 characters long
+	// and contains only characters from the geohash alphabet.
+	if len(value) < 1 || len(value) > 12 {
+		return false
+	}
+	for _, c := range value {
+		if strings.Index(geohashAlphabet, string(c)) == -1 {
+			return false
+		}
+	}
+	return true
+}

+ 6 - 0
psiphon/serverApi.go

@@ -699,6 +699,9 @@ func RecordRemoteServerListStat(
 	if config.DeviceRegion != "" {
 		params["device_region"] = config.DeviceRegion
 	}
+	if config.DeviceLocation != "" {
+		params["device_location"] = config.DeviceLocation
+	}
 
 	params["client_download_timestamp"] = common.TruncateTimestampToHour(common.GetCurrentTimestamp())
 	tunneledStr := "0"
@@ -938,6 +941,9 @@ func getBaseAPIParameters(
 	if config.DeviceRegion != "" {
 		params["device_region"] = config.DeviceRegion
 	}
+	if config.DeviceLocation != "" {
+		params["device_location"] = config.DeviceLocation
+	}
 
 	if filter == baseParametersAll {