-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
64 lines (55 loc) · 1.74 KB
/
router.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
package main
import (
"fmt"
"net"
"os"
"github.com/google/gopacket/routing"
)
type Route struct {
SourceHostname string
SourceInterfaceName string
SourceHardwareAddress net.HardwareAddr
SourceIP net.IP
GatewayIP net.IP
DestinationIP net.IP
}
func (r *Route) String() string {
if r.SourceIP == nil && r.GatewayIP == nil {
// If we failed to route through a gateway, log without a determined
// source IP or outgoing iface. (Assume IPv4!)
return fmt.Sprintf("[%s][%s › %s]", r.SourceHostname, GetLocalIPs(), r.DestinationIP)
} else if r.SourceIP.String() == r.DestinationIP.String() || r.SourceIP.IsLoopback() {
// If the source and destination are the same, the route is trivial.
return fmt.Sprintf("[%s][%s][%s]", r.SourceHostname, r.SourceInterfaceName, r.DestinationIP)
} else {
return fmt.Sprintf("[%s][%s][%s][%s › %s » %s]", r.SourceHostname, r.SourceInterfaceName, r.SourceHardwareAddress, r.SourceIP, r.GatewayIP, r.DestinationIP)
}
}
func GetRoute(ip net.IP) (*Route, error) {
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
route := &Route{
SourceHostname: hostname,
SourceInterfaceName: "",
SourceHardwareAddress: nil,
SourceIP: nil,
GatewayIP: nil,
DestinationIP: ip}
r, err := routing.New()
if err != nil {
return route, err
}
iface, gateway, source, err := r.Route(ip)
if err != nil {
// This is possibly a workaround until something like https://github.com/google/gopacket/pull/697 is released
return route, err
} else {
route.SourceInterfaceName = iface.Name
route.SourceHardwareAddress = iface.HardwareAddr
route.SourceIP = source
route.GatewayIP = gateway
return route, nil
}
}