Skip to content

Commit 9fe120a

Browse files
committed
Add Connection-Type, Domain, and ISP databases methods
1 parent 5bb6e77 commit 9fe120a

File tree

3 files changed

+105
-7
lines changed

3 files changed

+105
-7
lines changed

reader.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
package geoip2
66

77
import (
8-
"github.com/oschwald/maxminddb-golang"
98
"net"
9+
10+
"github.com/oschwald/maxminddb-golang"
1011
)
1112

1213
// The City structure corresponds to the data in the GeoIP2/GeoLite2 City
@@ -87,6 +88,25 @@ type Country struct {
8788
} `maxminddb:"traits"`
8889
}
8990

91+
// The ConnectionType structure corresponds to the data in the GeoIP2
92+
// Connection-Type database.
93+
type ConnectionType struct {
94+
ConnectionType string `maxminddb:"connection_type"`
95+
}
96+
97+
// The Domain structure corresponds to the data in the GeoIP2 Domain database.
98+
type Domain struct {
99+
Domain string `maxminddb:"domain"`
100+
}
101+
102+
// The ISP structure corresponds to the data in the GeoIP2 ISP database.
103+
type ISP struct {
104+
AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
105+
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
106+
ISP string `maxminddb:"isp"`
107+
Organization string `maxminddb:"organization"`
108+
}
109+
90110
// Reader holds the maxminddb.Reader structure. It should be created
91111
// using the Open function.
92112
type Reader struct {
@@ -127,6 +147,30 @@ func (r *Reader) Country(ipAddress net.IP) (*Country, error) {
127147
return &country, err
128148
}
129149

150+
// ConnectionType takes an IP address as a net.IP struct and returns a
151+
// ConnectionType struct and/or an error
152+
func (r *Reader) ConnectionType(ipAddress net.IP) (*ConnectionType, error) {
153+
var val ConnectionType
154+
err := r.mmdbReader.Lookup(ipAddress, &val)
155+
return &val, err
156+
}
157+
158+
// Domain takes an IP address as a net.IP struct and returns a
159+
// Domain struct and/or an error
160+
func (r *Reader) Domain(ipAddress net.IP) (*Domain, error) {
161+
var val Domain
162+
err := r.mmdbReader.Lookup(ipAddress, &val)
163+
return &val, err
164+
}
165+
166+
// ISP takes an IP address as a net.IP struct and returns a ISP struct and/or
167+
// an error
168+
func (r *Reader) ISP(ipAddress net.IP) (*ISP, error) {
169+
var val ISP
170+
err := r.mmdbReader.Lookup(ipAddress, &val)
171+
return &val, err
172+
}
173+
130174
// Close unmaps the database file from virtual memory and returns the
131175
// resources to the system.
132176
func (r *Reader) Close() {

reader_test.go

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package geoip2
22

33
import (
44
"fmt"
5-
. "launchpad.net/gocheck"
65
"math/rand"
76
"net"
87
"testing"
98
"time"
9+
. "launchpad.net/gocheck"
1010
)
1111

1212
func TestGeoIP2(t *testing.T) { TestingT(t) }
@@ -21,6 +21,7 @@ func (s *MySuite) TestReader(c *C) {
2121
c.Log(err)
2222
c.Fail()
2323
}
24+
defer reader.Close()
2425

2526
record, err := reader.City(net.ParseIP("81.2.69.160"))
2627
if err != nil {
@@ -89,8 +90,6 @@ func (s *MySuite) TestReader(c *C) {
8990
"ru": "США",
9091
"zh-CN": "美国",
9192
})
92-
93-
reader.Close()
9493
}
9594

9695
func (s *MySuite) TestMetroCode(c *C) {
@@ -99,6 +98,7 @@ func (s *MySuite) TestMetroCode(c *C) {
9998
c.Log(err)
10099
c.Fail()
101100
}
101+
defer reader.Close()
102102

103103
record, err := reader.City(net.ParseIP("216.160.83.56"))
104104
if err != nil {
@@ -107,15 +107,70 @@ func (s *MySuite) TestMetroCode(c *C) {
107107
}
108108

109109
c.Assert(record.Location.MetroCode, Equals, uint(819))
110+
}
111+
112+
func (s *MySuite) TestConnectionType(c *C) {
113+
reader, err := Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")
114+
if err != nil {
115+
c.Log(err)
116+
c.Fail()
117+
}
118+
defer reader.Close()
119+
120+
record, err := reader.ConnectionType(net.ParseIP("1.0.1.0"))
121+
if err != nil {
122+
c.Log(err)
123+
c.Fail()
124+
}
125+
c.Assert(record.ConnectionType, Equals, "Cable/DSL")
126+
127+
}
128+
129+
func (s *MySuite) TestDomain(c *C) {
130+
reader, err := Open("test-data/test-data/GeoIP2-Domain-Test.mmdb")
131+
if err != nil {
132+
c.Log(err)
133+
c.Fail()
134+
}
135+
defer reader.Close()
136+
137+
record, err := reader.Domain(net.ParseIP("1.2.0.0"))
138+
if err != nil {
139+
c.Log(err)
140+
c.Fail()
141+
}
142+
c.Assert(record.Domain, Equals, "maxmind.com")
143+
144+
}
145+
146+
func (s *MySuite) TestISP(c *C) {
147+
reader, err := Open("test-data/test-data/GeoIP2-ISP-Test.mmdb")
148+
if err != nil {
149+
c.Log(err)
150+
c.Fail()
151+
}
152+
defer reader.Close()
153+
154+
record, err := reader.ISP(net.ParseIP("1.128.0.0"))
155+
if err != nil {
156+
c.Log(err)
157+
c.Fail()
158+
}
159+
160+
c.Assert(record.AutonomousSystemNumber, Equals, uint(1221))
161+
162+
c.Assert(record.AutonomousSystemOrganization, Equals, "Telstra Pty Ltd")
163+
c.Assert(record.ISP, Equals, "Telstra Internet")
164+
c.Assert(record.Organization, Equals, "Telstra Internet")
110165

111-
reader.Close()
112166
}
113167

114168
func BenchmarkMaxMindDB(b *testing.B) {
115169
db, err := Open("GeoLite2-City.mmdb")
116170
if err != nil {
117171
b.Fatal(err)
118172
}
173+
defer db.Close()
119174

120175
r := rand.New(rand.NewSource(time.Now().UnixNano()))
121176

@@ -128,5 +183,4 @@ func BenchmarkMaxMindDB(b *testing.B) {
128183
b.Fatal(err)
129184
}
130185
}
131-
db.Close()
132186
}

0 commit comments

Comments
 (0)