-
Notifications
You must be signed in to change notification settings - Fork 3
/
signal.go
254 lines (210 loc) · 6.43 KB
/
signal.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
package star
import (
"context"
"errors"
"fmt"
"github.com/libp2p/go-libp2p-core/mux"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
ma "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multiaddr-net"
"github.com/pion/datachannel"
"github.com/pion/webrtc/v2"
"strings"
)
const (
maxMessageSize = 8192
messagePrefix = "42"
)
type signal struct {
transport transport.Transport
peerID peer.ID
peerMultiaddr ma.Multiaddr
signalMultiaddr ma.Multiaddr
acceptedCh <-chan transport.CapableConn
handshakeDataCh chan<- handshakeData
handshakeSubscription *handshakeSubscription
webRTCConfiguration webrtc.Configuration
multiplexer mux.Multiplexer
stopCh chan<- struct{}
}
type SignalConfiguration struct {
URLPath string
}
type sessionProperties struct {
SID string `json:"sid"`
PingIntervalMillis int64 `json:"pingInterval"`
PingTimeoutMillis int64 `json:"pingTimeout"`
}
var webrtcapi *webrtc.API
func init() {
settingEngine := webrtc.SettingEngine{}
settingEngine.DetachDataChannels()
webrtcapi = webrtc.NewAPI(webrtc.WithSettingEngine(settingEngine))
}
func newSignal(transport transport.Transport, signalMultiaddr ma.Multiaddr, addressBook addressBook, peerID peer.ID,
signalConfiguration SignalConfiguration, webRTCConfiguration webrtc.Configuration, multiplexer mux.Multiplexer) (*signal, error) {
url, err := createSignalURL(signalMultiaddr, signalConfiguration)
if err != nil {
return nil, err
}
peerMultiaddr, err := createPeerMultiaddr(signalMultiaddr, peerID)
if err != nil {
return nil, err
}
smartAddressBook := decorateSelfIgnoreAddressBook(addressBook, peerID)
handshakeSubscription := newHandshakeSubscription()
stopCh := make(chan struct{}, 2)
acceptedCh, handshakeDataCh := startClient(url, peerMultiaddr, smartAddressBook, handshakeSubscription, stopCh)
return &signal{
transport: transport,
peerID: peerID,
peerMultiaddr: peerMultiaddr,
signalMultiaddr: signalMultiaddr,
acceptedCh: acceptedCh,
handshakeSubscription: handshakeSubscription,
handshakeDataCh: handshakeDataCh,
stopCh: stopCh,
webRTCConfiguration: webRTCConfiguration,
multiplexer: multiplexer,
}, nil
}
func createSignalURL(addr ma.Multiaddr, configuration SignalConfiguration) (string, error) {
websocketAddr := addr.Decapsulate(protocolMultiaddr)
var buf strings.Builder
buf.WriteString(readProtocolForSignalURL(websocketAddr))
_, hostPort, err := manet.DialArgs(websocketAddr)
if err != nil {
return "", err
}
buf.WriteString(hostPort)
buf.WriteString(configuration.URLPath)
return buf.String(), nil
}
func createPeerMultiaddr(signalMultiaddr ma.Multiaddr, peerID peer.ID) (ma.Multiaddr, error) {
ipfsMultiaddr, err := ma.NewMultiaddr(fmt.Sprintf("/%s/%s", ipfsProtocolName, peerID.String()))
if err != nil {
logger.Fatal(err)
}
return signalMultiaddr.Encapsulate(ipfsMultiaddr), nil
}
func readProtocolForSignalURL(maddr ma.Multiaddr) string {
if _, err := maddr.ValueForProtocol(wssProtocolCode); err == nil {
return "wss://"
}
return "ws://"
}
func (s *signal) dial(ctx context.Context, remotePeerID peer.ID) (transport.CapableConn, error) {
peerConnection, err := webrtcapi.NewPeerConnection(s.webRTCConfiguration)
if err != nil {
return nil, err
}
offerDescription, err := peerConnection.CreateOffer(nil)
if err != nil {
return nil, err
}
err = peerConnection.SetLocalDescription(offerDescription)
if err != nil {
return nil, err
}
dstMultiaddr, err := ma.NewMultiaddr(fmt.Sprintf("/%s/%s", ipfsProtocolName, remotePeerID.String()))
if err != nil {
return nil, err
}
offer := handshakeData{
IntentID: createRandomIntentID(),
SrcMultiaddr: s.peerMultiaddr.String(),
DstMultiaddr: s.signalMultiaddr.Encapsulate(dstMultiaddr).String(),
Signal: offerDescription,
}
answer, err := s.doHandshake(ctx, offer)
if err != nil {
return nil, err
}
err = peerConnection.SetRemoteDescription(answer.Signal)
if err != nil {
return nil, err
}
return s.openConnection(ctx, offer.DstMultiaddr, peerConnection, false)
}
func (s *signal) accept() (transport.CapableConn, error) {
offer, ok := <-s.handshakeSubscription.unsubscribed()
if !ok {
return nil, errors.New("subscription channel has been closed")
}
peerConnection, err := webrtcapi.NewPeerConnection(s.webRTCConfiguration)
if err != nil {
return nil, err
}
err = peerConnection.SetRemoteDescription(offer.Signal)
if err != nil {
return nil, err
}
answerDescription, err := peerConnection.CreateAnswer(nil)
if err != nil {
return nil, err
}
err = peerConnection.SetLocalDescription(answerDescription)
if err != nil {
return nil, err
}
answer := handshakeData{
IntentID: offer.IntentID,
SrcMultiaddr: offer.SrcMultiaddr,
DstMultiaddr: s.peerMultiaddr.String(),
Signal: answerDescription,
Answer: true,
}
s.answerHandshake(answer)
return s.openConnection(context.Background(), offer.SrcMultiaddr, peerConnection, true)
}
func (s *signal) openConnection(ctx context.Context, destination string, peerConnection *webrtc.PeerConnection, isServer bool) (transport.CapableConn, error) {
dstMultiaddr, err := ma.NewMultiaddr(destination)
if err != nil {
return nil, err
}
value, err := dstMultiaddr.ValueForProtocol(ma.P_P2P)
if err != nil {
return nil, err
}
remotePeerID, err := peer.IDB58Decode(value)
if err != nil {
return nil, err
}
var detachedDataChannel datachannel.ReadWriteCloser
if !isServer {
channel, err := peerConnection.CreateDataChannel("data", nil)
if err != nil {
return nil, err
}
detachedCh := detachDataChannel(channel)
select {
case detached := <-detachedCh:
if detached.err != nil {
return nil, detached.err
}
detachedDataChannel = detached.dataChannel
case <-ctx.Done():
return nil, errors.New("detaching data channel cancelled")
}
}
return newConnection(connectionConfiguration{
remotePeerID: remotePeerID,
remotePeerMultiaddr: dstMultiaddr,
localPeerID: s.peerID,
localPeerMultiaddr: s.peerMultiaddr,
transport: s.transport,
multiplexer: s.multiplexer,
isServer: isServer,
}, peerConnection, detachedDataChannel), nil
}
func (s *signal) close() error {
return s.stopClient()
}
func (s *signal) stopClient() error {
s.stopCh <- struct{}{}
return nil
}
func createRandomIntentID() string {
return createRandomID("signal")
}