This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
listener.go
164 lines (131 loc) · 3.51 KB
/
listener.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
package libp2pwebrtcdirect
import (
"context"
"errors"
"fmt"
"net"
"net/http"
tpt "github.com/libp2p/go-libp2p-core/transport"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/pion/webrtc/v3"
)
// 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) {
lnet, lnaddr, err := manet.DialArgs(config.maAddr)
if err != nil {
return nil, err
}
ln, err := net.Listen(lnet, lnaddr)
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 {
log.Warnf("failed to start server: %v", srvErr)
}
}()
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 {
log.Warnf("failed to handle request: failed to parse signal")
return
}
answer, err := l.handleSignal(signals[0])
if err != nil {
log.Warnf("failed to handle request: failed to setup connection: %v", err)
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
_, err = fmt.Fprint(w, answer)
if err != nil {
log.Warnf("failed to handle request: failed to send answer: %v", err)
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
pc, err := api.NewPeerConnection(l.config.transport.webrtcOptions)
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)
}
// Complete ICE Gathering for single-shot signaling.
gatherComplete := webrtc.GatheringCompletePromise(pc)
err = pc.SetLocalDescription(answer)
if err != nil {
return "", fmt.Errorf("failed to set local description: %v", err)
}
<-gatherComplete
answerEnc, err := encodeSignal(*pc.LocalDescription())
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.
func (l *Listener) Accept() (tpt.CapableConn, error) {
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
}