From 4f26d94ddda99ba06898e774bdc265e1105ce1f0 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Sun, 24 Sep 2023 13:32:43 -0400 Subject: [PATCH] feat: geolocation Signed-off-by: Chris Gianelloni --- go.mod | 2 ++ go.sum | 4 ++++ main.go | 7 ++++--- utils.go | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d608f33..f5c002f 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/blinklabs-io/nview go 1.20 require ( + github.com/Shivam010/go-freeGeoIP v0.1.1 github.com/blinklabs-io/gouroboros v0.53.0 github.com/gdamore/tcell/v2 v2.6.0 github.com/kelseyhightower/envconfig v1.4.0 @@ -28,6 +29,7 @@ require ( github.com/mattn/go-runewidth v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect + github.com/patrickmn/go-cache v2.1.0+incompatible // 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 diff --git a/go.sum b/go.sum index 8248eda..1a03d0d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +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.53.0 h1:JRq7FJ2HP5Fv+RBREzHAskT74u3Es8Sra9ynW08sumo= github.com/blinklabs-io/gouroboros v0.53.0/go.mod h1:2wCCNNsHNYMT4gQB+bXS0Y99Oeu8+EM96hi7hW22C2w= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -42,6 +44,8 @@ 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/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= diff --git a/main.go b/main.go index 9190037..af87524 100644 --- a/main.go +++ b/main.go @@ -1121,11 +1121,13 @@ func getPeerText(ctx context.Context) string { if err != nil { return fmt.Sprintf(" [red]%s[white]", "Unable to convert port to string!") } + peerLocation := getGeoIP(ctx, peerIP) peerStats.RTTresults = append(peerStats.RTTresults, Peer{ IP: peerIP, Port: peerPort, Direction: peerDIR, RTT: peerRTT, + Location: peerLocation, }) sort.SliceStable(peerStats.RTTresults, func(i, j int) bool { return peerStats.RTTresults[i].RTT < peerStats.RTTresults[j].RTT @@ -1143,7 +1145,6 @@ func getPeerText(ctx context.Context) string { peerStats.PCT4 = float32(peerStats.CNT4) / float32(peerCNTreachable) * 100 peerStats.PCT4items = int(peerStats.PCT4) * granularitySmall / 100 } - // TODO: lookup geoIP data sb.WriteString(fmt.Sprintf(" [yellow]%-46s[white]\n", "Peer analysis done!")) peerAnalysisDate = uint64(time.Now().Unix() - 1) checkPeers = false @@ -1272,8 +1273,7 @@ func getPeerText(ctx context.Context) string { ) } } - // TODO: geolocation - peerLocationFmt := "---" + peerLocationFmt := peer.Location // Set color color := "fuchsia" @@ -1346,6 +1346,7 @@ type Peer struct { IP string RTT int Port int + Location string } func getProcessMetrics(ctx context.Context) (*process.Process, error) { diff --git a/utils.go b/utils.go index 72389e5..15c9033 100644 --- a/utils.go +++ b/utils.go @@ -16,11 +16,14 @@ package main import ( "context" + "fmt" "net" + "net/http" "os/exec" "strings" "time" + geoip "github.com/Shivam010/go-freeGeoIP" "github.com/blinklabs-io/nview/internal/config" ) @@ -62,3 +65,16 @@ func getPublicIP(ctx context.Context) (net.IP, error) { } return nil, nil } + +func getGeoIP(ctx context.Context, address string) string { + client := &geoip.Client{ + Cache: geoip.DefaultCache(), + HttpCli: &http.Client{Timeout: time.Second * 2}, + } + ip := geoip.ParseIP(address) + resp := client.GetGeoInfo(ctx, ip) + if err := resp.Error; err != nil { + return "---" // fmt.Sprintf("%s", resp.Error) + } + return fmt.Sprintf("%s, %s", resp.Info.City, resp.Info.CountryCode) +}