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-18 20:24:52 +01:00
|
|
|
|
|
|
|
"github.com/jech/galene/turnserver"
|
2020-12-28 02:25:46 +01:00
|
|
|
)
|
|
|
|
|
2021-03-02 21:54:35 +01:00
|
|
|
type timeoutError struct{}
|
|
|
|
|
|
|
|
func (e timeoutError) Error() string {
|
|
|
|
return "timeout"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e timeoutError) Timeout() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var errTimeout = timeoutError{}
|
|
|
|
|
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-19 01:18:11 +01:00
|
|
|
func Update() *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
|
|
|
|
2021-01-19 01:18:11 +01:00
|
|
|
found := false
|
2020-12-28 04:06:12 +01:00
|
|
|
if ICEFilename != "" {
|
2021-01-19 01:18:11 +01:00
|
|
|
found = true
|
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)
|
2021-01-19 01:18:11 +01:00
|
|
|
} else {
|
|
|
|
found = false
|
2021-01-02 00:34:59 +01:00
|
|
|
}
|
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
|
|
|
|
2021-01-19 18:04:39 +01:00
|
|
|
err := turnserver.StartStop(!found)
|
2021-01-19 01:18:11 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("TURN: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:24:52 +01:00
|
|
|
cf.ICEServers = append(cf.ICEServers, turnserver.ICEServers()...)
|
|
|
|
|
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 {
|
2021-01-19 01:18:11 +01:00
|
|
|
conf = Update()
|
2020-12-28 04:06:12 +01:00
|
|
|
} else if time.Since(conf.timestamp) > 2*time.Minute {
|
2021-01-19 01:18:11 +01:00
|
|
|
go Update()
|
2020-12-28 04:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &conf.conf
|
|
|
|
}
|
2021-01-14 01:47:56 +01:00
|
|
|
|
2021-02-26 13:22:47 +01:00
|
|
|
func RelayTest(timeout time.Duration) (time.Duration, error) {
|
2021-01-14 02:07:00 +01:00
|
|
|
|
2021-01-14 01:47:56 +01:00
|
|
|
conf := ICEConfiguration()
|
|
|
|
conf2 := *conf
|
|
|
|
conf2.ICETransportPolicy = webrtc.ICETransportPolicyRelay
|
2021-01-14 02:07:00 +01:00
|
|
|
|
|
|
|
var s webrtc.SettingEngine
|
|
|
|
s.SetHostAcceptanceMinWait(0)
|
|
|
|
s.SetSrflxAcceptanceMinWait(0)
|
|
|
|
s.SetPrflxAcceptanceMinWait(0)
|
|
|
|
s.SetRelayAcceptanceMinWait(0)
|
|
|
|
api := webrtc.NewAPI(webrtc.WithSettingEngine(s))
|
|
|
|
|
|
|
|
pc1, err := api.NewPeerConnection(conf2)
|
2021-01-14 01:47:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer pc1.Close()
|
|
|
|
pc2, err := webrtc.NewPeerConnection(*conf)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer pc2.Close()
|
|
|
|
|
|
|
|
pc1.OnICECandidate(func(c *webrtc.ICECandidate) {
|
|
|
|
if c != nil {
|
|
|
|
pc2.AddICECandidate(c.ToJSON())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
pc2.OnICECandidate(func(c *webrtc.ICECandidate) {
|
|
|
|
if c != nil {
|
|
|
|
pc1.AddICECandidate(c.ToJSON())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
d1, err := pc1.CreateDataChannel("loopback", nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch1 := make(chan error, 1)
|
|
|
|
d1.OnOpen(func() {
|
|
|
|
err := d1.Send([]byte(time.Now().Format(time.RFC3339Nano)))
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case ch1 <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
offer, err := pc1.CreateOffer(nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = pc1.SetLocalDescription(offer)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = pc2.SetRemoteDescription(*pc1.LocalDescription())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
answer, err := pc2.CreateAnswer(nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = pc2.SetLocalDescription(answer)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = pc1.SetRemoteDescription(*pc2.LocalDescription())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch2 := make(chan string, 1)
|
|
|
|
pc2.OnDataChannel(func(d2 *webrtc.DataChannel) {
|
|
|
|
d2.OnMessage(func(msg webrtc.DataChannelMessage) {
|
|
|
|
select {
|
|
|
|
case ch2 <- string(msg.Data):
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-26 13:22:47 +01:00
|
|
|
timer := time.NewTimer(timeout)
|
2021-01-14 01:47:56 +01:00
|
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
|
|
case err := <-ch1:
|
|
|
|
return 0, err
|
|
|
|
case msg := <-ch2:
|
|
|
|
tm, err := time.Parse(time.RFC3339Nano, msg)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return time.Now().Sub(tm), nil
|
|
|
|
case <-timer.C:
|
2021-03-02 21:54:35 +01:00
|
|
|
return 0, errTimeout
|
2021-01-14 01:47:56 +01:00
|
|
|
}
|
|
|
|
}
|