Skip to content

Commit

Permalink
Merge pull request #83 from blinklabs-io/feat/geolocation
Browse files Browse the repository at this point in the history
feat: geolocation
  • Loading branch information
wolf31o2 authored Oct 1, 2023
2 parents b23e50b + 4f26d94 commit 358cf56
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1272,8 +1273,7 @@ func getPeerText(ctx context.Context) string {
)
}
}
// TODO: geolocation
peerLocationFmt := "---"
peerLocationFmt := peer.Location

// Set color
color := "fuchsia"
Expand Down Expand Up @@ -1346,6 +1346,7 @@ type Peer struct {
IP string
RTT int
Port int
Location string
}

func getProcessMetrics(ctx context.Context) (*process.Process, error) {
Expand Down
16 changes: 16 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}

0 comments on commit 358cf56

Please sign in to comment.