forked from aogz/tokbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokbox.go
executable file
·362 lines (298 loc) · 8.81 KB
/
tokbox.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package tokbox
import (
"bytes"
"io"
"net/http"
"net/url"
"encoding/base64"
"encoding/json"
"crypto/hmac"
"crypto/sha1"
"fmt"
"math/rand"
"strings"
"time"
"sync"
"golang.org/x/net/context"
jwt "github.com/dgrijalva/jwt-go"
"github.com/google/uuid"
)
const (
apiHost = "https://api.opentok.com"
apiSession = "/session/create"
apiStartArchivingURL = "/v2/project/%s/archive"
apiStopArchivingURL = "/v2/project/%s/archive/%s/stop"
)
// MediaMode is the mode of media
type MediaMode string
const (
// MediaRouter The session will send streams using the OpenTok Media Router.
MediaRouter MediaMode = "disabled"
// P2P The session will attempt send streams directly between clients.
// If clients cannot connect due to firewall restrictions,
// the session uses the OpenTok TURN server to relay streams.
P2P = "enabled"
)
// ArchiveMode is the mode of archiving
type ArchiveMode string
const (
// ManualArchive The session will be manually archived (default option).
ManualArchive ArchiveMode = "manual"
// AlwaysArchive The session will be automatically archived.
AlwaysArchive = "always"
)
// Role is the type of user to be used
type Role string
const (
// Publisher A publisher can publish streams, subscribe to streams, and signal.
Publisher Role = "publisher"
// Subscriber A subscriber can only subscribe to streams.
Subscriber = "subscriber"
// Moderator In addition to the privileges granted to a publisher,
// in clients using the OpenTok.js 2.2 library,
// a moderator can call the <code>forceUnpublish()</code> and <code>forceDisconnect()</code>
// method of the Session object.
Moderator = "moderator"
)
// Tokbox is the main struct to be used for API
type Tokbox struct {
apiKey string
partnerSecret string
betaURL string //Endpoint for Beta Programs
}
// Session tokbox session
type Session struct {
SessionID string `json:"session_id"`
ProjectID string `json:"project_id"`
PartnerID string `json:"partner_id"`
CreateDt string `json:"create_dt"`
SessionStatus string `json:"session_status"`
MediaServerURL string `json:"media_server_url"`
T *Tokbox `json:"-"`
}
// Archive struct represents archive create response
type Archive struct {
CreatedAt int `json:"createdAt"`
Duration int `json:"duration"`
HasAudio bool `json:"hasAudio"`
HasVideo bool `json:"hasVideo"`
ID string `json:"id"`
Name string `json:"name"`
OutputMode string `json:"outputMode"`
ProjectID int `json:"projectId"`
Reason string `json:"reason"`
Resolution string `json:"resolution"`
SessionID string `json:"sessionId"`
Size int `json:"side"`
Status string `json:"status"`
URL string `json:"url"`
S *Session `json:"-"`
}
// New creates a new tokbox instance
func New(apikey, partnerSecret string) *Tokbox {
return &Tokbox{apikey, partnerSecret, ""}
}
func (t *Tokbox) jwtToken() (string, error) {
type TokboxClaims struct {
Ist string `json:"ist,omitempty"`
jwt.StandardClaims
}
claims := TokboxClaims{
"project",
jwt.StandardClaims{
Issuer: t.apiKey,
IssuedAt: time.Now().UTC().Unix(),
ExpiresAt: time.Now().UTC().Unix() + (2 * 24 * 60 * 60), // 2 hours; //NB: The maximum allowed expiration time range is 5 minutes.
Id: uuid.NewString(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(t.partnerSecret))
}
// NewSession Creates a new tokbox session or returns an error.
// See README file for full documentation: https://github.com/aogz/tokbox
// NOTE: ctx must be nil if *not* using Google App Engine
func (t *Tokbox) NewSession(location string, mm MediaMode, am ArchiveMode, ctx ...context.Context) (*Session, error) {
params := url.Values{}
if len(location) > 0 {
params.Add("location", location)
}
params.Add("p2p.preference", string(mm))
params.Add("archiveMode", string(am))
var endpoint string
if t.betaURL == "" {
endpoint = apiHost
} else {
endpoint = t.betaURL
}
req, err := http.NewRequest("POST", endpoint+apiSession, strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
//Create jwt token
jwt, err := t.jwtToken()
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("X-OPENTOK-AUTH", jwt)
if len(ctx) == 0 {
ctx = append(ctx, nil)
}
res, err := client(ctx[0]).Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("Tokbox returns error code: %v", res.StatusCode)
}
var s []Session
if err = json.NewDecoder(res.Body).Decode(&s); err != nil {
return nil, err
}
if len(s) < 1 {
return nil, fmt.Errorf("Tokbox did not return a session")
}
o := s[0]
o.T = t
return &o, nil
}
// StartArchiving starts archiving session
func (s *Session) StartArchiving(archiveVideo bool, archiveAudio bool, ctx ...context.Context) (*Archive, error) {
var archive Archive
values := map[string]interface{}{
"sessionId": s.SessionID,
"hasAudio": archiveAudio,
"hasVideo": archiveVideo,
}
jsonValue, _ := json.Marshal(values)
url := fmt.Sprintf(apiHost+apiStartArchivingURL, s.T.apiKey)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
// Create jwt token
jwt, err := s.T.jwtToken()
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-OPENTOK-AUTH", jwt)
if len(ctx) == 0 {
ctx = append(ctx, nil)
}
res, err := client(ctx[0]).Do(req)
if err != nil {
fmt.Println(err)
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
bodyBytes, _ := io.ReadAll(res.Body)
stringResponse := string(bodyBytes)
return nil, fmt.Errorf("Tokbox returns error code: %v. Message: %s", res.StatusCode, stringResponse)
}
if err = json.NewDecoder(res.Body).Decode(&archive); err != nil {
return nil, err
}
archive.S = s
return &archive, nil
}
// StopArchiving stops current archive
func (archive *Archive) StopArchiving(ctx ...context.Context) (*Archive, error) {
var response Archive
url := fmt.Sprintf(apiHost+apiStopArchivingURL, archive.S.T.apiKey, archive.ID)
req, err := http.NewRequest("POST", url, bytes.NewBufferString(""))
if err != nil {
return nil, err
}
// Create jwt token
jwt, err := archive.S.T.jwtToken()
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-OPENTOK-AUTH", jwt)
if len(ctx) == 0 {
ctx = append(ctx, nil)
}
res, err := client(ctx[0]).Do(req)
if err != nil {
fmt.Println(err)
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
bodyBytes, _ := io.ReadAll(res.Body)
stringResponse := string(bodyBytes)
return nil, fmt.Errorf("Tokbox returns error code: %v. Message: %s", res.StatusCode, stringResponse)
}
if err = json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}
response.S = archive.S
return &response, nil
}
// Token to crate json web token
func (s *Session) Token(role Role, connectionData string, expiration int64) (string, error) {
now := time.Now().UTC().Unix()
dataStr := ""
dataStr += "session_id=" + url.QueryEscape(s.SessionID)
dataStr += "&create_time=" + url.QueryEscape(fmt.Sprintf("%d", now))
if expiration > 0 {
dataStr += "&expire_time=" + url.QueryEscape(fmt.Sprintf("%d", now+expiration))
}
if len(role) > 0 {
dataStr += "&role=" + url.QueryEscape(string(role))
}
if len(connectionData) > 0 {
dataStr += "&connection_data=" + url.QueryEscape(connectionData)
}
dataStr += "&nonce=" + url.QueryEscape(fmt.Sprintf("%d", rand.Intn(999999)))
h := hmac.New(sha1.New, []byte(s.T.partnerSecret))
n, err := h.Write([]byte(dataStr))
if err != nil {
return "", err
}
if n != len(dataStr) {
return "", fmt.Errorf("hmac not enough bytes written %d != %d", n, len(dataStr))
}
preCoded := ""
preCoded += "partner_id=" + s.T.apiKey
preCoded += "&sig=" + fmt.Sprintf("%x:%s", h.Sum(nil), dataStr)
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
encoder.Write([]byte(preCoded))
encoder.Close()
return fmt.Sprintf("T1==%s", buf.String()), nil
}
// Tokens ...
func (s *Session) Tokens(n int, multithread bool, role Role, connectionData string, expiration int64) []string {
ret := []string{}
if multithread {
var w sync.WaitGroup
var lock sync.Mutex
w.Add(n)
for i := 0; i < n; i++ {
go func(role Role, connectionData string, expiration int64) {
a, e := s.Token(role, connectionData, expiration)
if e == nil {
lock.Lock()
ret = append(ret, a)
lock.Unlock()
}
w.Done()
}(role, connectionData, expiration)
}
w.Wait()
return ret
}
for i := 0; i < n; i++ {
a, e := s.Token(role, connectionData, expiration)
if e == nil {
ret = append(ret, a)
}
}
return ret
}