diff --git a/cmd/update.go b/cmd/update.go index 4912f1b..562b139 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -21,7 +21,7 @@ var updateCmd = &cobra.Command{ filePath := filepath.Join(constant.HomePath, "qqwry.dat") log.Println("正在下载最新纯真 IP 库...") - tmpData, err := qqwry.Download() + tmpData, err := qqwry.Download(filePath) if err != nil { log.Fatalln("下载失败", err.Error()) return diff --git a/pkg/common/struct.go b/pkg/common/struct.go index f8d6600..735d189 100644 --- a/pkg/common/struct.go +++ b/pkg/common/struct.go @@ -11,10 +11,9 @@ type FileData struct { // IPDB common ip database type IPDB struct { - Data *FileData - Offset uint32 - IndexLen uint32 - IPNum uint32 + Data *FileData + Offset uint32 + IPNum uint32 } // setOffset 设置偏移量 @@ -82,7 +81,7 @@ func (db *IPDB) ReadArea(offset uint32) []byte { return db.ReadString(offset) } -func (db *IPDB) GetMiddleOffset(start uint32, end uint32) uint32 { - records := ((end - start) / db.IndexLen) >> 1 - return start + records*db.IndexLen +func GetMiddleOffset(start uint32, end uint32, indexLen uint32) uint32 { + records := ((end - start) / indexLen) >> 1 + return start + records*indexLen } diff --git a/pkg/qqwry/qqwry.go b/pkg/qqwry/qqwry.go index 22994cb..68b7478 100644 --- a/pkg/qqwry/qqwry.go +++ b/pkg/qqwry/qqwry.go @@ -51,9 +51,8 @@ func NewQQwry(filePath string) QQwry { return QQwry{ IPDB: common.IPDB{ - Data: &fileInfo, - IndexLen: 7, - IPNum: (end-start)/7 + 1, + Data: &fileInfo, + IPNum: (end-start)/7 + 1, }, } } @@ -117,18 +116,18 @@ func (db *QQwry) searchIndex(ip uint32) uint32 { start := binary.LittleEndian.Uint32(header[:4]) end := binary.LittleEndian.Uint32(header[4:]) - buf := make([]byte, db.IndexLen) + buf := make([]byte, 7) mid := uint32(0) _ip := uint32(0) for { - mid = db.GetMiddleOffset(start, end) - buf = db.ReadData(db.IndexLen, mid) + mid = common.GetMiddleOffset(start, end, 7) + buf = db.ReadData(7, mid) _ip = binary.LittleEndian.Uint32(buf[:4]) - if end-start == db.IndexLen { + if end-start == 7 { offset := common.ByteToUInt32(buf[4:]) - buf = db.ReadData(db.IndexLen) + buf = db.ReadData(7) if ip < binary.LittleEndian.Uint32(buf[:4]) { return offset } diff --git a/pkg/zxipv6wry/zxipv6wry.go b/pkg/zxipv6wry/zxipv6wry.go index a7eb94b..26464c5 100644 --- a/pkg/zxipv6wry/zxipv6wry.go +++ b/pkg/zxipv6wry/zxipv6wry.go @@ -53,15 +53,12 @@ func NewZXwry(filePath string) ZXwry { return ZXwry{ IPDB: common.IPDB{ - Data: &fileInfo, - IndexLen: 11, + Data: &fileInfo, }, } } func (db ZXwry) Find(ip string) (result string) { - db.Offset = 0 - tp := big.NewInt(0) op := big.NewInt(0) tp.SetBytes(net.ParseIP(ip).To16()) @@ -101,16 +98,16 @@ func (db *ZXwry) searchIndex(ip uint64) uint32 { header := db.ReadData(16, 8) start := binary.LittleEndian.Uint32(header[8:]) counts := binary.LittleEndian.Uint32(header[:8]) - end := start + counts*db.IndexLen + end := start + counts*11 - buf := make([]byte, db.IndexLen) + buf := make([]byte, 11) for { - mid := db.GetMiddleOffset(start, end) + mid := common.GetMiddleOffset(start, end, 11) buf = db.ReadData(11, mid) ipBytes := binary.LittleEndian.Uint64(buf[:8]) - if end-start == db.IndexLen { + if end-start == 11 { if ip >= binary.LittleEndian.Uint64(db.ReadData(8, end)) { buf = db.ReadData(11, end) }