mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 02:36:57 -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
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue