mirror of
https://github.com/zu1k/nali.git
synced 2025-01-22 21:29:02 +08:00
remove indexLen
This commit is contained in:
parent
176c3d699e
commit
bee86980bb
@ -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
|
||||||
|
@ -11,10 +11,9 @@ type FileData struct {
|
|||||||
|
|
||||||
// IPDB common ip database
|
// IPDB common ip database
|
||||||
type IPDB struct {
|
type IPDB struct {
|
||||||
Data *FileData
|
Data *FileData
|
||||||
Offset uint32
|
Offset uint32
|
||||||
IndexLen uint32
|
IPNum uint32
|
||||||
IPNum uint32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setOffset 设置偏移量
|
// setOffset 设置偏移量
|
||||||
@ -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
|
||||||
}
|
}
|
||||||
|
@ -51,9 +51,8 @@ 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
|
||||||
}
|
}
|
||||||
|
@ -53,15 +53,12 @@ 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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user