diff --git a/go.mod b/go.mod index 6698832..4ea1821 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.13.0 github.com/gorilla/websocket v1.5.0 + github.com/lionsoul2014/ip2region v2.10.0+incompatible github.com/rodaine/table v1.0.1 github.com/stretchr/testify v1.7.1 // indirect github.com/tidwall/gjson v1.14.2 diff --git a/go.sum b/go.sum index 923cb66..c767257 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/lionsoul2014/ip2region v2.10.0+incompatible h1:HpgN+54Korm/I0xXNX6I6owmvAwtPxrcI6cHYqXKtLw= +github.com/lionsoul2014/ip2region v2.10.0+incompatible/go.mod h1:+ZBN7PBoh5gG6/y0ZQ85vJDBe21WnfbRrQQwTfliJJI= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= diff --git a/ipgeo/ip2region.go b/ipgeo/ip2region.go new file mode 100644 index 0000000..41ae0c0 --- /dev/null +++ b/ipgeo/ip2region.go @@ -0,0 +1,77 @@ +package ipgeo + +import ( + "errors" + "fmt" + "io" + "net/http" + "os" + + "github.com/lionsoul2014/ip2region/v1.0/binding/golang/ip2region" +) + +const ( + ipDataBasePath = "./ip2region.db" + defaultDownURL = "1" + originURL = "https://ghproxy.com/?q=https://github.com/bqf9979/ip2region/blob/master/data/ip2region.db?raw=true" +) + +func downloadDataBase() error { + fmt.Println("Downloading DataBase...") + resp, err := http.Get(originURL) + if err != nil { + return err + } + defer resp.Body.Close() + + // Create the file + out, err := os.Create(ipDataBasePath) + if err != nil { + return err + } + defer out.Close() + + // Write the body to file + _, err = io.Copy(out, resp.Body) + return err +} + +func IP2Region(ip string) (*IPGeoData, error) { + if _, err := os.Stat(ipDataBasePath); os.IsNotExist(err) { + if err = downloadDataBase(); err != nil { + panic("Download Failed!") + } + } + region, err := ip2region.New(ipDataBasePath) + if err != nil { + panic("Cannot find ip2region.db") + } + defer region.Close() + info, searchErr := region.MemorySearch(ip) + if searchErr != nil { + return &IPGeoData{}, errors.New("no results") + } + + if info.Country == "0" { + info.Country = "" + } + + if info.Province == "0" { + info.Province = "" + } + + if info.City == "0" { + info.City = "" + } + + if info.ISP == "0" { + info.ISP = "" + } + + return &IPGeoData{ + Owner: info.ISP, + Country: info.Country, + Prov: info.Province, + City: info.City, + }, nil +} diff --git a/ipgeo/ipgeo.go b/ipgeo/ipgeo.go index bdf1a05..6c4fab0 100644 --- a/ipgeo/ipgeo.go +++ b/ipgeo/ipgeo.go @@ -29,6 +29,8 @@ func GetSource(s string) Source { return IPApiCom case "IPINFO": return IPInfo + case "IP2REGION": + return IP2Region default: return LeoIP }