-
Notifications
You must be signed in to change notification settings - Fork 8
/
qqwrylib.go
202 lines (173 loc) · 4.5 KB
/
qqwrylib.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"encoding/binary"
"encoding/json"
"io/ioutil"
"log"
"net"
"os"
"github.com/yinheli/mahonia"
)
const (
INDEX_LEN = 7
REDIRECT_MODE_1 = 0x01
REDIRECT_MODE_2 = 0x02
)
type QQwry struct {
buff []byte
start uint32
end uint32
}
type Rq struct {
Ip string `json:"Ip"`
Country string `json:"City"`
City string `json:"Detail"`
Err int `json:"Err"`
Msg string `json:"ErrorMsg"`
}
func NewQQwry(file string) (qqwry *QQwry) {
qqwry = &QQwry{}
f, e := os.Open(file)
if e != nil {
log.Println(e)
return nil
}
defer f.Close()
qqwry.buff, e = ioutil.ReadAll(f)
if e != nil {
log.Println(e)
return nil
}
qqwry.start = binary.LittleEndian.Uint32(qqwry.buff[:4])
qqwry.end = binary.LittleEndian.Uint32(qqwry.buff[4:8])
return qqwry
}
func (this *Rq) String() string {
d, _ := json.Marshal(this)
return string(d)
}
func (this *QQwry) Find(ip string) *Rq {
rq := &Rq{Ip: ip}
if this.buff == nil {
rq.Err = 3
rq.Msg = "QQwry没有初始化"
return rq
}
var country []byte
var area []byte
ip_1 := net.ParseIP(ip)
if ip_1 == nil {
rq.Err = 1
rq.Msg = "错误的IP格式"
return rq
}
offset := this.searchRecord(binary.BigEndian.Uint32(ip_1.To4()))
if offset <= 0 {
rq.Err = 2
rq.Msg = "IP地址没找到归属地"
return rq
}
mode := this.readMode(offset + 4)
if mode == REDIRECT_MODE_1 {
countryOffset := this.readUint32FromByte3(offset + 5)
mode = this.readMode(countryOffset)
if mode == REDIRECT_MODE_2 {
c := this.readUint32FromByte3(countryOffset + 1)
country = this.readString(c)
countryOffset += 4
area = this.readArea(countryOffset)
} else {
country = this.readString(countryOffset)
countryOffset += uint32(len(country) + 1)
area = this.readArea(countryOffset)
}
} else if mode == REDIRECT_MODE_2 {
countryOffset := this.readUint32FromByte3(offset + 5)
country = this.readString(countryOffset)
area = this.readArea(offset + 8)
}
enc := mahonia.NewDecoder("gbk")
rq.Country = enc.ConvertString(string(country))
rq.City = enc.ConvertString(string(area))
return rq
}
func (this *QQwry) readUint32FromByte3(offset uint32) uint32 {
return byte3ToUInt32(this.buff[offset : offset+3])
}
func (this *QQwry) readMode(offset uint32) byte {
return this.buff[offset : offset+1][0]
}
func (this *QQwry) readString(offset uint32) []byte {
i := 0
for {
if this.buff[int(offset)+i] == 0 {
break
} else {
i++
}
}
return this.buff[offset : int(offset)+i]
}
func (this *QQwry) readArea(offset uint32) []byte {
mode := this.readMode(offset)
if mode == REDIRECT_MODE_1 || mode == REDIRECT_MODE_2 {
areaOffset := this.readUint32FromByte3(offset + 1)
if areaOffset == 0 {
return []byte("")
} else {
return this.readString(areaOffset)
}
} else {
return this.readString(offset)
}
return []byte("")
}
func (this *QQwry) getRecord(offset uint32) []byte {
return this.buff[offset : offset+INDEX_LEN]
}
func (this *QQwry) getIPFromRecord(buf []byte) uint32 {
return binary.LittleEndian.Uint32(buf[:4])
}
func (this *QQwry) getAddrFromRecord(buf []byte) uint32 {
return byte3ToUInt32(buf[4:7])
}
func (this *QQwry) searchRecord(ip uint32) uint32 {
start := this.start
end := this.end
// log.Printf("len info %v, %v ---- %v, %v", start, end, hex.EncodeToString(header[:4]), hex.EncodeToString(header[4:]))
for {
mid := this.getMiddleOffset(start, end)
buf := this.getRecord(mid)
_ip := this.getIPFromRecord(buf)
// log.Printf(">> %v, %v, %v -- %v", start, mid, end, hex.EncodeToString(buf[:4]))
if end-start == INDEX_LEN {
//log.Printf(">> %v, %v, %v -- %v", start, mid, end, hex.EncodeToString(buf[:4]))
offset := this.getAddrFromRecord(buf)
buf = this.getRecord(mid + INDEX_LEN)
if ip < this.getIPFromRecord(buf) {
return offset
} else {
return 0
}
}
// 找到的比较大,向前移
if _ip > ip {
end = mid
} else if _ip < ip { // 找到的比较小,向后移
start = mid
} else if _ip == ip {
return byte3ToUInt32(buf[4:7])
}
}
return 0
}
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
}