diff --git a/reader.go b/reader.go index ac5108f..671a50b 100644 --- a/reader.go +++ b/reader.go @@ -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 { @@ -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. diff --git a/reader_test.go b/reader_test.go index 2522e75..41fe8c7 100644 --- a/reader_test.go +++ b/reader_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestReader(t *testing.T) { @@ -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) diff --git a/test-data b/test-data index f6ed981..5d78487 160000 --- a/test-data +++ b/test-data @@ -1 +1 @@ -Subproject commit f6ed981c23b0eb33d7c07568e2177236252afda6 +Subproject commit 5d78487a1ba8df568ff000a0d3bdb22162db5851