1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-09 18:25:58 +01:00
galene/ice/ice.go

126 lines
2.7 KiB
Go
Raw Normal View History

package ice
import (
2021-01-01 23:50:34 +01:00
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
2021-01-01 23:50:34 +01:00
"fmt"
"log"
"os"
2020-12-28 04:06:12 +01:00
"sync/atomic"
"time"
"github.com/pion/webrtc/v3"
)
type Server struct {
URLs []string `json:"urls"`
Username string `json:"username,omitempty"`
Credential interface{} `json:"credential,omitempty"`
CredentialType string `json:"credentialType,omitempty"`
}
func getServer(server Server) (webrtc.ICEServer, error) {
s := webrtc.ICEServer{
URLs: server.URLs,
Username: server.Username,
Credential: server.Credential,
}
switch server.CredentialType {
case "", "password":
s.CredentialType = webrtc.ICECredentialTypePassword
case "oauth":
s.CredentialType = webrtc.ICECredentialTypeOauth
2021-01-01 23:50:34 +01:00
case "hmac-sha1":
cred, ok := server.Credential.(string)
if !ok {
return webrtc.ICEServer{},
errors.New("credential is not a string")
}
ts := time.Now().Unix() + 86400
var username string
if server.Username == "" {
username = fmt.Sprintf("%d", ts)
} else {
username = fmt.Sprintf("%d:%s", ts, server.Username)
}
mac := hmac.New(sha1.New, []byte(cred))
mac.Write([]byte(username))
buf := bytes.Buffer{}
e := base64.NewEncoder(base64.StdEncoding, &buf)
e.Write(mac.Sum(nil))
e.Close()
s.Username = username
s.Credential = string(buf.Bytes())
s.CredentialType = webrtc.ICECredentialTypePassword
default:
return webrtc.ICEServer{}, errors.New("unsupported credential type")
}
return s, nil
}
var ICEFilename string
var ICERelayOnly bool
type configuration struct {
conf webrtc.Configuration
2020-12-28 04:06:12 +01:00
timestamp time.Time
}
var conf atomic.Value
2020-12-28 04:06:12 +01:00
func updateICEConfiguration() *configuration {
2020-12-28 04:06:12 +01:00
now := time.Now()
var cf webrtc.Configuration
2020-12-28 04:06:12 +01:00
if ICEFilename != "" {
file, err := os.Open(ICEFilename)
if err != nil {
if !os.IsNotExist(err) {
log.Printf("Open %v: %v", ICEFilename, err)
}
2020-12-28 04:06:12 +01:00
} else {
defer file.Close()
d := json.NewDecoder(file)
var servers []Server
err = d.Decode(&servers)
2020-12-28 04:06:12 +01:00
if err != nil {
log.Printf("Get ICE configuration: %v", err)
}
for _, s := range servers {
ss, err := getServer(s)
if err != nil {
log.Printf("parse ICE server: %v", err)
continue
}
cf.ICEServers = append(cf.ICEServers, ss)
}
}
2020-12-28 04:06:12 +01:00
}
2020-12-28 04:06:12 +01:00
if ICERelayOnly {
cf.ICETransportPolicy = webrtc.ICETransportPolicyRelay
2020-12-28 04:06:12 +01:00
}
iceConf := configuration{
conf: cf,
2020-12-28 04:06:12 +01:00
timestamp: now,
}
conf.Store(&iceConf)
return &iceConf
}
func ICEConfiguration() *webrtc.Configuration {
conf, ok := conf.Load().(*configuration)
2020-12-28 04:06:12 +01:00
if !ok || time.Since(conf.timestamp) > 5*time.Minute {
conf = updateICEConfiguration()
} else if time.Since(conf.timestamp) > 2*time.Minute {
go updateICEConfiguration()
}
return &conf.conf
}