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