From 5041267be99a3f0e38f461fa96474802f968963a Mon Sep 17 00:00:00 2001 From: evilsocket Date: Thu, 3 May 2018 13:03:52 +0200 Subject: [PATCH] fixed net.FindGateway if there's a VPN interface active --- network/lan_test.go | 13 ++----------- network/net_gateway.go | 29 ++++++++++++++++------------- network/net_test.go | 32 -------------------------------- 3 files changed, 18 insertions(+), 56 deletions(-) diff --git a/network/lan_test.go b/network/lan_test.go index ce30d1ba..22a69fd4 100644 --- a/network/lan_test.go +++ b/network/lan_test.go @@ -1,7 +1,6 @@ package network import ( - "net" "testing" ) @@ -14,16 +13,8 @@ func buildExampleLAN() *LAN { } func buildExampleEndpoint() *Endpoint { - ifaces, _ := net.Interfaces() - var exampleIface net.Interface - for _, iface := range ifaces { - if iface.HardwareAddr != nil { - exampleIface = iface - break - } - } - foundEndpoint, _ := FindInterface(exampleIface.Name) - return foundEndpoint + iface, _ := FindInterface("") + return iface } func TestNewLAN(t *testing.T) { diff --git a/network/net_gateway.go b/network/net_gateway.go index 4a47698b..ece07c74 100644 --- a/network/net_gateway.go +++ b/network/net_gateway.go @@ -14,21 +14,24 @@ func FindGateway(iface *Endpoint) (*Endpoint, error) { return nil, err } + ifName := iface.Name() for _, line := range strings.Split(output, "\n") { - m := IPv4RouteParser.FindStringSubmatch(line) - if len(m) == IPv4RouteTokens { - return IPv4RouteIsGateway(iface.Name(), m, func(gateway string) (*Endpoint, error) { - if gateway == iface.IpAddress { - return iface, nil - } else { - // we have the address, now we need its mac - mac, err := ArpLookup(iface.Name(), gateway, false) - if err != nil { - return nil, err + if strings.Contains(line, ifName) { + m := IPv4RouteParser.FindStringSubmatch(line) + if len(m) == IPv4RouteTokens { + return IPv4RouteIsGateway(ifName, m, func(gateway string) (*Endpoint, error) { + if gateway == iface.IpAddress { + return iface, nil + } else { + // we have the address, now we need its mac + mac, err := ArpLookup(ifName, gateway, false) + if err != nil { + return nil, err + } + return NewEndpoint(gateway, mac), nil } - return NewEndpoint(gateway, mac), nil - } - }) + }) + } } } diff --git a/network/net_test.go b/network/net_test.go index 636aed84..4d93a416 100644 --- a/network/net_test.go +++ b/network/net_test.go @@ -87,38 +87,6 @@ func TestFindInterfaceByName(t *testing.T) { } } -func TestMatchByAddress(t *testing.T) { - ifaces, err := net.Interfaces() - if err != nil { - t.Error(err) - } - if len(ifaces) <= 0 { - t.Error("Unable to find any network interfaces to run test with.") - } - var exampleIface net.Interface - // emulate libpcap's pcap_lookupdev function to find - // default interface to test with ( maybe could use loopback ? ) - for _, iface := range ifaces { - if iface.HardwareAddr != nil { - exampleIface = iface - break - } - } - // check match by mac address - if !matchByAddress(exampleIface, exampleIface.HardwareAddr.String()) { - t.Error("unable to verify a match with a given interface and mac address") - } - addrs, err := exampleIface.Addrs() - if err != nil || len(addrs) <= 0 { - t.Error("Unable to find any ip addresses to run test with interface:", exampleIface.Name) - - } - // check match by ip address - if !matchByAddress(exampleIface, addrs[0].String()) { - t.Error("unable to verify a match with a given interface and ip address") - } -} - func TestFindInterface(t *testing.T) { ifaces, err := net.Interfaces() if err != nil {