github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/webrtc.go (about) 1 // Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "crypto/hmac" 8 "crypto/sha1" 9 "encoding/base64" 10 "net/http" 11 "strconv" 12 "strings" 13 "time" 14 15 "github.com/mattermost/mattermost-server/model" 16 "github.com/mattermost/mattermost-server/utils" 17 ) 18 19 func (a *App) GetWebrtcInfoForSession(sessionId string) (*model.WebrtcInfoResponse, *model.AppError) { 20 token, err := a.GetWebrtcToken(sessionId) 21 if err != nil { 22 return nil, err 23 } 24 25 result := &model.WebrtcInfoResponse{ 26 Token: token, 27 GatewayUrl: *a.Config().WebrtcSettings.GatewayWebsocketUrl, 28 } 29 30 if len(*a.Config().WebrtcSettings.StunURI) > 0 { 31 result.StunUri = *a.Config().WebrtcSettings.StunURI 32 } 33 34 if len(*a.Config().WebrtcSettings.TurnURI) > 0 { 35 timestamp := strconv.FormatInt(utils.EndOfDay(time.Now().AddDate(0, 0, 1)).Unix(), 10) 36 username := timestamp + ":" + *a.Config().WebrtcSettings.TurnUsername 37 38 result.TurnUri = *a.Config().WebrtcSettings.TurnURI 39 result.TurnPassword = GenerateTurnPassword(username, *a.Config().WebrtcSettings.TurnSharedKey) 40 result.TurnUsername = username 41 } 42 43 return result, nil 44 } 45 46 func (a *App) GetWebrtcToken(sessionId string) (string, *model.AppError) { 47 if !*a.Config().WebrtcSettings.Enable { 48 return "", model.NewAppError("WebRTC.getWebrtcToken", "api.webrtc.disabled.app_error", nil, "", http.StatusNotImplemented) 49 } 50 51 token := base64.StdEncoding.EncodeToString([]byte(sessionId)) 52 53 data := make(map[string]string) 54 data["janus"] = "add_token" 55 data["token"] = token 56 data["transaction"] = model.NewId() 57 data["admin_secret"] = *a.Config().WebrtcSettings.GatewayAdminSecret 58 59 rq, _ := http.NewRequest("POST", *a.Config().WebrtcSettings.GatewayAdminUrl, strings.NewReader(model.MapToJson(data))) 60 rq.Header.Set("Content-Type", "application/json") 61 62 if rp, err := a.HTTPService.MakeClient(true).Do(rq); err != nil { 63 return "", model.NewAppError("WebRTC.Token", "model.client.connecting.app_error", nil, err.Error(), http.StatusInternalServerError) 64 } else if rp.StatusCode >= 300 { 65 defer consumeAndClose(rp) 66 return "", model.AppErrorFromJson(rp.Body) 67 } else { 68 janusResponse := model.GatewayResponseFromJson(rp.Body) 69 if janusResponse.Status != "success" { 70 return "", model.NewAppError("getWebrtcToken", "api.webrtc.register_token.app_error", nil, "", http.StatusInternalServerError) 71 } 72 } 73 74 return token, nil 75 } 76 77 func GenerateTurnPassword(username string, secret string) string { 78 key := []byte(secret) 79 h := hmac.New(sha1.New, key) 80 h.Write([]byte(username)) 81 return base64.StdEncoding.EncodeToString(h.Sum(nil)) 82 } 83 84 func (a *App) RevokeWebrtcToken(sessionId string) { 85 token := base64.StdEncoding.EncodeToString([]byte(sessionId)) 86 data := make(map[string]string) 87 data["janus"] = "remove_token" 88 data["token"] = token 89 data["transaction"] = model.NewId() 90 data["admin_secret"] = *a.Config().WebrtcSettings.GatewayAdminSecret 91 92 rq, _ := http.NewRequest("POST", *a.Config().WebrtcSettings.GatewayAdminUrl, strings.NewReader(model.MapToJson(data))) 93 rq.Header.Set("Content-Type", "application/json") 94 95 // we do not care about the response 96 a.HTTPService.MakeClient(true).Do(rq) 97 }