diff --git a/analyse.go b/analyse.go index 5292860..10fcab7 100644 --- a/analyse.go +++ b/analyse.go @@ -18,12 +18,12 @@ type AnalyseResult struct { Isp string `json:"isp,omitempty"` // 运营商 } -func (app *App) Analyse(item string) AnalyseResult { - isIp := app.isIpv4OrIpv6(item) +func (c *Client) Analyse(item string) AnalyseResult { + isIp := c.isIpv4OrIpv6(item) switch isIp { case ipv4: - info := app.V4db.Find(item) - search, err := app.V4Region.MemorySearch(item) + info := c.V4db.Find(item) + search, err := c.V4Region.MemorySearch(item) if err != nil { return AnalyseResult{ IP: info.IP, @@ -40,7 +40,7 @@ func (app *App) Analyse(item string) AnalyseResult { } } case ipv6: - info := app.V6db.Find(item) + info := c.V6db.Find(item) return AnalyseResult{ IP: info.IP, Country: info.Country, @@ -55,7 +55,7 @@ func (app *App) Analyse(item string) AnalyseResult { } // CheckIpv4 检查数据是不是IPV4 -func (app *App) CheckIpv4(ips string) bool { +func (c *Client) CheckIpv4(ips string) bool { if len(ips) > 3 { return false } @@ -73,7 +73,7 @@ func (app *App) CheckIpv4(ips string) bool { } // CheckIpv6 检测是不是IPV6 -func (app *App) CheckIpv6(ips string) bool { +func (c *Client) CheckIpv6(ips string) bool { if ips == "" { return true } diff --git a/app_test.go b/app_test.go deleted file mode 100644 index 9197542..0000000 --- a/app_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package goip - -import ( - "net" - "testing" -) - -var app App - -func TestIp(t *testing.T) { - app.InitIPData() - t.Logf("%+v", net.ParseIP("61.241.55.180").To4()) - t.Logf("%+v", net.ParseIP("240e:3b4:38e4:3295:7093:af6c:e545:f2e9").To16()) - t.Logf("%+v", app.V4db.Find("61.241.55.180")) - t.Logf("%+v", app.V4db.Find("116.7.98.130")) - t.Logf("%+v", app.V6db.Find("240e:3b4:38e4:3295:7093:af6c:e545:f2e9")) - t.Log(app.V4Region.MemorySearch("61.241.55.180")) -} - -func BenchmarkIpv4(b *testing.B) { - app.InitIPData() - for i := 0; i < b.N; i++ { - b.Log(app.V4db.Find("61.241.55.180")) - } -} - -func BenchmarkIpv5(b *testing.B) { - app.InitIPData() - for i := 0; i < b.N; i++ { - b.Log(app.V6db.Find("240e:3b4:38e4:3295:7093:af6c:e545:f2e9")) - } -} diff --git a/app.go b/goip.go similarity index 67% rename from app.go rename to goip.go index 0eb23c6..818d629 100644 --- a/app.go +++ b/goip.go @@ -8,31 +8,34 @@ import ( "strings" ) -type App struct { +type Client struct { V4Region ip2region.Ip2Region // IPV4 V4db v4.Pointer // IPV4 V6db v6.Pointer // IPV6 } -func (app *App) InitIPData() { +// NewIp 实例化 +func NewIp() *Client { + app := &Client{} v4Num := app.V4db.InitIPV4Data() log.Printf("IPV4 库加载完成 共加载:%d 条 IP 记录\n", v4Num) v6Num := app.V6db.InitIPV4Data() log.Printf("IPV6 库加载完成 共加载:%d 条 IP 记录\n", v6Num) + return app } -func (app *App) Ipv4(ip string) (res v4.Result, resInfo ip2region.IpInfo) { - res = app.V4db.Find(ip) - resInfo, _ = app.V4Region.MemorySearch(ip) +func (c *Client) Ipv4(ip string) (res v4.Result, resInfo ip2region.IpInfo) { + res = c.V4db.Find(ip) + resInfo, _ = c.V4Region.MemorySearch(ip) return res, resInfo } -func (app *App) Ipv6(ip string) (res v6.Result) { - res = app.V6db.Find(ip) +func (c *Client) Ipv6(ip string) (res v6.Result) { + res = c.V6db.Find(ip) return res } -func (app *App) isIpv4OrIpv6(ip string) string { +func (c *Client) isIpv4OrIpv6(ip string) string { if len(ip) < 7 { return "" } @@ -40,7 +43,7 @@ func (app *App) isIpv4OrIpv6(ip string) string { if len(arrIpv4) == 4 { //. 判断IPv4 for _, val := range arrIpv4 { - if !app.CheckIpv4(val) { + if !c.CheckIpv4(val) { return "" } } @@ -50,7 +53,7 @@ func (app *App) isIpv4OrIpv6(ip string) string { if len(arrIpv6) == 8 { // 判断Ipv6 for _, val := range arrIpv6 { - if !app.CheckIpv6(val) { + if !c.CheckIpv6(val) { return "Neither" } } diff --git a/goip_test.go b/goip_test.go new file mode 100644 index 0000000..3ef3a44 --- /dev/null +++ b/goip_test.go @@ -0,0 +1,34 @@ +package goip + +import ( + "net" + "testing" +) + +func lod() *Client { + return NewIp() +} + +func TestIp(t *testing.T) { + client := lod() + t.Logf("%+v", net.ParseIP("61.241.55.180").To4()) + t.Logf("%+v", net.ParseIP("240e:3b4:38e4:3295:7093:af6c:e545:f2e9").To16()) + t.Logf("%+v", client.V4db.Find("61.241.55.180")) + t.Logf("%+v", client.V4db.Find("116.7.98.130")) + t.Logf("%+v", client.V6db.Find("240e:3b4:38e4:3295:7093:af6c:e545:f2e9")) + t.Log(client.V4Region.MemorySearch("61.241.55.180")) +} + +func BenchmarkIpv4(b *testing.B) { + client := lod() + for i := 0; i < b.N; i++ { + b.Log(client.V4db.Find("61.241.55.180")) + } +} + +func BenchmarkIpv5(b *testing.B) { + client := lod() + for i := 0; i < b.N; i++ { + b.Log(client.V6db.Find("240e:3b4:38e4:3295:7093:af6c:e545:f2e9")) + } +}