forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webrtc.go
97 lines (78 loc) · 3.08 KB
/
webrtc.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
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"net/http"
"strconv"
"strings"
"time"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
func (a *App) GetWebrtcInfoForSession(sessionId string) (*model.WebrtcInfoResponse, *model.AppError) {
token, err := a.GetWebrtcToken(sessionId)
if err != nil {
return nil, err
}
result := &model.WebrtcInfoResponse{
Token: token,
GatewayUrl: *a.Config().WebrtcSettings.GatewayWebsocketUrl,
}
if len(*a.Config().WebrtcSettings.StunURI) > 0 {
result.StunUri = *a.Config().WebrtcSettings.StunURI
}
if len(*a.Config().WebrtcSettings.TurnURI) > 0 {
timestamp := strconv.FormatInt(utils.EndOfDay(time.Now().AddDate(0, 0, 1)).Unix(), 10)
username := timestamp + ":" + *a.Config().WebrtcSettings.TurnUsername
result.TurnUri = *a.Config().WebrtcSettings.TurnURI
result.TurnPassword = GenerateTurnPassword(username, *a.Config().WebrtcSettings.TurnSharedKey)
result.TurnUsername = username
}
return result, nil
}
func (a *App) GetWebrtcToken(sessionId string) (string, *model.AppError) {
if !*a.Config().WebrtcSettings.Enable {
return "", model.NewAppError("WebRTC.getWebrtcToken", "api.webrtc.disabled.app_error", nil, "", http.StatusNotImplemented)
}
token := base64.StdEncoding.EncodeToString([]byte(sessionId))
data := make(map[string]string)
data["janus"] = "add_token"
data["token"] = token
data["transaction"] = model.NewId()
data["admin_secret"] = *a.Config().WebrtcSettings.GatewayAdminSecret
rq, _ := http.NewRequest("POST", *a.Config().WebrtcSettings.GatewayAdminUrl, strings.NewReader(model.MapToJson(data)))
rq.Header.Set("Content-Type", "application/json")
if rp, err := a.HTTPClient(true).Do(rq); err != nil {
return "", model.NewAppError("WebRTC.Token", "model.client.connecting.app_error", nil, err.Error(), http.StatusInternalServerError)
} else if rp.StatusCode >= 300 {
defer consumeAndClose(rp)
return "", model.AppErrorFromJson(rp.Body)
} else {
janusResponse := model.GatewayResponseFromJson(rp.Body)
if janusResponse.Status != "success" {
return "", model.NewAppError("getWebrtcToken", "api.webrtc.register_token.app_error", nil, "", http.StatusInternalServerError)
}
}
return token, nil
}
func GenerateTurnPassword(username string, secret string) string {
key := []byte(secret)
h := hmac.New(sha1.New, key)
h.Write([]byte(username))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func (a *App) RevokeWebrtcToken(sessionId string) {
token := base64.StdEncoding.EncodeToString([]byte(sessionId))
data := make(map[string]string)
data["janus"] = "remove_token"
data["token"] = token
data["transaction"] = model.NewId()
data["admin_secret"] = *a.Config().WebrtcSettings.GatewayAdminSecret
rq, _ := http.NewRequest("POST", *a.Config().WebrtcSettings.GatewayAdminUrl, strings.NewReader(model.MapToJson(data)))
rq.Header.Set("Content-Type", "application/json")
// we do not care about the response
a.HTTPClient(true).Do(rq)
}