-
Notifications
You must be signed in to change notification settings - Fork 37
/
qqwry.go
178 lines (152 loc) · 3.93 KB
/
qqwry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package qqwry
import (
"encoding/binary"
"github.com/yinheli/mahonia"
// "encoding/hex"
"net"
"os"
)
const (
INDEX_LEN = 7
REDIRECT_MODE_1 = 0x01
REDIRECT_MODE_2 = 0x02
)
// @author yinheli
type QQwry struct {
Ip string
Country string
City string
filepath string
file *os.File
}
func NewQQwry(file string) (qqwry *QQwry) {
qqwry = &QQwry{filepath: file}
return
}
func (this *QQwry) Find(ip string) {
if this.filepath == "" {
return
}
file, err := os.OpenFile(this.filepath, os.O_RDONLY, 0400)
defer file.Close()
if err != nil {
return
}
this.file = file
this.Ip = ip
offset := this.searchIndex(binary.BigEndian.Uint32(net.ParseIP(ip).To4()))
// log.Println("loc offset:", offset)
if offset <= 0 {
return
}
var country []byte
var area []byte
mode := this.readMode(offset + 4)
// log.Println("mode", mode)
if mode == REDIRECT_MODE_1 {
countryOffset := this.readUInt24()
mode = this.readMode(countryOffset)
// log.Println("1 - mode", mode)
if mode == REDIRECT_MODE_2 {
c := this.readUInt24()
country = this.readString(c)
countryOffset += 4
} else {
country = this.readString(countryOffset)
countryOffset += uint32(len(country) + 1)
}
area = this.readArea(countryOffset)
} else if mode == REDIRECT_MODE_2 {
countryOffset := this.readUInt24()
country = this.readString(countryOffset)
area = this.readArea(offset + 8)
} else {
country = this.readString(offset + 4)
area = this.readArea(offset + uint32(5+len(country)))
}
enc := mahonia.NewDecoder("gbk")
this.Country = enc.ConvertString(string(country))
this.City = enc.ConvertString(string(area))
}
func (this *QQwry) readMode(offset uint32) byte {
this.file.Seek(int64(offset), 0)
mode := make([]byte, 1)
this.file.Read(mode)
return mode[0]
}
func (this *QQwry) readArea(offset uint32) []byte {
mode := this.readMode(offset)
if mode == REDIRECT_MODE_1 || mode == REDIRECT_MODE_2 {
areaOffset := this.readUInt24()
if areaOffset == 0 {
return []byte("")
} else {
return this.readString(areaOffset)
}
} else {
return this.readString(offset)
}
return []byte("")
}
func (this *QQwry) readString(offset uint32) []byte {
this.file.Seek(int64(offset), 0)
data := make([]byte, 0, 30)
buf := make([]byte, 1)
for {
this.file.Read(buf)
if buf[0] == 0 {
break
}
data = append(data, buf[0])
}
return data
}
func (this *QQwry) searchIndex(ip uint32) uint32 {
header := make([]byte, 8)
this.file.Seek(0, 0)
this.file.Read(header)
start := binary.LittleEndian.Uint32(header[:4])
end := binary.LittleEndian.Uint32(header[4:])
// log.Printf("len info %v, %v ---- %v, %v", start, end, hex.EncodeToString(header[:4]), hex.EncodeToString(header[4:]))
for {
mid := this.getMiddleOffset(start, end)
this.file.Seek(int64(mid), 0)
buf := make([]byte, INDEX_LEN)
this.file.Read(buf)
_ip := binary.LittleEndian.Uint32(buf[:4])
// log.Printf(">> %v, %v, %v -- %v", start, mid, end, hex.EncodeToString(buf[:4]))
if end-start == INDEX_LEN {
offset := byte3ToUInt32(buf[4:])
this.file.Read(buf)
if ip < binary.LittleEndian.Uint32(buf[:4]) {
return offset
} else {
return 0
}
}
// 找到的比较大,向前移
if _ip > ip {
end = mid
} else if _ip < ip { // 找到的比较小,向后移
start = mid
} else if _ip == ip {
return byte3ToUInt32(buf[4:])
}
}
return 0
}
func (this *QQwry) readUInt24() uint32 {
buf := make([]byte, 3)
this.file.Read(buf)
return byte3ToUInt32(buf)
}
func (this *QQwry) getMiddleOffset(start uint32, end uint32) uint32 {
records := ((end - start) / INDEX_LEN) >> 1
return start + records*INDEX_LEN
}
func byte3ToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000
return i
}