Skip to content

Commit

Permalink
Support Enterprise database. Closes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed May 9, 2019
1 parent 42d566f commit 347ca45
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
77 changes: 77 additions & 0 deletions reader.go
Expand Up @@ -14,6 +14,71 @@ import (
"github.com/oschwald/maxminddb-golang"
)

// The Enterprise struct corresponds to the data in the GeoIP2 Enterprise
// database.
type Enterprise struct {
City struct {
Confidence uint8 `maxminddb:"confidence"`
GeoNameID uint `maxminddb:"geoname_id"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"city"`
Continent struct {
Code string `maxminddb:"code"`
GeoNameID uint `maxminddb:"geoname_id"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"continent"`
Country struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
Confidence uint8 `maxminddb:"confidence"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
} `maxminddb:"country"`
Location struct {
AccuracyRadius uint16 `maxminddb:"accuracy_radius"`
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
MetroCode uint `maxminddb:"metro_code"`
TimeZone string `maxminddb:"time_zone"`
} `maxminddb:"location"`
Postal struct {
Code string `maxminddb:"code"`
Confidence uint8 `maxminddb:"confidence"`
} `maxminddb:"postal"`
RegisteredCountry struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
Confidence uint8 `maxminddb:"confidence"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
} `maxminddb:"registered_country"`
RepresentedCountry struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
Type string `maxminddb:"type"`
} `maxminddb:"represented_country"`
Subdivisions []struct {
Confidence uint8 `maxminddb:"confidence"`
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"subdivisions"`
Traits struct {
AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
ConnectionType string `maxminddb:"connection_type"`
Domain string `maxminddb:"domain"`
IsAnonymousProxy bool `maxminddb:"is_anonymous_proxy"`
IsLegitimateProxy bool `maxminddb:"is_legitimate_proxy"`
IsSatelliteProvider bool `maxminddb:"is_satellite_provider"`
ISP string `maxminddb:"isp"`
Organization string `maxminddb:"organization"`
UserType string `maxminddb:"user_type"`
} `maxminddb:"traits"`
}

// The City struct corresponds to the data in the GeoIP2/GeoLite2 City
// databases.
type City struct {
Expand Down Expand Up @@ -234,6 +299,18 @@ func getDBType(reader *maxminddb.Reader) (databaseType, error) {
}
}

// Enterprise takes an IP address as a net.IP struct and returns an Enterprise
// struct and/or an error. This is intended to be used with the GeoIP2
// Enterprise database.
func (r *Reader) Enterprise(ipAddress net.IP) (*Enterprise, error) {
if isEnterprise&r.databaseType == 0 {
return nil, InvalidMethodError{"Enterprise", r.Metadata().DatabaseType}
}
var enterprise Enterprise
err := r.mmdbReader.Lookup(ipAddress, &enterprise)
return &enterprise, err
}

// City takes an IP address as a net.IP struct and returns a City struct
// and/or an error. Although this can be used with other databases, this
// method generally should be used with the GeoIP2 or GeoLite2 City databases.
Expand Down
18 changes: 18 additions & 0 deletions reader_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReader(t *testing.T) {
Expand Down Expand Up @@ -193,6 +194,23 @@ func TestDomain(t *testing.T) {
assert.Equal(t, "maxmind.com", record.Domain)
}

func TestEnterprise(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-Enterprise-Test.mmdb")
require.Nil(t, err)

defer reader.Close()

record, err := reader.Enterprise(net.ParseIP("74.209.24.0"))
require.Nil(t, err)

assert.Equal(t, uint8(11), record.City.Confidence)

assert.Equal(t, uint(14671), record.Traits.AutonomousSystemNumber)
assert.Equal(t, "FairPoint Communications", record.Traits.AutonomousSystemOrganization)
assert.Equal(t, "Cable/DSL", record.Traits.ConnectionType)
assert.Equal(t, "frpt.net", record.Traits.Domain)
}

func TestISP(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-ISP-Test.mmdb")
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion test-data
Submodule test-data updated 34 files
+1 −0 .gitignore
+19 −7 MaxMind-DB-spec.md
+8 −0 source-data/GeoIP2-ISP-Test.json
+195 −0 source-data/GeoIP2-Precision-Enterprise-Test.json
+ test-data/GeoIP2-Anonymous-IP-Test.mmdb
+ test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb
+ test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb
+ test-data/GeoIP2-City-Test.mmdb
+ test-data/GeoIP2-Connection-Type-Test.mmdb
+ test-data/GeoIP2-Country-Test.mmdb
+ test-data/GeoIP2-DensityIncome-Test.mmdb
+ test-data/GeoIP2-Domain-Test.mmdb
+ test-data/GeoIP2-Enterprise-Test.mmdb
+ test-data/GeoIP2-ISP-Test.mmdb
+ test-data/GeoIP2-Precision-Enterprise-Test.mmdb
+ test-data/GeoIP2-User-Count-Test.mmdb
+ test-data/GeoLite2-ASN-Test.mmdb
+ test-data/MaxMind-DB-no-ipv4-search-tree.mmdb
+ test-data/MaxMind-DB-string-value-entries.mmdb
+ test-data/MaxMind-DB-test-broken-pointers-24.mmdb
+ test-data/MaxMind-DB-test-broken-search-tree-24.mmdb
+ test-data/MaxMind-DB-test-decoder.mmdb
+ test-data/MaxMind-DB-test-ipv4-24.mmdb
+ test-data/MaxMind-DB-test-ipv4-28.mmdb
+ test-data/MaxMind-DB-test-ipv4-32.mmdb
+ test-data/MaxMind-DB-test-ipv6-24.mmdb
+ test-data/MaxMind-DB-test-ipv6-28.mmdb
+ test-data/MaxMind-DB-test-ipv6-32.mmdb
+ test-data/MaxMind-DB-test-metadata-pointers.mmdb
+ test-data/MaxMind-DB-test-mixed-24.mmdb
+ test-data/MaxMind-DB-test-mixed-28.mmdb
+ test-data/MaxMind-DB-test-mixed-32.mmdb
+ test-data/MaxMind-DB-test-nested.mmdb
+19 −1 test-data/write-test-data.pl

0 comments on commit 347ca45

Please sign in to comment.