|
|
|
@ -7,10 +7,10 @@ import (
@@ -7,10 +7,10 @@ import (
|
|
|
|
|
"net/http" |
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
"github.com/OhYee/rainbow/log" |
|
|
|
|
"github.com/OhYee/blotter/output" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type IPInfo struct { |
|
|
|
|
type ipAPIResponse struct { |
|
|
|
|
Status string |
|
|
|
|
Country string |
|
|
|
|
CountryCode string |
|
|
|
@ -33,23 +33,25 @@ type IPInfo struct {
@@ -33,23 +33,25 @@ type IPInfo struct {
|
|
|
|
|
Hosting bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func GetPositionFromIP(ip string) string { |
|
|
|
|
func ipAPI(ip string) string { |
|
|
|
|
apiName := "ip-api" |
|
|
|
|
|
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://ip-api.com/json/%s?fields=66842623&lang=zh-CN", ip)) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error.Printf("failed to get response from ip-api, due to %s", err) |
|
|
|
|
output.ErrOutput.Printf("failed to get response from %s for %s due to %s", apiName, ip, err) |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error.Printf("failed to read response from ip-api, due to %s", err) |
|
|
|
|
output.ErrOutput.Printf("failed to read response from %s for %s due to %s", apiName, ip, err) |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ipInfo := new(IPInfo) |
|
|
|
|
ipInfo := new(ipAPIResponse) |
|
|
|
|
err = json.Unmarshal(b, ipInfo) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error.Printf("failed to read json from ip-api response body, due to %s", err) |
|
|
|
|
output.ErrOutput.Printf("failed to read json from %s for %s, due to %s", apiName, ip, err) |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -72,3 +74,73 @@ func GetPositionFromIP(ip string) string {
@@ -72,3 +74,73 @@ func GetPositionFromIP(ip string) string {
|
|
|
|
|
|
|
|
|
|
return strings.Join(positionArr, ",") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type ohyeeAPIResponse struct { |
|
|
|
|
Success bool |
|
|
|
|
IP string |
|
|
|
|
Continent string |
|
|
|
|
Country string |
|
|
|
|
Region string |
|
|
|
|
City string |
|
|
|
|
Mobile bool |
|
|
|
|
Proxy bool |
|
|
|
|
Lat float64 |
|
|
|
|
Lon float64 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func ohyeeAPI(ip string) string { |
|
|
|
|
const apiName = "ohyee api" |
|
|
|
|
|
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://fc.ohyee.cc/ip?ip=%s", ip)) |
|
|
|
|
if err != nil { |
|
|
|
|
output.ErrOutput.Printf("failed to get response from %s for %s, due to %s", apiName, ip, err) |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body) |
|
|
|
|
if err != nil { |
|
|
|
|
output.ErrOutput.Printf("failed to read response from %s for %s, due to %s", apiName, ip, err) |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ipInfo := new(ohyeeAPIResponse) |
|
|
|
|
err = json.Unmarshal(b, ipInfo) |
|
|
|
|
if err != nil { |
|
|
|
|
output.ErrOutput.Printf("failed to read json from %s for %s, due to %s", apiName, ip, err) |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
positionArr := make([]string, 0, 3) |
|
|
|
|
if ipInfo.Country != "" { |
|
|
|
|
positionArr = append(positionArr, ipInfo.Country) |
|
|
|
|
} |
|
|
|
|
if ipInfo.Region != "" { |
|
|
|
|
positionArr = append(positionArr, ipInfo.Region) |
|
|
|
|
} |
|
|
|
|
if ipInfo.City != "" { |
|
|
|
|
positionArr = append(positionArr, ipInfo.City) |
|
|
|
|
} |
|
|
|
|
if ipInfo.Mobile { |
|
|
|
|
positionArr = append(positionArr, "移动端") |
|
|
|
|
} |
|
|
|
|
if ipInfo.Proxy { |
|
|
|
|
positionArr = append(positionArr, "代理") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return strings.Join(positionArr, ",") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var apis = []func(string) string{ |
|
|
|
|
ipAPI, |
|
|
|
|
ohyeeAPI, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func GetPositionFromIP(ip string) string { |
|
|
|
|
for _, f := range apis { |
|
|
|
|
if pos := f(ip); pos != "" { |
|
|
|
|
return pos |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|