From a95d612bbe8f3352dbb6abe6f6c41ead081edc29 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Thu, 21 Jan 2021 18:06:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=BF=AE=E6=AD=A3=E5=91=BD=E5=90=8D):=20co?= =?UTF-8?q?untry=E8=B0=83=E6=95=B4=E4=B8=BAcity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qqwry.go | 22 +++++++++++----------- qqwry_test.go | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/qqwry.go b/qqwry.go index f1ac920..4e37989 100644 --- a/qqwry.go +++ b/qqwry.go @@ -24,8 +24,8 @@ const ( ) type cache struct { - Country string - Area string + City string + Area string } func byte3ToUInt32(data []byte) uint32 { @@ -43,9 +43,9 @@ func gb18030Decode(src []byte) string { } // QueryIP 从内存或缓存查询IP -func QueryIP(queryIp string) (country string, area string, err error) { +func QueryIP(queryIp string) (city string, area string, err error) { if v, ok := ipCache.Load(queryIp); ok { - country = v.(cache).Country + city = v.(cache).City area = v.(cache).Area return } @@ -99,19 +99,19 @@ func QueryIP(queryIp string) (country string, area string, err error) { } for i := posCA; i < dataLen; i++ { if data[i] == 0 { - country = string(data[posCA:i]) + city = string(data[posCA:i]) break } } if mode != redirectMode2 { - posC += uint32(len(country) + 1) + posC += uint32(len(city) + 1) } areaPos = posC case redirectMode2: posCA := byte3ToUInt32(data[posM+1 : posM+4]) for i := posCA; i < dataLen; i++ { if data[i] == 0 { - country = string(data[posCA:i]) + city = string(data[posCA:i]) break } } @@ -120,11 +120,11 @@ func QueryIP(queryIp string) (country string, area string, err error) { posCA := offset + 4 for i := posCA; i < dataLen; i++ { if data[i] == 0 { - country = string(data[posCA:i]) + city = string(data[posCA:i]) break } } - areaPos = offset + uint32(5+len(country)) + areaPos = offset + uint32(5+len(city)) } areaMode := data[areaPos] if areaMode == redirectMode1 || areaMode == redirectMode2 { @@ -138,9 +138,9 @@ func QueryIP(queryIp string) (country string, area string, err error) { } } } - country = gb18030Decode([]byte(country)) + city = gb18030Decode([]byte(city)) area = gb18030Decode([]byte(area)) - ipCache.Store(queryIp, cache{Country: country, Area: area}) + ipCache.Store(queryIp, cache{City: city, Area: area}) return } diff --git a/qqwry_test.go b/qqwry_test.go index b2c7b87..53e087b 100644 --- a/qqwry_test.go +++ b/qqwry_test.go @@ -12,9 +12,9 @@ func init() { func TestQueryIP(t *testing.T) { queryIp := "1.1.1.1" - country, area, err := QueryIP(queryIp) + city, area, err := QueryIP(queryIp) if err != nil { t.Fatal(err) } - t.Log(country, area) + t.Logf("城市:%s,区域:%s", city, area) }