update
continuous-integration/drone/push Build is passing Details

master
李光春 2 years ago
parent 85f39b11f7
commit fcba180161

@ -18,12 +18,12 @@ type AnalyseResult struct {
Isp string `json:"isp,omitempty"` // 运营商 Isp string `json:"isp,omitempty"` // 运营商
} }
func (app *App) Analyse(item string) AnalyseResult { func (c *Client) Analyse(item string) AnalyseResult {
isIp := app.isIpv4OrIpv6(item) isIp := c.isIpv4OrIpv6(item)
switch isIp { switch isIp {
case ipv4: case ipv4:
info := app.V4db.Find(item) info := c.V4db.Find(item)
search, err := app.V4Region.MemorySearch(item) search, err := c.V4Region.MemorySearch(item)
if err != nil { if err != nil {
return AnalyseResult{ return AnalyseResult{
IP: info.IP, IP: info.IP,
@ -40,7 +40,7 @@ func (app *App) Analyse(item string) AnalyseResult {
} }
} }
case ipv6: case ipv6:
info := app.V6db.Find(item) info := c.V6db.Find(item)
return AnalyseResult{ return AnalyseResult{
IP: info.IP, IP: info.IP,
Country: info.Country, Country: info.Country,
@ -55,7 +55,7 @@ func (app *App) Analyse(item string) AnalyseResult {
} }
// CheckIpv4 检查数据是不是IPV4 // CheckIpv4 检查数据是不是IPV4
func (app *App) CheckIpv4(ips string) bool { func (c *Client) CheckIpv4(ips string) bool {
if len(ips) > 3 { if len(ips) > 3 {
return false return false
} }
@ -73,7 +73,7 @@ func (app *App) CheckIpv4(ips string) bool {
} }
// CheckIpv6 检测是不是IPV6 // CheckIpv6 检测是不是IPV6
func (app *App) CheckIpv6(ips string) bool { func (c *Client) CheckIpv6(ips string) bool {
if ips == "" { if ips == "" {
return true return true
} }

@ -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"))
}
}

@ -8,31 +8,34 @@ import (
"strings" "strings"
) )
type App struct { type Client struct {
V4Region ip2region.Ip2Region // IPV4 V4Region ip2region.Ip2Region // IPV4
V4db v4.Pointer // IPV4 V4db v4.Pointer // IPV4
V6db v6.Pointer // IPV6 V6db v6.Pointer // IPV6
} }
func (app *App) InitIPData() { // NewIp 实例化
func NewIp() *Client {
app := &Client{}
v4Num := app.V4db.InitIPV4Data() v4Num := app.V4db.InitIPV4Data()
log.Printf("IPV4 库加载完成 共加载:%d 条 IP 记录\n", v4Num) log.Printf("IPV4 库加载完成 共加载:%d 条 IP 记录\n", v4Num)
v6Num := app.V6db.InitIPV4Data() v6Num := app.V6db.InitIPV4Data()
log.Printf("IPV6 库加载完成 共加载:%d 条 IP 记录\n", v6Num) log.Printf("IPV6 库加载完成 共加载:%d 条 IP 记录\n", v6Num)
return app
} }
func (app *App) Ipv4(ip string) (res v4.Result, resInfo ip2region.IpInfo) { func (c *Client) Ipv4(ip string) (res v4.Result, resInfo ip2region.IpInfo) {
res = app.V4db.Find(ip) res = c.V4db.Find(ip)
resInfo, _ = app.V4Region.MemorySearch(ip) resInfo, _ = c.V4Region.MemorySearch(ip)
return res, resInfo return res, resInfo
} }
func (app *App) Ipv6(ip string) (res v6.Result) { func (c *Client) Ipv6(ip string) (res v6.Result) {
res = app.V6db.Find(ip) res = c.V6db.Find(ip)
return res return res
} }
func (app *App) isIpv4OrIpv6(ip string) string { func (c *Client) isIpv4OrIpv6(ip string) string {
if len(ip) < 7 { if len(ip) < 7 {
return "" return ""
} }
@ -40,7 +43,7 @@ func (app *App) isIpv4OrIpv6(ip string) string {
if len(arrIpv4) == 4 { if len(arrIpv4) == 4 {
//. 判断IPv4 //. 判断IPv4
for _, val := range arrIpv4 { for _, val := range arrIpv4 {
if !app.CheckIpv4(val) { if !c.CheckIpv4(val) {
return "" return ""
} }
} }
@ -50,7 +53,7 @@ func (app *App) isIpv4OrIpv6(ip string) string {
if len(arrIpv6) == 8 { if len(arrIpv6) == 8 {
// 判断Ipv6 // 判断Ipv6
for _, val := range arrIpv6 { for _, val := range arrIpv6 {
if !app.CheckIpv6(val) { if !c.CheckIpv6(val) {
return "Neither" return "Neither"
} }
} }

@ -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"))
}
}
Loading…
Cancel
Save