-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.go
246 lines (205 loc) · 5.7 KB
/
connection.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
package star
import (
"errors"
"fmt"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/mux"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
"github.com/multiformats/go-multiaddr"
ma "github.com/multiformats/go-multiaddr"
"github.com/pion/datachannel"
"github.com/pion/webrtc/v2"
"net"
"sync"
)
type connection struct {
id string
peerConnection *webrtc.PeerConnection
initChannel datachannel.ReadWriteCloser
configuration connectionConfiguration
dataChannelDetachedCh chan chan detachResult
m sync.RWMutex
muxedConnection mux.MuxedConn
}
var _ transport.CapableConn = new(connection)
var fakeNetAddress net.Addr
func init() {
var err error
fakeNetAddress, err = net.ResolveTCPAddr("tcp4", "0.0.0.0:1")
if err != nil {
logger.Fatalf("can't resolve fake TCP address")
}
}
type connectionConfiguration struct {
remotePeerID peer.ID
remotePeerMultiaddr ma.Multiaddr
localPeerID peer.ID
localPeerMultiaddr ma.Multiaddr
transport transport.Transport
multiplexer mux.Multiplexer
isServer bool
}
type detachResult struct {
dataChannel datachannel.ReadWriteCloser
err error
}
func newConnection(configuration connectionConfiguration, peerConnection *webrtc.PeerConnection,
initChannel datachannel.ReadWriteCloser) *connection {
dataChannelDetachedCh := make(chan chan detachResult)
peerConnection.OnDataChannel(func(dc *webrtc.DataChannel) {
dataChannelDetachedCh <- detachDataChannel(dc)
})
return &connection{
id: createRandomID("connection"),
peerConnection: peerConnection,
configuration: configuration,
dataChannelDetachedCh: dataChannelDetachedCh,
initChannel: initChannel,
}
}
func detachDataChannel(dataChannel *webrtc.DataChannel) chan detachResult {
detachedCh := make(chan detachResult)
dataChannel.OnOpen(func() {
channel, err := dataChannel.Detach()
detachedCh <- detachResult{channel, err}
})
return detachedCh
}
func (c *connection) OpenStream() (mux.MuxedStream, error) {
logger.Debugf("%s: Open stream", c.id)
muxedConnection, err := c.getMuxedConnection()
if err != nil {
return nil, err
}
if muxedConnection != nil {
return muxedConnection.OpenStream()
}
rawDataChannel := c.checkInitChannel()
if rawDataChannel == nil {
pc, err := c.getPeerConnection()
if err != nil {
return nil, err
}
dc, err := pc.CreateDataChannel("data", nil)
if err != nil {
return nil, err
}
detached := <-detachDataChannel(dc)
if detached.err != nil {
return nil, detached.err
}
rawDataChannel = detached.dataChannel
}
return c.muxedConnection.OpenStream()
}
func (c *connection) checkInitChannel() datachannel.ReadWriteCloser {
c.m.Lock()
defer c.m.Unlock()
if c.initChannel != nil {
ch := c.initChannel
c.initChannel = nil
return ch
}
return nil
}
func (c *connection) getPeerConnection() (*webrtc.PeerConnection, error) {
c.m.RLock()
pc := c.peerConnection
c.m.RUnlock()
if pc == nil {
return nil, errors.New("peer connection closed")
}
return pc, nil
}
func (c *connection) AcceptStream() (mux.MuxedStream, error) {
logger.Debugf("%s: Accept stream", c.id)
muxedConnection, err := c.getMuxedConnection()
if err != nil {
return nil, err
}
if muxedConnection != nil {
return muxedConnection.AcceptStream()
}
rawDataChannel := c.checkInitChannel()
if rawDataChannel == nil {
rawDataChannel, err = c.awaitDataChannelDetached()
}
return c.muxedConnection.AcceptStream()
}
func (c *connection) getMuxedConnection() (mux.MuxedConn, error) {
c.m.Lock()
defer c.m.Unlock()
if c.muxedConnection != nil {
return c.muxedConnection, nil
}
rawDataChannel := c.initChannel
if rawDataChannel == nil {
var err error
rawDataChannel, err = c.awaitDataChannelDetached()
if err != nil {
return nil, err
}
}
var err error
c.muxedConnection, err = c.configuration.multiplexer.NewConn(newStream(rawDataChannel, fakeNetAddress),
c.configuration.isServer)
if err != nil {
return nil, err
}
return c.muxedConnection, nil
}
func (c *connection) awaitDataChannelDetached() (datachannel.ReadWriteCloser, error) {
detachedCh, ok := <-c.dataChannelDetachedCh
if !ok {
return nil, errors.New("connection closed")
}
detached := <-detachedCh
return detached.dataChannel, detached.err
}
func (c *connection) IsClosed() bool {
c.m.RLock()
pc := c.peerConnection
c.m.RUnlock()
return pc == nil
}
func (c *connection) Close() error {
logger.Debugf("%s: Close connection (no actions)", c.id)
c.m.Lock()
defer c.m.Unlock()
var err error
if c.peerConnection != nil {
err = c.peerConnection.Close()
}
c.peerConnection = nil
close(c.dataChannelDetachedCh)
return err
}
func (c *connection) LocalPeer() peer.ID {
return c.configuration.localPeerID
}
func (c *connection) RemotePeer() peer.ID {
return c.configuration.remotePeerID
}
func (c *connection) LocalMultiaddr() multiaddr.Multiaddr {
return c.configuration.localPeerMultiaddr
}
func (c *connection) RemoteMultiaddr() multiaddr.Multiaddr {
return c.configuration.remotePeerMultiaddr
}
func (c *connection) Transport() transport.Transport {
return c.configuration.transport
}
func (c *connection) String() string {
return fmt.Sprintf("WebRTC connection (ID: %s, localPeerID: %v, localPeerMultiaddr: %v, remotePeerID: %v, remotePeerMultiaddr: %v",
c.id, c.configuration.localPeerID, c.configuration.localPeerMultiaddr,
c.configuration.remotePeerID, c.configuration.remotePeerMultiaddr)
}
func (c *connection) LocalPrivateKey() crypto.PrivKey {
logger.Warningf("%s: Local private key undefined", c.id)
return nil
}
func (c *connection) RemotePublicKey() crypto.PubKey {
logger.Warningf("%s: Remote public key undefined", c.id)
return nil
}