forked from costela/wesher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2p.go
151 lines (124 loc) · 3.1 KB
/
p2p.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
package p2p
import (
"context"
"time"
"github.com/derlaft/w2wesher/config"
"github.com/derlaft/w2wesher/networkstate"
"github.com/derlaft/w2wesher/runnergroup"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/pnet"
"github.com/multiformats/go-multiaddr"
"golang.org/x/sync/semaphore"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)
const maxParallelConnects = 8
const connectTimeout = time.Second * 16
var log = logging.Logger("w2wesher:p2p")
type Node interface {
Run(context.Context) error
}
type Wireguard interface {
AnnounceInfo() networkstate.WireguardState
Update()
}
type worker struct {
host host.Host
topic *pubsub.Topic
pubsub *pubsub.PubSub
pk crypto.PrivKey
psk []byte
state *networkstate.State
wgControl Wireguard
newConnectionSem *semaphore.Weighted
cfg *config.Config
}
func New(cfg *config.Config, state *networkstate.State, wgControl Wireguard) (Node, error) {
pk, err := cfg.P2P.LoadPrivateKey()
if err != nil {
return nil, err
}
psk, err := cfg.P2P.LoadPsk()
if err != nil {
return nil, err
}
return &worker{
cfg: cfg,
pk: pk,
psk: psk,
state: state,
wgControl: wgControl,
newConnectionSem: semaphore.NewWeighted(maxParallelConnects),
}, nil
}
func (w *worker) connect(ctx context.Context, p peer.AddrInfo) {
err := w.newConnectionSem.Acquire(ctx, 1)
if err != nil {
log.
With("err", err).
Error("failed acquire newConnectionSem")
return
}
defer w.newConnectionSem.Release(1)
// context timeout
ctx, cancel := context.WithTimeout(ctx, connectTimeout)
defer cancel()
log.With("addr", p).Debug("connecting to the peer")
err = w.host.Connect(ctx, p)
if err != nil {
log.
With("addr", p).
With("err", err).
Error("failed to connect to the peer")
}
}
func (w *worker) updateAddrs() {
ret := make(map[peer.ID]multiaddr.Multiaddr)
n := w.host.Network()
for _, peer := range n.Peers() {
for _, addr := range n.ConnsToPeer(peer) {
ret[peer] = addr.RemoteMultiaddr()
// we can only use one addr in wg
break
}
}
w.state.UpdateAddrs(ret)
w.wgControl.Update()
}
func (w *worker) Run(ctx context.Context) error {
log.Debug("starting")
// make sure it fails on invalid psk
pnet.ForcePrivateNetwork = true
h, err := libp2p.New(
libp2p.Identity(w.pk),
libp2p.ListenAddrStrings(w.cfg.P2P.ListenAddr),
libp2p.PrivateNetwork(w.psk),
libp2p.EnableNATService(),
libp2p.NATPortMap(),
)
if err != nil {
return err
}
w.host = h
err = w.initializePubsub(ctx)
if err != nil {
return err
}
err = w.initialBootstrap(ctx)
if err != nil {
return err
}
log.
With("id", h.ID().String()).
Info("initialization complete, starting periodic updates")
return runnergroup.
New(ctx).
Go(w.periodicAnnounce).
Go(w.consumeAnnounces).
Go(w.periodicBootstrap).
Go(w.sendWelcomeAnnounces).
Wait()
}