diff --git a/cmd/echoip/main.go b/cmd/echoip/main.go index f3d4c94..e66ca70 100644 --- a/cmd/echoip/main.go +++ b/cmd/echoip/main.go @@ -10,6 +10,9 @@ import ( "github.com/mpolden/echoip/http" "github.com/mpolden/echoip/iputil" "github.com/mpolden/echoip/iputil/geo" + "github.com/mpolden/echoip/iputil/ipstack" + parser "github.com/mpolden/echoip/iputil/paser" + ipstackApi "github.com/qioalice/ipstack" ) type multiValueFlag []string @@ -29,6 +32,9 @@ func init() { } func main() { + var ipstackApiKey string + service := flag.String("d", "geoip", "Which database to use, 'ipstack' or 'geoip'") + flag.StringVar(&ipstackApiKey, "S", "", "IP Stack API Key") countryFile := flag.String("f", "", "Path to GeoIP country database") cityFile := flag.String("c", "", "Path to GeoIP city database") asnFile := flag.String("a", "", "Path to GeoIP ASN database") @@ -42,17 +48,31 @@ func main() { var headers multiValueFlag flag.Var(&headers, "H", "Header to trust for remote IP, if present (e.g. X-Real-IP)") flag.Parse() + if len(flag.Args()) != 0 { flag.Usage() return } - r, err := geo.Open(*countryFile, *cityFile, *asnFile) - if err != nil { - log.Fatal(err) + var parser parser.Parser + if *service == "geoip" { + geo, err := geo.Open(*countryFile, *cityFile, *asnFile) + if err != nil { + log.Fatal(err) + } + parser = &geo } + + if *service == "ipstack" { + if err := ipstackApi.Init(ipstackApiKey); err != nil { + log.Fatal(err) + } + ips := ipstack.IPStack{} + parser = &ips + } + cache := http.NewCache(*cacheSize) - server := http.New(r, cache, *profile) + server := http.New(parser, cache, *profile) server.IPHeaders = headers if _, err := os.Stat(*template); err == nil { server.Template = *template diff --git a/go.mod b/go.mod index 040fac8..55d4af8 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.13 require ( github.com/oschwald/geoip2-golang v1.5.0 + github.com/qioalice/ipstack v1.0.1 // indirect golang.org/x/sys v0.0.0-20210223212115-eede4237b368 // indirect ) diff --git a/go.sum b/go.sum index f00e6ee..e79f682 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/oschwald/maxminddb-golang v1.8.0 h1:Uh/DSnGoxsyp/KYbY1AuP0tYEwfs0sCph github.com/oschwald/maxminddb-golang v1.8.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/qioalice/ipstack v1.0.1 h1:Ync2O+tR2AH9/TzTg4aeJxd9c1Xz2CeH1joECwnhvms= +github.com/qioalice/ipstack v1.0.1/go.mod h1:6eB9LdNCUdUoOsfDB8Pn2GpmD2I+f2k3yR30ceuf/rY= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= diff --git a/html/index.html b/html/index.html index 9557084..8041592 100644 --- a/html/index.html +++ b/html/index.html @@ -168,13 +168,18 @@ {{ end }} {{ end }} - {{ if .Country }} + {{ if .Country }} {{ if .UsingGeoIP }}

This information is provided from the GeoLite2 database created by MaxMind, available from www.maxmind.com

- {{ end }} {{ if .Latitude }} + {{ end}} {{ if .UsingIPStack }} +

+ This information is provided by + www.ipstack.com +

+ {{ end}} {{ end }} {{ if .Latitude }}

Map