-
Notifications
You must be signed in to change notification settings - Fork 517
/
router_darwin.go
152 lines (134 loc) · 3.96 KB
/
router_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//go:build darwin
package routing
import (
"bufio"
"bytes"
"fmt"
"net"
"os/exec"
"strings"
"github.com/pkg/errors"
"github.com/projectdiscovery/gologger"
sliceutil "github.com/projectdiscovery/utils/slice"
stringsutil "github.com/projectdiscovery/utils/strings"
"go.uber.org/multierr"
)
// New creates a routing engine for Darwin
func New() (Router, error) {
var routes []*Route
netstatCmd := exec.Command("netstat", "-nr")
netstatOutput, err := netstatCmd.Output()
if err != nil {
var route4, route6 *Route
// create default routes with outgoing ips
ip4, ip6, errOutboundIps := GetOutboundIPs()
if ip4 != nil {
interface4, err := FindInterfaceByIp(ip4)
if err != nil {
return nil, err
}
route4 = &Route{
Type: IPv4,
Default: true,
DefaultSourceIP: ip4,
NetworkInterface: interface4,
}
routes = append(routes, route4)
}
// try to find outbound route for ipv6
if ip6 != nil {
interface6, _ := FindInterfaceByIp(ip6)
route6 = &Route{
Type: IPv6,
Default: true,
DefaultSourceIP: ip6,
NetworkInterface: interface6,
}
routes = append(routes, route6)
} else {
// if we fail, use the same network interface for ipv4
route6 = &Route{
Type: IPv6,
Default: true,
NetworkInterface: route4.NetworkInterface,
}
routes = append(routes, route6)
}
if len(routes) > 0 {
return &RouterDarwin{Routes: routes}, nil
}
return nil, multierr.Combine(err, errOutboundIps)
}
var lastType RouteType
scanner := bufio.NewScanner(bytes.NewReader(netstatOutput))
for scanner.Scan() {
outputLine := strings.TrimSpace(scanner.Text())
if outputLine == "" {
continue
}
parts := stringsutil.SplitAny(outputLine, " \t")
if len(parts) >= 4 && !sliceutil.Contains(parts, "Destination") {
expire := "-1"
if len(parts) > 4 {
expire = parts[4]
}
route := &Route{
Default: stringsutil.EqualFoldAny(parts[0], "default"),
Destination: parts[0],
Gateway: parts[1],
Flags: parts[2],
Expire: expire,
}
if networkInterface, err := net.InterfaceByName(parts[3]); err == nil {
route.NetworkInterface = networkInterface
}
hasDots := stringsutil.ContainsAny(route.Destination, ".") || stringsutil.ContainsAny(route.Gateway, ".")
hasSemicolon := stringsutil.ContainsAny(route.Destination, ":") || stringsutil.ContainsAny(route.Gateway, ":")
switch {
case hasDots:
route.Type = IPv4
case hasSemicolon:
route.Type = IPv6
default:
// use last route type and print a warning
if lastType != "" {
gologger.Debug().Msgf("using '%s' for unknown route type: '%s'\n", lastType, outputLine)
route.Type = lastType
} else {
// we can't determine the route type
return nil, fmt.Errorf("could not determine route type for: '%s'", outputLine)
}
}
lastType = route.Type
routes = append(routes, route)
}
}
return &RouterDarwin{Routes: routes}, err
}
type RouterDarwin struct {
Routes []*Route
}
func (r *RouterDarwin) Route(dst net.IP) (iface *net.Interface, gateway, preferredSrc net.IP, err error) {
route, err := FindRouteForIp(dst, r.Routes)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "could not find route")
}
if route.DefaultSourceIP != nil {
return nil, nil, route.DefaultSourceIP, nil
}
if route.NetworkInterface == nil {
return nil, nil, nil, errors.Wrap(err, "could not find network interface")
}
ip, err := FindSourceIpForIp(route, dst)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "could not find source ip")
}
return route.NetworkInterface, net.ParseIP(route.Gateway), ip, nil
}
func (r *RouterDarwin) RouteWithSrc(input net.HardwareAddr, src, dst net.IP) (iface *net.Interface, gateway, preferredSrc net.IP, err error) {
route, err := FindRouteWithHwAndIp(input, src, r.Routes)
if err != nil {
return nil, nil, nil, err
}
return route.NetworkInterface, net.ParseIP(route.Gateway), src, nil
}