mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-04-10 17:55:23 +08:00
更好地支持IPv6
This commit is contained in:
@@ -4,10 +4,12 @@ package iplibrary
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"io"
|
||||
"math/big"
|
||||
"net"
|
||||
"runtime"
|
||||
"sort"
|
||||
@@ -21,8 +23,8 @@ type Reader struct {
|
||||
|
||||
regionMap map[string]*ipRegion // 缓存重复的区域用来节约内存
|
||||
|
||||
ipV4Items []ipv4Item
|
||||
ipV6Items []ipv6Item
|
||||
ipV4Items []ipv4ItemV1
|
||||
ipV6Items []ipv6ItemV1
|
||||
|
||||
lastIPFrom uint64
|
||||
lastCountryId uint16
|
||||
@@ -32,16 +34,16 @@ type Reader struct {
|
||||
lastProviderId uint16
|
||||
}
|
||||
|
||||
// NewReader 创建新Reader对象
|
||||
func NewReader(reader io.Reader) (*Reader, error) {
|
||||
// NewReaderV1 创建新Reader对象
|
||||
func NewReaderV1(reader io.Reader) (*Reader, error) {
|
||||
var libReader = &Reader{
|
||||
regionMap: map[string]*ipRegion{},
|
||||
}
|
||||
|
||||
if runtime.NumCPU() >= 4 /** CPU数量较多的通常有着大内存 **/ {
|
||||
libReader.ipV4Items = make([]ipv4Item, 0, 6_000_000)
|
||||
libReader.ipV4Items = make([]ipv4ItemV1, 0, 6_000_000)
|
||||
} else {
|
||||
libReader.ipV4Items = make([]ipv4Item, 0, 600_000)
|
||||
libReader.ipV4Items = make([]ipv4ItemV1, 0, 600_000)
|
||||
}
|
||||
|
||||
err := libReader.load(reader)
|
||||
@@ -131,7 +133,7 @@ func (this *Reader) Lookup(ip net.IP) *QueryResult {
|
||||
return &QueryResult{}
|
||||
}
|
||||
|
||||
var ipLong = configutils.IP2Long(ip)
|
||||
var ipLong = this.ip2long(ip)
|
||||
var isV4 = configutils.IsIPv4(ip)
|
||||
var resultItem any
|
||||
if isV4 {
|
||||
@@ -170,11 +172,11 @@ func (this *Reader) Meta() *Meta {
|
||||
return this.meta
|
||||
}
|
||||
|
||||
func (this *Reader) IPv4Items() []ipv4Item {
|
||||
func (this *Reader) IPv4Items() []ipv4ItemV1 {
|
||||
return this.ipV4Items
|
||||
}
|
||||
|
||||
func (this *Reader) IPv6Items() []ipv6Item {
|
||||
func (this *Reader) IPv6Items() []ipv6ItemV1 {
|
||||
return this.ipV6Items
|
||||
}
|
||||
|
||||
@@ -304,13 +306,13 @@ func (this *Reader) parseLine(line []byte) error {
|
||||
}
|
||||
|
||||
if version == "4" {
|
||||
this.ipV4Items = append(this.ipV4Items, ipv4Item{
|
||||
this.ipV4Items = append(this.ipV4Items, ipv4ItemV1{
|
||||
IPFrom: uint32(ipFrom),
|
||||
IPTo: uint32(ipTo),
|
||||
Region: region,
|
||||
})
|
||||
} else {
|
||||
this.ipV6Items = append(this.ipV6Items, ipv6Item{
|
||||
this.ipV6Items = append(this.ipV6Items, ipv6ItemV1{
|
||||
IPFrom: ipFrom,
|
||||
IPTo: ipTo,
|
||||
Region: region,
|
||||
@@ -328,3 +330,18 @@ func (this *Reader) decodeUint64(s string) uint64 {
|
||||
i, _ := strconv.ParseUint(s, 10, 64)
|
||||
return i
|
||||
}
|
||||
|
||||
func (this *Reader) ip2long(netIP net.IP) uint64 {
|
||||
if len(netIP) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
var b4 = netIP.To4()
|
||||
if b4 != nil {
|
||||
return uint64(binary.BigEndian.Uint32(b4.To4()))
|
||||
}
|
||||
|
||||
var i = big.NewInt(0)
|
||||
i.SetBytes(netIP.To16())
|
||||
return i.Uint64()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user