Skip to content

Commit

Permalink
Merge branch 'feature/format_comment' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
freshcn committed Feb 18, 2017
2 parents 5b57c8a + 9fc90b4 commit 074ebec
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 65 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
64 changes: 33 additions & 31 deletions qqwry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"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 Down Expand Up @@ -45,19 +46,19 @@ 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])
Expand All @@ -77,16 +78,17 @@ func (q *QQwry) ReadData(num int, offset ...int64) (rs []byte) {
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 @@ -127,26 +129,26 @@ func (q *QQwry) Find(ip string) (res resultQQwry) {
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 @@ -161,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 @@ -194,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
28 changes: 15 additions & 13 deletions response.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package main

import (
"fmt"
"net/http"

"github.com/pquerna/ffjson/ffjson"
"fmt"
)

func NewResponse(w http.ResponseWriter, r *http.Request) response {
// NewResponse 创建一个新的response对象
func NewResponse(w http.ResponseWriter, r *http.Request) Response {
r.ParseForm()
return response{
return Response{
w: w,
r: r,
}
}

// 返回正确的信息
func (r *response) ReturnSuccess(data interface{}) {
// ReturnSuccess 返回正确的信息
func (r *Response) ReturnSuccess(data interface{}) {
r.Return(data, 200)
}

// 返回错误信息
func (r *response) ReturnError(statuscode, code int, errMsg string) {
r.Return(map[string]interface{}{"errcode":code, "errmsg":errMsg}, statuscode)
// ReturnError 返回错误信息
func (r *Response) ReturnError(statuscode, code int, errMsg string) {
r.Return(map[string]interface{}{"errcode": code, "errmsg": errMsg}, statuscode)
}

// 向客户返回回数据
func (r *response) Return(data interface{}, code int) {
// Return 向客户返回回数据
func (r *Response) Return(data interface{}, code int) {
jsonp := r.IsJSONP()

rs, err := ffjson.Marshal(data)
if err != nil {
if err != nil {
code = 500
rs = []byte(fmt.Sprintf(`{"errcode":500, "errmsg":"%s"}`, err.Error()))
}
Expand All @@ -44,8 +46,8 @@ func (r *response) Return(data interface{}, code int) {
}
}

// 是否为jsonp 请求
func (r *response) IsJSONP() string {
// IsJSONP 是否为jsonp 请求
func (r *Response) IsJSONP() string {
if r.r.Form.Get("callback") != "" {
return r.r.Form.Get("callback")
}
Expand Down

0 comments on commit 074ebec

Please sign in to comment.