fix(更改区域): 区域更改为运营商

This commit is contained in:
xiaoqidun 2021-07-08 11:39:43 +08:00
parent ccbae8531d
commit f37264f5ab
5 changed files with 35 additions and 36 deletions

View File

@ -5,8 +5,7 @@ Golang QQWry高性能纯真IP查询库。
# 使用须知 # 使用须知
1. 仅支持ipv4查询。 1. 仅支持ipv4查询。
2. city可能是城市也可能是国家。 2. city可能是国家。
3. area可能是区域也可能是运营商。
# 使用说明 # 使用说明
@ -24,8 +23,8 @@ func main() {
panic(err) panic(err)
} }
// 从内存或缓存查询IP // 从内存或缓存查询IP
city, area, err := qqwry.QueryIP("1.1.1.1") city, isp, err := qqwry.QueryIP("1.1.1.1")
log.Printf("城市:%s区域:%s错误%v", city, area, err) log.Printf("城市:%s运营商:%s错误%v", city, isp, err)
} }
``` ```

View File

@ -16,10 +16,10 @@ func main() {
return return
} }
queryIp := os.Args[1] queryIp := os.Args[1]
city, area, err := qqwry.QueryIP(queryIp) city, isp, err := qqwry.QueryIP(queryIp)
if err != nil { if err != nil {
fmt.Printf("错误:%v\n", err) fmt.Printf("错误:%v\n", err)
return return
} }
fmt.Printf("城市:%s区域:%s\n", city, area) fmt.Printf("城市:%s运营商:%s\n", city, isp)
} }

View File

@ -26,7 +26,7 @@ const (
type cache struct { type cache struct {
City string City string
Area string Isp string
} }
func byte3ToUInt32(data []byte) uint32 { func byte3ToUInt32(data []byte) uint32 {
@ -44,10 +44,10 @@ func gb18030Decode(src []byte) string {
} }
// QueryIP 从内存或缓存查询IP // QueryIP 从内存或缓存查询IP
func QueryIP(queryIp string) (city string, area string, err error) { func QueryIP(queryIp string) (city string, isp string, err error) {
if v, ok := ipCache.Load(queryIp); ok { if v, ok := ipCache.Load(queryIp); ok {
city = v.(cache).City city = v.(cache).City
area = v.(cache).Area isp = v.(cache).Isp
return return
} }
ip := net.ParseIP(queryIp).To4() ip := net.ParseIP(queryIp).To4()
@ -88,7 +88,7 @@ func QueryIP(queryIp string) (city string, area string, err error) {
} }
posM := offset + 4 posM := offset + 4
mode := data[posM] mode := data[posM]
var areaPos uint32 var ispPos uint32
switch mode { switch mode {
case redirectMode1: case redirectMode1:
posC := byte3ToUInt32(data[posM+1 : posM+4]) posC := byte3ToUInt32(data[posM+1 : posM+4])
@ -107,7 +107,7 @@ func QueryIP(queryIp string) (city string, area string, err error) {
if mode != redirectMode2 { if mode != redirectMode2 {
posC += uint32(len(city) + 1) posC += uint32(len(city) + 1)
} }
areaPos = posC ispPos = posC
case redirectMode2: case redirectMode2:
posCA := byte3ToUInt32(data[posM+1 : posM+4]) posCA := byte3ToUInt32(data[posM+1 : posM+4])
for i := posCA; i < dataLen; i++ { for i := posCA; i < dataLen; i++ {
@ -116,7 +116,7 @@ func QueryIP(queryIp string) (city string, area string, err error) {
break break
} }
} }
areaPos = offset + 8 ispPos = offset + 8
default: default:
posCA := offset + 4 posCA := offset + 4
for i := posCA; i < dataLen; i++ { for i := posCA; i < dataLen; i++ {
@ -125,31 +125,31 @@ func QueryIP(queryIp string) (city string, area string, err error) {
break break
} }
} }
areaPos = offset + uint32(5+len(city)) ispPos = offset + uint32(5+len(city))
}
areaMode := data[areaPos]
if areaMode == redirectMode1 || areaMode == redirectMode2 {
areaPos = byte3ToUInt32(data[areaPos+1 : areaPos+4])
}
if areaPos > 0 {
for i := areaPos; i < dataLen; i++ {
if data[i] == 0 {
area = string(data[areaPos:i])
break
}
}
} }
if city != "" { if city != "" {
city = strings.TrimSpace(gb18030Decode([]byte(city))) city = strings.TrimSpace(gb18030Decode([]byte(city)))
} }
if area != "" { ispMode := data[ispPos]
if strings.Contains(area, "CZ88.NET") { if ispMode == redirectMode1 || ispMode == redirectMode2 {
area = "" ispPos = byte3ToUInt32(data[ispPos+1 : ispPos+4])
} else { }
area = strings.TrimSpace(gb18030Decode([]byte(area))) if ispPos > 0 {
for i := ispPos; i < dataLen; i++ {
if data[i] == 0 {
isp = string(data[ispPos:i])
if isp != "" {
if strings.Contains(isp, "CZ88.NET") {
isp = ""
} else {
isp = strings.TrimSpace(gb18030Decode([]byte(isp)))
}
}
break
}
} }
} }
ipCache.Store(queryIp, cache{City: city, Area: area}) ipCache.Store(queryIp, cache{City: city, Isp: isp})
return return
} }

View File

@ -12,9 +12,9 @@ func init() {
func TestQueryIP(t *testing.T) { func TestQueryIP(t *testing.T) {
queryIp := "1.1.1.1" queryIp := "1.1.1.1"
city, area, err := QueryIP(queryIp) city, isp, err := QueryIP(queryIp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Logf("城市:%s区域:%s", city, area) t.Logf("城市:%s运营商:%s", city, isp)
} }

View File

@ -13,7 +13,7 @@ type resp struct {
IP string `json:"ip"` IP string `json:"ip"`
Err string `json:"err"` Err string `json:"err"`
City string `json:"city"` City string `json:"city"`
Area string `json:"area"` Isp string `json:"isp"`
} }
func init() { func init() {
@ -35,12 +35,12 @@ func IpAPI(writer http.ResponseWriter, request *http.Request) {
ip, _, _ = net.SplitHostPort(request.RemoteAddr) ip, _, _ = net.SplitHostPort(request.RemoteAddr)
} }
rw := &resp{IP: ip} rw := &resp{IP: ip}
city, area, err := qqwry.QueryIP(ip) city, isp, err := qqwry.QueryIP(ip)
if err != nil { if err != nil {
rw.Err = err.Error() rw.Err = err.Error()
} else { } else {
rw.City = city rw.City = city
rw.Area = area rw.Isp = isp
} }
b, _ := json.MarshalIndent(rw, "", " ") b, _ := json.MarshalIndent(rw, "", " ")
_, _ = writer.Write(b) _, _ = writer.Write(b)