RawISPInfo should be an object instead of string

This commit is contained in:
Maddie Zhan 2020-03-05 00:06:43 +08:00
parent 73318e153b
commit 0725be907d
3 changed files with 11 additions and 34 deletions

View file

@ -58,8 +58,8 @@ var (
)
type Result struct {
ProcessedString string `json:"processedString"`
RawISPInfo string `json:"rawIspInfo"`
ProcessedString string `json:"processedString"`
RawISPInfo IPInfoResponse `json:"rawIspInfo"`
}
type IPInfoResponse struct {
@ -129,19 +129,6 @@ 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 {
// if ISP info is not available (i.e. localhost testing), use ProcessedString as Organization
ret.Organization = r.ProcessedString
}
return ret, err
}
func Record(w http.ResponseWriter, r *http.Request) {
ipAddr, _, _ := net.SplitHostPort(r.RemoteAddr)
userAgent := r.UserAgent()
@ -215,13 +202,6 @@ func DrawPNG(w http.ResponseWriter, r *http.Request) {
return
}
ispInfo, err := result.GetISPInfo()
if err != nil {
log.Errorf("Error parsing ISP info: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
canvas := image.NewRGBA(image.Rectangle{
Min: image.Point{},
Max: image.Point{
@ -342,9 +322,9 @@ func DrawPNG(w http.ResponseWriter, r *http.Request) {
drawer.Src = colorISP
drawer.Dot = freetype.Pt(6, canvasHeight-ctx.PointToFixed(14).Round()-15)
removeRegexp := regexp.MustCompile(`AS\d+\s`)
org := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
if ispInfo.Country != "" {
org += ", " + ispInfo.Country
org := removeRegexp.ReplaceAllString(result.RawISPInfo.Organization, "")
if result.RawISPInfo.Country != "" {
org += ", " + result.RawISPInfo.Country
}
drawer.DrawString(org)

View file

@ -49,18 +49,18 @@ func getIPInfoURL(address string) string {
return ipInfoURL
}
func getIPInfo(addr string) (string, results.IPInfoResponse) {
func getIPInfo(addr string) results.IPInfoResponse {
var ret results.IPInfoResponse
resp, err := http.DefaultClient.Get(getIPInfoURL(addr))
if err != nil {
log.Errorf("Error getting response from ipinfo.io: %s", err)
return "", ret
return ret
}
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("Error reading response from ipinfo.io: %s", err)
return "", ret
return ret
}
defer resp.Body.Close()
@ -68,7 +68,7 @@ func getIPInfo(addr string) (string, results.IPInfoResponse) {
log.Errorf("Error parsing response from ipinfo.io: %s", err)
}
return string(raw), ret
return ret
}
func getServerLocation() (float64, float64) {

View file

@ -160,8 +160,8 @@ func getIP(w http.ResponseWriter, r *http.Request) {
ret.ProcessedString = clientIP
if getISPInfo {
rawIspInfo, ispInfo := getIPInfo(clientIP)
ret.RawISPInfo = rawIspInfo
ispInfo := getIPInfo(clientIP)
ret.RawISPInfo = ispInfo
removeRegexp := regexp.MustCompile(`AS\d+\s`)
isp := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
@ -179,9 +179,6 @@ func getIP(w http.ResponseWriter, r *http.Request) {
}
ret.ProcessedString += " - " + isp
} else {
// return an empty JSON object to avoid parse errors
ret.RawISPInfo = "{}"
}
render.JSON(w, r, ret)