mirror of
https://github.com/xiaoqidun/qqwry.git
synced 2023-06-27 14:38:22 +08:00
fix(更改区域): 区域更改为运营商
This commit is contained in:
parent
ccbae8531d
commit
f37264f5ab
@ -5,8 +5,7 @@ Golang QQWry,高性能纯真IP查询库。
|
||||
# 使用须知
|
||||
|
||||
1. 仅支持ipv4查询。
|
||||
2. city可能是城市,也可能是国家。
|
||||
3. area可能是区域,也可能是运营商。
|
||||
2. city可能是国家。
|
||||
|
||||
# 使用说明
|
||||
|
||||
@ -24,8 +23,8 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
// 从内存或缓存查询IP
|
||||
city, area, err := qqwry.QueryIP("1.1.1.1")
|
||||
log.Printf("城市:%s,区域:%s,错误:%v", city, area, err)
|
||||
city, isp, err := qqwry.QueryIP("1.1.1.1")
|
||||
log.Printf("城市:%s,运营商:%s,错误:%v", city, isp, err)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -16,10 +16,10 @@ func main() {
|
||||
return
|
||||
}
|
||||
queryIp := os.Args[1]
|
||||
city, area, err := qqwry.QueryIP(queryIp)
|
||||
city, isp, err := qqwry.QueryIP(queryIp)
|
||||
if err != nil {
|
||||
fmt.Printf("错误:%v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("城市:%s,区域:%s\n", city, area)
|
||||
fmt.Printf("城市:%s,运营商:%s\n", city, isp)
|
||||
}
|
||||
|
50
qqwry.go
50
qqwry.go
@ -26,7 +26,7 @@ const (
|
||||
|
||||
type cache struct {
|
||||
City string
|
||||
Area string
|
||||
Isp string
|
||||
}
|
||||
|
||||
func byte3ToUInt32(data []byte) uint32 {
|
||||
@ -44,10 +44,10 @@ func gb18030Decode(src []byte) string {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
city = v.(cache).City
|
||||
area = v.(cache).Area
|
||||
isp = v.(cache).Isp
|
||||
return
|
||||
}
|
||||
ip := net.ParseIP(queryIp).To4()
|
||||
@ -88,7 +88,7 @@ func QueryIP(queryIp string) (city string, area string, err error) {
|
||||
}
|
||||
posM := offset + 4
|
||||
mode := data[posM]
|
||||
var areaPos uint32
|
||||
var ispPos uint32
|
||||
switch mode {
|
||||
case redirectMode1:
|
||||
posC := byte3ToUInt32(data[posM+1 : posM+4])
|
||||
@ -107,7 +107,7 @@ func QueryIP(queryIp string) (city string, area string, err error) {
|
||||
if mode != redirectMode2 {
|
||||
posC += uint32(len(city) + 1)
|
||||
}
|
||||
areaPos = posC
|
||||
ispPos = posC
|
||||
case redirectMode2:
|
||||
posCA := byte3ToUInt32(data[posM+1 : posM+4])
|
||||
for i := posCA; i < dataLen; i++ {
|
||||
@ -116,7 +116,7 @@ func QueryIP(queryIp string) (city string, area string, err error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
areaPos = offset + 8
|
||||
ispPos = offset + 8
|
||||
default:
|
||||
posCA := offset + 4
|
||||
for i := posCA; i < dataLen; i++ {
|
||||
@ -125,31 +125,31 @@ func QueryIP(queryIp string) (city string, area string, err error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
areaPos = 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
|
||||
}
|
||||
}
|
||||
ispPos = offset + uint32(5+len(city))
|
||||
}
|
||||
if city != "" {
|
||||
city = strings.TrimSpace(gb18030Decode([]byte(city)))
|
||||
}
|
||||
if area != "" {
|
||||
if strings.Contains(area, "CZ88.NET") {
|
||||
area = ""
|
||||
} else {
|
||||
area = strings.TrimSpace(gb18030Decode([]byte(area)))
|
||||
ispMode := data[ispPos]
|
||||
if ispMode == redirectMode1 || ispMode == redirectMode2 {
|
||||
ispPos = byte3ToUInt32(data[ispPos+1 : ispPos+4])
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -12,9 +12,9 @@ func init() {
|
||||
|
||||
func TestQueryIP(t *testing.T) {
|
||||
queryIp := "1.1.1.1"
|
||||
city, area, err := QueryIP(queryIp)
|
||||
city, isp, err := QueryIP(queryIp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("城市:%s,区域:%s", city, area)
|
||||
t.Logf("城市:%s,运营商:%s", city, isp)
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ type resp struct {
|
||||
IP string `json:"ip"`
|
||||
Err string `json:"err"`
|
||||
City string `json:"city"`
|
||||
Area string `json:"area"`
|
||||
Isp string `json:"isp"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -35,12 +35,12 @@ func IpAPI(writer http.ResponseWriter, request *http.Request) {
|
||||
ip, _, _ = net.SplitHostPort(request.RemoteAddr)
|
||||
}
|
||||
rw := &resp{IP: ip}
|
||||
city, area, err := qqwry.QueryIP(ip)
|
||||
city, isp, err := qqwry.QueryIP(ip)
|
||||
if err != nil {
|
||||
rw.Err = err.Error()
|
||||
} else {
|
||||
rw.City = city
|
||||
rw.Area = area
|
||||
rw.Isp = isp
|
||||
}
|
||||
b, _ := json.MarshalIndent(rw, "", " ")
|
||||
_, _ = writer.Write(b)
|
||||
|
Loading…
x
Reference in New Issue
Block a user