Skip to content

Commit

Permalink
run: go fmt; use 'MAIN' for the address portion to use system's prima…
Browse files Browse the repository at this point in the history
…ry interface
  • Loading branch information
jftuga committed Oct 2, 2023
1 parent 5c755bb commit 4a5b624
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
10 changes: 7 additions & 3 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions docker_build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 8 additions & 3 deletions geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
//
Expand Down
24 changes: 24 additions & 0 deletions nics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4a5b624

Please sign in to comment.