Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
freshcn committed Feb 18, 2017
2 parents 0e52665 + 074ebec commit a2e36d4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ qqwry.iml
qqwry.dat
qqwry
qqwry_linux_amd64

launch.json
29 changes: 17 additions & 12 deletions consts.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
package main

import (
"os"
"net/http"
"os"
)

const (
INDEX_LEN = 7 // 索引长度
REDIRECT_MODE_1 = 0x01 // 国家的类型, 指向另一个指向
REDIRECT_MODE_2 = 0x02 // 国家的类型, 指向一个指向
// IndexLen 索引长度
IndexLen = 7
// RedirectMode1 国家的类型, 指向另一个指向
RedirectMode1 = 0x01
// RedirectMode2 国家的类型, 指向一个指向
RedirectMode2 = 0x02
)

type resultQQwry struct {
Ip string `json:"ip"`
// ResultQQwry 归属地信息
type ResultQQwry struct {
IP string `json:"ip"`
Country string `json:"country"`
Area string `json:"area"`
}

type fileData struct {
Data []byte
Data []byte
FilePath string
Path *os.File
IpNum int64
IPNum int64
}

// QQwry 纯真ip库
type QQwry struct {
Data *fileData
Offset int64
Data *fileData
Offset int64
}

// 向客户端返回数据的
type response struct {
// Response 向客户端返回数据的
type Response struct {
r *http.Request
w http.ResponseWriter
}
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"flag"
"fmt"
"log"
"net/http"
"runtime"
"time"
"fmt"
"strings"
"time"
)

func main() {
Expand All @@ -16,20 +16,20 @@ func main() {
port := flag.String("port", "2060", "HTTP 请求监听端口号")
flag.Parse()

IpData.FilePath = *datFile
IPData.FilePath = *datFile

startTime := time.Now().UnixNano()
res := IpData.InitIpData()
res := IPData.InitIPData()

if v, ok := res.(error); ok {
log.Panic(v)
}
endTime := time.Now().UnixNano()

log.Printf("IP 库加载完成 共加载:%d 条 IP 记录, 所花时间:%.1f ms\n", IpData.IpNum, float64(endTime-startTime)/1000000)
log.Printf("IP 库加载完成 共加载:%d 条 IP 记录, 所花时间:%.1f ms\n", IPData.IPNum, float64(endTime-startTime)/1000000)

// 下面开始加载 http 相关的服务
http.HandleFunc("/", findIp)
http.HandleFunc("/", findIP)

log.Printf("开始监听网络端口:%s", *port)

Expand All @@ -38,8 +38,8 @@ func main() {
}
}

// 查找 IP 地址的接口
func findIp(w http.ResponseWriter, r *http.Request) {
// findIP 查找 IP 地址的接口
func findIP(w http.ResponseWriter, r *http.Request) {
res := NewResponse(w, r)

ip := r.Form.Get("ip")
Expand All @@ -53,7 +53,7 @@ func findIp(w http.ResponseWriter, r *http.Request) {

qqWry := NewQQwry()

rs := map[string]resultQQwry{}
rs := map[string]ResultQQwry{}
if len(ips) > 0 {
for _, v := range ips {
rs[v] = qqWry.Find(v)
Expand Down
83 changes: 42 additions & 41 deletions qqwry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package main
import (
"encoding/binary"
"errors"
"github.com/axgle/mahonia"
"io/ioutil"
"log"
"net"
"os"
"strings"

"golang.org/x/text/encoding/simplifiedchinese"
)

var IpData fileData
// IPData IP库的数据
var IPData fileData

// 初始化ip库数据到内存中
func (f *fileData) InitIpData() (rs interface{}) {
// InitIPData 初始化ip库数据到内存中
func (f *fileData) InitIPData() (rs interface{}) {

// 判断文件是否存在
_, err := os.Stat(f.FilePath)
Expand All @@ -31,7 +33,6 @@ func (f *fileData) InitIpData() (rs interface{}) {
}
defer f.Path.Close()


tmpData, err := ioutil.ReadAll(f.Path)
if err != nil {
log.Println(err)
Expand All @@ -45,48 +46,49 @@ func (f *fileData) InitIpData() (rs interface{}) {
start := binary.LittleEndian.Uint32(buf[:4])
end := binary.LittleEndian.Uint32(buf[4:])

f.IpNum = int64((end-start)/INDEX_LEN + 1)
f.IPNum = int64((end-start)/IndexLen + 1)

return true
}

// 新建 qqwry 类型
// NewQQwry 新建 qqwry 类型
func NewQQwry() QQwry {
return QQwry{
Data: &IpData,
Data: &IPData,
}
}

// 从文件中读取数据
// ReadData 从文件中读取数据
func (q *QQwry) ReadData(num int, offset ...int64) (rs []byte) {
if len(offset) > 0 {
q.SetOffset(offset[0])
}
nums := int64(num)
end := q.Offset+nums
end := q.Offset + nums
dataNum := int64(len(q.Data.Data))
if (q.Offset > dataNum) {
if q.Offset > dataNum {
return nil
}

if (end > dataNum) {
if end > dataNum {
end = dataNum
}
rs = q.Data.Data[q.Offset : end]
rs = q.Data.Data[q.Offset:end]
q.Offset = end
return
}

// 设置偏移量
// SetOffset 设置偏移量
func (q *QQwry) SetOffset(offset int64) {
q.Offset = offset
}

func (q *QQwry) Find(ip string) (res resultQQwry) {
// Find ip地址查询对应归属地信息
func (q *QQwry) Find(ip string) (res ResultQQwry) {

res = resultQQwry{}
res = ResultQQwry{}

res.Ip = ip
res.IP = ip
if strings.Count(ip, ".") != 3 {
return res
}
Expand All @@ -99,10 +101,10 @@ func (q *QQwry) Find(ip string) (res resultQQwry) {
var area []byte

mode := q.readMode(offset + 4)
if mode == REDIRECT_MODE_1 {
if mode == RedirectMode1 {
countryOffset := q.readUInt24()
mode = q.readMode(countryOffset)
if mode == REDIRECT_MODE_2 {
if mode == RedirectMode2 {
c := q.readUInt24()
country = q.readString(c)
countryOffset += 4
Expand All @@ -111,7 +113,7 @@ func (q *QQwry) Find(ip string) (res resultQQwry) {
countryOffset += uint32(len(country) + 1)
}
area = q.readArea(countryOffset)
} else if mode == REDIRECT_MODE_2 {
} else if mode == RedirectMode2 {
countryOffset := q.readUInt24()
country = q.readString(countryOffset)
area = q.readArea(offset + 8)
Expand All @@ -120,34 +122,33 @@ func (q *QQwry) Find(ip string) (res resultQQwry) {
area = q.readArea(offset + uint32(5+len(country)))
}


enc := mahonia.NewDecoder("gbk")
res.Country = enc.ConvertString(string(country))
res.Area = enc.ConvertString(string(area))
enc := simplifiedchinese.GBK.NewDecoder()
res.Country, _ = enc.String(string(country))
res.Area, _ = enc.String(string(area))

return
}

// readMode 获取偏移值类型
func (q *QQwry) readMode(offset uint32) byte {
mode := q.ReadData(1, int64(offset))
return mode[0]
}

// readArea 读取区域
func (q *QQwry) readArea(offset uint32) []byte {
mode := q.readMode(offset)
if mode == REDIRECT_MODE_1 || mode == REDIRECT_MODE_2 {
if mode == RedirectMode1 || mode == RedirectMode2 {
areaOffset := q.readUInt24()
if areaOffset == 0 {
return []byte("")
} else {
return q.readString(areaOffset)
}
} else {
return q.readString(offset)
return q.readString(areaOffset)
}
return []byte("")
return q.readString(offset)
}

// readString 获取字符串
func (q *QQwry) readString(offset uint32) []byte {
q.SetOffset(int64(offset))
data := make([]byte, 0, 30)
Expand All @@ -162,29 +163,29 @@ func (q *QQwry) readString(offset uint32) []byte {
return data
}

// searchIndex 查找索引位置
func (q *QQwry) searchIndex(ip uint32) uint32 {
header := q.ReadData(8, 0)

start := binary.LittleEndian.Uint32(header[:4])
end := binary.LittleEndian.Uint32(header[4:])

buf := make([]byte, INDEX_LEN)
buf := make([]byte, IndexLen)
mid := uint32(0)
_ip := uint32(0)

for {
mid = q.getMiddleOffset(start, end)
buf = q.ReadData(INDEX_LEN, int64(mid))
buf = q.ReadData(IndexLen, int64(mid))
_ip = binary.LittleEndian.Uint32(buf[:4])

if end-start == INDEX_LEN {
if end-start == IndexLen {
offset := byteToUInt32(buf[4:])
buf = q.ReadData(INDEX_LEN)
buf = q.ReadData(IndexLen)
if ip < binary.LittleEndian.Uint32(buf[:4]) {
return offset
} else {
return 0
}
return 0
}

// 找到的比较大,向前移
Expand All @@ -195,22 +196,22 @@ func (q *QQwry) searchIndex(ip uint32) uint32 {
} else if _ip == ip {
return byteToUInt32(buf[4:])
}

}
return 0
}

// readUInt24
func (q *QQwry) readUInt24() uint32 {
buf := q.ReadData(3)
return byteToUInt32(buf)
}

// getMiddleOffset
func (q *QQwry) getMiddleOffset(start uint32, end uint32) uint32 {
records := ((end - start) / INDEX_LEN) >> 1
return start + records*INDEX_LEN
records := ((end - start) / IndexLen) >> 1
return start + records*IndexLen
}

// 将 byte 转换为uint32
// byteToUInt32 将 byte 转换为uint32
func byteToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
Expand Down
Loading

0 comments on commit a2e36d4

Please sign in to comment.