| 1 | package routing |
| 2 | |
| 3 | import ( |
| 4 | "github.com/bettercap/bettercap/core" |
| 5 | "github.com/evilsocket/islazy/str" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | var parser = regexp.MustCompile(`^.+\d+\s+([^\s]+)\s+\d+\s+(.+)$`) |
| 11 | |
| 12 | func update() ([]Route, error) { |
| 13 | table = make([]Route, 0) |
| 14 | |
| 15 | for ip, inet := range map[RouteType]string{IPv4: "ipv4", IPv6: "ipv6"} { |
| 16 | output, err := core.Exec("netsh", []string{"interface", inet, "show", "route"}) |
| 17 | if err != nil { |
| 18 | return nil, err |
| 19 | } |
| 20 | |
| 21 | for _, line := range strings.Split(output, "\n") { |
| 22 | if line = str.Trim(line); len(line) > 0 { |
| 23 | matches := parser.FindStringSubmatch(line) |
| 24 | if num := len(matches); num == 3 { |
| 25 | route := Route{ |
| 26 | Type: ip, |
| 27 | Destination: matches[1], |
| 28 | Device: matches[2], |
| 29 | } |
| 30 | |
| 31 | if route.Destination == "0.0.0.0/0" || route.Destination == "::/0" { |
| 32 | route.Default = true |
| 33 | route.Gateway = route.Device |
| 34 | route.Device = "" |
| 35 | } |
| 36 | |
| 37 | table = append(table, route) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return table, nil |
| 44 | } |