2022-02-20 11:28:25 +08:00
|
|
|
package ip2region
|
|
|
|
|
|
|
|
import (
|
2022-06-27 17:03:09 +08:00
|
|
|
"errors"
|
2022-02-20 11:28:25 +08:00
|
|
|
"fmt"
|
2022-06-27 17:03:09 +08:00
|
|
|
"io/ioutil"
|
2022-02-20 11:28:25 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
2022-02-20 11:41:26 +08:00
|
|
|
"strings"
|
2022-02-20 11:28:25 +08:00
|
|
|
|
|
|
|
"github.com/lionsoul2014/ip2region/binding/golang/ip2region"
|
2022-06-27 17:03:09 +08:00
|
|
|
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
2022-02-20 11:28:25 +08:00
|
|
|
"github.com/zu1k/nali/pkg/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Ip2Region struct {
|
2022-06-27 17:03:09 +08:00
|
|
|
seacher *xdb.Searcher
|
|
|
|
db_old *ip2region.Ip2Region
|
2022-02-20 11:28:25 +08:00
|
|
|
}
|
|
|
|
|
2022-03-02 12:34:11 +08:00
|
|
|
func NewIp2Region(filePath string) (*Ip2Region, error) {
|
2022-02-20 11:28:25 +08:00
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
log.Println("文件不存在,尝试从网络获取最新 ip2region 库")
|
|
|
|
_, err = Download(filePath)
|
|
|
|
if err != nil {
|
2022-03-02 12:34:11 +08:00
|
|
|
return nil, err
|
2022-02-20 11:28:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 17:03:09 +08:00
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(filePath, ".xdb"):
|
|
|
|
f, err := os.OpenFile(filePath, os.O_RDONLY, 0400)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-02-20 11:28:25 +08:00
|
|
|
|
2022-06-27 17:03:09 +08:00
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
searcher, err := xdb.NewWithBuffer(data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("无法解析 ip2region xdb 数据库: %s\n", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Ip2Region{
|
|
|
|
seacher: searcher,
|
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
region, err := ip2region.New(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Ip2Region{
|
|
|
|
db_old: region,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-02-20 11:28:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db Ip2Region) Find(query string, params ...string) (result fmt.Stringer, err error) {
|
2022-06-27 17:03:09 +08:00
|
|
|
if db.seacher != nil {
|
|
|
|
res, err := db.seacher.SearchByStr(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return common.Result{
|
|
|
|
Country: strings.ReplaceAll(res, "|0", ""),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
} else if db.db_old != nil {
|
|
|
|
ip, err := db.db_old.MemorySearch(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-20 11:28:25 +08:00
|
|
|
|
2022-06-27 17:03:09 +08:00
|
|
|
area := ""
|
|
|
|
if ip.Province != "0" {
|
|
|
|
area = ip.Province
|
|
|
|
}
|
|
|
|
if ip.City != "0" && strings.EqualFold(ip.City, ip.Province) {
|
|
|
|
area = area + " " + ip.Province
|
|
|
|
}
|
|
|
|
if ip.ISP != "0" {
|
|
|
|
area = area + " " + ip.ISP
|
|
|
|
}
|
2022-02-20 11:28:25 +08:00
|
|
|
|
2022-06-27 17:03:09 +08:00
|
|
|
return common.Result{
|
|
|
|
Country: ip.Country,
|
|
|
|
Area: area,
|
|
|
|
}, nil
|
2022-02-20 11:28:25 +08:00
|
|
|
}
|
2022-06-27 17:03:09 +08:00
|
|
|
|
|
|
|
return nil, errors.New("ip2region 未初始化")
|
2022-02-20 11:28:25 +08:00
|
|
|
}
|