/
epdisc.go
217 lines (168 loc) · 4.6 KB
/
epdisc.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
// Package epdisc implements endpoint (EP) discovery using Interactive Connection Establishment (ICE).
package epdisc
import (
"errors"
"fmt"
"net"
"github.com/pion/ice/v2"
"github.com/pion/stun"
"go.uber.org/zap"
"cunicu.li/cunicu/pkg/crypto"
"cunicu.li/cunicu/pkg/daemon"
"cunicu.li/cunicu/pkg/log"
epdiscproto "cunicu.li/cunicu/pkg/proto/feature/epdisc"
)
var Get = daemon.RegisterFeature(New, 50) //nolint:gochecknoglobals
type Interface struct {
*daemon.Interface
nat *NAT
natRule *NATRule
natRuleSrflx *NATRule
// muxConns is a list of UDP connections which are used by pion/ice
// agents for muxing
muxConns []net.PacketConn
mux ice.UDPMux
muxSrflx ice.UniversalUDPMux
muxPort int
muxSrflxPort int
Peers map[*daemon.Peer]*Peer
logger *log.Logger
}
func New(di *daemon.Interface) (*Interface, error) {
if !di.Settings.DiscoverEndpoints {
return nil, daemon.ErrFeatureDeactivated
}
i := &Interface{
Interface: di,
Peers: map[*daemon.Peer]*Peer{},
logger: log.Global.Named("epdisc").With(zap.String("intf", di.Name())),
}
i.AddPeerHandler(i)
i.AddModifiedHandler(i)
i.Bind().AddOpenHandler(i)
// Create per-interface UDP muxes
if i.Settings.ICE.HasCandidateType(ice.CandidateTypeHost) {
if err := i.setupUDPMux(); err != nil && !errors.Is(err, errNotSupported) {
return nil, fmt.Errorf("failed to setup host UDP mux: %w", err)
}
}
if i.Settings.ICE.HasCandidateType(ice.CandidateTypeServerReflexive) {
if err := i.setupUniversalUDPMux(); err != nil && !errors.Is(err, errNotSupported) {
return nil, fmt.Errorf("failed to setup srflx UDP mux: %w", err)
}
}
// Setup Netfilter port forwarding for non-userspace devices
if i.Settings.PortForwarding && !i.IsUserspace() {
if err := i.setupNAT(); err != nil {
return nil, fmt.Errorf("failed to setup NAT: %w", err)
}
}
return i, nil
}
func (i *Interface) Start() error {
i.logger.Info("Started endpoint discovery")
return nil
}
func (i *Interface) Close() error {
i.Bind().RemoveOpenHandler(i)
for _, p := range i.Peers {
if err := p.Close(); err != nil {
return fmt.Errorf("failed to close peer '%s': %w", p, err)
}
}
if i.nat != nil {
if err := i.nat.Close(); err != nil {
return fmt.Errorf("failed to de-initialize NAT: %w", err)
}
}
if i.mux != nil {
if err := i.mux.Close(); err != nil {
return fmt.Errorf("failed to do-initialize UDP mux: %w", err)
}
}
if i.muxSrflx != nil {
if err := i.muxSrflx.Close(); err != nil {
return fmt.Errorf("failed to do-initialize srflx UDP mux: %w", err)
}
}
return nil
}
func (i *Interface) Marshal() *epdiscproto.Interface {
is := &epdiscproto.Interface{}
if i.mux != nil {
is.MuxPort = uint32(i.muxPort)
}
if i.muxSrflx != nil {
is.MuxSrflxPort = uint32(i.muxSrflxPort)
}
if i.nat == nil {
is.NatType = epdiscproto.NATType_NONE
} else {
is.NatType = epdiscproto.NATType_NFTABLES
}
return is
}
func (i *Interface) PeerByPublicKey(pk crypto.Key) *Peer {
if cp, ok := i.Interface.Peers[pk]; ok {
return i.Peers[cp]
}
return nil
}
// Endpoint returns the best guess about our own endpoint
func (i *Interface) Endpoint() (*net.UDPAddr, error) {
var ep *net.UDPAddr
var bestPrio uint32
for _, p := range i.Peers {
cs, err := p.agent.GetLocalCandidates()
if err != nil {
return nil, err
}
for _, c := range cs {
switch c.Type() {
case ice.CandidateTypeHost, ice.CandidateTypeServerReflexive:
if !c.NetworkType().IsUDP() {
continue
}
if c.Priority() > bestPrio {
bestPrio = c.Priority()
ep = &net.UDPAddr{
IP: net.ParseIP(c.Address()),
Port: c.Port(),
}
}
case ice.CandidateTypePeerReflexive, ice.CandidateTypeRelay, ice.CandidateTypeUnspecified:
}
}
}
// No connected peers? Initiate a STUN binding request ourself..
if ep == nil {
c, err := stun.Dial("tcp", "stun.cunicu.li:3478")
if err != nil {
panic(err)
}
// Building binding request with random transaction id.
message := stun.MustBuild(stun.TransactionID, stun.BindingRequest)
var ip net.IP
// Sending request to STUN server, waiting for response message.
if err := c.Do(message, func(res stun.Event) {
if res.Error != nil {
panic(res.Error)
}
// Decoding XOR-MAPPED-ADDRESS attribute from message.
var xorAddr stun.XORMappedAddress
if err := xorAddr.GetFrom(res.Message); err != nil {
panic(err)
}
ip = xorAddr.IP
}); err != nil {
return nil, err
}
ep = &net.UDPAddr{
IP: ip,
Port: i.ListenPort,
}
}
return ep, nil
}