feat: geo ip.

This commit is contained in:
tx7do
2023-10-19 15:05:49 +08:00
parent 92be8b878a
commit 01bdf5cc06
21 changed files with 343 additions and 38 deletions

28
geoip/qqwry/utils.go Normal file
View File

@@ -0,0 +1,28 @@
package qqwry
import (
"bytes"
"io"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
func gb18030Decode(src []byte) string {
in := bytes.NewReader(src)
out := transform.NewReader(in, simplifiedchinese.GB18030.NewDecoder())
d, _ := io.ReadAll(out)
return string(d)
}
// getMiddleOffset 取得begin和end之间的偏移量用于二分搜索
func getMiddleOffset(start uint32, end uint32) uint32 {
return start + (((end-start)/ipRecordLength)>>1)*ipRecordLength
}
func byte3ToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000
return i
}