| 1 | package routing |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strings" |
| 6 | |
| 7 | "github.com/bettercap/bettercap/core" |
| 8 | "github.com/evilsocket/islazy/str" |
| 9 | ) |
| 10 | |
| 11 | var parser = regexp.MustCompile(`^([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+).*$`) |
| 12 | |
| 13 | func update() ([]Route, error) { |
| 14 | table = make([]Route, 0) |
| 15 | |
| 16 | output, err := core.Exec("netstat", []string{"-n", "-r"}) |
| 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 == 5 && matches[1] != "Destination" { |
| 25 | route := Route{ |
| 26 | Destination: matches[1], |
| 27 | Gateway: matches[2], |
| 28 | Flags: matches[3], |
| 29 | Device: matches[4], |
| 30 | Default: matches[1] == "default", |
| 31 | } |
| 32 | |
| 33 | if strings.ContainsRune(route.Destination, '.') || strings.ContainsRune(route.Gateway, '.') { |
| 34 | route.Type = IPv4 |
| 35 | } else { |
| 36 | route.Type = IPv6 |
| 37 | } |
| 38 | |
| 39 | table = append(table, route) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return table, nil |
| 45 | } |