parent
bb3ce86430
commit
8b0cd6dfee
@ -0,0 +1,39 @@
|
||||
package goip
|
||||
|
||||
import (
|
||||
"go.dtapp.net/goip/geoip"
|
||||
"go.dtapp.net/goip/ip2region"
|
||||
"go.dtapp.net/goip/ip2region_v2"
|
||||
"go.dtapp.net/goip/ipv6wry"
|
||||
"go.dtapp.net/goip/qqwry"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
ip2regionV2Client *ip2region_v2.Client
|
||||
ip2regionClient *ip2region.Client
|
||||
qqwryClient *qqwry.Client
|
||||
geoIpClient *geoip.Client
|
||||
ipv6wryClient *ipv6wry.Client
|
||||
}
|
||||
|
||||
// NewIp 实例化
|
||||
func NewIp() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
c.ip2regionV2Client, _ = ip2region_v2.New()
|
||||
|
||||
c.ip2regionClient = ip2region.New()
|
||||
|
||||
c.qqwryClient = qqwry.New()
|
||||
|
||||
c.geoIpClient, _ = geoip.New()
|
||||
|
||||
c.ipv6wryClient = ipv6wry.New()
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
c.geoIpClient.Close()
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package goip
|
||||
|
||||
const Version = "1.0.32"
|
||||
const Version = "1.0.33"
|
||||
|
@ -1,80 +0,0 @@
|
||||
package goip
|
||||
|
||||
import (
|
||||
"go.dtapp.net/goip/geoip"
|
||||
"go.dtapp.net/goip/ip2region"
|
||||
"go.dtapp.net/goip/ip2region_v2"
|
||||
v4 "go.dtapp.net/goip/v4"
|
||||
v6 "go.dtapp.net/goip/v6"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
V4Region ip2region.Ip2Region // IPV4
|
||||
V4db v4.Pointer // IPV4
|
||||
V6db v6.Pointer // IPV6
|
||||
ip2regionV2Client *ip2region_v2.Client
|
||||
geoIpClient *geoip.Client
|
||||
GeoIpLicenseKey string
|
||||
}
|
||||
|
||||
// NewIp 实例化
|
||||
func NewIp() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
v4Num := c.V4db.InitIPV4Data()
|
||||
log.Printf("IPV4 库加载完成 共加载:%d 条 IP 记录\n", v4Num)
|
||||
|
||||
v6Num := c.V6db.InitIPV4Data()
|
||||
log.Printf("IPV6 库加载完成 共加载:%d 条 IP 记录\n", v6Num)
|
||||
|
||||
c.ip2regionV2Client, _ = ip2region_v2.New()
|
||||
|
||||
c.geoIpClient, _ = geoip.New()
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
c.geoIpClient.Close()
|
||||
}
|
||||
|
||||
func (c *Client) Ipv4(ip string) (res v4.Result, resInfo ip2region.IpInfo) {
|
||||
res = c.V4db.Find(ip)
|
||||
resInfo, _ = c.V4Region.MemorySearch(ip)
|
||||
return res, resInfo
|
||||
}
|
||||
|
||||
func (c *Client) Ipv6(ip string) (res v6.Result) {
|
||||
res = c.V6db.Find(ip)
|
||||
return res
|
||||
}
|
||||
|
||||
func (c *Client) isIpv4OrIpv6(ip string) string {
|
||||
if len(ip) < 7 {
|
||||
return ""
|
||||
}
|
||||
arrIpv4 := strings.Split(ip, ".")
|
||||
if len(arrIpv4) == 4 {
|
||||
//. 判断IPv4
|
||||
for _, val := range arrIpv4 {
|
||||
if !c.CheckIpv4(val) {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return ipv4
|
||||
}
|
||||
arrIpv6 := strings.Split(ip, ":")
|
||||
if len(arrIpv6) == 8 {
|
||||
// 判断Ipv6
|
||||
for _, val := range arrIpv6 {
|
||||
if !c.CheckIpv6(val) {
|
||||
return "Neither"
|
||||
}
|
||||
}
|
||||
return ipv6
|
||||
}
|
||||
return ""
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package ip2region
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"go.dtapp.net/gostring"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
IndexBlockLength = 12
|
||||
)
|
||||
|
||||
//go:embed ip2region.db
|
||||
var dbBuff []byte
|
||||
|
||||
type Client struct {
|
||||
// db file handler
|
||||
dbFileHandler *os.File
|
||||
|
||||
//header block info
|
||||
|
||||
headerSip []int64
|
||||
headerPtr []int64
|
||||
headerLen int64
|
||||
|
||||
// super block index info
|
||||
firstIndexPtr int64
|
||||
lastIndexPtr int64
|
||||
totalBlocks int64
|
||||
|
||||
// for memory mode only
|
||||
// the original db binary string
|
||||
|
||||
dbFile string
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// 获取Ip信息
|
||||
func getIpInfo(ipStr string, cityId int64, line []byte) (result QueryResult) {
|
||||
|
||||
lineSlice := strings.Split(string(line), "|")
|
||||
length := len(lineSlice)
|
||||
result.CityId = cityId
|
||||
if length < 5 {
|
||||
for i := 0; i <= 5-length; i++ {
|
||||
lineSlice = append(lineSlice, "")
|
||||
}
|
||||
}
|
||||
|
||||
if lineSlice[0] != "0" {
|
||||
result.Country = gostring.SpaceAndLineBreak(lineSlice[0])
|
||||
}
|
||||
if lineSlice[1] != "0" {
|
||||
result.Region = gostring.SpaceAndLineBreak(lineSlice[1])
|
||||
}
|
||||
if lineSlice[2] != "0" {
|
||||
result.Province = gostring.SpaceAndLineBreak(lineSlice[2])
|
||||
}
|
||||
if lineSlice[3] != "0" {
|
||||
result.City = gostring.SpaceAndLineBreak(lineSlice[3])
|
||||
}
|
||||
if lineSlice[4] != "0" {
|
||||
result.Isp = gostring.SpaceAndLineBreak(lineSlice[4])
|
||||
}
|
||||
|
||||
result.Ip = ipStr
|
||||
return result
|
||||
}
|
||||
|
||||
func getLong(b []byte, offset int64) int64 {
|
||||
|
||||
val := int64(b[offset]) |
|
||||
int64(b[offset+1])<<8 |
|
||||
int64(b[offset+2])<<16 |
|
||||
int64(b[offset+3])<<24
|
||||
|
||||
return val
|
||||
|
||||
}
|
||||
|
||||
func ip2long(IpStr string) (int64, error) {
|
||||
bits := strings.Split(IpStr, ".")
|
||||
if len(bits) != 4 {
|
||||
return 0, errors.New("ip format error")
|
||||
}
|
||||
|
||||
var sum int64
|
||||
for i, n := range bits {
|
||||
bit, _ := strconv.ParseInt(n, 10, 64)
|
||||
sum += bit << uint(24-8*i)
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package ip2region_v2
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed ip2region.xdb
|
||||
var cBuff []byte
|
||||
|
||||
type Client struct {
|
||||
db *Searcher
|
||||
}
|
||||
|
||||
func New() (*Client, error) {
|
||||
|
||||
var err error
|
||||
c := &Client{}
|
||||
|
||||
// 1、从 dbPath 加载整个 xdb 到内存
|
||||
|
||||
// 2、用全局的 cBuff 创建完全基于内存的查询对象。
|
||||
c.db, err = NewWithBuffer(cBuff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, err
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
package ipv6wry
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
header []byte
|
||||
country []byte
|
||||
area []byte
|
||||
v6ip uint64
|
||||
offset uint32
|
||||
start uint32
|
||||
end uint32
|
||||
)
|
||||
|
||||
//go:embed ipv6wry.db
|
||||
var datBuff []byte
|
||||
|
||||
type Client struct {
|
||||
Offset uint32
|
||||
ItemLen uint32
|
||||
IndexLen uint32
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
buf := datBuff[0:8]
|
||||
start := binary.LittleEndian.Uint32(buf[:4])
|
||||
end := binary.LittleEndian.Uint32(buf[4:])
|
||||
|
||||
num := int64((end-start)/7 + 1)
|
||||
log.Printf("ipv6wry.db 共加载:%d 条ip记录\n", num)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// ReadData 从文件中读取数据
|
||||
func (c *Client) readData(length uint32) (rs []byte) {
|
||||
end := c.Offset + length
|
||||
dataNum := uint32(len(datBuff))
|
||||
if c.Offset > dataNum {
|
||||
return nil
|
||||
}
|
||||
|
||||
if end > dataNum {
|
||||
end = dataNum
|
||||
}
|
||||
rs = datBuff[c.Offset:end]
|
||||
c.Offset = end
|
||||
return rs
|
||||
}
|
||||
|
||||
func (c *Client) getAddr() ([]byte, []byte) {
|
||||
mode := c.readData(1)[0]
|
||||
if mode == 0x01 {
|
||||
// [IP][0x01][国家和地区信息的绝对偏移地址]
|
||||
c.Offset = byteToUInt32(c.readData(3))
|
||||
return c.getAddr()
|
||||
}
|
||||
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
|
||||
_offset := c.Offset - 1
|
||||
c1 := c.readArea(_offset)
|
||||
if mode == 0x02 {
|
||||
c.Offset = 4 + _offset
|
||||
} else {
|
||||
c.Offset = _offset + uint32(1+len(c1))
|
||||
}
|
||||
c2 := c.readArea(c.Offset)
|
||||
return c1, c2
|
||||
}
|
||||
|
||||
func (c *Client) readArea(offset uint32) []byte {
|
||||
c.Offset = offset
|
||||
mode := c.readData(1)[0]
|
||||
if mode == 0x01 || mode == 0x02 {
|
||||
return c.readArea(byteToUInt32(c.readData(3)))
|
||||
}
|
||||
c.Offset = offset
|
||||
return c.readString()
|
||||
}
|
||||
|
||||
func (c *Client) readString() []byte {
|
||||
data := make([]byte, 0)
|
||||
for {
|
||||
buf := c.readData(1)
|
||||
if buf[0] == 0 {
|
||||
break
|
||||
}
|
||||
data = append(data, buf[0])
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func (c *Client) searchIndex(ip uint64) uint32 {
|
||||
|
||||
c.ItemLen = 8
|
||||
c.IndexLen = 11
|
||||
|
||||
header = datBuff[8:24]
|
||||
start = binary.LittleEndian.Uint32(header[8:])
|
||||
counts := binary.LittleEndian.Uint32(header[:8])
|
||||
end = start + counts*c.IndexLen
|
||||
|
||||
buf := make([]byte, c.IndexLen)
|
||||
|
||||
for {
|
||||
mid := start + c.IndexLen*(((end-start)/c.IndexLen)>>1)
|
||||
buf = datBuff[mid : mid+c.IndexLen]
|
||||
_ip := binary.LittleEndian.Uint64(buf[:c.ItemLen])
|
||||
|
||||
if end-start == c.IndexLen {
|
||||
if ip >= binary.LittleEndian.Uint64(datBuff[end:end+c.ItemLen]) {
|
||||
buf = datBuff[end : end+c.IndexLen]
|
||||
}
|
||||
return byteToUInt32(buf[c.ItemLen:])
|
||||
}
|
||||
|
||||
if _ip > ip {
|
||||
end = mid
|
||||
} else if _ip < ip {
|
||||
start = mid
|
||||
} else if _ip == ip {
|
||||
return byteToUInt32(buf[c.ItemLen:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func byteToUInt32(data []byte) uint32 {
|
||||
i := uint32(data[0]) & 0xff
|
||||
i |= (uint32(data[1]) << 8) & 0xff00
|
||||
i |= (uint32(data[2]) << 16) & 0xff0000
|
||||
return i
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package v6
|
||||
package ipv6wry
|
||||
|
||||
import (
|
||||
"github.com/saracen/go7z"
|
@ -0,0 +1,82 @@
|
||||
package ipv6wry
|
||||
|
||||
import (
|
||||
"go.dtapp.net/gostring"
|
||||
"math/big"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// QueryResult 返回
|
||||
type QueryResult struct {
|
||||
Ip string `json:"ip,omitempty"` // ip
|
||||
Country string `json:"country,omitempty"` // 国家
|
||||
Province string `json:"province,omitempty"` // 省份
|
||||
City string `json:"city,omitempty"` // 城市
|
||||
Area string `json:"area,omitempty"` // 区域
|
||||
Isp string `json:"isp,omitempty"` // 运营商
|
||||
}
|
||||
|
||||
// Query ip地址查询对应归属地信息
|
||||
func (c *Client) Query(ipAddress net.IP) (result QueryResult) {
|
||||
|
||||
result.Ip = ipAddress.String()
|
||||
|
||||
c.Offset = 0
|
||||
|
||||
tp := big.NewInt(0)
|
||||
op := big.NewInt(0)
|
||||
tp.SetBytes(ipAddress.To16())
|
||||
op.SetString("18446744073709551616", 10)
|
||||
op.Div(tp, op)
|
||||
tp.SetString("FFFFFFFFFFFFFFFF", 16)
|
||||
op.And(op, tp)
|
||||
|
||||
v6ip = op.Uint64()
|
||||
offset = c.searchIndex(v6ip)
|
||||
c.Offset = offset
|
||||
|
||||
country, area = c.getAddr()
|
||||
|
||||
// 解析地区数据
|
||||
info := strings.Split(string(country), "\t")
|
||||
if len(info) > 0 {
|
||||
i := 1
|
||||
for {
|
||||
if i > len(info) {
|
||||
break
|
||||
}
|
||||
switch i {
|
||||
case 1:
|
||||
result.Country = info[i-1]
|
||||
result.Country = gostring.SpaceAndLineBreak(result.Country)
|
||||
case 2:
|
||||
result.Province = info[i-1]
|
||||
result.Province = gostring.SpaceAndLineBreak(result.Province)
|
||||
case 3:
|
||||
result.City = info[i-1]
|
||||
result.City = gostring.SpaceAndLineBreak(result.City)
|
||||
case 4:
|
||||
result.Area = info[i-1]
|
||||
result.Area = gostring.SpaceAndLineBreak(result.Area)
|
||||
}
|
||||
i++ // 自增
|
||||
}
|
||||
} else {
|
||||
result.Country = string(country)
|
||||
result.Country = gostring.SpaceAndLineBreak(result.Country)
|
||||
}
|
||||
// 运营商
|
||||
result.Isp = string(area)
|
||||
|
||||
// Delete ZX (防止不相关的信息产生干扰)
|
||||
if result.Isp == "ZX" || result.Isp == "" {
|
||||
result.Isp = ""
|
||||
} else {
|
||||
result.Isp = " " + result.Isp
|
||||
}
|
||||
|
||||
result.Isp = gostring.SpaceAndLineBreak(result.Isp)
|
||||
|
||||
return result
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package goip
|
||||
|
||||
import "strings"
|
||||
|
||||
var (
|
||||
ipv4 = "IPV4"
|
||||
ipv6 = "IPV6"
|
||||
)
|
||||
|
||||
func (c *Client) isIpv4OrIpv6(ip string) string {
|
||||
if len(ip) < 7 {
|
||||
return ""
|
||||
}
|
||||
arrIpv4 := strings.Split(ip, ".")
|
||||
if len(arrIpv4) == 4 {
|
||||
//. 判断IPv4
|
||||
for _, val := range arrIpv4 {
|
||||
if !c.CheckIpv4(val) {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return ipv4
|
||||
}
|
||||
arrIpv6 := strings.Split(ip, ":")
|
||||
if len(arrIpv6) == 8 {
|
||||
// 判断Ipv6
|
||||
for _, val := range arrIpv6 {
|
||||
if !c.CheckIpv6(val) {
|
||||
return "Neither"
|
||||
}
|
||||
}
|
||||
return ipv6
|
||||
}
|
||||
return ""
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
package qqwry
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
header []byte
|
||||
country []byte
|
||||
area []byte
|
||||
offset uint32
|
||||
start uint32
|
||||
end uint32
|
||||
)
|
||||
|
||||
//go:embed qqwry.dat
|
||||
var datBuff []byte
|
||||
|
||||
type Client struct {
|
||||
Offset uint32
|
||||
ItemLen uint32
|
||||
IndexLen uint32
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
buf := datBuff[0:8]
|
||||
start := binary.LittleEndian.Uint32(buf[:4])
|
||||
end := binary.LittleEndian.Uint32(buf[4:])
|
||||
|
||||
num := int64((end-start)/7 + 1)
|
||||
log.Printf("qqwry.dat 共加载:%d 条ip记录\n", num)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// ReadData 从文件中读取数据
|
||||
func (c *Client) readData(length uint32) (rs []byte) {
|
||||
end := c.Offset + length
|
||||
dataNum := uint32(len(datBuff))
|
||||
if c.Offset > dataNum {
|
||||
return nil
|
||||
}
|
||||
|
||||
if end > dataNum {
|
||||
end = dataNum
|
||||
}
|
||||
rs = datBuff[c.Offset:end]
|
||||
c.Offset = end
|
||||
return rs
|
||||
}
|
||||
|
||||
// 获取地址信息
|
||||
func (c *Client) getAddr() ([]byte, []byte) {
|
||||
mode := c.readData(1)[0]
|
||||
if mode == 0x01 {
|
||||
// [IP][0x01][国家和地区信息的绝对偏移地址]
|
||||
c.Offset = byteToUInt32(c.readData(3))
|
||||
return c.getAddr()
|
||||
}
|
||||
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
|
||||
_offset := c.Offset - 1
|
||||
c1 := c.readArea(_offset)
|
||||
if mode == 0x02 {
|
||||
c.Offset = 4 + _offset
|
||||
} else {
|
||||
c.Offset = _offset + uint32(1+len(c1))
|
||||
}
|
||||
c2 := c.readArea(c.Offset)
|
||||
return c1, c2
|
||||
}
|
||||
|
||||
// 读取区
|
||||
func (c *Client) readArea(offset uint32) []byte {
|
||||
c.Offset = offset
|
||||
mode := c.readData(1)[0]
|
||||
if mode == 0x01 || mode == 0x02 {
|
||||
return c.readArea(byteToUInt32(c.readData(3)))
|
||||
}
|
||||
c.Offset = offset
|
||||
return c.readString()
|
||||
}
|
||||
|
||||
// 读取字符串
|
||||
func (c *Client) readString() []byte {
|
||||
data := make([]byte, 0)
|
||||
for {
|
||||
buf := c.readData(1)
|
||||
if buf[0] == 0 {
|
||||
break
|
||||
}
|
||||
data = append(data, buf[0])
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// 搜索索引
|
||||
func (c *Client) searchIndex(ip uint32) uint32 {
|
||||
c.ItemLen = 4
|
||||
c.IndexLen = 7
|
||||
header = datBuff[0:8]
|
||||
start = binary.LittleEndian.Uint32(header[:4])
|
||||
end = binary.LittleEndian.Uint32(header[4:])
|
||||
|
||||
buf := make([]byte, c.IndexLen)
|
||||
|
||||
for {
|
||||
mid := start + c.IndexLen*(((end-start)/c.IndexLen)>>1)
|
||||
buf = datBuff[mid : mid+c.IndexLen]
|
||||
_ip := binary.LittleEndian.Uint32(buf[:c.ItemLen])
|
||||
|
||||
if end-start == c.IndexLen {
|
||||
if ip >= binary.LittleEndian.Uint32(datBuff[end:end+c.ItemLen]) {
|
||||
buf = datBuff[end : end+c.IndexLen]
|
||||
}
|
||||
return byteToUInt32(buf[c.ItemLen:])
|
||||
}
|
||||
|
||||
if _ip > ip {
|
||||
end = mid
|
||||
} else if _ip < ip {
|
||||
start = mid
|
||||
} else if _ip == ip {
|
||||
return byteToUInt32(buf[c.ItemLen:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 字节转UInt32
|
||||
func byteToUInt32(data []byte) uint32 {
|
||||
i := uint32(data[0]) & 0xff
|
||||
i |= (uint32(data[1]) << 8) & 0xff00
|
||||
i |= (uint32(data[2]) << 16) & 0xff0000
|
||||
return i
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package v4
|
||||
package qqwry
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -0,0 +1,53 @@
|
||||
package qqwry
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"go.dtapp.net/gostring"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"net"
|
||||
)
|
||||
|
||||
// QueryResult 返回
|
||||
type QueryResult struct {
|
||||
Ip string `json:"ip,omitempty"` // ip
|
||||
Country string `json:"country,omitempty"` // 国家或地区
|
||||
Area string `json:"area,omitempty"` // 区域
|
||||
}
|
||||
|
||||
// Query ip地址查询对应归属地信息
|
||||
func (c *Client) Query(ipAddress net.IP) (result QueryResult, err error) {
|
||||
|
||||
c.Offset = 0
|
||||
|
||||
// 偏移
|
||||
offset = c.searchIndex(binary.BigEndian.Uint32(ipAddress.To4()))
|
||||
if offset <= 0 {
|
||||
return QueryResult{}, errors.New("搜索失败")
|
||||
}
|
||||
|
||||
result.Ip = ipAddress.String()
|
||||
|
||||
c.Offset = offset + c.ItemLen
|
||||
|
||||
country, area = c.getAddr()
|
||||
|
||||
enc := simplifiedchinese.GBK.NewDecoder()
|
||||
|
||||
result.Country, _ = enc.String(string(country))
|
||||
|
||||
result.Country = gostring.SpaceAndLineBreak(result.Country)
|
||||
|
||||
result.Area, _ = enc.String(string(area))
|
||||
|
||||
// Delete CZ88.NET (防止不相关的信息产生干扰)
|
||||
if result.Area == " CZ88.NET" || result.Area == "" {
|
||||
result.Area = ""
|
||||
} else {
|
||||
result.Area = " " + result.Area
|
||||
}
|
||||
|
||||
result.Area = gostring.SpaceAndLineBreak(result.Area)
|
||||
|
||||
return result, nil
|
||||
}
|
@ -1,223 +0,0 @@
|
||||
package v4
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/binary"
|
||||
"go.dtapp.net/gostring"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
header []byte
|
||||
country []byte
|
||||
area []byte
|
||||
offset uint32
|
||||
start uint32
|
||||
end uint32
|
||||
)
|
||||
|
||||
//go:embed qqwry.dat
|
||||
var dat []byte
|
||||
|
||||
type Pointer struct {
|
||||
Offset uint32
|
||||
ItemLen uint32
|
||||
IndexLen uint32
|
||||
}
|
||||
|
||||
// Result 返回
|
||||
type Result struct {
|
||||
IP string `json:"ip,omitempty"` // 输入的ip地址
|
||||
Country string `json:"country,omitempty"` // 国家或地区
|
||||
Area string `json:"area,omitempty"` // 区域
|
||||
}
|
||||
|
||||
// InitIPV4Data 加载
|
||||
func (q *Pointer) InitIPV4Data() int64 {
|
||||
buf := dat[0:8]
|
||||
start := binary.LittleEndian.Uint32(buf[:4])
|
||||
end := binary.LittleEndian.Uint32(buf[4:])
|
||||
|
||||
return int64((end-start)/7 + 1)
|
||||
}
|
||||
|
||||
// ReadData 从文件中读取数据
|
||||
func (q *Pointer) readData(length uint32) (rs []byte) {
|
||||
end := q.Offset + length
|
||||
dataNum := uint32(len(dat))
|
||||
if q.Offset > dataNum {
|
||||
return nil
|
||||
}
|
||||
|
||||
if end > dataNum {
|
||||
end = dataNum
|
||||
}
|
||||
rs = dat[q.Offset:end]
|
||||
q.Offset = end
|
||||
return rs
|
||||
}
|
||||
|
||||
// Find ip地址查询对应归属地信息
|
||||
func (q *Pointer) Find(ipStr string) (res Result) {
|
||||
|
||||
// 赋值
|
||||
res.IP = ipStr
|
||||
if net.ParseIP(ipStr).To4() == nil {
|
||||
log.Println("不是ip地址")
|
||||
// 不是ip地址
|
||||
return res
|
||||
}
|
||||
|
||||
q.Offset = 0
|
||||
|
||||
// 偏移
|
||||
offset = q.searchIndex(binary.BigEndian.Uint32(net.ParseIP(ipStr).To4()))
|
||||
if offset <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
q.Offset = offset + q.ItemLen
|
||||
|
||||
country, area = q.getAddr()
|
||||
|
||||
enc := simplifiedchinese.GBK.NewDecoder()
|
||||
|
||||
res.Country, _ = enc.String(string(country))
|
||||
res.Country = gostring.SpaceAndLineBreak(res.Country)
|
||||
|
||||
res.Area, _ = enc.String(string(area))
|
||||
|
||||
// Delete CZ88.NET (防止不相关的信息产生干扰)
|
||||
if res.Area == " CZ88.NET" || res.Area == "" {
|
||||
res.Area = ""
|
||||
} else {
|
||||
res.Area = " " + res.Area
|
||||
}
|
||||
|
||||
res.Area = gostring.SpaceAndLineBreak(res.Area)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Query ip地址查询对应归属地信息
|
||||
func (q *Pointer) Query(ipAddress net.IP) (res Result) {
|
||||
|
||||
q.Offset = 0
|
||||
|
||||
// 偏移
|
||||
offset = q.searchIndex(binary.BigEndian.Uint32(ipAddress.To4()))
|
||||
if offset <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
q.Offset = offset + q.ItemLen
|
||||
|
||||
country, area = q.getAddr()
|
||||
|
||||
enc := simplifiedchinese.GBK.NewDecoder()
|
||||
|
||||
res.Country, _ = enc.String(string(country))
|
||||
|
||||
res.Country = gostring.SpaceAndLineBreak(res.Country)
|
||||
|
||||
res.Area, _ = enc.String(string(area))
|
||||
|
||||
// Delete CZ88.NET (防止不相关的信息产生干扰)
|
||||
if res.Area == " CZ88.NET" || res.Area == "" {
|
||||
res.Area = ""
|
||||
} else {
|
||||
res.Area = " " + res.Area
|
||||
}
|
||||
|
||||
res.Area = gostring.SpaceAndLineBreak(res.Area)
|
||||
|
||||
res.IP = ipAddress.String()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 获取地址信息
|
||||
func (q *Pointer) getAddr() ([]byte, []byte) {
|
||||
mode := q.readData(1)[0]
|
||||
if mode == 0x01 {
|
||||
// [IP][0x01][国家和地区信息的绝对偏移地址]
|
||||
q.Offset = byteToUInt32(q.readData(3))
|
||||
return q.getAddr()
|
||||
}
|
||||
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
|
||||
_offset := q.Offset - 1
|
||||
c1 := q.readArea(_offset)
|
||||
if mode == 0x02 {
|
||||
q.Offset = 4 + _offset
|
||||
} else {
|
||||
q.Offset = _offset + uint32(1+len(c1))
|
||||
}
|
||||
c2 := q.readArea(q.Offset)
|
||||
return c1, c2
|
||||
}
|
||||
|
||||
// 读取区
|
||||
func (q *Pointer) readArea(offset uint32) []byte {
|
||||
q.Offset = offset
|
||||
mode := q.readData(1)[0]
|
||||
if mode == 0x01 || mode == 0x02 {
|
||||
return q.readArea(byteToUInt32(q.readData(3)))
|
||||
}
|
||||
q.Offset = offset
|
||||
return q.readString()
|
||||
}
|
||||
|
||||
// 读取字符串
|
||||
func (q *Pointer) readString() []byte {
|
||||
data := make([]byte, 0)
|
||||
for {
|
||||
buf := q.readData(1)
|
||||
if buf[0] == 0 {
|
||||
break
|
||||
}
|
||||
data = append(data, buf[0])
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// 搜索索引
|
||||
func (q *Pointer) searchIndex(ip uint32) uint32 {
|
||||
q.ItemLen = 4
|
||||
q.IndexLen = 7
|
||||
header = dat[0:8]
|
||||
start = binary.LittleEndian.Uint32(header[:4])
|
||||
end = binary.LittleEndian.Uint32(header[4:])
|
||||
|
||||
buf := make([]byte, q.IndexLen)
|
||||