88 "os"
99 "strings"
1010
11- "github.com/lionsoul2014/ip2region/binding/golang/ip2region"
1211 "github.com/lionsoul2014/ip2region/binding/golang/xdb"
12+
1313 "github.com/zu1k/nali/pkg/common"
1414 "github.com/zu1k/nali/pkg/download"
1515)
@@ -21,7 +21,6 @@ var DownloadUrls = []string{
2121
2222type Ip2Region struct {
2323 seacher * xdb.Searcher
24- db_old * ip2region.Ip2Region
2524}
2625
2726func NewIp2Region (filePath string ) (* Ip2Region , error ) {
@@ -34,35 +33,24 @@ func NewIp2Region(filePath string) (*Ip2Region, error) {
3433 }
3534 }
3635
37- switch {
38- case strings .HasSuffix (filePath , ".xdb" ):
39- f , err := os .OpenFile (filePath , os .O_RDONLY , 0400 )
40- if err != nil {
41- return nil , err
42- }
43- defer f .Close ()
36+ f , err := os .OpenFile (filePath , os .O_RDONLY , 0400 )
37+ if err != nil {
38+ return nil , err
39+ }
40+ defer f .Close ()
4441
45- data , err := ioutil .ReadAll (f )
46- if err != nil {
47- return nil , err
48- }
49- searcher , err := xdb .NewWithBuffer (data )
50- if err != nil {
51- fmt .Printf ("无法解析 ip2region xdb 数据库: %s\n " , err )
52- return nil , err
53- }
54- return & Ip2Region {
55- seacher : searcher ,
56- }, nil
57- default :
58- region , err := ip2region .New (filePath )
59- if err != nil {
60- return nil , err
61- }
62- return & Ip2Region {
63- db_old : region ,
64- }, nil
42+ data , err := ioutil .ReadAll (f )
43+ if err != nil {
44+ return nil , err
45+ }
46+ searcher , err := xdb .NewWithBuffer (data )
47+ if err != nil {
48+ fmt .Printf ("无法解析 ip2region xdb 数据库: %s\n " , err )
49+ return nil , err
6550 }
51+ return & Ip2Region {
52+ seacher : searcher ,
53+ }, nil
6654}
6755
6856func (db Ip2Region ) Find (query string , params ... string ) (result fmt.Stringer , err error ) {
@@ -75,27 +63,6 @@ func (db Ip2Region) Find(query string, params ...string) (result fmt.Stringer, e
7563 Country : strings .ReplaceAll (res , "|0" , "" ),
7664 }, nil
7765 }
78- } else if db .db_old != nil {
79- ip , err := db .db_old .MemorySearch (query )
80- if err != nil {
81- return nil , err
82- }
83-
84- area := ""
85- if ip .Province != "0" {
86- area = ip .Province
87- }
88- if ip .City != "0" && strings .EqualFold (ip .City , ip .Province ) {
89- area = area + " " + ip .Province
90- }
91- if ip .ISP != "0" {
92- area = area + " " + ip .ISP
93- }
94-
95- return common.Result {
96- Country : ip .Country ,
97- Area : area ,
98- }, nil
9966 }
10067
10168 return nil , errors .New ("ip2region 未初始化" )
0 commit comments