mirror of
https://github.com/xiaoqidun/qqwry.git
synced 2023-06-27 14:38:22 +08:00
Compare commits
No commits in common. "514f7fcd373ea698cfe0ffd9daddf7b15bb7e0d2" and "3caa55c1c70e34a13e37595d1b23295b09791415" have entirely different histories.
514f7fcd37
...
3caa55c1c7
12
README.md
12
README.md
@ -1,12 +1,6 @@
|
|||||||
# QQWry [![Go Reference](https://pkg.go.dev/badge/github.com/xiaoqidun/qqwry.svg)](https://pkg.go.dev/github.com/xiaoqidun/qqwry)
|
# QQWry [![Go Reference](https://pkg.go.dev/badge/github.com/xiaoqidun/qqwry.svg)](https://pkg.go.dev/github.com/xiaoqidun/qqwry)
|
||||||
|
|
||||||
Golang QQWry,高性能纯真IP查询库。
|
Golang QQWry,内存操作,线程安全,支持缓存的纯真IP查询库
|
||||||
|
|
||||||
# 使用须知
|
|
||||||
|
|
||||||
1. 仅支持ipv4查询。
|
|
||||||
2. city可能是城市,也可能是国家。
|
|
||||||
3. area可能是区域,也可能是运营商。
|
|
||||||
|
|
||||||
# 使用说明
|
# 使用说明
|
||||||
|
|
||||||
@ -24,8 +18,8 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
// 从内存或缓存查询IP
|
// 从内存或缓存查询IP
|
||||||
city, area, err := qqwry.QueryIP("1.1.1.1")
|
country, area, err := qqwry.QueryIP("1.1.1.1")
|
||||||
log.Printf("城市:%s,区域:%s,错误:%v", city, area, err)
|
log.Printf("国家:%s,区域:%s,错误:%v", country, area, err)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
20
qqwry.go
20
qqwry.go
@ -24,7 +24,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type cache struct {
|
type cache struct {
|
||||||
City string
|
Country string
|
||||||
Area string
|
Area string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,9 +43,9 @@ func gb18030Decode(src []byte) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// QueryIP 从内存或缓存查询IP
|
// QueryIP 从内存或缓存查询IP
|
||||||
func QueryIP(queryIp string) (city string, area string, err error) {
|
func QueryIP(queryIp string) (country string, area string, err error) {
|
||||||
if v, ok := ipCache.Load(queryIp); ok {
|
if v, ok := ipCache.Load(queryIp); ok {
|
||||||
city = v.(cache).City
|
country = v.(cache).Country
|
||||||
area = v.(cache).Area
|
area = v.(cache).Area
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -99,19 +99,19 @@ func QueryIP(queryIp string) (city string, area string, err error) {
|
|||||||
}
|
}
|
||||||
for i := posCA; i < dataLen; i++ {
|
for i := posCA; i < dataLen; i++ {
|
||||||
if data[i] == 0 {
|
if data[i] == 0 {
|
||||||
city = string(data[posCA:i])
|
country = string(data[posCA:i])
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if mode != redirectMode2 {
|
if mode != redirectMode2 {
|
||||||
posC += uint32(len(city) + 1)
|
posC += uint32(len(country) + 1)
|
||||||
}
|
}
|
||||||
areaPos = posC
|
areaPos = 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++ {
|
||||||
if data[i] == 0 {
|
if data[i] == 0 {
|
||||||
city = string(data[posCA:i])
|
country = string(data[posCA:i])
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,11 +120,11 @@ func QueryIP(queryIp string) (city string, area string, err error) {
|
|||||||
posCA := offset + 4
|
posCA := offset + 4
|
||||||
for i := posCA; i < dataLen; i++ {
|
for i := posCA; i < dataLen; i++ {
|
||||||
if data[i] == 0 {
|
if data[i] == 0 {
|
||||||
city = string(data[posCA:i])
|
country = string(data[posCA:i])
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
areaPos = offset + uint32(5+len(city))
|
areaPos = offset + uint32(5+len(country))
|
||||||
}
|
}
|
||||||
areaMode := data[areaPos]
|
areaMode := data[areaPos]
|
||||||
if areaMode == redirectMode1 || areaMode == redirectMode2 {
|
if areaMode == redirectMode1 || areaMode == redirectMode2 {
|
||||||
@ -138,9 +138,9 @@ func QueryIP(queryIp string) (city string, area string, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
city = gb18030Decode([]byte(city))
|
country = gb18030Decode([]byte(country))
|
||||||
area = gb18030Decode([]byte(area))
|
area = gb18030Decode([]byte(area))
|
||||||
ipCache.Store(queryIp, cache{City: city, Area: area})
|
ipCache.Store(queryIp, cache{Country: country, Area: area})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
country, area, err := QueryIP(queryIp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Logf("城市:%s,区域:%s", city, area)
|
t.Log(country, area)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user