From 9f4a447374fe0dc18cbcb093d150f2e436cd4163 Mon Sep 17 00:00:00 2001 From: zu1k Date: Sun, 19 Jul 2020 19:51:54 +0800 Subject: [PATCH] fix common --- README.md | 4 ++++ pkg/common/struct.go | 1 - pkg/zxipv6wry/zxipv6wry.go | 33 +++++++++++++++------------------ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index efd807f..f4d9782 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,10 @@ export NALI_DB=geoip - [Cobra CLI库](https://github.com/spf13/cobra) - [Nali-cli](https://github.com/SukkaW/nali-cli) +## Origin + +Inspired by [](https://github.com/meteoral/Nali) and [nali-cli](https://github.com/SukkaW/nali-cli) + ## License MIT diff --git a/pkg/common/struct.go b/pkg/common/struct.go index 58849a3..f8d6600 100644 --- a/pkg/common/struct.go +++ b/pkg/common/struct.go @@ -13,7 +13,6 @@ type FileData struct { type IPDB struct { Data *FileData Offset uint32 - ItemLen uint32 IndexLen uint32 IPNum uint32 } diff --git a/pkg/zxipv6wry/zxipv6wry.go b/pkg/zxipv6wry/zxipv6wry.go index fd3aeb0..73c0eff 100644 --- a/pkg/zxipv6wry/zxipv6wry.go +++ b/pkg/zxipv6wry/zxipv6wry.go @@ -45,7 +45,7 @@ func NewZXwry(filePath string) ZXwry { return ZXwry{ IPDB: common.IPDB{ Data: &fileInfo, - IndexLen: 7, + IndexLen: 11, }, } } @@ -77,22 +77,19 @@ func (db *ZXwry) getAddr(offset uint32) (string, string) { offset = db.ReadUInt24() return db.getAddr(offset) } - _offset := db.Offset - 1 - c1 := db.ReadArea(_offset) + realOffset := db.Offset - 1 + c1 := db.ReadArea(realOffset) if mode == common.RedirectMode2 { - db.Offset = 4 + _offset + db.Offset = 4 + realOffset } else { - db.Offset = _offset + uint32(1+len(c1)) + db.Offset = realOffset + uint32(1+len(c1)) } c2 := db.ReadArea(db.Offset) return string(c1), string(c2) } func (db *ZXwry) searchIndex(ip uint64) uint32 { - db.ItemLen = 8 - db.IndexLen = 11 - - header := db.Data.Data[8:24] + header := db.ReadData(16, 8) start := binary.LittleEndian.Uint32(header[8:]) counts := binary.LittleEndian.Uint32(header[:8]) end := start + counts*db.IndexLen @@ -101,22 +98,22 @@ func (db *ZXwry) searchIndex(ip uint64) uint32 { for { mid := db.GetMiddleOffset(start, end) - buf = db.Data.Data[mid : mid+db.IndexLen] - _ip := binary.LittleEndian.Uint64(buf[:db.ItemLen]) + buf = db.ReadData(11, mid) + ipBytes := binary.LittleEndian.Uint64(buf[:8]) if end-start == db.IndexLen { - if ip >= binary.LittleEndian.Uint64(db.Data.Data[end:end+db.ItemLen]) { - buf = db.Data.Data[end : end+db.IndexLen] + if ip >= binary.LittleEndian.Uint64(db.ReadData(8, end)) { + buf = db.ReadData(11, end) } - return common.ByteToUInt32(buf[db.ItemLen:]) + return common.ByteToUInt32(buf[8:]) } - if _ip > ip { + if ipBytes > ip { end = mid - } else if _ip < ip { + } else if ipBytes < ip { start = mid - } else if _ip == ip { - return common.ByteToUInt32(buf[db.ItemLen:]) + } else if ipBytes == ip { + return common.ByteToUInt32(buf[8:]) } } }