Skip to content

Commit 4a5b624

Browse files
committed
run: go fmt; use 'MAIN' for the address portion to use system's primary interface
1 parent 5c755bb commit 4a5b624

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

cmd.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ import (
2323
"syscall"
2424
"time"
2525

26+
"github.com/alecthomas/kingpin/v2"
2627
"github.com/olekukonko/tablewriter"
2728
"go.uber.org/zap"
2829
"go.uber.org/zap/zapcore"
29-
"github.com/alecthomas/kingpin/v2"
3030
)
3131

32-
const version = "0.7.1"
32+
const version = "0.7.2"
3333

3434
var (
3535
list = kingpin.Flag("int", "list local interface IP addresses").Short('i').Bool()
36-
from = kingpin.Flag("from", "from IP address:port").Short('f').String()
36+
from = kingpin.Flag("from", "from IP address:port; use 'MAIN' for the address portion to use system's primary interface").Short('f').String()
3737
to = kingpin.Flag("to", "to IP address:port").Short('t').String()
3838
examples = kingpin.Flag("examples", "show command line example and then exit").Bool()
3939
versionOnly = kingpin.Flag("version", "show version and then exit").Bool()
@@ -298,6 +298,10 @@ func main() {
298298
os.Exit(1)
299299
}
300300

301+
if strings.HasPrefix(*from, "MAIN:") {
302+
*from = strings.Replace(*from, "MAIN", getMainNic(), 1)
303+
}
304+
301305
if len(*loc) > 0 && 0 == *distance {
302306
kingpin.FatalUsage("--distance must be used with --loc")
303307
os.Exit(1)

docker_build_image.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ if [ $# -eq 0 ] ; then
6969
exit 1
7070
fi
7171

72-
TAG=$1
73-
IMG=gofwd:${TAG}
72+
IMG=$1
7473
BUND="ca-bundle.crt"
7574
SC="ssl/certs/"
7675
GF="gofwd"

geoip.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ type ipInfoResult struct {
2828
getIpInfo issues a web query to ipinfo.io
2929
The JSON result is converted to an ipInfoResult struct
3030
Args:
31+
3132
ip: an IPv4 address
3233
3334
Returns:
35+
3436
an ipInfoResult struct containing the information returned by the service
3537
*/
3638
func getIPInfo(ip string) (ipInfoResult, error) {
@@ -122,9 +124,11 @@ func validateLocation(localGeoIP ipInfoResult, remoteGeoIP ipInfoResult, restric
122124
latlon2coord converts a string such as "36.0525,-79.107" to a tuple of floats
123125
124126
Args:
127+
125128
latlon: a string in "lat, lon" format
126129
127130
Returns:
131+
128132
a tuple in (float64, float64) format
129133
*/
130134
func latlon2coord(latlon string) (float64, float64, error) {
@@ -147,9 +151,10 @@ func hsin(theta float64) float64 {
147151
}
148152

149153
// HaversineDistance returns the distance (in miles) between two points of
150-
// a given longitude and latitude relatively accurately (using a spherical
151-
// approximation of the Earth) through the Haversin Distance Formula for
152-
// great arc distance on a sphere with accuracy for small distances
154+
//
155+
// a given longitude and latitude relatively accurately (using a spherical
156+
// approximation of the Earth) through the Haversin Distance Formula for
157+
// great arc distance on a sphere with accuracy for small distances
153158
//
154159
// point coordinates are supplied in degrees and converted into rad. in the func
155160
//

nics.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ func extractIPAddrs(ifaceName string, allAddresses []net.Addr, brief bool) ([]st
6666
return allIPv4, allIPv6
6767
}
6868

69+
func getMainNic() string {
70+
adapters, err := net.Interfaces()
71+
if err != nil {
72+
return "0.0.0.0"
73+
}
74+
75+
for _, iface := range adapters {
76+
allAddresses, err := iface.Addrs()
77+
if err != nil {
78+
return "0.0.0.0"
79+
}
80+
81+
allIPv4, allIPv6 := extractIPAddrs(iface.Name, allAddresses, true)
82+
if len(allIPv4) == 1 && strings.ToLower(iface.Name) == "eth0" {
83+
for _, ipWithMask := range allIPv4 {
84+
ip := strings.Split(ipWithMask, "/")
85+
return ip[0]
86+
}
87+
}
88+
}
89+
90+
return "0.0.0.0"
91+
}
92+
6993
func networkInterfaces(brief bool, debug bool) ([]string, []string, error) {
7094
adapters, err := net.Interfaces()
7195
if err != nil {

0 commit comments

Comments
 (0)