Codebase list golang-github-oschwald-geoip2-golang / 3e490e8
Import upstream version 1.5.0+git20210604.1.f8b0583 Debian Janitor 2 years ago
12 changed file(s) with 223 addition(s) and 90 deletion(s). Raw diff Collapse all Expand all
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
44 [linters]
55 disable-all = true
66 enable = [
7 "bodyclose",
78 "deadcode",
89 "depguard",
910 "errcheck",
11 "exportloopref",
1012 "goconst",
1113 "gocyclo",
1214 "gocritic",
13 "gofmt",
15 "gofumpt",
1416 "golint",
1517 "gosec",
1618 "gosimple",
19 "govet",
1720 "ineffassign",
1821 "maligned",
1922 "misspell",
2023 "nakedret",
24 "noctx",
25 "nolintlint",
26 "sqlclosecheck",
2127 "staticcheck",
2228 "structcheck",
29 "stylecheck",
2330 "typecheck",
2431 "unconvert",
2532 "unparam",
33 "unused",
2634 "varcheck",
27 "vet",
2835 "vetshadow",
2936 ]
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
-48
.travis.yml less more
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
00 # GeoIP2 Reader for Go #
11
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)
43
54 This library reads MaxMind [GeoLite2](http://dev.maxmind.com/geoip/geoip2/geolite2/)
65 and [GeoIP2](http://www.maxmind.com/en/geolocation_landing) databases.
109 All data for the database record is decoded using this library. If you only
1110 need several fields, you may get superior performance by using maxminddb's
1211 `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)
1413 in the maxminddb repository for an example of this.)
1514
1615 ## Installation ##
1010 func Example() {
1111 db, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
1212 if err != nil {
13 log.Fatal(err)
13 log.Panic(err)
1414 }
1515 defer db.Close()
1616 // If you are using strings that may be invalid, check that ip is not nil
1717 ip := net.ParseIP("81.2.69.142")
1818 record, err := db.City(ip)
1919 if err != nil {
20 log.Fatal(err)
20 log.Panic(err)
2121 }
2222 fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
2323 fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])
22 go 1.9
33
44 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
77 )
11 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22 github.com/oschwald/maxminddb-golang v1.6.0 h1:KAJSjdHQ8Kv45nFIbtoLGrGWqHFajOIm7skTyz/+Dls=
33 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=
48 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
59 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10 github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
611 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
712 github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
813 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=
923 golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g=
1024 golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1125 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1226 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1327 gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
1428 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=
6565 Names map[string]string `maxminddb:"names"`
6666 } `maxminddb:"subdivisions"`
6767 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 StaticIPScore float64 `maxminddb:"static_ip_score"`
77 Organization string `maxminddb:"organization"`
78 UserType string `maxminddb:"user_type"`
7879 } `maxminddb:"traits"`
7980 }
8081
166167 // The AnonymousIP struct corresponds to the data in the GeoIP2
167168 // Anonymous IP database.
168169 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"`
170 IsAnonymous bool `maxminddb:"is_anonymous"`
171 IsAnonymousVPN bool `maxminddb:"is_anonymous_vpn"`
172 IsHostingProvider bool `maxminddb:"is_hosting_provider"`
173 IsPublicProxy bool `maxminddb:"is_public_proxy"`
174 IsResidentialProxy bool `maxminddb:"is_residential_proxy"`
175 IsTorExitNode bool `maxminddb:"is_tor_exit_node"`
174176 }
175177
176178 // The ASN struct corresponds to the data in the GeoLite2 ASN database.
256258
257259 // FromBytes takes a byte slice corresponding to a GeoIP2/GeoLite2 database
258260 // 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
261 // used directly; any modification of it after opening the database will result
260262 // in errors while reading from the database.
261263 func FromBytes(bytes []byte) (*Reader, error) {
262264 reader, err := maxminddb.FromBytes(bytes)
271273 switch reader.Metadata.DatabaseType {
272274 case "GeoIP2-Anonymous-IP":
273275 return isAnonymousIP, nil
274 case "GeoLite2-ASN":
276 case "DBIP-ASN-Lite (compat=GeoLite2-ASN)",
277 "GeoLite2-ASN":
275278 return isASN, nil
276279 // We allow City lookups on Country for back compat
277280 case "DBIP-City-Lite",
278 "DBIP-City",
279281 "DBIP-Country-Lite",
280282 "DBIP-Country",
283 "DBIP-Location (compat=City)",
281284 "GeoLite2-City",
282285 "GeoIP2-City",
283286 "GeoIP2-City-Africa",
293296 return isConnectionType, nil
294297 case "GeoIP2-Domain":
295298 return isDomain, nil
296 case "DBIP-Location-ISP (compat=Enterprise)",
299 case "DBIP-ISP (compat=Enterprise)",
300 "DBIP-Location-ISP (compat=Enterprise)",
297301 "GeoIP2-Enterprise":
298302 return isEnterprise | isCity | isCountry, nil
299303 case "GeoIP2-ISP",
6464 )
6565
6666 assert.Equal(t, uint(2635167), record.Country.GeoNameID)
67 assert.True(t, record.Country.IsInEuropeanUnion)
67 assert.False(t, record.Country.IsInEuropeanUnion)
6868 assert.Equal(t, "GB", record.Country.IsoCode)
6969 assert.Equal(t,
7070 map[string]string{
136136 record, err := reader.AnonymousIP(net.ParseIP("1.2.0.0"))
137137 assert.Nil(t, err)
138138
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)
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)
145146 }
146147
147148 func TestASN(t *testing.T) {
178179 record, err := reader.Country(net.ParseIP("81.2.69.160"))
179180 assert.Nil(t, err)
180181
181 assert.True(t, record.Country.IsInEuropeanUnion)
182 assert.False(t, record.Country.IsInEuropeanUnion)
182183 assert.False(t, record.RegisteredCountry.IsInEuropeanUnion)
183184 assert.False(t, record.RepresentedCountry.IsInEuropeanUnion)
184185 }
208209 assert.Equal(t, "FairPoint Communications", record.Traits.AutonomousSystemOrganization)
209210 assert.Equal(t, "Cable/DSL", record.Traits.ConnectionType)
210211 assert.Equal(t, "frpt.net", record.Traits.Domain)
212 assert.Equal(t, float64(0.34), record.Traits.StaticIPScore)
211213 }
212214
213215 func TestISP(t *testing.T) {
228230 // This ensures the compiler does not optimize away the function call
229231 var cityResult *City
230232
231 func BenchmarkMaxMindDB(b *testing.B) {
233 func BenchmarkCity(b *testing.B) {
232234 db, err := Open("GeoLite2-City.mmdb")
233235 if err != nil {
234236 b.Fatal(err)
239241
240242 var city *City
241243
244 ip := make(net.IP, 4)
242245 for i := 0; i < b.N; i++ {
243 ip := randomIPv4Address(r)
246 randomIPv4Address(r, ip)
244247 city, err = db.City(ip)
245248 if err != nil {
246249 b.Fatal(err)
249252 cityResult = city
250253 }
251254
252 func randomIPv4Address(r *rand.Rand) net.IP {
255 // This ensures the compiler does not optimize away the function call
256 var asnResult *ASN
257
258 func BenchmarkASN(b *testing.B) {
259 db, err := Open("GeoLite2-ASN.mmdb")
260 if err != nil {
261 b.Fatal(err)
262 }
263 defer db.Close()
264
265 r := rand.New(rand.NewSource(0))
266
267 var asn *ASN
268
269 ip := make(net.IP, 4)
270 for i := 0; i < b.N; i++ {
271 randomIPv4Address(r, ip)
272 asn, err = db.ASN(ip)
273 if err != nil {
274 b.Fatal(err)
275 }
276 }
277 asnResult = asn
278 }
279
280 func randomIPv4Address(r *rand.Rand, ip net.IP) {
253281 num := r.Uint32()
254 return []byte{byte(num >> 24), byte(num >> 16), byte(num >> 8),
255 byte(num)}
256 }
282 ip[0] = byte(num >> 24)
283 ip[1] = byte(num >> 16)
284 ip[2] = byte(num >> 8)
285 ip[3] = byte(num)
286 }