From 4a5b6247ae8758021c55688e8b2fc8ebf7792583 Mon Sep 17 00:00:00 2001 From: John Taylor Date: Mon, 2 Oct 2023 18:13:43 -0400 Subject: [PATCH] run: go fmt; use 'MAIN' for the address portion to use system's primary interface --- cmd.go | 10 +++++++--- docker_build_image.sh | 3 +-- geoip.go | 11 ++++++++--- nics.go | 24 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/cmd.go b/cmd.go index 0cedbb6..83ba531 100644 --- a/cmd.go +++ b/cmd.go @@ -23,17 +23,17 @@ import ( "syscall" "time" + "github.com/alecthomas/kingpin/v2" "github.com/olekukonko/tablewriter" "go.uber.org/zap" "go.uber.org/zap/zapcore" - "github.com/alecthomas/kingpin/v2" ) -const version = "0.7.1" +const version = "0.7.2" var ( list = kingpin.Flag("int", "list local interface IP addresses").Short('i').Bool() - from = kingpin.Flag("from", "from IP address:port").Short('f').String() + from = kingpin.Flag("from", "from IP address:port; use 'MAIN' for the address portion to use system's primary interface").Short('f').String() to = kingpin.Flag("to", "to IP address:port").Short('t').String() examples = kingpin.Flag("examples", "show command line example and then exit").Bool() versionOnly = kingpin.Flag("version", "show version and then exit").Bool() @@ -298,6 +298,10 @@ func main() { os.Exit(1) } + if strings.HasPrefix(*from, "MAIN:") { + *from = strings.Replace(*from, "MAIN", getMainNic(), 1) + } + if len(*loc) > 0 && 0 == *distance { kingpin.FatalUsage("--distance must be used with --loc") os.Exit(1) diff --git a/docker_build_image.sh b/docker_build_image.sh index 788dff2..e61c87d 100755 --- a/docker_build_image.sh +++ b/docker_build_image.sh @@ -69,8 +69,7 @@ if [ $# -eq 0 ] ; then exit 1 fi -TAG=$1 -IMG=gofwd:${TAG} +IMG=$1 BUND="ca-bundle.crt" SC="ssl/certs/" GF="gofwd" diff --git a/geoip.go b/geoip.go index 7116357..1f3a16d 100644 --- a/geoip.go +++ b/geoip.go @@ -28,9 +28,11 @@ type ipInfoResult struct { getIpInfo issues a web query to ipinfo.io The JSON result is converted to an ipInfoResult struct Args: + ip: an IPv4 address Returns: + an ipInfoResult struct containing the information returned by the service */ func getIPInfo(ip string) (ipInfoResult, error) { @@ -122,9 +124,11 @@ func validateLocation(localGeoIP ipInfoResult, remoteGeoIP ipInfoResult, restric latlon2coord converts a string such as "36.0525,-79.107" to a tuple of floats Args: + latlon: a string in "lat, lon" format Returns: + a tuple in (float64, float64) format */ func latlon2coord(latlon string) (float64, float64, error) { @@ -147,9 +151,10 @@ func hsin(theta float64) float64 { } // HaversineDistance returns the distance (in miles) between two points of -// a given longitude and latitude relatively accurately (using a spherical -// approximation of the Earth) through the Haversin Distance Formula for -// great arc distance on a sphere with accuracy for small distances +// +// a given longitude and latitude relatively accurately (using a spherical +// approximation of the Earth) through the Haversin Distance Formula for +// great arc distance on a sphere with accuracy for small distances // // point coordinates are supplied in degrees and converted into rad. in the func // diff --git a/nics.go b/nics.go index 89fe685..ffa2f3a 100644 --- a/nics.go +++ b/nics.go @@ -66,6 +66,30 @@ func extractIPAddrs(ifaceName string, allAddresses []net.Addr, brief bool) ([]st return allIPv4, allIPv6 } +func getMainNic() string { + adapters, err := net.Interfaces() + if err != nil { + return "0.0.0.0" + } + + for _, iface := range adapters { + allAddresses, err := iface.Addrs() + if err != nil { + return "0.0.0.0" + } + + allIPv4, allIPv6 := extractIPAddrs(iface.Name, allAddresses, true) + if len(allIPv4) == 1 && strings.ToLower(iface.Name) == "eth0" { + for _, ipWithMask := range allIPv4 { + ip := strings.Split(ipWithMask, "/") + return ip[0] + } + } + } + + return "0.0.0.0" +} + func networkInterfaces(brief bool, debug bool) ([]string, []string, error) { adapters, err := net.Interfaces() if err != nil {