2019-01-22 11:02:08 +00:00
|
|
|
package libp2pwebrtcdirect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2019-06-02 10:20:25 +00:00
|
|
|
tpt "github.com/libp2p/go-libp2p-core/transport"
|
2019-01-22 11:02:08 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2021-05-19 17:41:54 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2021-03-21 13:23:34 +00:00
|
|
|
"github.com/pion/webrtc/v3"
|
2019-01-22 11:02:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Listener is an interface closely resembling the net.Listener interface.
|
|
|
|
type Listener struct {
|
|
|
|
config *connConfig
|
|
|
|
accept chan *Conn
|
|
|
|
|
|
|
|
srv *http.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListener(config *connConfig) (*Listener, error) {
|
2019-08-21 13:16:17 +00:00
|
|
|
lnet, lnaddr, err := manet.DialArgs(config.maAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-22 11:02:08 +00:00
|
|
|
|
2019-08-21 13:16:17 +00:00
|
|
|
ln, err := net.Listen(lnet, lnaddr)
|
2019-01-22 11:02:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to listen: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the addr after listening
|
|
|
|
tcpMa, err := manet.FromNetAddr(ln.Addr())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed create ma: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpMa := tcpMa.Encapsulate(httpma)
|
|
|
|
maAddr := httpMa.Encapsulate(webrtcma)
|
|
|
|
|
|
|
|
config.addr = ln.Addr()
|
|
|
|
config.maAddr = maAddr
|
|
|
|
|
|
|
|
l := &Listener{
|
|
|
|
config: config,
|
|
|
|
accept: make(chan *Conn),
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", l.handler)
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
srvErr := srv.Serve(ln)
|
|
|
|
if srvErr != nil {
|
2021-05-19 17:41:54 +00:00
|
|
|
log.Warnf("failed to start server: %v", srvErr)
|
2019-01-22 11:02:08 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
l.srv = srv
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
signals, ok := r.Form["signal"]
|
|
|
|
if !ok || len(signals) != 1 {
|
2021-05-19 17:41:54 +00:00
|
|
|
log.Warnf("failed to handle request: failed to parse signal")
|
2019-01-22 11:02:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
answer, err := l.handleSignal(signals[0])
|
|
|
|
if err != nil {
|
2021-05-19 17:41:54 +00:00
|
|
|
log.Warnf("failed to handle request: failed to setup connection: %v", err)
|
2019-01-22 11:02:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:21:32 +00:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
2019-01-22 11:02:08 +00:00
|
|
|
_, err = fmt.Fprint(w, answer)
|
|
|
|
if err != nil {
|
2021-05-19 17:41:54 +00:00
|
|
|
log.Warnf("failed to handle request: failed to send answer: %v", err)
|
2019-01-22 11:02:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) handleSignal(offerStr string) (string, error) {
|
|
|
|
offer, err := decodeSignal(offerStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to decode offer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
api := l.config.transport.api
|
2019-02-21 14:19:05 +00:00
|
|
|
pc, err := api.NewPeerConnection(l.config.transport.webrtcOptions)
|
2019-01-22 11:02:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pc.SetRemoteDescription(offer); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to set remote description: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
answer, err := pc.CreateAnswer(nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to create answer: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:23:34 +00:00
|
|
|
// Complete ICE Gathering for single-shot signaling.
|
|
|
|
gatherComplete := webrtc.GatheringCompletePromise(pc)
|
2019-02-21 14:19:05 +00:00
|
|
|
err = pc.SetLocalDescription(answer)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to set local description: %v", err)
|
|
|
|
}
|
2021-03-21 13:23:34 +00:00
|
|
|
<-gatherComplete
|
2019-02-21 14:19:05 +00:00
|
|
|
|
2021-03-21 13:23:34 +00:00
|
|
|
answerEnc, err := encodeSignal(*pc.LocalDescription())
|
2019-01-22 11:02:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to encode answer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := newConn(l.config, pc, nil)
|
|
|
|
l.accept <- c
|
|
|
|
|
|
|
|
return answerEnc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept waits for and returns the next connection to the listener.
|
2019-06-02 10:20:25 +00:00
|
|
|
func (l *Listener) Accept() (tpt.CapableConn, error) {
|
2019-01-22 11:02:08 +00:00
|
|
|
conn, ok := <-l.accept
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Listener closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the listener.
|
|
|
|
// Any blocked Accept operations will be unblocked and return errors.
|
|
|
|
func (l *Listener) Close() error {
|
|
|
|
err := l.srv.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
close(l.accept)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address.
|
|
|
|
func (l *Listener) Addr() net.Addr {
|
|
|
|
return l.config.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multiaddr returns the listener's network Multi address.
|
|
|
|
func (l *Listener) Multiaddr() ma.Multiaddr {
|
|
|
|
return l.config.maAddr
|
|
|
|
}
|