mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
7ec7c20db8
commit
e6bd5a9584
3 changed files with 70 additions and 45 deletions
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -13,6 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrNoIfaces = errors.New("No active interfaces found.")
|
var ErrNoIfaces = errors.New("No active interfaces found.")
|
||||||
|
var ErrNoGateway = errors.New("Could not detect gateway.")
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MonitorModeAddress = "0.0.0.0"
|
MonitorModeAddress = "0.0.0.0"
|
||||||
|
@ -142,47 +142,3 @@ func FindInterface(name string) (*Endpoint, error) {
|
||||||
|
|
||||||
return nil, ErrNoIfaces
|
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
37
network/net_gateway.go
Normal 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
|
||||||
|
}
|
32
network/net_gateway_android.go
Normal file
32
network/net_gateway_android.go
Normal 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
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue