mirror of
https://github.com/bettercap/bettercap
synced 2025-07-10 07:13:36 -07:00
fix automatic gateway detection for Linux
This commit is contained in:
parent
aba29e03f6
commit
c6740a5750
1 changed files with 48 additions and 24 deletions
|
@ -1,43 +1,67 @@
|
||||||
package routing
|
package routing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bettercap/bettercap/core"
|
|
||||||
"github.com/evilsocket/islazy/str"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bettercap/bettercap/core"
|
||||||
|
"github.com/evilsocket/islazy/str"
|
||||||
)
|
)
|
||||||
|
|
||||||
var parser = regexp.MustCompile(`^(.+)\sdev\s([^\s]+)\s(.+)$`)
|
var (
|
||||||
|
routeHeadings []string
|
||||||
|
whitespaceParser = regexp.MustCompile(`\s+`)
|
||||||
|
)
|
||||||
|
|
||||||
func update() ([]Route, error) {
|
func update() ([]Route, error) {
|
||||||
table = make([]Route, 0)
|
table = make([]Route, 0)
|
||||||
|
|
||||||
for ip, inet := range map[RouteType]string{IPv4: "inet", IPv6: "inet6"} {
|
output, err := core.Exec("netstat", []string{"-r", "-n"})
|
||||||
output, err := core.Exec("ip", []string{"-f", inet, "route"})
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for _, line := range strings.Split(output, "\n") {
|
for _, line := range strings.Split(output, "\n") {
|
||||||
if line = str.Trim(line); len(line) > 0 {
|
if line = str.Trim(line); len(line) != 0 {
|
||||||
matches := parser.FindStringSubmatch(line)
|
parts := whitespaceParser.Split(line, -1)
|
||||||
if num := len(matches); num == 4 {
|
if parts[0] == "Kernel" {
|
||||||
route := Route{
|
continue
|
||||||
Type: ip,
|
}
|
||||||
Destination: matches[1],
|
|
||||||
Device: matches[2],
|
|
||||||
Flags: matches[3],
|
|
||||||
Default: strings.Index(matches[1], "default ") == 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
if idx := strings.Index(route.Destination, " via "); idx >= 0 {
|
if parts[0] == "Destination" {
|
||||||
route.Gateway = route.Destination[idx + len(" via "):]
|
routeHeadings = parts
|
||||||
route.Destination = route.Destination[:idx]
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
table = append(table, route)
|
route := Route{}
|
||||||
|
for i, s := range parts {
|
||||||
|
switch routeHeadings[i] {
|
||||||
|
case "Destination":
|
||||||
|
route.Destination = s
|
||||||
|
break
|
||||||
|
case "Flags":
|
||||||
|
route.Flags = s
|
||||||
|
break
|
||||||
|
case "Gateway":
|
||||||
|
route.Gateway = s
|
||||||
|
break
|
||||||
|
case "Iface":
|
||||||
|
route.Device = s
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
route.Default = strings.Contains(route.Flags, "G")
|
||||||
|
|
||||||
|
if route.Destination != "" {
|
||||||
|
if strings.ContainsRune(route.Destination, '.') || strings.ContainsRune(route.Gateway, '.') {
|
||||||
|
route.Type = "IPv4"
|
||||||
|
} else {
|
||||||
|
route.Type = "IPv6"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
table = append(table, route)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue