-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
wireguard_proxy_client.go
207 lines (195 loc) · 6.1 KB
/
wireguard_proxy_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
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
/*
Copyright 2023 Avi Zimmerman <avi.zimmerman@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package datachannels
import (
"fmt"
"log/slog"
"net"
"sync"
"time"
"github.com/pion/webrtc/v3"
"github.com/webmeshproj/webmesh/pkg/common"
"github.com/webmeshproj/webmesh/pkg/context"
"github.com/webmeshproj/webmesh/pkg/meshnet/relay"
"github.com/webmeshproj/webmesh/pkg/meshnet/transport"
)
// WireGuardProxyClient is a WireGuard proxy client. It is used for outgoing
// requests to establish a WireGuard proxy connection.
type WireGuardProxyClient struct {
conn *webrtc.PeerConnection
localAddr *net.UDPAddr
readyc chan struct{}
closec chan struct{}
bufferSize int
}
// NewWireGuardProxyClient creates a new WireGuardProxyClient using the given signaling transport.
// Traffic will be proxied to the wireguard interface listening on targetPort. It contains a method
// for retrieving the local address to use as a WireGuard endpoint for the peer on the other side of
// the proxy.
func NewWireGuardProxyClient(ctx context.Context, rt transport.WebRTCSignalTransport, targetPort uint16) (*WireGuardProxyClient, error) {
log := context.LoggerFrom(ctx)
log.Debug("Starting signaling transport")
err := rt.Start(ctx)
if err != nil {
return nil, fmt.Errorf("failed to start signaling transport: %w", err)
}
defer rt.Close()
s := webrtc.SettingEngine{}
s.DetachDataChannels()
s.SetIncludeLoopbackCandidate(true)
api := webrtc.NewAPI(webrtc.WithSettingEngine(s))
c, err := api.NewPeerConnection(webrtc.Configuration{
ICEServers: rt.TURNServers(),
})
if err != nil {
defer rt.Close()
return nil, fmt.Errorf("failed to create peer connection: %w", err)
}
pc := &WireGuardProxyClient{
conn: c,
readyc: make(chan struct{}),
closec: make(chan struct{}),
bufferSize: DefaultWireGuardProxyBuffer,
}
err = pc.conn.SetRemoteDescription(rt.RemoteDescription())
if err != nil {
defer pc.Close()
return nil, fmt.Errorf("failed to set remote description: %w", err)
}
errs := make(chan error, 10)
pc.conn.OnICECandidate(func(c *webrtc.ICECandidate) {
if c == nil {
return
}
log.Debug("Sending ICE candidate", "candidate", c.ToJSON().Candidate)
err := rt.SendCandidate(ctx, c.ToJSON())
if err != nil {
if transport.IsSignalTransportClosed(err) {
return
}
defer rt.Close()
errs <- fmt.Errorf("failed to send ICE candidate: %w", err)
}
})
var mu sync.Mutex
pc.conn.OnICEConnectionStateChange(func(s webrtc.ICEConnectionState) {
mu.Lock()
defer mu.Unlock()
log.Debug("ICE connection state changed", "state", s.String())
if s == webrtc.ICEConnectionStateConnected {
candidatePair, err := pc.conn.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
if err != nil {
log.Error("Failed to get selected candidate pair", slog.String("error", err.Error()))
return
}
log.Debug("ICE connection established", slog.Any("local", candidatePair.Local), slog.Any("remote", candidatePair.Remote))
return
}
if s == webrtc.ICEConnectionStateFailed || s == webrtc.ICEConnectionStateClosed {
log.Info("ICE connection has closed", "reason", s.String())
defer pc.Close()
select {
case <-pc.closec:
return
default:
}
close(pc.closec)
}
})
dc, err := pc.conn.CreateDataChannel("wireguard-proxy", &webrtc.DataChannelInit{
ID: common.Pointer(uint16(0)),
Negotiated: common.Pointer(true),
})
if err != nil {
defer pc.Close()
return nil, fmt.Errorf("create data channel: %w", err)
}
relay, err := relay.NewLocalUDP(relay.UDPOptions{
TargetPort: targetPort,
})
if err != nil {
defer pc.Close()
return nil, fmt.Errorf("dial: %w", err)
}
pc.localAddr = &net.UDPAddr{
IP: net.IPv6loopback,
Port: int(relay.LocalAddr().Port()),
}
dc.OnClose(func() {
log.Debug("Client side WireGuard datachannel closed")
})
dc.OnOpen(func() {
log.Debug("Client side datachannel opened")
var err error
rw, err := dc.Detach()
if err != nil {
log.Error("Failed to detach data channel", slog.String("error", err.Error()))
return
}
close(pc.readyc)
err = relay.Relay(ctx, rw)
if err != nil {
log.Error("Failed to relay", slog.String("error", err.Error()))
}
})
// Create and send an answer
answer, err := pc.conn.CreateAnswer(nil)
if err != nil {
defer pc.Close()
return nil, fmt.Errorf("failed to create answer: %w", err)
}
err = rt.SendDescription(ctx, answer)
if err != nil {
defer pc.Close()
return nil, fmt.Errorf("failed to send answer: %w", err)
}
// Set local description and start UDP listener
err = pc.conn.SetLocalDescription(answer)
if err != nil {
defer pc.Close()
return nil, fmt.Errorf("failed to set local description: %w", err)
}
// Receive ICE candidates
go func() {
for candidate := range rt.Candidates() {
log.Debug("Received ICE candidate", "candidate", candidate.Candidate)
if err := pc.conn.AddICECandidate(candidate); err != nil {
errs <- fmt.Errorf("failed to add ICE candidate: %w", err)
}
}
}()
select {
case err := <-errs:
return nil, err
case <-time.After(time.Second * 30):
return nil, fmt.Errorf("timed out waiting for data channel to open")
case <-pc.readyc:
}
return pc, nil
}
// LocalAddr returns the local UDP address for the proxy. This should be
// used as the endpoint for the WireGuard interface.
func (w *WireGuardProxyClient) LocalAddr() *net.UDPAddr {
return &net.UDPAddr{
IP: net.IP{127, 0, 0, 1},
Port: w.localAddr.Port,
}
}
// Closed returns a channel that is closed when the proxy is closed.
func (w *WireGuardProxyClient) Closed() <-chan struct{} {
return w.closec
}
// Close closes the proxy.
func (w *WireGuardProxyClient) Close() error {
return w.conn.Close()
}