Skip to content

Commit

Permalink
Merge pull request #143 from blinklabs-io/feat/maxmind-geolocation
Browse files Browse the repository at this point in the history
feat: maxmind geolocation
  • Loading branch information
wolf31o2 authored Feb 9, 2024
2 parents 66d2b7f + ece1217 commit 2f0db97
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ prometheus:
```

An example configuration is provided at `config.yaml.example`.

## GeoLocation

We embed free GeoLite2 city data created by MaxMind, available
from https://www.maxmind.com and licensed under CC BY-SA 4.0
<https://creativecommons.org/licenses/by-sa/4.0/>
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module github.com/blinklabs-io/nview
go 1.20

require (
github.com/Shivam010/go-freeGeoIP v0.1.1
github.com/blinklabs-io/gouroboros v0.70.1
github.com/gdamore/tcell/v2 v2.7.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/mikioh/tcp v0.0.0-20190314235350-803a9b46060c
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b
github.com/oschwald/geoip2-golang v1.9.0
github.com/prometheus/client_model v0.5.0
github.com/prometheus/common v0.46.0
github.com/rivo/tview v0.0.0-20230406072732-e22ce9588bb4
Expand All @@ -27,7 +27,7 @@ require (
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/oschwald/maxminddb-golang v1.11.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/Shivam010/go-freeGeoIP v0.1.1 h1:C3429ZzKXlltMrpb/7OIgUb2uR8xkAVEIdcslyeLqkE=
github.com/Shivam010/go-freeGeoIP v0.1.1/go.mod h1:wQ0uai0rKfE8rS97cPg9zUVPo4rwEfJcfU5Y8tvj49M=
github.com/blinklabs-io/gouroboros v0.70.1 h1:Jj+d8yoRRQnWjv3qwUvicavyi2AGdhDqCxNVWXhZhb8=
github.com/blinklabs-io/gouroboros v0.70.1/go.mod h1:ecxtryJb+vIeVizQl4nYJr0tVdyIgOTze7R9xeGqFu4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -38,8 +36,10 @@ github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b h1:z78hV3sbSMAUoyUM
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b/go.mod h1:lxPUiZwKoFL8DUUmalo2yJJUCxbPKtm8OKfqr2/FTNU=
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc h1:PTfri+PuQmWDqERdnNMiD9ZejrlswWrCpBEZgWOiTrc=
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc/go.mod h1:cGKTAVKx4SxOuR/czcZ/E2RSJ3sfHs8FpHhQ5CWMf9s=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc=
github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y=
github.com/oschwald/maxminddb-golang v1.11.0 h1:aSXMqYR/EPNjGE8epgqwDay+P30hCBZIveY0WZbAWh0=
github.com/oschwald/maxminddb-golang v1.11.0/go.mod h1:YmVI+H0zh3ySFR3w+oz8PCfglAFj3PuCmui13+P9zDg=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
Binary file added resources/GeoLite2-City.mmdb
Binary file not shown.
1 change: 1 addition & 0 deletions resources/GeoLite2-City.mmdb.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20240206
35 changes: 23 additions & 12 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package main

import (
"context"
_ "embed"
"fmt"
"net"
"net/http"
"os/exec"
"strings"
"time"

geoip "github.com/Shivam010/go-freeGeoIP"
"github.com/oschwald/geoip2-golang"

"github.com/blinklabs-io/nview/internal/config"
)

Expand Down Expand Up @@ -68,18 +69,28 @@ func getPublicIP(ctx context.Context) (net.IP, error) {
return nil, nil
}

// MaxMind database (20240206), available from https://www.maxmind.com
//
//go:embed resources/GeoLite2-City.mmdb
var MaxmindDB []byte

func getGeoIP(ctx context.Context, address string) string {
client := &geoip.Client{
Cache: geoip.DefaultCache(),
HttpCli: &http.Client{Timeout: time.Second * 2},
db, err := geoip2.FromBytes(MaxmindDB)
if err != nil {
return "---"
}
ip := geoip.ParseIP(address)
resp := client.GetGeoInfo(ctx, ip)
if err := resp.Error; err != nil {
return "---" // fmt.Sprintf("%s", resp.Error)
defer db.Close()
ip := net.ParseIP(address)
record, err := db.City(ip)
if err != nil {
return "---"
}
if resp.Info.City == "" {
return resp.Info.CountryCode
if len(record.City.Names["en"]) == 0 {
if len(record.Country.IsoCode) == 0 {
return "---"
} else {
return fmt.Sprintf("%v", record.Country.IsoCode)
}
}
return fmt.Sprintf("%s, %s", resp.Info.City, resp.Info.CountryCode)
return fmt.Sprintf("%v, %v", record.City.Names["en"], record.Country.IsoCode)
}

0 comments on commit 2f0db97

Please sign in to comment.