-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-egress.go
191 lines (159 loc) · 4.05 KB
/
proxy-egress.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
package main
import (
"context"
"fmt"
"log"
"net"
"sync"
"time"
"github.com/pion/webrtc/v3"
"github.com/xtaci/smux"
)
type EgressProxy struct {
signal SignalEgress
signalTimeout time.Duration
peer *webrtc.PeerConnection
endpoints []Endpoint
}
func (egp *EgressProxy) Start(ctx context.Context) (err error) {
ctx, cancel := context.WithCancel(ctx)
egp.peer.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) {
switch pcs {
case webrtc.PeerConnectionStateFailed:
err = fmt.Errorf("peer connection failed")
egp.Stop()
case webrtc.PeerConnectionStateDisconnected:
egp.Stop()
case webrtc.PeerConnectionStateClosed:
cancel()
}
})
var sctx = ctx
if egp.signalTimeout > 0 {
var scancel context.CancelFunc
sctx, scancel = context.WithTimeout(ctx, egp.signalTimeout)
defer scancel()
}
if err := egp.createDummyDataChannel(); err != nil {
return err
}
offer, err := egp.peer.CreateOffer(nil)
if err != nil {
return fmt.Errorf("create webrtc offer failed: %w", err)
}
if err = egp.peer.SetLocalDescription(offer); err != nil {
return fmt.Errorf("set local description failed: %w", err)
}
iceDone := gatherICE(sctx, egp.peer, egp.signal)
if err := egp.signal.SendOffer(sctx, egp.peer.LocalDescription().SDP); err != nil {
return fmt.Errorf("send offer failed: %w", err)
}
answer, err := egp.signal.RecvAnswer(sctx)
if err != nil {
return fmt.Errorf("receive answer failed: %w", err)
}
err = egp.peer.SetRemoteDescription(webrtc.SessionDescription{
Type: webrtc.SDPTypeAnswer,
SDP: string(answer),
})
if err != nil {
return fmt.Errorf("set remote description failed: %w", err)
}
go func() {
defer cancel()
if errt := egp.startTunnels(sctx); errt != nil {
err = fmt.Errorf("start tunnels failed: %w", errt)
}
}()
chanRecv(sctx, iceDone)
egp.signal.Close()
<-ctx.Done()
egp.Stop()
return
}
// createDummyDataChannel create auto-closed data channel only to populate SDP
func (egp *EgressProxy) createDummyDataChannel() (err error) {
dc, err := egp.peer.CreateDataChannel("", nil)
if err != nil {
return fmt.Errorf("create dummy data channel failed: %w", err)
}
dc.OnOpen(func() { dc.Close() })
dc.OnError(func(error) { dc.Close() })
return
}
func (egp *EgressProxy) startTunnels(ctx context.Context) (err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var wg sync.WaitGroup
defer wg.Wait()
for _, ep := range egp.endpoints {
if err = ctx.Err(); err != nil {
return
}
dc, err := egp.peer.CreateDataChannel(ep.String(), nil)
if err != nil {
cancel()
return fmt.Errorf("create data channel failed: %w", err)
}
wg.Add(1)
ep := ep
go func() {
defer wg.Done()
if err := egp.startTunnel(ctx, dc, ep); err != nil {
log.Println("egress: create tunnel failed: ", err)
}
}()
}
return
}
func (egp *EgressProxy) startTunnel(ctx context.Context, dc *webrtc.DataChannel, ep Endpoint) (err error) {
defer dc.Close()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
dcc, err := NewDataChannelConn(ctx, dc)
if err != nil {
return fmt.Errorf("create new datachannel connection failed: %w", err)
}
defer dcc.Close()
session, err := smux.Client(dcc, nil)
if err != nil {
return fmt.Errorf("create client session failed: %w", err)
}
defer session.Close()
listener, err := net.Listen("tcp", ep.local)
if err != nil {
return fmt.Errorf("listen to local socket failed: %w", err)
}
defer listener.Close()
go func() {
chanRecv(ctx, session.CloseChan())
cancel()
listener.Close()
}()
for {
if ctx.Err() != nil {
break
}
conn, err := listener.Accept()
if err != nil {
log.Println("egress: accept connection error:", err)
continue
}
log.Println("egress: new connection: ", conn.RemoteAddr())
stream, err := session.OpenStream()
if err != nil {
log.Println("egress: open stream error:", err)
continue
}
go func() {
err = relay(conn, stream)
if err != nil {
log.Println("egress: relay error:", err)
}
}()
}
return
}
func (egp *EgressProxy) Stop() (err error) {
return egp.peer.Close()
}