@@ -0,0 +1,78 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"91porn-server/common/httputil"
|
||||
"91porn-server/common/log"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultCity = "广州"
|
||||
defaultProvince = "广东"
|
||||
ipNewURL = "https://loc.ztgba.com/self/getLocationByIp"
|
||||
)
|
||||
|
||||
// 老接口
|
||||
type LocationResp struct {
|
||||
RegionName string `json:"region_name"`
|
||||
CityName string `json:"city_name"`
|
||||
IP string `json:"ip"`
|
||||
}
|
||||
|
||||
func GetLocationByIP(ip string) (string, string) {
|
||||
|
||||
return "-", "-"
|
||||
|
||||
// var params = map[string]string{"ip": ip}
|
||||
// loc := LocationResp{}
|
||||
// c, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
// defer cancel()
|
||||
// if _, err := httputil.DefaultClientPostJsonWithRespWithCtx(c, &loc, ipNewURL, nil, params); err != nil {
|
||||
// log.Error("GetLocationByIP error", log.Any("ip", ip), log.E(err))
|
||||
// return defaultCity, defaultProvince
|
||||
// }
|
||||
// if (len(loc.RegionName) == 0 && len(loc.CityName) == 0) || loc.RegionName == "局域网" || loc.RegionName == "柬埔寨" {
|
||||
// return defaultCity, defaultProvince
|
||||
// }
|
||||
// if len(loc.CityName) == 0 {
|
||||
// return loc.RegionName, loc.RegionName
|
||||
// }
|
||||
// return loc.CityName, loc.RegionName
|
||||
}
|
||||
|
||||
type CountryResp struct {
|
||||
Country string `json:"country_name"`
|
||||
RegionName string `json:"region_name"`
|
||||
IP string `json:"ip"`
|
||||
}
|
||||
|
||||
// InChina
|
||||
// 香港返回false
|
||||
func InChina(ip string) bool {
|
||||
var params = map[string]string{"ip": ip}
|
||||
resp := CountryResp{}
|
||||
if _, err := httputil.DefaultClientPostJsonWithResp(&resp, ipNewURL, nil, params); err != nil {
|
||||
log.Error("InChina error", log.Any("ip", ip), log.E(err))
|
||||
return true
|
||||
}
|
||||
if resp.RegionName == "香港" {
|
||||
return false
|
||||
}
|
||||
return resp.Country == "中国"
|
||||
}
|
||||
|
||||
func GetLocalIP() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil && ipnet.IP.IsGlobalUnicast() {
|
||||
return ipnet.IP.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user