Support isp and distance query params on getIP

This commit is contained in:
Maddie Zhan 2020-03-04 18:50:21 +08:00
parent 687a5aeca0
commit 79b4c54770
2 changed files with 24 additions and 16 deletions

View file

@ -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 {

View file

@ -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
removeRegexp := regexp.MustCompile(`AS\d+\s`)
isp := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
if isp == "" {
isp = "Unknown ISP"
}
if ispInfo.Country != "" {
isp += ", " + ispInfo.Country
}
if ispInfo.Location != "" && getDistance {
isp += " (" + calculateDistance(ispInfo.Location, config.LoadedConfig().DistanceUnit) + ")"
}
ret.ProcessedString += " - " + isp
}
if ispInfo.Country != "" {
isp += ", " + ispInfo.Country
}
if ispInfo.Location != "" {
isp += " (" + calculateDistance(ispInfo.Location, config.LoadedConfig().DistanceUnit) + ")"
}
ret.ProcessedString = clientIP + " - " + isp
render.JSON(w, r, ret)
}