1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 21:29:02 +08:00

remove indexLen

This commit is contained in:
zu1k 2020-07-22 06:45:58 +08:00
parent 176c3d699e
commit bee86980bb
4 changed files with 19 additions and 24 deletions

View File

@ -21,7 +21,7 @@ var updateCmd = &cobra.Command{
filePath := filepath.Join(constant.HomePath, "qqwry.dat") filePath := filepath.Join(constant.HomePath, "qqwry.dat")
log.Println("正在下载最新纯真 IP 库...") log.Println("正在下载最新纯真 IP 库...")
tmpData, err := qqwry.Download() tmpData, err := qqwry.Download(filePath)
if err != nil { if err != nil {
log.Fatalln("下载失败", err.Error()) log.Fatalln("下载失败", err.Error())
return return

View File

@ -13,7 +13,6 @@ type FileData struct {
type IPDB struct { type IPDB struct {
Data *FileData Data *FileData
Offset uint32 Offset uint32
IndexLen uint32
IPNum uint32 IPNum uint32
} }
@ -82,7 +81,7 @@ func (db *IPDB) ReadArea(offset uint32) []byte {
return db.ReadString(offset) return db.ReadString(offset)
} }
func (db *IPDB) GetMiddleOffset(start uint32, end uint32) uint32 { func GetMiddleOffset(start uint32, end uint32, indexLen uint32) uint32 {
records := ((end - start) / db.IndexLen) >> 1 records := ((end - start) / indexLen) >> 1
return start + records*db.IndexLen return start + records*indexLen
} }

View File

@ -52,7 +52,6 @@ func NewQQwry(filePath string) QQwry {
return QQwry{ return QQwry{
IPDB: common.IPDB{ IPDB: common.IPDB{
Data: &fileInfo, Data: &fileInfo,
IndexLen: 7,
IPNum: (end-start)/7 + 1, IPNum: (end-start)/7 + 1,
}, },
} }
@ -117,18 +116,18 @@ func (db *QQwry) searchIndex(ip uint32) uint32 {
start := binary.LittleEndian.Uint32(header[:4]) start := binary.LittleEndian.Uint32(header[:4])
end := binary.LittleEndian.Uint32(header[4:]) end := binary.LittleEndian.Uint32(header[4:])
buf := make([]byte, db.IndexLen) buf := make([]byte, 7)
mid := uint32(0) mid := uint32(0)
_ip := uint32(0) _ip := uint32(0)
for { for {
mid = db.GetMiddleOffset(start, end) mid = common.GetMiddleOffset(start, end, 7)
buf = db.ReadData(db.IndexLen, mid) buf = db.ReadData(7, mid)
_ip = binary.LittleEndian.Uint32(buf[:4]) _ip = binary.LittleEndian.Uint32(buf[:4])
if end-start == db.IndexLen { if end-start == 7 {
offset := common.ByteToUInt32(buf[4:]) offset := common.ByteToUInt32(buf[4:])
buf = db.ReadData(db.IndexLen) buf = db.ReadData(7)
if ip < binary.LittleEndian.Uint32(buf[:4]) { if ip < binary.LittleEndian.Uint32(buf[:4]) {
return offset return offset
} }

View File

@ -54,14 +54,11 @@ func NewZXwry(filePath string) ZXwry {
return ZXwry{ return ZXwry{
IPDB: common.IPDB{ IPDB: common.IPDB{
Data: &fileInfo, Data: &fileInfo,
IndexLen: 11,
}, },
} }
} }
func (db ZXwry) Find(ip string) (result string) { func (db ZXwry) Find(ip string) (result string) {
db.Offset = 0
tp := big.NewInt(0) tp := big.NewInt(0)
op := big.NewInt(0) op := big.NewInt(0)
tp.SetBytes(net.ParseIP(ip).To16()) tp.SetBytes(net.ParseIP(ip).To16())
@ -101,16 +98,16 @@ func (db *ZXwry) searchIndex(ip uint64) uint32 {
header := db.ReadData(16, 8) header := db.ReadData(16, 8)
start := binary.LittleEndian.Uint32(header[8:]) start := binary.LittleEndian.Uint32(header[8:])
counts := 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 { for {
mid := db.GetMiddleOffset(start, end) mid := common.GetMiddleOffset(start, end, 11)
buf = db.ReadData(11, mid) buf = db.ReadData(11, mid)
ipBytes := binary.LittleEndian.Uint64(buf[:8]) ipBytes := binary.LittleEndian.Uint64(buf[:8])
if end-start == db.IndexLen { if end-start == 11 {
if ip >= binary.LittleEndian.Uint64(db.ReadData(8, end)) { if ip >= binary.LittleEndian.Uint64(db.ReadData(8, end)) {
buf = db.ReadData(11, end) buf = db.ReadData(11, end)
} }