misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
evilsocket 2018-02-25 03:32:59 +01:00
commit e6bd5a9584
3 changed files with 70 additions and 45 deletions

37
network/net_gateway.go Normal file
View file

@ -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
}