forked from porjo/babelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
270 lines (244 loc) · 6.21 KB
/
http.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/pion/webrtc/v4"
)
const PingInterval = 10 * time.Second
const WriteWait = 10 * time.Second
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type wsMsg struct {
Key string
Value json.RawMessage
}
type CmdConnect struct {
Channel string
Password string
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
gconn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
clientAddress := gconn.RemoteAddr().String()
xFwdIP := r.Header.Get("X-Forwarded-For")
if xFwdIP != "" {
clientAddress += " (" + xFwdIP + ")"
}
c := NewConn(gconn)
defer c.Close()
c.peer, err = NewWebRTCPeer()
if err != nil {
c.logger.Error("NewWebRTCPeer error", "err", err.Error())
return
}
c.logger.Info("client connected", "addr", clientAddress)
// setup ping/pong to keep connection open
pingCh := time.Tick(PingInterval)
// websocket connections support one concurrent reader and one concurrent writer.
// we put reads in a new goroutine below and leave writes in the main goroutine
wsInMsg := make(chan wsMsg)
wsReadQuitChan := make(chan struct{})
go func() {
defer close(wsReadQuitChan)
defer c.logger.Debug("ws read goroutine quit")
for {
msgType, raw, err := c.wsConn.ReadMessage()
if err != nil {
c.logger.Error("ReadMessage error", "err", err)
return
}
c.logger.Debug("read message", "msg", string(raw))
if msgType != websocket.TextMessage {
c.logger.Error("unknown message type", "type", msgType)
return
}
var msg wsMsg
err = json.Unmarshal(raw, &msg)
if err != nil {
c.logger.Error(err.Error())
return
}
wsInMsg <- msg
}
}()
for {
select {
case msg := <-wsInMsg:
err = c.handleWSMsg(msg)
if err != nil {
j, _ := json.Marshal(err.Error())
m := wsMsg{Key: "error", Value: j}
err = c.writeMsg(m)
if err != nil {
c.logger.Error("writemsg error", "err", err.Error())
}
return
}
case <-wsReadQuitChan:
return
case <-c.quitchan:
c.logger.Debug("quitChan closed")
return
case info := <-c.infoChan:
j, err := json.Marshal(info)
if err != nil {
c.logger.Error("marshal error", "err", err.Error())
return
}
m := wsMsg{Key: "info", Value: j}
err = c.writeMsg(m)
if err != nil {
c.logger.Error("writemsg error", "err", err.Error())
return
}
case <-pingCh:
err := c.wsConn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(WriteWait))
if err != nil {
c.logger.Error("ping client error", "err", err.Error())
return
}
}
}
}
func (c *Conn) handleWSMsg(msg wsMsg) error {
var err error
switch msg.Key {
case "ice_candidate":
var candidate webrtc.ICECandidateInit
err = json.Unmarshal(msg.Value, &candidate)
if err != nil {
return err
}
if candidate.Candidate != "" {
if err = c.peer.pc.AddICECandidate(candidate); err != nil {
c.logger.Error("AddICECandidate error", "err", err.Error())
return err
}
}
case "get_channels":
// send list of channels to client
channels := reg.GetChannels()
c.logger.Debug("channels", "c", channels)
j, err := json.Marshal(channels)
if err != nil {
c.logger.Error("getchannels marshal", "err", err)
return err
}
m := wsMsg{Key: "channels", Value: j}
err = c.writeMsg(m)
if err != nil {
c.logger.Error(err.Error())
return err
}
case "session_subscriber":
// subscriber session is only partially setup here as we have to wait for
// channel selection to complete the setup
var offer webrtc.SessionDescription
err = json.Unmarshal(msg.Value, &offer)
if err != nil {
return err
}
if err := c.peer.pc.SetRemoteDescription(offer); err != nil {
return err
}
// If there is no error, send a success message
m := wsMsg{Key: "session_received"}
err = c.writeMsg(m)
if err != nil {
c.logger.Error(err.Error())
return err
}
case "session_publisher":
c.isPublisher = true
var offer webrtc.SessionDescription
err = json.Unmarshal(msg.Value, &offer)
if err != nil {
return err
}
err = c.setupSessionPublisher(offer)
if err != nil {
c.logger.Error("setupSession error", "err", err)
return err
}
if publisherPassword != "" {
m := wsMsg{Key: "password_required"}
err = c.writeMsg(m)
if err != nil {
c.logger.Error(err.Error())
return err
}
}
case "connect_publisher":
cmd := CmdConnect{}
err = json.Unmarshal(msg.Value, &cmd)
if err != nil {
return err
}
err := c.connectPublisher(cmd)
if err != nil {
c.logger.Error("connectPublisher error", "err", err)
return err
}
case "connect_subscriber":
cmd := CmdConnect{}
err = json.Unmarshal(msg.Value, &cmd)
if err != nil {
return err
}
// finish subscriber session setup here
c.channelName = cmd.Channel
err = c.setupSessionSubscriber()
if err != nil {
c.logger.Error("setupSession error", "err", err)
return err
}
if cmd.Channel == "" {
return fmt.Errorf("channel cannot be empty")
}
if channelRegexp.MatchString(cmd.Channel) {
return fmt.Errorf("channel name must contain only alphanumeric characters")
}
c.logger.Info("setting up subscriber for channel", "channel", c.channelName)
s := reg.NewSubscriber()
c.clientID = s.ID
go func() {
for {
select {
case <-c.quitchan:
return
case <-s.QuitChan:
j, _ := json.Marshal(c.channelName)
m := wsMsg{Key: "channel_closed", Value: j}
c.writeMsg(m)
close(c.quitchan)
return
}
}
}()
if err := reg.AddSubscriber(c.channelName, s); err != nil {
return err
}
}
return nil
}