/
bind.go
261 lines (202 loc) · 5.74 KB
/
bind.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package wg
import (
"errors"
"fmt"
"io"
"net"
"net/netip"
"slices"
"sync"
"github.com/pion/ice/v2"
"go.uber.org/zap"
wgconn "golang.zx2c4.com/wireguard/conn"
"cunicu.li/cunicu/pkg/log"
netx "cunicu.li/cunicu/pkg/net"
)
var ErrNoConn = errors.New("no connection for endpoint")
type BindConn interface {
Receive(buf []byte) (int, wgconn.Endpoint, error)
Send(buf []byte, ep wgconn.Endpoint) (int, error)
ListenPort() (uint16, bool)
SetMark(mark uint32) error
BindClose() error
}
// BindKernelConn is a BindConn which is consumed by a Kernel WireGuard interface
type BindKernelConn interface {
BindConn
WriteKernel([]byte) (int, error)
}
type BindHandler interface {
OnBindOpen(b *Bind, port uint16)
}
// Compile-time assertion
var _ (wgconn.Bind) = (*Bind)(nil)
type Bind struct {
Conns []BindConn
onOpen []BindHandler
onPacket []netx.PacketHandler
endpoints sync.Map
logger *log.Logger
}
func NewBind(logger *log.Logger) *Bind {
return &Bind{
logger: logger.Named("bind"),
}
}
// Open puts the Bind into a listening state on a given port and reports the actual
// port that it bound to. Passing zero results in a random selection.
// fns is the set of functions that will be called to receive packets.
func (b *Bind) Open(port uint16) ([]wgconn.ReceiveFunc, uint16, error) { //nolint:gocognit
b.Conns = nil
for _, h := range b.onOpen {
h.OnBindOpen(b, port)
}
// Add a fallback conn in case no other connections have been registered
if len(b.Conns) == 0 {
if err := b.addFallbackConnection(port); err != nil {
return nil, 0, err
}
}
rcvFns := []wgconn.ReceiveFunc{}
for _, conn := range b.Conns {
conn := conn
rcvFn := func(packets [][]byte, sizes []int, eps []wgconn.Endpoint) (int, error) {
if len(packets) != 1 {
panic("batch size not 1?")
}
buf := packets[0]
n, cep, err := conn.Receive(buf)
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, ice.ErrClosed) || errors.Is(err, net.ErrClosed) {
err = net.ErrClosed
} else {
b.logger.Error("Failed to receive packet", zap.Error(err))
}
b.logger.Debug("Connection closed", zap.Error(err))
return -1, err
}
ep := cep.(*BindEndpoint) //nolint:forcetypeassert
if n > 0 {
b.logger.Debug("Received packet from bind",
zap.Int("len", n),
zap.String("ep", ep.DstToString()),
zap.Binary("data", buf[:n]))
}
sizes[0] = n
eps[0] = ep
// Update endpoint map
if ep.Conn == nil {
ep.Conn = conn
}
// Call handlers
for _, h := range b.onPacket {
if abort, err := h.OnPacketRead(buf[:n], ep.DstUDPAddr()); err != nil {
return -1, fmt.Errorf("failed to call handler: %w", err)
} else if abort {
return 0, nil
}
}
return 1, err
}
rcvFns = append(rcvFns, rcvFn)
}
b.logger.Debug("Opened bind",
zap.Uint16("port", port),
zap.Int("#conns", len(rcvFns)))
return rcvFns, port, nil
}
// Close closes the Bind listener.
// All fns returned by Open must return net.ErrClosed after a call to Close.
func (b *Bind) Close() error {
for _, c := range b.Conns {
if err := c.BindClose(); err != nil {
return fmt.Errorf("failed to close connection: %w", err)
}
}
b.logger.Debug("Closed bind")
return nil
}
// SetMark sets the mark for each packet sent through this Bind.
// This mark is passed to the kernel as the socket option SO_MARK.
func (b *Bind) SetMark(mark uint32) error {
b.logger.Debug("Set mark", zap.Uint32("mark", mark))
for _, c := range b.Conns {
if err := c.SetMark(mark); err != nil {
return err
}
}
return nil
}
// Send writes a packet to an endpoint.
func (b *Bind) Send(packets [][]byte, cep wgconn.Endpoint) error {
if len(packets) != 1 {
panic("batch size not 1?")
}
buf := packets[0]
ep := cep.(*BindEndpoint) //nolint:forcetypeassert
if ep.Conn == nil {
return fmt.Errorf("%w: %s", ErrNoConn, ep.DstToString())
}
b.logger.Debug("Send packets",
zap.Int("cnt", len(packets)),
zap.Any("ep", ep.DstToString()),
zap.Binary("data", buf))
_, err := ep.Conn.Send(buf, ep)
return err
}
// Endpoint returns an Endpoint containing ap.
func (b *Bind) Endpoint(ap netip.AddrPort) *BindEndpoint {
ep, _ := b.endpoints.LoadOrStore(ap, &BindEndpoint{
AddrPort: ap,
})
return ep.(*BindEndpoint) //nolint:forcetypeassert
}
// ParseEndpoint creates a new endpoint from a string.
// Implements wgconn.Bind
func (b *Bind) ParseEndpoint(s string) (ep wgconn.Endpoint, err error) {
b.logger.Debug("Parse endpoint", zap.String("ep", s))
e, err := netip.ParseAddrPort(s)
return b.Endpoint(e), err
}
// BatchSize is the number of buffers expected to be passed to
// the ReceiveFuncs, and the maximum expected to be passed to SendBatch.
// Implements wgconn.Bind
func (b *Bind) BatchSize() int {
return 1
}
func (b *Bind) AddConn(conn net.PacketConn) {
bindConn := newBindPacketConn(b, conn)
b.Conns = append(b.Conns, bindConn)
}
func (b *Bind) AddOpenHandler(h BindHandler) {
if !slices.Contains(b.onOpen, h) {
b.onOpen = append(b.onOpen, h)
}
}
func (b *Bind) RemoveOpenHandler(h BindHandler) {
if idx := slices.Index(b.onOpen, h); idx > -1 {
b.onOpen = slices.Delete(b.onOpen, idx, idx+1)
}
}
func (b *Bind) AddPacketHandler(h netx.PacketHandler) {
if !slices.Contains(b.onPacket, h) {
b.onPacket = append(b.onPacket, h)
}
}
func (b *Bind) RemovePacketHandler(h netx.PacketHandler) {
if idx := slices.Index(b.onPacket, h); idx > -1 {
b.onPacket = slices.Delete(b.onPacket, idx, idx+1)
}
}
func (b *Bind) addFallbackConnection(port uint16) error {
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
Port: int(port),
})
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
b.AddConn(udpConn)
return nil
}