2021-01-02 00:20:47 +01:00
|
|
|
package ice
|
2020-12-28 02:25:46 +01:00
|
|
|
|
|
|
|
import (
|
2021-01-01 23:50:34 +01:00
|
|
|
"bytes"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
2020-12-28 02:25:46 +01:00
|
|
|
"encoding/json"
|
2021-01-01 22:20:58 +01:00
|
|
|
"errors"
|
2021-01-01 23:50:34 +01:00
|
|
|
"fmt"
|
2020-12-28 02:25:46 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-12-28 04:06:12 +01:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2020-12-28 02:25:46 +01:00
|
|
|
|
|
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
)
|
|
|
|
|
2021-01-02 00:20:47 +01:00
|
|
|
type Server struct {
|
2020-12-28 02:25:46 +01:00
|
|
|
URLs []string `json:"urls"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
Credential interface{} `json:"credential,omitempty"`
|
|
|
|
CredentialType string `json:"credentialType,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-01-02 00:20:47 +01:00
|
|
|
func getServer(server Server) (webrtc.ICEServer, error) {
|
2021-01-01 22:20:58 +01:00
|
|
|
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
|
2021-01-01 22:20:58 +01:00
|
|
|
default:
|
|
|
|
return webrtc.ICEServer{}, errors.New("unsupported credential type")
|
|
|
|
}
|
|
|
|
return s, nil
|
2020-12-28 02:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var ICEFilename string
|
2020-12-28 02:56:49 +01:00
|
|
|
var ICERelayOnly bool
|
2020-12-28 02:25:46 +01:00
|
|
|
|
2021-01-02 00:20:47 +01:00
|
|
|
type configuration struct {
|
2021-01-01 22:20:58 +01:00
|
|
|
conf webrtc.Configuration
|
2020-12-28 04:06:12 +01:00
|
|
|
timestamp time.Time
|
|
|
|
}
|
2020-12-28 02:25:46 +01:00
|
|
|
|
2021-01-02 00:20:47 +01:00
|
|
|
var conf atomic.Value
|
2020-12-28 04:06:12 +01:00
|
|
|
|
2021-01-02 00:20:47 +01:00
|
|
|
func updateICEConfiguration() *configuration {
|
2020-12-28 04:06:12 +01:00
|
|
|
now := time.Now()
|
2021-01-02 00:20:47 +01:00
|
|
|
var cf webrtc.Configuration
|
2020-12-28 04:06:12 +01:00
|
|
|
|
|
|
|
if ICEFilename != "" {
|
2020-12-28 02:25:46 +01:00
|
|
|
file, err := os.Open(ICEFilename)
|
|
|
|
if err != nil {
|
2021-01-02 00:34:59 +01:00
|
|
|
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)
|
2021-01-02 00:20:47 +01:00
|
|
|
var servers []Server
|
2021-01-01 22:20:58 +01:00
|
|
|
err = d.Decode(&servers)
|
2020-12-28 04:06:12 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Get ICE configuration: %v", err)
|
|
|
|
}
|
2021-01-01 22:20:58 +01:00
|
|
|
for _, s := range servers {
|
2021-01-02 00:20:47 +01:00
|
|
|
ss, err := getServer(s)
|
2021-01-01 22:20:58 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("parse ICE server: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2021-01-02 00:20:47 +01:00
|
|
|
cf.ICEServers = append(cf.ICEServers, ss)
|
2021-01-01 22:20:58 +01:00
|
|
|
}
|
2020-12-28 02:25:46 +01:00
|
|
|
}
|
2020-12-28 04:06:12 +01:00
|
|
|
}
|
2020-12-28 02:25:46 +01:00
|
|
|
|
2020-12-28 04:06:12 +01:00
|
|
|
if ICERelayOnly {
|
2021-01-02 00:20:47 +01:00
|
|
|
cf.ICETransportPolicy = webrtc.ICETransportPolicyRelay
|
2020-12-28 04:06:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-02 00:20:47 +01:00
|
|
|
iceConf := configuration{
|
|
|
|
conf: cf,
|
2020-12-28 04:06:12 +01:00
|
|
|
timestamp: now,
|
|
|
|
}
|
2021-01-02 00:20:47 +01:00
|
|
|
conf.Store(&iceConf)
|
2020-12-28 02:25:46 +01:00
|
|
|
return &iceConf
|
|
|
|
}
|
|
|
|
|
2021-01-01 22:20:58 +01:00
|
|
|
func ICEConfiguration() *webrtc.Configuration {
|
2021-01-02 00:20:47 +01:00
|
|
|
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
|
|
|
|
}
|