RawISPInfo should be an object instead of string
This commit is contained in:
parent
73318e153b
commit
0725be907d
|
@ -58,8 +58,8 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
ProcessedString string `json:"processedString"`
|
ProcessedString string `json:"processedString"`
|
||||||
RawISPInfo string `json:"rawIspInfo"`
|
RawISPInfo IPInfoResponse `json:"rawIspInfo"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPInfoResponse struct {
|
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) {
|
func Record(w http.ResponseWriter, r *http.Request) {
|
||||||
ipAddr, _, _ := net.SplitHostPort(r.RemoteAddr)
|
ipAddr, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||||||
userAgent := r.UserAgent()
|
userAgent := r.UserAgent()
|
||||||
|
@ -215,13 +202,6 @@ func DrawPNG(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
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{
|
canvas := image.NewRGBA(image.Rectangle{
|
||||||
Min: image.Point{},
|
Min: image.Point{},
|
||||||
Max: image.Point{
|
Max: image.Point{
|
||||||
|
@ -342,9 +322,9 @@ func DrawPNG(w http.ResponseWriter, r *http.Request) {
|
||||||
drawer.Src = colorISP
|
drawer.Src = colorISP
|
||||||
drawer.Dot = freetype.Pt(6, canvasHeight-ctx.PointToFixed(14).Round()-15)
|
drawer.Dot = freetype.Pt(6, canvasHeight-ctx.PointToFixed(14).Round()-15)
|
||||||
removeRegexp := regexp.MustCompile(`AS\d+\s`)
|
removeRegexp := regexp.MustCompile(`AS\d+\s`)
|
||||||
org := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
|
org := removeRegexp.ReplaceAllString(result.RawISPInfo.Organization, "")
|
||||||
if ispInfo.Country != "" {
|
if result.RawISPInfo.Country != "" {
|
||||||
org += ", " + ispInfo.Country
|
org += ", " + result.RawISPInfo.Country
|
||||||
}
|
}
|
||||||
drawer.DrawString(org)
|
drawer.DrawString(org)
|
||||||
|
|
||||||
|
|
|
@ -49,18 +49,18 @@ func getIPInfoURL(address string) string {
|
||||||
return ipInfoURL
|
return ipInfoURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIPInfo(addr string) (string, results.IPInfoResponse) {
|
func getIPInfo(addr string) results.IPInfoResponse {
|
||||||
var ret results.IPInfoResponse
|
var ret results.IPInfoResponse
|
||||||
resp, err := http.DefaultClient.Get(getIPInfoURL(addr))
|
resp, err := http.DefaultClient.Get(getIPInfoURL(addr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error getting response from ipinfo.io: %s", err)
|
log.Errorf("Error getting response from ipinfo.io: %s", err)
|
||||||
return "", ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
raw, err := ioutil.ReadAll(resp.Body)
|
raw, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error reading response from ipinfo.io: %s", err)
|
log.Errorf("Error reading response from ipinfo.io: %s", err)
|
||||||
return "", ret
|
return ret
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
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)
|
log.Errorf("Error parsing response from ipinfo.io: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(raw), ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServerLocation() (float64, float64) {
|
func getServerLocation() (float64, float64) {
|
||||||
|
|
|
@ -160,8 +160,8 @@ func getIP(w http.ResponseWriter, r *http.Request) {
|
||||||
ret.ProcessedString = clientIP
|
ret.ProcessedString = clientIP
|
||||||
|
|
||||||
if getISPInfo {
|
if getISPInfo {
|
||||||
rawIspInfo, ispInfo := getIPInfo(clientIP)
|
ispInfo := getIPInfo(clientIP)
|
||||||
ret.RawISPInfo = rawIspInfo
|
ret.RawISPInfo = ispInfo
|
||||||
|
|
||||||
removeRegexp := regexp.MustCompile(`AS\d+\s`)
|
removeRegexp := regexp.MustCompile(`AS\d+\s`)
|
||||||
isp := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
|
isp := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
|
||||||
|
@ -179,9 +179,6 @@ func getIP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.ProcessedString += " - " + isp
|
ret.ProcessedString += " - " + isp
|
||||||
} else {
|
|
||||||
// return an empty JSON object to avoid parse errors
|
|
||||||
ret.RawISPInfo = "{}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render.JSON(w, r, ret)
|
render.JSON(w, r, ret)
|
||||||
|
|
Loading…
Reference in a new issue