Skip to content

Commit

Permalink
Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterclaerhout committed May 25, 2020
1 parent 4b6afe3 commit bcd1824
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
22 changes: 18 additions & 4 deletions database.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package geoip

import (
"net"
"sync"

"github.com/oschwald/maxminddb-golang"
Expand Down Expand Up @@ -47,9 +46,12 @@ func (database *Database) Lookup(ipaddress string) (*IPLocation, error) {
}
defer db.Close()

ip := net.ParseIP(ipaddress)
if ip == nil {
return nil, errors.New("Invalid IP address")
ip, private, err := isPrivateIP(ipaddress)
if err != nil {
return nil, err
}
if private {
return nil, nil
}

if err := db.Lookup(ip, &location); err != nil {
Expand Down Expand Up @@ -79,6 +81,9 @@ func (database *Database) CountryCode(ipaddress string) (string, error) {
if err != nil {
return "", err
}
if location == nil {
return "", nil
}
return location.CountryCode(), nil
}

Expand All @@ -88,6 +93,9 @@ func (database *Database) CountryName(ipaddress string) (string, error) {
if err != nil {
return "", err
}
if location == nil {
return "", nil
}
return location.CountryName(), nil
}

Expand All @@ -104,6 +112,9 @@ func (database *Database) RegionName(ipaddress string) (string, error) {
if err != nil {
return "", err
}
if location == nil {
return "", nil
}
return location.RegionName(), nil
}

Expand All @@ -113,5 +124,8 @@ func (database *Database) TimeZone(ipaddress string) (string, error) {
if err != nil {
return "", err
}
if location == nil {
return "", nil
}
return location.TimeZone(), nil
}
23 changes: 23 additions & 0 deletions database_internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package geoip

import (
"errors"
"net"
)

// isPrivateIP returns true if the IP address is an internal one
func isPrivateIP(ipAddress string) (net.IP, bool, error) {

ip := net.ParseIP(ipAddress)
if ip == nil {
return nil, false, errors.New("Invalid IP")
}

_, private24BitBlock, _ := net.ParseCIDR("10.0.0.0/8")
_, private20BitBlock, _ := net.ParseCIDR("172.16.0.0/12")
_, private16BitBlock, _ := net.ParseCIDR("192.168.0.0/16")
private := private24BitBlock.Contains(ip) || private20BitBlock.Contains(ip) || private16BitBlock.Contains(ip)

return ip, private, nil

}
4 changes: 4 additions & 0 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestDatabaseCountryCode(t *testing.T) {
{"5.145.169.67", "ES", false},
{"170.84.87.234", "BR", false},
{"116.209.59.102", "CN", false},
{"192.168.0.1", "", false},
{"invalid", "", true},
{"", "", true},
}
Expand Down Expand Up @@ -128,6 +129,7 @@ func TestDatabaseCountryName(t *testing.T) {
{"5.145.169.67", "Spain", false},
{"170.84.87.234", "Brazil", false},
{"116.209.59.102", "China", false},
{"192.168.0.1", "", false},
{"invalid", "", true},
{"", "", true},
}
Expand Down Expand Up @@ -175,6 +177,7 @@ func TestDatabaseRegionName(t *testing.T) {
{"5.145.169.67", "west-europe", false},
{"170.84.87.234", "south-brazil", false},
{"116.209.59.102", "southeast-asia", false},
{"192.168.0.1", "", false},
{"invalid", "", true},
{"", "", true},
}
Expand Down Expand Up @@ -223,6 +226,7 @@ func TestDatabaseTimeZone(t *testing.T) {
{"5.145.169.67", "Europe/Madrid", false},
{"170.84.87.234", "America/Recife", false},
{"116.209.59.102", "Asia/Shanghai", false},
{"192.168.0.1", "", false},
{"invalid", "", true},
{"", "", true},
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ module github.com/pieterclaerhout/go-geoip/v2
go 1.14

require (
github.com/Flaque/filet v0.0.0-20190209224823-fc4d33cfcf93
github.com/go-chi/chi v4.1.1+incompatible
github.com/joho/godotenv v1.3.0
github.com/markusthoemmes/goautoneg v0.0.0-20190713162725-c6008fefa5b1
github.com/oschwald/maxminddb-golang v1.6.0
github.com/pieterclaerhout/go-log v1.14.0
github.com/pieterclaerhout/go-webserver/v2 v2.0.3
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.2.2 // indirect
github.com/stretchr/testify v1.4.0
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Flaque/filet v0.0.0-20190209224823-fc4d33cfcf93 h1:NnAUCP75PRm8yWE7+MZBIAR6PA9iwsBYEc6ZNYOy+AQ=
github.com/Flaque/filet v0.0.0-20190209224823-fc4d33cfcf93/go.mod h1:TK+jB3mBs+8ZMWhU5BqZKnZWJ1MrLo8etNVg51ueTBo=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
Expand Down Expand Up @@ -40,14 +43,18 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rotisserie/eris v0.4.0 h1:wfZW5hp90Y386s54DoJDK2Th3ycZotiBGM7b5b5aIHI=
github.com/rotisserie/eris v0.4.0/go.mod h1:lODN/gtqebxPHRbCcWeCYOE350FC2M3V/oAPT2wKxAU=
github.com/sanity-io/litter v1.2.0 h1:DGJO0bxH/+C2EukzOSBmAlxmkhVMGqzvcx/rvySYw9M=
github.com/sanity-io/litter v1.2.0/go.mod h1:JF6pZUFgu2Q0sBZ+HSV35P8TVPI1TTzEwyu9FXAw2W4=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tidwall/gjson v1.3.2 h1:+7p3qQFaH3fOMXAJSrdZwGKcOO/lYdGS0HqGhPqDdTI=
github.com/tidwall/gjson v1.3.2/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
Expand Down Expand Up @@ -85,5 +92,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=

0 comments on commit bcd1824

Please sign in to comment.