New upstream release.
Debian Janitor
2 years ago
0 | version: 2 | |
1 | updates: | |
2 | - package-ecosystem: gomod | |
3 | directory: "/" | |
4 | schedule: | |
5 | interval: daily | |
6 | time: "13:00" | |
7 | open-pull-requests-limit: 10 |
0 | name: "Code scanning - action" | |
1 | ||
2 | on: | |
3 | push: | |
4 | pull_request: | |
5 | schedule: | |
6 | - cron: '0 12 * * 5' | |
7 | ||
8 | jobs: | |
9 | CodeQL-Build: | |
10 | ||
11 | runs-on: ubuntu-latest | |
12 | ||
13 | steps: | |
14 | - name: Checkout repository | |
15 | uses: actions/checkout@v2 | |
16 | with: | |
17 | # We must fetch at least the immediate parents so that if this is | |
18 | # a pull request then we can checkout the head. | |
19 | fetch-depth: 2 | |
20 | ||
21 | # If this run was triggered by a pull request event, then checkout | |
22 | # the head of the pull request instead of the merge commit. | |
23 | - run: git checkout HEAD^2 | |
24 | if: ${{ github.event_name == 'pull_request' }} | |
25 | ||
26 | # Initializes the CodeQL tools for scanning. | |
27 | - name: Initialize CodeQL | |
28 | uses: github/codeql-action/init@v1 | |
29 | # Override language selection by uncommenting this and choosing your languages | |
30 | # with: | |
31 | # languages: go, javascript, csharp, python, cpp, java | |
32 | ||
33 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). | |
34 | # If this step fails, then you should remove it and run the build manually (see below) | |
35 | - name: Autobuild | |
36 | uses: github/codeql-action/autobuild@v1 | |
37 | ||
38 | # âšī¸ Command-line programs to run using the OS shell. | |
39 | # đ https://git.io/JvXDl | |
40 | ||
41 | # âī¸ If the Autobuild fails above, remove it and uncomment the following three lines | |
42 | # and modify them (or add more) to build your code if your project | |
43 | # uses a compiled language | |
44 | ||
45 | #- run: | | |
46 | # make bootstrap | |
47 | # make release | |
48 | ||
49 | - name: Perform CodeQL Analysis | |
50 | uses: github/codeql-action/analyze@v1 |
0 | name: Go | |
1 | ||
2 | on: [push, pull_request] | |
3 | ||
4 | jobs: | |
5 | ||
6 | build: | |
7 | name: Build | |
8 | strategy: | |
9 | matrix: | |
10 | go-version: [1.13.x, 1.14.x, 1.15.x] | |
11 | platform: [ubuntu-latest, macos-latest, windows-latest] | |
12 | runs-on: ${{ matrix.platform }} | |
13 | steps: | |
14 | - name: Set up Go 1.x | |
15 | uses: actions/setup-go@v2 | |
16 | with: | |
17 | go-version: ${{ matrix.go-version }} | |
18 | id: go | |
19 | ||
20 | - name: Check out code into the Go module directory | |
21 | uses: actions/checkout@v2 | |
22 | with: | |
23 | submodules: true | |
24 | ||
25 | - name: Get dependencies | |
26 | run: go get -v -t -d ./... | |
27 | ||
28 | - name: Build | |
29 | run: go build -v . | |
30 | ||
31 | - name: Test | |
32 | run: go test -race -v ./... |
0 | name: golangci-lint | |
1 | ||
2 | on: [push, pull_request] | |
3 | ||
4 | jobs: | |
5 | golangci: | |
6 | name: lint | |
7 | runs-on: ubuntu-latest | |
8 | steps: | |
9 | - uses: actions/checkout@v2 | |
10 | - name: golangci-lint | |
11 | uses: golangci/golangci-lint-action@v2 | |
12 | with: | |
13 | version: v1.37 |
4 | 4 | [linters] |
5 | 5 | disable-all = true |
6 | 6 | enable = [ |
7 | "bodyclose", | |
7 | 8 | "deadcode", |
8 | 9 | "depguard", |
9 | 10 | "errcheck", |
11 | "exportloopref", | |
10 | 12 | "goconst", |
11 | 13 | "gocyclo", |
12 | 14 | "gocritic", |
13 | "gofmt", | |
15 | "gofumpt", | |
14 | 16 | "golint", |
15 | 17 | "gosec", |
16 | 18 | "gosimple", |
19 | "govet", | |
17 | 20 | "ineffassign", |
18 | 21 | "maligned", |
19 | 22 | "misspell", |
20 | 23 | "nakedret", |
24 | "noctx", | |
25 | "nolintlint", | |
26 | "sqlclosecheck", | |
21 | 27 | "staticcheck", |
22 | 28 | "structcheck", |
29 | "stylecheck", | |
23 | 30 | "typecheck", |
24 | 31 | "unconvert", |
25 | 32 | "unparam", |
33 | "unused", | |
26 | 34 | "varcheck", |
27 | "vet", | |
28 | 35 | "vetshadow", |
29 | 36 | ] |
37 | ||
38 | [linters-settings.errcheck] | |
39 | ignore = "Close,fmt:.*" | |
40 | ||
41 | [linters-settings.gofumpt] | |
42 | extra-rules = true | |
43 | ||
44 | [issues] | |
45 | exclude-use-default = false | |
46 | ||
47 | [[issues.exclude-rules]] | |
48 | linters = [ | |
49 | "gosec" | |
50 | ] | |
51 | ||
52 | # G304 - Potential file inclusion via variable (gosec) | |
53 | # G404 - "Use of weak random number generator (math/rand instead of crypto/rand)" | |
54 | # We only use this in tests. | |
55 | text = "G304|G404" |
0 | language: go | |
1 | ||
2 | go: | |
3 | - 1.9.x | |
4 | - 1.10.x | |
5 | - 1.11.x | |
6 | - 1.12.x | |
7 | - 1.13.x | |
8 | - tip | |
9 | ||
10 | os: | |
11 | - linux | |
12 | - linux-ppc64le | |
13 | - osx | |
14 | - windows | |
15 | ||
16 | matrix: | |
17 | allow_failures: | |
18 | - go: tip | |
19 | ||
20 | install: | |
21 | - go get -v -t ./... | |
22 | ||
23 | before_script: | |
24 | - | | |
25 | if [[ $TRAVIS_GO_VERSION == '1.13.x' && $TRAVIS_OS_NAME == 'linux' && $(arch) != 'ppc64le' ]]; then | |
26 | curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin | |
27 | fi | |
28 | ||
29 | script: | |
30 | - | | |
31 | if [ $(arch) == "ppc64le" ]; then | |
32 | go test -cpu 1,4 -v | |
33 | else | |
34 | go test -race -cpu 1,4 -v | |
35 | fi | |
36 | - | | |
37 | if [ $(arch) == "ppc64le" ]; then | |
38 | go test -v -tags appengine | |
39 | else | |
40 | go test -race -v -tags appengine | |
41 | fi | |
42 | - | | |
43 | if [[ $TRAVIS_GO_VERSION == '1.13.x' && $TRAVIS_OS_NAME == 'linux' && $(arch) != 'ppc64le' ]]; then | |
44 | golangci-lint run | |
45 | fi | |
46 | ||
47 | sudo: false |
0 | 0 | # GeoIP2 Reader for Go # |
1 | 1 | |
2 | [![Build Status](https://travis-ci.org/oschwald/geoip2-golang.png?branch=master)](https://travis-ci.org/oschwald/geoip2-golang) | |
3 | [![GoDoc](https://godoc.org/github.com/oschwald/geoip2-golang?status.png)](https://godoc.org/github.com/oschwald/geoip2-golang) | |
2 | [![PkgGoDev](https://pkg.go.dev/badge/github.com/oschwald/geoip2-golang)](https://pkg.go.dev/github.com/oschwald/geoip2-golang) | |
4 | 3 | |
5 | 4 | This library reads MaxMind [GeoLite2](http://dev.maxmind.com/geoip/geoip2/geolite2/) |
6 | 5 | and [GeoIP2](http://www.maxmind.com/en/geolocation_landing) databases. |
10 | 9 | All data for the database record is decoded using this library. If you only |
11 | 10 | need several fields, you may get superior performance by using maxminddb's |
12 | 11 | `Lookup` directly with a result struct that only contains the required fields. |
13 | (See [example_test.go](https://github.com/oschwald/maxminddb-golang/blob/master/example_test.go) | |
12 | (See [example_test.go](https://github.com/oschwald/maxminddb-golang/blob/main/example_test.go) | |
14 | 13 | in the maxminddb repository for an example of this.) |
15 | 14 | |
16 | 15 | ## Installation ## |
0 | golang-github-oschwald-geoip2-golang (1.6.1-1) UNRELEASED; urgency=low | |
1 | ||
2 | * New upstream release. | |
3 | ||
4 | -- Debian Janitor <janitor@jelmer.uk> Sat, 29 Jan 2022 22:08:35 -0000 | |
5 | ||
0 | 6 | golang-github-oschwald-geoip2-golang (1.4.0-1) unstable; urgency=medium |
1 | 7 | |
2 | 8 | [ Cyril Brulebois ] |
10 | 10 | func Example() { |
11 | 11 | db, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb") |
12 | 12 | if err != nil { |
13 | log.Fatal(err) | |
13 | log.Panic(err) | |
14 | 14 | } |
15 | 15 | defer db.Close() |
16 | 16 | // If you are using strings that may be invalid, check that ip is not nil |
17 | 17 | ip := net.ParseIP("81.2.69.142") |
18 | 18 | record, err := db.City(ip) |
19 | 19 | if err != nil { |
20 | log.Fatal(err) | |
20 | log.Panic(err) | |
21 | 21 | } |
22 | 22 | fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"]) |
23 | 23 | fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"]) |
2 | 2 | go 1.9 |
3 | 3 | |
4 | 4 | require ( |
5 | github.com/oschwald/maxminddb-golang v1.6.0 | |
6 | github.com/stretchr/testify v1.4.0 | |
5 | github.com/oschwald/maxminddb-golang v1.8.0 | |
6 | github.com/stretchr/testify v1.7.0 | |
7 | 7 | ) |
1 | 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
2 | 2 | github.com/oschwald/maxminddb-golang v1.6.0 h1:KAJSjdHQ8Kv45nFIbtoLGrGWqHFajOIm7skTyz/+Dls= |
3 | 3 | github.com/oschwald/maxminddb-golang v1.6.0/go.mod h1:DUJFucBg2cvqx42YmDa/+xHvb0elJtOm3o4aFQ/nb/w= |
4 | github.com/oschwald/maxminddb-golang v1.7.0 h1:JmU4Q1WBv5Q+2KZy5xJI+98aUwTIrPPxZUkd5Cwr8Zc= | |
5 | github.com/oschwald/maxminddb-golang v1.7.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis= | |
6 | github.com/oschwald/maxminddb-golang v1.8.0 h1:Uh/DSnGoxsyp/KYbY1AuP0tYEwfs0sCph9p/UMXK/Hk= | |
7 | github.com/oschwald/maxminddb-golang v1.8.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis= | |
4 | 8 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
5 | 9 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
10 | github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= | |
6 | 11 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
7 | 12 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= |
8 | 13 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= |
14 | github.com/stretchr/testify v1.5.0 h1:DMOzIV76tmoDNE9pX6RSN0aDtCYeCg5VueieJaAo1uw= | |
15 | github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= | |
16 | github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= | |
17 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= | |
18 | github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho= | |
19 | github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | |
20 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | |
21 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | |
22 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | |
9 | 23 | golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g= |
10 | 24 | golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
11 | 25 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
12 | 26 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
13 | 27 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= |
14 | 28 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
29 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | |
30 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
65 | 65 | Names map[string]string `maxminddb:"names"` |
66 | 66 | } `maxminddb:"subdivisions"` |
67 | 67 | Traits struct { |
68 | AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"` | |
69 | AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"` | |
70 | ConnectionType string `maxminddb:"connection_type"` | |
71 | Domain string `maxminddb:"domain"` | |
72 | IsAnonymousProxy bool `maxminddb:"is_anonymous_proxy"` | |
73 | IsLegitimateProxy bool `maxminddb:"is_legitimate_proxy"` | |
74 | IsSatelliteProvider bool `maxminddb:"is_satellite_provider"` | |
75 | ISP string `maxminddb:"isp"` | |
76 | Organization string `maxminddb:"organization"` | |
77 | UserType string `maxminddb:"user_type"` | |
68 | AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"` | |
69 | AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"` | |
70 | ConnectionType string `maxminddb:"connection_type"` | |
71 | Domain string `maxminddb:"domain"` | |
72 | IsAnonymousProxy bool `maxminddb:"is_anonymous_proxy"` | |
73 | IsLegitimateProxy bool `maxminddb:"is_legitimate_proxy"` | |
74 | IsSatelliteProvider bool `maxminddb:"is_satellite_provider"` | |
75 | ISP string `maxminddb:"isp"` | |
76 | MobileCountryCode string `maxminddb:"mobile_country_code"` | |
77 | MobileNetworkCode string `maxminddb:"mobile_network_code"` | |
78 | Organization string `maxminddb:"organization"` | |
79 | StaticIPScore float64 `maxminddb:"static_ip_score"` | |
80 | UserType string `maxminddb:"user_type"` | |
78 | 81 | } `maxminddb:"traits"` |
79 | 82 | } |
80 | 83 | |
166 | 169 | // The AnonymousIP struct corresponds to the data in the GeoIP2 |
167 | 170 | // Anonymous IP database. |
168 | 171 | type AnonymousIP struct { |
169 | IsAnonymous bool `maxminddb:"is_anonymous"` | |
170 | IsAnonymousVPN bool `maxminddb:"is_anonymous_vpn"` | |
171 | IsHostingProvider bool `maxminddb:"is_hosting_provider"` | |
172 | IsPublicProxy bool `maxminddb:"is_public_proxy"` | |
173 | IsTorExitNode bool `maxminddb:"is_tor_exit_node"` | |
172 | IsAnonymous bool `maxminddb:"is_anonymous"` | |
173 | IsAnonymousVPN bool `maxminddb:"is_anonymous_vpn"` | |
174 | IsHostingProvider bool `maxminddb:"is_hosting_provider"` | |
175 | IsPublicProxy bool `maxminddb:"is_public_proxy"` | |
176 | IsResidentialProxy bool `maxminddb:"is_residential_proxy"` | |
177 | IsTorExitNode bool `maxminddb:"is_tor_exit_node"` | |
174 | 178 | } |
175 | 179 | |
176 | 180 | // The ASN struct corresponds to the data in the GeoLite2 ASN database. |
195 | 199 | AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"` |
196 | 200 | AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"` |
197 | 201 | ISP string `maxminddb:"isp"` |
202 | MobileCountryCode string `maxminddb:"mobile_country_code"` | |
203 | MobileNetworkCode string `maxminddb:"mobile_network_code"` | |
198 | 204 | Organization string `maxminddb:"organization"` |
199 | 205 | } |
200 | 206 | |
256 | 262 | |
257 | 263 | // FromBytes takes a byte slice corresponding to a GeoIP2/GeoLite2 database |
258 | 264 | // file and returns a Reader struct or an error. Note that the byte slice is |
259 | // use directly; any modification of it after opening the database will result | |
265 | // used directly; any modification of it after opening the database will result | |
260 | 266 | // in errors while reading from the database. |
261 | 267 | func FromBytes(bytes []byte) (*Reader, error) { |
262 | 268 | reader, err := maxminddb.FromBytes(bytes) |
271 | 277 | switch reader.Metadata.DatabaseType { |
272 | 278 | case "GeoIP2-Anonymous-IP": |
273 | 279 | return isAnonymousIP, nil |
274 | case "GeoLite2-ASN": | |
280 | case "DBIP-ASN-Lite (compat=GeoLite2-ASN)", | |
281 | "GeoLite2-ASN": | |
275 | 282 | return isASN, nil |
276 | 283 | // We allow City lookups on Country for back compat |
277 | 284 | case "DBIP-City-Lite", |
278 | "DBIP-City", | |
279 | 285 | "DBIP-Country-Lite", |
280 | 286 | "DBIP-Country", |
287 | "DBIP-Location (compat=City)", | |
281 | 288 | "GeoLite2-City", |
282 | 289 | "GeoIP2-City", |
283 | 290 | "GeoIP2-City-Africa", |
293 | 300 | return isConnectionType, nil |
294 | 301 | case "GeoIP2-Domain": |
295 | 302 | return isDomain, nil |
296 | case "DBIP-Location-ISP (compat=Enterprise)", | |
303 | case "DBIP-ISP (compat=Enterprise)", | |
304 | "DBIP-Location-ISP (compat=Enterprise)", | |
297 | 305 | "GeoIP2-Enterprise": |
298 | 306 | return isEnterprise | isCity | isCountry, nil |
299 | 307 | case "GeoIP2-ISP", |
10 | 10 | |
11 | 11 | func TestReader(t *testing.T) { |
12 | 12 | reader, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb") |
13 | assert.Nil(t, err) | |
13 | require.NoError(t, err) | |
14 | 14 | |
15 | 15 | defer reader.Close() |
16 | 16 | |
17 | 17 | record, err := reader.City(net.ParseIP("81.2.69.160")) |
18 | assert.Nil(t, err) | |
18 | require.NoError(t, err) | |
19 | 19 | |
20 | 20 | m := reader.Metadata() |
21 | 21 | assert.Equal(t, uint(2), m.BinaryFormatMajorVersion) |
64 | 64 | ) |
65 | 65 | |
66 | 66 | assert.Equal(t, uint(2635167), record.Country.GeoNameID) |
67 | assert.True(t, record.Country.IsInEuropeanUnion) | |
67 | assert.False(t, record.Country.IsInEuropeanUnion) | |
68 | 68 | assert.Equal(t, "GB", record.Country.IsoCode) |
69 | 69 | assert.Equal(t, |
70 | 70 | map[string]string{ |
119 | 119 | |
120 | 120 | func TestMetroCode(t *testing.T) { |
121 | 121 | reader, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb") |
122 | assert.Nil(t, err) | |
122 | require.NoError(t, err) | |
123 | 123 | defer reader.Close() |
124 | 124 | |
125 | 125 | record, err := reader.City(net.ParseIP("216.160.83.56")) |
126 | assert.Nil(t, err) | |
126 | require.NoError(t, err) | |
127 | 127 | |
128 | 128 | assert.Equal(t, uint(819), record.Location.MetroCode) |
129 | 129 | } |
130 | 130 | |
131 | 131 | func TestAnonymousIP(t *testing.T) { |
132 | 132 | reader, err := Open("test-data/test-data/GeoIP2-Anonymous-IP-Test.mmdb") |
133 | assert.Nil(t, err) | |
133 | require.NoError(t, err) | |
134 | 134 | defer reader.Close() |
135 | 135 | |
136 | 136 | record, err := reader.AnonymousIP(net.ParseIP("1.2.0.0")) |
137 | assert.Nil(t, err) | |
138 | ||
139 | assert.Equal(t, true, record.IsAnonymous) | |
140 | ||
141 | assert.Equal(t, true, record.IsAnonymousVPN) | |
142 | assert.Equal(t, false, record.IsHostingProvider) | |
143 | assert.Equal(t, false, record.IsPublicProxy) | |
144 | assert.Equal(t, false, record.IsTorExitNode) | |
137 | require.NoError(t, err) | |
138 | ||
139 | assert.True(t, record.IsAnonymous) | |
140 | ||
141 | assert.True(t, record.IsAnonymousVPN) | |
142 | assert.False(t, record.IsHostingProvider) | |
143 | assert.False(t, record.IsPublicProxy) | |
144 | assert.False(t, record.IsTorExitNode) | |
145 | assert.False(t, record.IsResidentialProxy) | |
145 | 146 | } |
146 | 147 | |
147 | 148 | func TestASN(t *testing.T) { |
148 | 149 | reader, err := Open("test-data/test-data/GeoLite2-ASN-Test.mmdb") |
149 | assert.Nil(t, err) | |
150 | require.NoError(t, err) | |
150 | 151 | defer reader.Close() |
151 | 152 | |
152 | 153 | record, err := reader.ASN(net.ParseIP("1.128.0.0")) |
153 | assert.Nil(t, err) | |
154 | require.NoError(t, err) | |
154 | 155 | |
155 | 156 | assert.Equal(t, uint(1221), record.AutonomousSystemNumber) |
156 | 157 | |
159 | 160 | |
160 | 161 | func TestConnectionType(t *testing.T) { |
161 | 162 | reader, err := Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb") |
162 | assert.Nil(t, err) | |
163 | require.NoError(t, err) | |
163 | 164 | |
164 | 165 | defer reader.Close() |
165 | 166 | |
166 | 167 | record, err := reader.ConnectionType(net.ParseIP("1.0.1.0")) |
167 | assert.Nil(t, err) | |
168 | require.NoError(t, err) | |
168 | 169 | |
169 | 170 | assert.Equal(t, "Cable/DSL", record.ConnectionType) |
170 | 171 | } |
171 | 172 | |
172 | 173 | func TestCountry(t *testing.T) { |
173 | 174 | reader, err := Open("test-data/test-data/GeoIP2-Country-Test.mmdb") |
174 | assert.Nil(t, err) | |
175 | require.NoError(t, err) | |
175 | 176 | |
176 | 177 | defer reader.Close() |
177 | 178 | |
178 | 179 | record, err := reader.Country(net.ParseIP("81.2.69.160")) |
179 | assert.Nil(t, err) | |
180 | ||
181 | assert.True(t, record.Country.IsInEuropeanUnion) | |
180 | require.NoError(t, err) | |
181 | ||
182 | assert.False(t, record.Country.IsInEuropeanUnion) | |
182 | 183 | assert.False(t, record.RegisteredCountry.IsInEuropeanUnion) |
183 | 184 | assert.False(t, record.RepresentedCountry.IsInEuropeanUnion) |
184 | 185 | } |
185 | 186 | |
186 | 187 | func TestDomain(t *testing.T) { |
187 | 188 | reader, err := Open("test-data/test-data/GeoIP2-Domain-Test.mmdb") |
188 | assert.Nil(t, err) | |
189 | require.NoError(t, err) | |
189 | 190 | defer reader.Close() |
190 | 191 | |
191 | 192 | record, err := reader.Domain(net.ParseIP("1.2.0.0")) |
192 | assert.Nil(t, err) | |
193 | require.NoError(t, err) | |
193 | 194 | assert.Equal(t, "maxmind.com", record.Domain) |
194 | 195 | } |
195 | 196 | |
208 | 209 | assert.Equal(t, "FairPoint Communications", record.Traits.AutonomousSystemOrganization) |
209 | 210 | assert.Equal(t, "Cable/DSL", record.Traits.ConnectionType) |
210 | 211 | assert.Equal(t, "frpt.net", record.Traits.Domain) |
212 | assert.Equal(t, float64(0.34), record.Traits.StaticIPScore) | |
213 | ||
214 | record, err = reader.Enterprise(net.ParseIP("149.101.100.0")) | |
215 | require.NoError(t, err) | |
216 | ||
217 | assert.Equal(t, uint(6167), record.Traits.AutonomousSystemNumber) | |
218 | ||
219 | assert.Equal(t, "CELLCO-PART", record.Traits.AutonomousSystemOrganization) | |
220 | assert.Equal(t, "Verizon Wireless", record.Traits.ISP) | |
221 | assert.Equal(t, "310", record.Traits.MobileCountryCode) | |
222 | assert.Equal(t, "004", record.Traits.MobileNetworkCode) | |
211 | 223 | } |
212 | 224 | |
213 | 225 | func TestISP(t *testing.T) { |
214 | 226 | reader, err := Open("test-data/test-data/GeoIP2-ISP-Test.mmdb") |
215 | assert.Nil(t, err) | |
216 | defer reader.Close() | |
217 | ||
218 | record, err := reader.ISP(net.ParseIP("1.128.0.0")) | |
219 | assert.Nil(t, err) | |
220 | ||
221 | assert.Equal(t, uint(1221), record.AutonomousSystemNumber) | |
222 | ||
223 | assert.Equal(t, "Telstra Pty Ltd", record.AutonomousSystemOrganization) | |
224 | assert.Equal(t, "Telstra Internet", record.ISP) | |
225 | assert.Equal(t, "Telstra Internet", record.Organization) | |
227 | require.NoError(t, err) | |
228 | defer reader.Close() | |
229 | ||
230 | record, err := reader.ISP(net.ParseIP("149.101.100.0")) | |
231 | require.NoError(t, err) | |
232 | ||
233 | assert.Equal(t, uint(6167), record.AutonomousSystemNumber) | |
234 | ||
235 | assert.Equal(t, "CELLCO-PART", record.AutonomousSystemOrganization) | |
236 | assert.Equal(t, "Verizon Wireless", record.ISP) | |
237 | assert.Equal(t, "310", record.MobileCountryCode) | |
238 | assert.Equal(t, "004", record.MobileNetworkCode) | |
239 | assert.Equal(t, "Verizon Wireless", record.Organization) | |
226 | 240 | } |
227 | 241 | |
228 | 242 | // This ensures the compiler does not optimize away the function call |
229 | 243 | var cityResult *City |
230 | 244 | |
231 | func BenchmarkMaxMindDB(b *testing.B) { | |
245 | func BenchmarkCity(b *testing.B) { | |
232 | 246 | db, err := Open("GeoLite2-City.mmdb") |
233 | 247 | if err != nil { |
234 | 248 | b.Fatal(err) |
239 | 253 | |
240 | 254 | var city *City |
241 | 255 | |
256 | ip := make(net.IP, 4) | |
242 | 257 | for i := 0; i < b.N; i++ { |
243 | ip := randomIPv4Address(r) | |
258 | randomIPv4Address(r, ip) | |
244 | 259 | city, err = db.City(ip) |
245 | 260 | if err != nil { |
246 | 261 | b.Fatal(err) |
249 | 264 | cityResult = city |
250 | 265 | } |
251 | 266 | |
252 | func randomIPv4Address(r *rand.Rand) net.IP { | |
267 | // This ensures the compiler does not optimize away the function call | |
268 | var asnResult *ASN | |
269 | ||
270 | func BenchmarkASN(b *testing.B) { | |
271 | db, err := Open("GeoLite2-ASN.mmdb") | |
272 | if err != nil { | |
273 | b.Fatal(err) | |
274 | } | |
275 | defer db.Close() | |
276 | ||
277 | r := rand.New(rand.NewSource(0)) | |
278 | ||
279 | var asn *ASN | |
280 | ||
281 | ip := make(net.IP, 4) | |
282 | for i := 0; i < b.N; i++ { | |
283 | randomIPv4Address(r, ip) | |
284 | asn, err = db.ASN(ip) | |
285 | if err != nil { | |
286 | b.Fatal(err) | |
287 | } | |
288 | } | |
289 | asnResult = asn | |
290 | } | |
291 | ||
292 | func randomIPv4Address(r *rand.Rand, ip net.IP) { | |
253 | 293 | num := r.Uint32() |
254 | return []byte{byte(num >> 24), byte(num >> 16), byte(num >> 8), | |
255 | byte(num)} | |
256 | } | |
294 | ip[0] = byte(num >> 24) | |
295 | ip[1] = byte(num >> 16) | |
296 | ip[2] = byte(num >> 8) | |
297 | ip[3] = byte(num) | |
298 | } |