Browse Source

Support isp and distance query params on getIP

Maddie Zhan 5 years ago
parent
commit
79b4c54770
2 changed files with 22 additions and 14 deletions
  1. 1 0
      results/telemetry.go
  2. 21 14
      web/web.go

+ 1 - 0
results/telemetry.go

@@ -132,6 +132,7 @@ func init() {
 func (r *Result) GetISPInfo() (IPInfoResponse, error) {
 	var ret IPInfoResponse
 	var err error
+
 	if r.RawISPInfo != "" {
 		err = json.Unmarshal([]byte(r.RawISPInfo), &ret)
 	} else {

+ 21 - 14
web/web.go

@@ -154,25 +154,32 @@ func getIP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	rawIspInfo, ispInfo := getIPInfo(clientIP)
-	ret.RawISPInfo = rawIspInfo
+	getISPInfo := r.FormValue("isp") == "true"
+	getDistance := r.FormValue("distance") == "true"
 
-	removeRegexp := regexp.MustCompile(`AS\d+\s`)
-	isp := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
+	ret.ProcessedString = clientIP
 
-	if isp == "" {
-		isp = "Unknown ISP"
-	}
+	if getISPInfo {
+		rawIspInfo, ispInfo := getIPInfo(clientIP)
+		ret.RawISPInfo = rawIspInfo
 
-	if ispInfo.Country != "" {
-		isp += ", " + ispInfo.Country
-	}
+		removeRegexp := regexp.MustCompile(`AS\d+\s`)
+		isp := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
 
-	if ispInfo.Location != "" {
-		isp += " (" + calculateDistance(ispInfo.Location, config.LoadedConfig().DistanceUnit) + ")"
-	}
+		if isp == "" {
+			isp = "Unknown ISP"
+		}
 
-	ret.ProcessedString = clientIP + " - " + isp
+		if ispInfo.Country != "" {
+			isp += ", " + ispInfo.Country
+		}
+
+		if ispInfo.Location != "" && getDistance {
+			isp += " (" + calculateDistance(ispInfo.Location, config.LoadedConfig().DistanceUnit) + ")"
+		}
+
+		ret.ProcessedString += " - " + isp
+	}
 
 	render.JSON(w, r, ret)
 }