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

View file

@ -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.")
}

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
}

View file

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