-
Notifications
You must be signed in to change notification settings - Fork 18
/
meshboi_client.go
75 lines (57 loc) · 1.56 KB
/
meshboi_client.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
package meshboi
import (
"net"
"sync"
"time"
log "github.com/sirupsen/logrus"
"inet.af/netaddr"
)
type MeshboiClient struct {
peerStore *PeerConnStore
rolloClient RolodexClient
tunRouter TunRouter
peerConnector PeerConnector
}
func NewMeshBoiClient(tun TunConn, vpnIpPrefix netaddr.IPPrefix, rolodexIP netaddr.IP, rolodexPort int, networkName string, meshPSK []byte) (*MeshboiClient, error) {
listenAddr := &net.UDPAddr{IP: net.ParseIP("0.0.0.0")}
dtlsConfig := getDtlsConfig(vpnIpPrefix.IP, meshPSK)
multiplexConn, err := NewMultiplexedDTLSConn(listenAddr, dtlsConfig)
if err != nil {
log.Error("Error creating multiplexed conn ", err)
return nil, err
}
rolodexAddr := &net.UDPAddr{IP: rolodexIP.IPAddr().IP, Port: rolodexPort}
rolodexConn, err := multiplexConn.Dial(rolodexAddr)
if err != nil {
log.Error("Error connecting to rolodex server")
return nil, err
}
mc := MeshboiClient{}
mc.peerStore = NewPeerConnStore()
mc.peerConnector = NewPeerConnector(multiplexConn, mc.peerStore, tun)
mc.rolloClient = NewRolodexClient(networkName, rolodexConn, time.Duration(5*time.Second), mc.peerConnector.OnNetworkMapUpdate)
mc.tunRouter = NewTunRouter(tun, mc.peerStore)
return &mc, nil
}
func (mc *MeshboiClient) Run() {
var wg sync.WaitGroup
wg.Add(3)
go func() {
mc.tunRouter.Run()
wg.Done()
}()
go func() {
mc.peerConnector.ListenForPeers()
wg.Done()
}()
go func() {
mc.rolloClient.Run()
wg.Done()
}()
wg.Wait()
}
func (mc *MeshboiClient) Stop() {
mc.rolloClient.Stop()
mc.peerConnector.Stop()
mc.tunRouter.Stop()
}