1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 13:19: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")
log.Println("正在下载最新纯真 IP 库...")
tmpData, err := qqwry.Download()
tmpData, err := qqwry.Download(filePath)
if err != nil {
log.Fatalln("下载失败", err.Error())
return

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}