mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 02:36:57 -07:00
fix: refactored routing logic (fixes #701)
This commit is contained in:
parent
88a83192ef
commit
43a93fd866
11 changed files with 202 additions and 80 deletions
|
@ -3,46 +3,26 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/bettercap/bettercap/core"
|
||||
|
||||
"github.com/evilsocket/islazy/str"
|
||||
"github.com/bettercap/bettercap/routing"
|
||||
)
|
||||
|
||||
func FindGateway(iface *Endpoint) (*Endpoint, error) {
|
||||
Debug("FindGateway(%s) [cmd=%v opts=%v parser=%v]", iface.Name(), IPv4RouteCmd, IPv4RouteCmdOpts, IPv4RouteParser)
|
||||
|
||||
output, err := core.Exec(IPv4RouteCmd, IPv4RouteCmdOpts)
|
||||
gateway, err := routing.Gateway(routing.IPv4, iface.Name())
|
||||
if err != nil {
|
||||
Debug("FindGateway(%s): core.Exec failed with %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Debug("FindGateway(%s) output:\n%s", iface.Name(), output)
|
||||
|
||||
ifName := iface.Name()
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
if line = str.Trim(line); strings.Contains(line, ifName) {
|
||||
m := IPv4RouteParser.FindStringSubmatch(line)
|
||||
if len(m) >= IPv4RouteTokens {
|
||||
Debug("FindGateway(%s) line '%s' matched with %v", iface.Name(), line, m)
|
||||
return IPv4RouteIsGateway(ifName, m, func(gateway string) (*Endpoint, error) {
|
||||
if gateway == iface.IpAddress {
|
||||
Debug("gateway is the interface")
|
||||
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
|
||||
}
|
||||
Debug("gateway is %s[%s]", gateway, mac)
|
||||
return NewEndpoint(gateway, mac), nil
|
||||
}
|
||||
})
|
||||
}
|
||||
if gateway == iface.IpAddress {
|
||||
Debug("gateway is the interface")
|
||||
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
|
||||
}
|
||||
Debug("gateway is %s[%s]", gateway, mac)
|
||||
return NewEndpoint(gateway, mac), nil
|
||||
}
|
||||
|
||||
Debug("FindGateway(%s): nothing found :/", iface.Name())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue