-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zu1k <[email protected]>
- Loading branch information
zu1k
committed
Feb 20, 2022
1 parent
89baf98
commit 2179515
Showing
6 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ip2region | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/lionsoul2014/ip2region/binding/golang/ip2region" | ||
|
||
"github.com/zu1k/nali/pkg/common" | ||
) | ||
|
||
type Ip2Region struct { | ||
db *ip2region.Ip2Region | ||
} | ||
|
||
func NewIp2Region(filePath string) Ip2Region { | ||
_, err := os.Stat(filePath) | ||
if err != nil && os.IsNotExist(err) { | ||
log.Println("文件不存在,尝试从网络获取最新 ip2region 库") | ||
_, err = Download(filePath) | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
region, err := ip2region.New(filePath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return Ip2Region{ | ||
db: region, | ||
} | ||
} | ||
|
||
func (db Ip2Region) Find(query string, params ...string) (result fmt.Stringer, err error) { | ||
ip, err := db.db.MemorySearch(query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fmt.Println(ip) | ||
|
||
result = common.Result{ | ||
Country: ip.Country, | ||
Area: ip.Province, | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ip2region | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/zu1k/nali/pkg/common" | ||
) | ||
|
||
func Download(filePath string) (data []byte, err error) { | ||
data, err = getData() | ||
if err != nil { | ||
log.Printf("CDN数据库下载失败,请手动下载解压后保存到本地: %s \n", filePath) | ||
log.Println("下载链接:", githubUrl) | ||
return | ||
} | ||
|
||
common.ExistThenRemove(filePath) | ||
if err := ioutil.WriteFile(filePath, data, 0644); err == nil { | ||
log.Printf("已将最新的 ip2region 保存到本地: %s \n", filePath) | ||
} | ||
return | ||
} | ||
|
||
const ( | ||
githubUrl = "https://raw.githubusercontent.com/lionsoul2014/ip2region/master/data/ip2region.db" | ||
jsdelivrUrl = "https://cdn.jsdelivr.net/gh/lionsoul2014/ip2region/data/ip2region.db" | ||
) | ||
|
||
func getData() (data []byte, err error) { | ||
resp, err := http.Get(jsdelivrUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != 200 { | ||
resp, err = http.Get(githubUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return body, nil | ||
} |