新增代理服务与边缘节点注册功能
This commit is contained in:
40
client/geo/ipapi.go
Normal file
40
client/geo/ipapi.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package geo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/pkg/errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Ipapi() (prov, city, isp string, err error) {
|
||||
const endpoint = "http://ip-api.com/json/?fields=regionName,city,as&lang=zh-CN"
|
||||
|
||||
resp, err := http.Get(endpoint)
|
||||
if err != nil {
|
||||
return "", "", "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", "", "", err
|
||||
}
|
||||
|
||||
var data struct {
|
||||
RegionName string `json:"regionName"`
|
||||
City string `json:"city"`
|
||||
As string `json:"as"`
|
||||
}
|
||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||
if err != nil {
|
||||
return "", "", "", err
|
||||
}
|
||||
|
||||
prov = data.RegionName
|
||||
city = data.City
|
||||
isp = data.As
|
||||
if prov == "" || city == "" || isp == "" {
|
||||
return "", "", "", errors.New("解析数据为空")
|
||||
}
|
||||
|
||||
return prov, city, isp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user