tracemap支持IP-API.COM做为API,修正第三方API部分地区显示问题

This commit is contained in:
tsosunchia
2023-09-07 16:35:51 +08:00
parent 46ce56f3a7
commit b967ee411d
4 changed files with 44 additions and 14 deletions

View File

@@ -306,7 +306,8 @@ func Excute() {
fmt.Println(err)
return
}
if !*disableMaptrace && (strings.ToUpper(*dataOrigin) == "LEOMOEAPI" || strings.ToUpper(*dataOrigin) == "IPINFO") {
if !*disableMaptrace &&
(util.StringInSlice(strings.ToUpper(*dataOrigin), []string{"LEOMOEAPI", "IPINFO", "IPINFO", "IP-API.COM", "IPAPI.COM"})) {
url, err := tracemap.GetMapUrl(string(r))
if err != nil {
log.Fatalln(err)

View File

@@ -2,17 +2,19 @@ package ipgeo
import (
"errors"
"github.com/xgadget-lab/nexttrace/util"
"io"
"log"
"net/http"
"regexp"
"strconv"
"time"
"github.com/tidwall/gjson"
)
func IPApiCom(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, error) {
url := "http://ip-api.com/json/" + ip + "?fields=status,message,country,regionName,city,isp,as"
url := "http://ip-api.com/json/" + ip + "?fields=status,message,country,regionName,city,isp,district,as,lat,lon"
client := &http.Client{
// 2 秒超时
Timeout: timeout,
@@ -32,12 +34,27 @@ func IPApiCom(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, e
}
re := regexp.MustCompile("[0-9]+")
var country = res.Get("country").String()
var prov = res.Get("region").String()
var city = res.Get("city").String()
var district = res.Get("district").String()
if util.StringInSlice(country, []string{"Hong Kong", "Taiwan", "Macao"}) {
district = prov + " " + city + " " + district
city = country
prov = ""
country = "China"
}
lat, _ := strconv.ParseFloat(res.Get("lat").String(), 32)
lng, _ := strconv.ParseFloat(res.Get("lon").String(), 32)
return &IPGeoData{
Asnumber: re.FindString(res.Get("as").String()),
Country: res.Get("country").String(),
City: res.Get("city").String(),
Prov: res.Get("regionName").String(),
Country: country,
City: city,
Prov: prov,
District: district,
Owner: res.Get("isp").String(),
Lat: lat,
Lng: lng,
}, nil
}

View File

@@ -1,6 +1,7 @@
package ipgeo
import (
"github.com/xgadget-lab/nexttrace/util"
"io"
"net/http"
"strconv"
@@ -11,7 +12,7 @@ import (
)
func IPInfo(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, error) {
url := "https://ipinfo.io/" + ip + "?token=" + token.ipinfo
url := "http://ipinfo.io/" + ip + "?token=" + token.ipinfo
client := &http.Client{
// 2 秒超时
Timeout: timeout,
@@ -29,11 +30,6 @@ func IPInfo(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, err
res := gjson.ParseBytes(body)
var country string
country = res.Get("country").String()
if res.Get("country").String() == "HK" || res.Get("country").String() == "TW" {
country = "CN"
}
// ISO-3166 转换
var countryMap = map[string]string{
"AF": "Afghanistan",
@@ -286,13 +282,19 @@ func IPInfo(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, err
"ZM": "Zambia",
"ZW": "Zimbabwe",
}
country = countryMap[country]
var country = res.Get("country").String()
var prov = res.Get("region").String()
var city = res.Get("city").String()
var district = ""
if util.StringInSlice(country, []string{"TW", "MO", "HK"}) {
district = prov + " " + city
city = countryMap[country]
prov = ""
country = "CN"
}
country = countryMap[country]
var anycast = false
//"anycast": true,
if res.Get("anycast").String() == "true" {
country = "ANYCAST"
prov = "ANYCAST"
@@ -329,6 +331,7 @@ func IPInfo(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, err
Country: country,
City: city,
Prov: prov,
District: district,
Owner: owner,
Lat: lat,
Lng: lng,

View File

@@ -210,3 +210,12 @@ func GetPowProvider() string {
}
return ""
}
func StringInSlice(val string, list []string) bool {
for _, v := range list {
if v == val {
return true
}
}
return false
}