diff --git a/const.go b/const.go index 8290a9a..aa09f24 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package goip -const Version = "1.0.27" +const Version = "1.0.28" diff --git a/ip2region/ip2region.db b/ip2region/ip.db similarity index 100% rename from ip2region/ip2region.db rename to ip2region/ip.db diff --git a/ip2region/ip2region.go b/ip2region/ip2region.go index a767860..cef1492 100644 --- a/ip2region/ip2region.go +++ b/ip2region/ip2region.go @@ -6,6 +6,7 @@ import ( "go.dtapp.net/gostring" "io/ioutil" "log" + "net" "os" "strconv" "strings" @@ -36,7 +37,7 @@ type Ip2Region struct { dbFile string } -//go:embed ip2region.db +//go:embed ip.db var dbBinStr []byte type IpInfo struct { @@ -89,6 +90,11 @@ func getIpInfo(ipStr string, cityId int64, line []byte) (ipInfo IpInfo) { func (r *Ip2Region) MemorySearch(ipStr string) (ipInfo IpInfo, err error) { ipInfo.IP = ipStr + if net.ParseIP(ipStr).To4() == nil { + if net.ParseIP(ipStr).To16() == nil { + return ipInfo, err + } + } if r.totalBlocks == 0 { diff --git a/v4/qqwry.dat b/v4/ip.dat similarity index 100% rename from v4/qqwry.dat rename to v4/ip.dat diff --git a/v4/ipv4.go b/v4/ipv4.go index 2cef237..1ca739e 100644 --- a/v4/ipv4.go +++ b/v4/ipv4.go @@ -20,7 +20,7 @@ var ( end uint32 ) -//go:embed qqwry.dat +//go:embed ip.dat var dat []byte type Pointer struct { @@ -62,11 +62,11 @@ func (q *Pointer) readData(length uint32) (rs []byte) { } // Find ip地址查询对应归属地信息 -func (q *Pointer) Find(ip string) (res Result) { +func (q *Pointer) Find(ipStr string) (res Result) { // 赋值 - res.IP = ip - if net.ParseIP("61.241.55.180").To4() == nil { + res.IP = ipStr + if net.ParseIP(ipStr).To4() == nil { // 不是ip地址 return res } @@ -74,7 +74,7 @@ func (q *Pointer) Find(ip string) (res Result) { q.Offset = 0 // 偏移 - offset = q.searchIndex(binary.BigEndian.Uint32(net.ParseIP(ip).To4())) + offset = q.searchIndex(binary.BigEndian.Uint32(net.ParseIP(ipStr).To4())) if offset <= 0 { return } diff --git a/v6/ipv6wry.db b/v6/ip.db similarity index 100% rename from v6/ipv6wry.db rename to v6/ip.db diff --git a/v6/ipv6.go b/v6/ipv6.go index 414426f..024d1ff 100644 --- a/v6/ipv6.go +++ b/v6/ipv6.go @@ -31,7 +31,7 @@ type Result struct { Isp string `json:"isp,omitempty"` // 运营商 } -//go:embed ipv6wry.db +//go:embed ip.db var dat []byte type Pointer struct { @@ -66,15 +66,19 @@ func (q *Pointer) readData(length uint32) (rs []byte) { } // Find ip地址查询对应归属地信息 -func (q *Pointer) Find(ip string) (res Result) { +func (q *Pointer) Find(ipStr string) (res Result) { res = Result{} - res.IP = ip + res.IP = ipStr + if net.ParseIP(ipStr).To16() == nil { + return Result{} + } + q.Offset = 0 tp := big.NewInt(0) op := big.NewInt(0) - tp.SetBytes(net.ParseIP(ip).To16()) + tp.SetBytes(net.ParseIP(ipStr).To16()) op.SetString("18446744073709551616", 10) op.Div(tp, op) tp.SetString("FFFFFFFFFFFFFFFF", 16)