diff --git a/network/net.go b/network/net.go index cffcf9c4..137bae71 100644 --- a/network/net.go +++ b/network/net.go @@ -5,7 +5,6 @@ import ( "fmt" "net" "regexp" - "runtime" "strconv" "strings" @@ -13,6 +12,7 @@ import ( ) var ErrNoIfaces = errors.New("No active interfaces found.") +var ErrNoGateway = errors.New("Could not detect gateway.") const ( MonitorModeAddress = "0.0.0.0" @@ -142,47 +142,3 @@ func FindInterface(name string) (*Endpoint, error) { return nil, ErrNoIfaces } - -func FindGateway(iface *Endpoint) (*Endpoint, error) { - output, err := core.Exec(IPv4RouteCmd, IPv4RouteCmdOpts) - if err != nil { - return nil, err - } - - isAndroid := runtime.GOOS == "android" - - 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 && isAndroid == false { - return iface, nil - } else { - // we have the address, now we need its mac - mac, err := ArpLookup(iface.Name(), gateway, false) - if err != nil { - fmt.Printf("%s\n", err) - } - return NewEndpoint(gateway, mac), nil - } - }) - } - } - - if isAndroid { - output, err = core.Exec("getprop", []string{"net.dns1"}) - if err != nil { - return nil, err - } - gateway := core.Trim(output) - // we have the address, now we need its mac - mac, err := ArpLookup(iface.Name(), gateway, false) - if err != nil { - fmt.Printf("%s\n", err) - } - return NewEndpoint(gateway, mac), nil - - } - - return nil, fmt.Errorf("Could not detect the gateway.") -} diff --git a/network/net_gateway.go b/network/net_gateway.go new file mode 100644 index 00000000..46577c65 --- /dev/null +++ b/network/net_gateway.go @@ -0,0 +1,37 @@ +// +build !android + +package network + +import ( + "fmt" + "strings" + + "github.com/bettercap/bettercap/core" +) + +func FindGateway(iface *Endpoint) (*Endpoint, error) { + output, err := core.Exec(IPv4RouteCmd, IPv4RouteCmdOpts) + if err != nil { + return nil, err + } + + 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 { + fmt.Printf("%s\n", err) + } + return NewEndpoint(gateway, mac), nil + } + }) + } + } + + return nil, ErrNoGateway +} diff --git a/network/net_gateway_android.go b/network/net_gateway_android.go new file mode 100644 index 00000000..9f20ed76 --- /dev/null +++ b/network/net_gateway_android.go @@ -0,0 +1,32 @@ +package net + +import ( + "errors" + "fmt" + "net" + "regexp" + "runtime" + "strconv" + "strings" + + "github.com/bettercap/bettercap/core" +) + +func FindGateway(iface *Endpoint) (*Endpoint, error) { + output, err = core.Exec("getprop", []string{"net.dns1"}) + if err != nil { + return nil, err + } + + gw := core.Trim(output) + if IPv4Validator.MatchString(gw) { + // we have the address, now we need its mac + mac, err := ArpLookup(iface.Name(), gw, false) + if err != nil { + return nil, err + } + return NewEndpoint(gateway, mac), nil + } + + return nil, ErrNoGateway +}