Skip to content

Commit 2d2cad6

Browse files
authored
add dbip databases support (#4)
1 parent 78e6358 commit 2d2cad6

File tree

6 files changed

+123
-20
lines changed

6 files changed

+123
-20
lines changed

.gitignore

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1 @@
1-
testdata/GeoIP2-City.mmdb
2-
testdata/GeoIP2-Connection-Type.mmdb
3-
testdata/GeoIP2-Country.mmdb
4-
testdata/GeoIP2-ISP.mmdb
5-
testdata/GeoLite2-ASN.mmdb
6-
testdata/GeoLite2-City.mmdb
7-
testdata/GeoLite2-Country.mmdb
8-
9-
testdata/GeoIP2-Anonymous-IP-Test.mmdb
10-
testdata/GeoIP2-City-Test.mmdb
11-
testdata/GeoIP2-Connection-Type-Test.mmdb
12-
testdata/GeoIP2-Country-Test.mmdb
13-
testdata/GeoIP2-Domain-Test.mmdb
14-
testdata/GeoIP2-Enterprise-Test.mmdb
15-
testdata/GeoIP2-ISP-Test.mmdb
16-
testdata/GeoLite2-ASN-Test.mmdb
1+
testdata

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,37 @@ connection_type-24 3883234 305 ns/op 32 B/op 2 allocs/o
5858
connection_type_parallel-24 34284831 32.1 ns/op 32 B/op 2 allocs/op
5959
```
6060

61+
## Supported databases types
62+
63+
### Country
64+
- GeoIP2-Country
65+
- GeoLite2-Country
66+
- DBIP-Country
67+
- DBIP-Country-Lite
68+
69+
### City
70+
- GeoIP2-City
71+
- GeoLite2-City
72+
- GeoIP2-Enterprise
73+
- DBIP-City-Lite
74+
75+
### ISP
76+
- GeoIP2-ISP
77+
78+
### ASN
79+
- GeoLite2-ASN
80+
- DBIP-ASN-Lite
81+
- DBIP-ASN-Lite (compat=GeoLite2-ASN)
82+
83+
### Connection Type
84+
- GeoIP2-Connection-Type
85+
86+
### Anonymous IP
87+
- GeoIP2-Anonymous-IP
88+
89+
### Domain
90+
- GeoIP2-Domain
91+
6192
## License
6293

6394
[MIT License](LICENSE).

reader_asn.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ func NewASNReader(buffer []byte) (*ASNReader, error) {
5454
if err != nil {
5555
return nil, err
5656
}
57-
if reader.metadata.DatabaseType != "GeoLite2-ASN" {
57+
if reader.metadata.DatabaseType != "GeoLite2-ASN" &&
58+
reader.metadata.DatabaseType != "DBIP-ASN-Lite" &&
59+
reader.metadata.DatabaseType != "DBIP-ASN-Lite (compat=GeoLite2-ASN)" {
5860
return nil, errors.New("wrong MaxMind DB ASN type: " + reader.metadata.DatabaseType)
5961
}
6062
return &ASNReader{

reader_city.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ func NewCityReader(buffer []byte) (*CityReader, error) {
9090
}
9191
if reader.metadata.DatabaseType != "GeoIP2-City" &&
9292
reader.metadata.DatabaseType != "GeoLite2-City" &&
93-
reader.metadata.DatabaseType != "GeoIP2-Enterprise" {
93+
reader.metadata.DatabaseType != "GeoIP2-Enterprise" &&
94+
reader.metadata.DatabaseType != "DBIP-City-Lite" {
9495
return nil, errors.New("wrong MaxMind DB City type: " + reader.metadata.DatabaseType)
9596
}
9697
return &CityReader{

reader_country.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ func NewCountryReader(buffer []byte) (*CountryReader, error) {
6969
return nil, err
7070
}
7171
if reader.metadata.DatabaseType != "GeoIP2-Country" &&
72-
reader.metadata.DatabaseType != "GeoLite2-Country" {
72+
reader.metadata.DatabaseType != "GeoLite2-Country" &&
73+
reader.metadata.DatabaseType != "DBIP-Country" &&
74+
reader.metadata.DatabaseType != "DBIP-Country-Lite" {
7375
return nil, errors.New("wrong MaxMind DB Country type: " + reader.metadata.DatabaseType)
7476
}
7577
return &CountryReader{

reader_test.go

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Test DB https://github.com/maxmind/MaxMind-DB
1+
// Test DB
2+
// https://github.com/maxmind/MaxMind-DB
3+
// https://db-ip.com/db/lite.php
24
package geoip2
35

46
import (
@@ -406,3 +408,83 @@ func TestASN(t *testing.T) {
406408
t.Fatal()
407409
}
408410
}
411+
412+
func TestDBIPCity(t *testing.T) {
413+
reader, err := NewCityReaderFromFile("testdata/dbip-city-lite.mmdb")
414+
if err != nil {
415+
t.Fatal(err)
416+
}
417+
record, err := reader.Lookup(net.ParseIP("66.30.184.198"))
418+
if err != nil {
419+
t.Fatal(err)
420+
}
421+
if record.City.GeoNameID != 0 {
422+
t.Fatal()
423+
}
424+
if record.City.Names["en"] != "Boston" {
425+
t.Fatal()
426+
}
427+
if record.Location.Latitude != 42.3601 {
428+
t.Fatal()
429+
}
430+
if record.Location.Longitude != -71.0589 {
431+
t.Fatal()
432+
}
433+
if len(record.Subdivisions) != 1 {
434+
t.Fatal()
435+
}
436+
if record.Subdivisions[0].Names["en"] != "Massachusetts" {
437+
t.Fatal()
438+
}
439+
}
440+
441+
func TestDBIPCountry(t *testing.T) {
442+
reader, err := NewCountryReaderFromFile("testdata/dbip-country-lite.mmdb")
443+
if err != nil {
444+
t.Fatal(err)
445+
}
446+
record, err := reader.Lookup(net.ParseIP("66.30.184.198"))
447+
if err != nil {
448+
t.Fatal(err)
449+
}
450+
if record.Continent.GeoNameID != 6255149 {
451+
t.Fatal()
452+
}
453+
if record.Continent.Code != "NA" {
454+
t.Fatal()
455+
}
456+
if record.Continent.Names["en"] != "North America" ||
457+
record.Continent.Names["ru"] != "Северная Америка" {
458+
t.Fatal()
459+
}
460+
if record.Country.GeoNameID != 6252001 {
461+
t.Fatal()
462+
}
463+
if record.Country.ISOCode != "US" {
464+
t.Fatal()
465+
}
466+
if record.Country.Names["fr"] != "États-Unis" ||
467+
record.Country.Names["pt-BR"] != "Estados Unidos" {
468+
t.Fatal()
469+
}
470+
if record.Country.IsInEuropeanUnion {
471+
t.Fatal()
472+
}
473+
}
474+
475+
func TestDBIPASN(t *testing.T) {
476+
reader, err := NewASNReaderFromFile("testdata/dbip-asn-lite.mmdb")
477+
if err != nil {
478+
t.Fatal(err)
479+
}
480+
record, err := reader.Lookup(net.ParseIP("66.30.184.198"))
481+
if err != nil {
482+
t.Fatal(err)
483+
}
484+
if record.AutonomousSystemNumber != 7922 {
485+
t.Fatal()
486+
}
487+
if record.AutonomousSystemOrganization != "Comcast Cable Communications, LLC" {
488+
t.Fatal()
489+
}
490+
}

0 commit comments

Comments
 (0)