-
Notifications
You must be signed in to change notification settings - Fork 3
/
routing.go
97 lines (84 loc) · 2.2 KB
/
routing.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
package p2p
import (
"context"
"net"
"sync"
"time"
"github.com/hyprspace/hyprspace/config"
"github.com/libp2p/go-libp2p/core/control"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
ma "github.com/multiformats/go-multiaddr"
"github.com/vishvananda/netlink"
)
type ParallelRouting struct {
routings []routedhost.Routing
}
func (pr ParallelRouting) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
var wg sync.WaitGroup
var mutex sync.Mutex
var info peer.AddrInfo
info.ID = p
subCtx, cancelSubCtx := context.WithTimeout(ctx, 30*time.Second)
for _, r := range pr.routings {
wg.Add(1)
r2 := r
go func() {
defer wg.Done()
ai, err := r2.FindPeer(subCtx, p)
if err == nil {
mutex.Lock()
defer mutex.Unlock()
info.Addrs = append(info.Addrs, ai.Addrs...)
// give the other routings a short time period to find a better address
time.AfterFunc(500*time.Millisecond, cancelSubCtx)
}
}()
}
wg.Wait()
cancelSubCtx()
return info, nil
}
type RecursionGater struct {
config *config.Config
ifindex int
}
func NewRecursionGater(config *config.Config) RecursionGater {
link, err := netlink.LinkByName(config.Interface)
if err != nil {
panic(err)
}
return RecursionGater{
config: config,
ifindex: link.Attrs().Index,
}
}
func (rg RecursionGater) InterceptAddrDial(pid peer.ID, addr ma.Multiaddr) bool {
if ip4str, err := addr.ValueForProtocol(ma.P_IP4); err == nil {
ip4 := net.ParseIP(ip4str)
if rte, ok := rg.config.FindRouteForIP(ip4); ok {
if rte.Target.ID == pid {
routes, err := netlink.RouteGet(ip4)
if err == nil {
if len(routes) > 0 && routes[0].LinkIndex == rg.ifindex {
return false
}
}
}
}
}
return true
}
func (rg RecursionGater) InterceptPeerDial(pid peer.ID) bool {
return true
}
func (rg RecursionGater) InterceptAccept(addrs network.ConnMultiaddrs) bool {
return true
}
func (rg RecursionGater) InterceptSecured(direction network.Direction, pid peer.ID, addrs network.ConnMultiaddrs) bool {
return true
}
func (rg RecursionGater) InterceptUpgraded(network.Conn) (bool, control.DisconnectReason) {
return true, 0
}