fix(修正命名): country调整为city

This commit is contained in:
xiaoqidun 2021-01-21 18:06:18 +08:00
parent 3caa55c1c7
commit a95d612bbe
2 changed files with 13 additions and 13 deletions

View File

@ -24,8 +24,8 @@ const (
) )
type cache struct { type cache struct {
Country string City string
Area string Area string
} }
func byte3ToUInt32(data []byte) uint32 { func byte3ToUInt32(data []byte) uint32 {
@ -43,9 +43,9 @@ func gb18030Decode(src []byte) string {
} }
// QueryIP 从内存或缓存查询IP // 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 { if v, ok := ipCache.Load(queryIp); ok {
country = v.(cache).Country city = v.(cache).City
area = v.(cache).Area area = v.(cache).Area
return return
} }
@ -99,19 +99,19 @@ func QueryIP(queryIp string) (country 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 {
country = string(data[posCA:i]) city = string(data[posCA:i])
break break
} }
} }
if mode != redirectMode2 { if mode != redirectMode2 {
posC += uint32(len(country) + 1) posC += uint32(len(city) + 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 {
country = string(data[posCA:i]) city = string(data[posCA:i])
break break
} }
} }
@ -120,11 +120,11 @@ func QueryIP(queryIp string) (country 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 {
country = string(data[posCA:i]) city = string(data[posCA:i])
break break
} }
} }
areaPos = offset + uint32(5+len(country)) areaPos = offset + uint32(5+len(city))
} }
areaMode := data[areaPos] areaMode := data[areaPos]
if areaMode == redirectMode1 || areaMode == redirectMode2 { 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)) area = gb18030Decode([]byte(area))
ipCache.Store(queryIp, cache{Country: country, Area: area}) ipCache.Store(queryIp, cache{City: city, Area: area})
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"
country, area, err := QueryIP(queryIp) city, area, err := QueryIP(queryIp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(country, area) t.Logf("城市:%s区域%s", city, area)
} }