2020-04-06 06:45:47 +00:00
|
|
|
package webtorrent
|
2020-04-05 03:55:14 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-20 00:21:31 +00:00
|
|
|
"expvar"
|
2020-04-05 03:55:14 +00:00
|
|
|
"fmt"
|
2020-04-20 00:21:31 +00:00
|
|
|
"io"
|
2020-04-05 03:55:14 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-04-20 00:21:31 +00:00
|
|
|
"github.com/anacrolix/missinggo/v2/pproffd"
|
2020-04-05 03:55:14 +00:00
|
|
|
"github.com/pion/datachannel"
|
|
|
|
|
|
|
|
"github.com/pion/webrtc/v2"
|
|
|
|
)
|
|
|
|
|
2020-04-07 02:17:21 +00:00
|
|
|
var (
|
2020-04-20 00:21:31 +00:00
|
|
|
metrics = expvar.NewMap("webtorrent")
|
|
|
|
api = func() *webrtc.API {
|
2020-04-13 04:31:39 +00:00
|
|
|
// Enable the detach API (since it's non-standard but more idiomatic).
|
2020-04-07 02:17:21 +00:00
|
|
|
s := webrtc.SettingEngine{}
|
|
|
|
s.DetachDataChannels()
|
|
|
|
return webrtc.NewAPI(webrtc.WithSettingEngine(s))
|
|
|
|
}()
|
|
|
|
config = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}}
|
|
|
|
newPeerConnectionMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
2020-04-20 00:21:31 +00:00
|
|
|
type wrappedPeerConnection struct {
|
|
|
|
*webrtc.PeerConnection
|
2020-05-03 08:45:12 +00:00
|
|
|
closeMu sync.Mutex
|
2020-04-20 00:21:31 +00:00
|
|
|
pproffd.CloseWrapper
|
2020-04-07 02:17:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 08:45:12 +00:00
|
|
|
func (me *wrappedPeerConnection) Close() error {
|
|
|
|
me.closeMu.Lock()
|
|
|
|
defer me.closeMu.Unlock()
|
2020-04-20 00:21:31 +00:00
|
|
|
return me.CloseWrapper.Close()
|
|
|
|
}
|
2020-04-05 03:55:14 +00:00
|
|
|
|
2020-05-03 08:45:12 +00:00
|
|
|
func newPeerConnection() (*wrappedPeerConnection, error) {
|
2020-04-20 00:21:31 +00:00
|
|
|
newPeerConnectionMu.Lock()
|
|
|
|
defer newPeerConnectionMu.Unlock()
|
|
|
|
pc, err := api.NewPeerConnection(config)
|
|
|
|
if err != nil {
|
2020-05-03 08:45:12 +00:00
|
|
|
return nil, err
|
2020-04-20 00:21:31 +00:00
|
|
|
}
|
2020-05-03 08:45:12 +00:00
|
|
|
return &wrappedPeerConnection{
|
|
|
|
PeerConnection: pc,
|
|
|
|
CloseWrapper: pproffd.NewCloseWrapper(pc),
|
2020-04-20 00:21:31 +00:00
|
|
|
}, nil
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 00:21:31 +00:00
|
|
|
// newOffer creates a transport and returns a WebRTC offer to be announced
|
|
|
|
func newOffer() (
|
2020-05-03 08:45:12 +00:00
|
|
|
peerConnection *wrappedPeerConnection,
|
2020-04-20 00:21:31 +00:00
|
|
|
dataChannel *webrtc.DataChannel,
|
|
|
|
offer webrtc.SessionDescription,
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
peerConnection, err = newPeerConnection()
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-20 00:21:31 +00:00
|
|
|
return
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
2020-04-20 00:21:31 +00:00
|
|
|
dataChannel, err = peerConnection.CreateDataChannel("webrtc-datachannel", nil)
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-20 00:21:31 +00:00
|
|
|
peerConnection.Close()
|
|
|
|
return
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
2020-04-20 00:21:31 +00:00
|
|
|
offer, err = peerConnection.CreateOffer(nil)
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-20 00:21:31 +00:00
|
|
|
peerConnection.Close()
|
|
|
|
return
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
err = peerConnection.SetLocalDescription(offer)
|
|
|
|
if err != nil {
|
2020-04-20 00:21:31 +00:00
|
|
|
peerConnection.Close()
|
|
|
|
return
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
2020-04-20 00:21:31 +00:00
|
|
|
return
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 00:21:31 +00:00
|
|
|
func initAnsweringPeerConnection(
|
2020-05-03 08:45:12 +00:00
|
|
|
peerConnection *wrappedPeerConnection,
|
2020-04-20 00:21:31 +00:00
|
|
|
offer webrtc.SessionDescription,
|
|
|
|
) (answer webrtc.SessionDescription, err error) {
|
|
|
|
err = peerConnection.SetRemoteDescription(offer)
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-20 00:21:31 +00:00
|
|
|
return
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
2020-04-20 00:21:31 +00:00
|
|
|
answer, err = peerConnection.CreateAnswer(nil)
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-20 00:21:31 +00:00
|
|
|
return
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
2020-04-20 00:21:31 +00:00
|
|
|
err = peerConnection.SetLocalDescription(answer)
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-20 00:21:31 +00:00
|
|
|
return
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
2020-04-20 00:21:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:08:43 +00:00
|
|
|
// newAnsweringPeerConnection creates a transport from a WebRTC offer and and returns a WebRTC answer to be
|
2020-04-20 00:21:31 +00:00
|
|
|
// announced.
|
2020-04-21 08:08:43 +00:00
|
|
|
func newAnsweringPeerConnection(offer webrtc.SessionDescription) (
|
2020-05-03 08:45:12 +00:00
|
|
|
peerConn *wrappedPeerConnection, answer webrtc.SessionDescription, err error,
|
2020-04-20 00:21:31 +00:00
|
|
|
) {
|
2020-04-21 08:08:43 +00:00
|
|
|
peerConn, err = newPeerConnection()
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-21 08:08:43 +00:00
|
|
|
err = fmt.Errorf("failed to create new connection: %w", err)
|
2020-04-20 00:21:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-04-21 08:08:43 +00:00
|
|
|
answer, err = initAnsweringPeerConnection(peerConn, offer)
|
2020-04-20 00:21:31 +00:00
|
|
|
if err != nil {
|
2020-04-21 08:08:43 +00:00
|
|
|
peerConn.Close()
|
2020-04-20 00:21:31 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *outboundOffer) setAnswer(answer webrtc.SessionDescription, onOpen func(datachannel.ReadWriteCloser)) error {
|
|
|
|
setDataChannelOnOpen(t.dataChannel, t.peerConnection, onOpen)
|
|
|
|
err := t.peerConnection.SetRemoteDescription(answer)
|
|
|
|
return err
|
|
|
|
}
|
2020-04-05 03:55:14 +00:00
|
|
|
|
2020-04-20 00:21:31 +00:00
|
|
|
type datachannelReadWriter interface {
|
|
|
|
datachannel.Reader
|
|
|
|
datachannel.Writer
|
|
|
|
io.Reader
|
|
|
|
io.Writer
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 00:21:31 +00:00
|
|
|
type ioCloserFunc func() error
|
2020-04-05 03:55:14 +00:00
|
|
|
|
2020-04-20 00:21:31 +00:00
|
|
|
func (me ioCloserFunc) Close() error {
|
|
|
|
return me()
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 00:21:31 +00:00
|
|
|
func setDataChannelOnOpen(
|
|
|
|
dc *webrtc.DataChannel,
|
2020-05-03 08:45:12 +00:00
|
|
|
pc *wrappedPeerConnection,
|
2020-04-20 00:21:31 +00:00
|
|
|
onOpen func(closer datachannel.ReadWriteCloser),
|
|
|
|
) {
|
2020-04-05 03:55:14 +00:00
|
|
|
dc.OnOpen(func() {
|
|
|
|
raw, err := dc.Detach()
|
|
|
|
if err != nil {
|
2020-04-20 00:21:31 +00:00
|
|
|
// This shouldn't happen if the API is configured correctly, and we call from OnOpen.
|
|
|
|
panic(err)
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
2020-04-20 00:21:31 +00:00
|
|
|
onOpen(hookDataChannelCloser(raw, pc))
|
2020-04-05 03:55:14 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-20 00:21:31 +00:00
|
|
|
|
2020-05-04 23:00:43 +00:00
|
|
|
// Hooks the datachannel's Close to Close the owning PeerConnection. The datachannel takes ownership
|
|
|
|
// and responsibility for the PeerConnection.
|
2020-05-03 08:45:12 +00:00
|
|
|
func hookDataChannelCloser(dcrwc datachannel.ReadWriteCloser, pc *wrappedPeerConnection) datachannel.ReadWriteCloser {
|
2020-04-20 00:21:31 +00:00
|
|
|
return struct {
|
|
|
|
datachannelReadWriter
|
|
|
|
io.Closer
|
|
|
|
}{
|
|
|
|
dcrwc,
|
|
|
|
ioCloserFunc(pc.Close),
|
|
|
|
}
|
|
|
|
}
|