forked from strukturag/nextcloud-spreed-signaling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_proxy.go
270 lines (219 loc) · 6.05 KB
/
api_proxy.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
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2020 struktur AG
*
* @author Joachim Bauch <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package signaling
import (
"fmt"
"gopkg.in/dgrijalva/jwt-go.v3"
)
type ProxyClientMessage struct {
// The unique request id (optional).
Id string `json:"id,omitempty"`
// The type of the request.
Type string `json:"type"`
// Filled for type "hello"
Hello *HelloProxyClientMessage `json:"hello,omitempty"`
Bye *ByeProxyClientMessage `json:"bye,omitempty"`
Command *CommandProxyClientMessage `json:"command,omitempty"`
Payload *PayloadProxyClientMessage `json:"payload,omitempty"`
}
func (m *ProxyClientMessage) CheckValid() error {
switch m.Type {
case "":
return fmt.Errorf("type missing")
case "hello":
if m.Hello == nil {
return fmt.Errorf("hello missing")
} else if err := m.Hello.CheckValid(); err != nil {
return err
}
case "bye":
if m.Bye != nil {
// Bye contents are optional
if err := m.Bye.CheckValid(); err != nil {
return err
}
}
case "command":
if m.Command == nil {
return fmt.Errorf("command missing")
} else if err := m.Command.CheckValid(); err != nil {
return err
}
case "payload":
if m.Payload == nil {
return fmt.Errorf("payload missing")
} else if err := m.Payload.CheckValid(); err != nil {
return err
}
}
return nil
}
func (m *ProxyClientMessage) NewErrorServerMessage(e *Error) *ProxyServerMessage {
return &ProxyServerMessage{
Id: m.Id,
Type: "error",
Error: e,
}
}
func (m *ProxyClientMessage) NewWrappedErrorServerMessage(e error) *ProxyServerMessage {
return m.NewErrorServerMessage(NewError("internal_error", e.Error()))
}
// ProxyServerMessage is a message that is sent from the server to a client.
type ProxyServerMessage struct {
Id string `json:"id,omitempty"`
Type string `json:"type"`
Error *Error `json:"error,omitempty"`
Hello *HelloProxyServerMessage `json:"hello,omitempty"`
Bye *ByeProxyServerMessage `json:"bye,omitempty"`
Command *CommandProxyServerMessage `json:"command,omitempty"`
Payload *PayloadProxyServerMessage `json:"payload,omitempty"`
Event *EventProxyServerMessage `json:"event,omitempty"`
}
func (r *ProxyServerMessage) CloseAfterSend(session Session) bool {
if r.Type == "bye" {
return true
}
return false
}
// Type "hello"
type TokenClaims struct {
jwt.StandardClaims
}
type HelloProxyClientMessage struct {
Version string `json:"version"`
ResumeId string `json:"resumeid"`
Features []string `json:"features,omitempty"`
// The authentication credentials.
Token string `json:"token"`
}
func (m *HelloProxyClientMessage) CheckValid() error {
if m.Version != HelloVersion {
return fmt.Errorf("unsupported hello version: %s", m.Version)
}
if m.ResumeId == "" {
if m.Token == "" {
return fmt.Errorf("token missing")
}
}
return nil
}
type HelloProxyServerMessage struct {
Version string `json:"version"`
SessionId string `json:"sessionid"`
Server *HelloServerMessageServer `json:"server,omitempty"`
}
// Type "bye"
type ByeProxyClientMessage struct {
}
func (m *ByeProxyClientMessage) CheckValid() error {
// No additional validation required.
return nil
}
type ByeProxyServerMessage struct {
Reason string `json:"reason"`
}
// Type "command"
type CommandProxyClientMessage struct {
Type string `json:"type"`
StreamType string `json:"streamType,omitempty"`
PublisherId string `json:"publisherId,omitempty"`
ClientId string `json:"clientId,omitempty"`
}
func (m *CommandProxyClientMessage) CheckValid() error {
switch m.Type {
case "":
return fmt.Errorf("type missing")
case "create-publisher":
if m.StreamType == "" {
return fmt.Errorf("stream type missing")
}
case "create-subscriber":
if m.PublisherId == "" {
return fmt.Errorf("publisher id missing")
}
if m.StreamType == "" {
return fmt.Errorf("stream type missing")
}
case "delete-publisher":
fallthrough
case "delete-subscriber":
if m.ClientId == "" {
return fmt.Errorf("client id missing")
}
}
return nil
}
type CommandProxyServerMessage struct {
Id string `json:"id,omitempty"`
}
// Type "payload"
type PayloadProxyClientMessage struct {
Type string `json:"type"`
ClientId string `json:"clientId"`
Payload map[string]interface{} `json:"payload,omitempty"`
}
func (m *PayloadProxyClientMessage) CheckValid() error {
switch m.Type {
case "":
return fmt.Errorf("type missing")
case "offer":
fallthrough
case "answer":
fallthrough
case "candidate":
if len(m.Payload) == 0 {
return fmt.Errorf("payload missing")
}
case "endOfCandidates":
fallthrough
case "requestoffer":
// No payload required.
}
if m.ClientId == "" {
return fmt.Errorf("client id missing")
}
return nil
}
type PayloadProxyServerMessage struct {
Type string `json:"type"`
ClientId string `json:"clientId"`
Payload map[string]interface{} `json:"payload"`
}
// Type "event"
type EventProxyServerMessage struct {
Type string `json:"type"`
ClientId string `json:"clientId,omitempty"`
Load int64 `json:"load,omitempty"`
}
// Information on a proxy in the etcd cluster.
type ProxyInformationEtcd struct {
Address string `json:"address"`
}
func (p *ProxyInformationEtcd) CheckValid() error {
if p.Address == "" {
return fmt.Errorf("address missing")
}
if p.Address[len(p.Address)-1] != '/' {
p.Address += "/"
}
return nil
}