Add support for TURN static auth secret credentials (#3796)
Closes https://github.com/livekit/livekit/issues/2245
This commit is contained in:
@@ -87,6 +87,10 @@ rtc:
|
|||||||
# port: 443
|
# port: 443
|
||||||
# # tls, tcp, or udp
|
# # tls, tcp, or udp
|
||||||
# protocol: tls
|
# protocol: tls
|
||||||
|
# # Shared secret for TURN server authentication
|
||||||
|
# secret: ""
|
||||||
|
# ttl: 14400 # seconds
|
||||||
|
# # Insecure username/password authentication
|
||||||
# username: ""
|
# username: ""
|
||||||
# credential: ""
|
# credential: ""
|
||||||
# # allows LiveKit to monitor congestion when sending streams and automatically
|
# # allows LiveKit to monitor congestion when sending streams and automatically
|
||||||
|
|||||||
@@ -141,6 +141,12 @@ type TURNServer struct {
|
|||||||
Protocol string `yaml:"protocol,omitempty"`
|
Protocol string `yaml:"protocol,omitempty"`
|
||||||
Username string `yaml:"username,omitempty"`
|
Username string `yaml:"username,omitempty"`
|
||||||
Credential string `yaml:"credential,omitempty"`
|
Credential string `yaml:"credential,omitempty"`
|
||||||
|
// Secret is used for TURN static auth secrets mechanism. When provided,
|
||||||
|
// dynamic credentials are generated using HMAC-SHA1 instead of static Username/Credential
|
||||||
|
Secret string `yaml:"secret,omitempty"`
|
||||||
|
// TTL is the time-to-live in seconds for generated credentials when using Secret.
|
||||||
|
// Defaults to 14400 seconds (4 hours) if not specified
|
||||||
|
TTL int `yaml:"ttl,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CongestionControlConfig struct {
|
type CongestionControlConfig struct {
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -1018,12 +1021,35 @@ func (r *RoomManager) iceServersForParticipant(apiKey string, participant types.
|
|||||||
case "udp":
|
case "udp":
|
||||||
transport = "udp"
|
transport = "udp"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var username, credential string
|
||||||
|
if s.Secret != "" {
|
||||||
|
// Generate dynamic credentials using TURN static auth secrets
|
||||||
|
ttl := s.TTL
|
||||||
|
if ttl == 0 {
|
||||||
|
ttl = 14400 // Default 4 hours
|
||||||
|
}
|
||||||
|
|
||||||
|
expiry := time.Now().Add(time.Duration(ttl) * time.Second).Unix()
|
||||||
|
participantID := string(participant.ID())
|
||||||
|
username = fmt.Sprintf("%d:%s", expiry, participantID)
|
||||||
|
|
||||||
|
// HMAC-SHA1 signature
|
||||||
|
h := hmac.New(sha1.New, []byte(s.Secret))
|
||||||
|
h.Write([]byte(username))
|
||||||
|
credential = base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||||||
|
} else {
|
||||||
|
// Use static credentials
|
||||||
|
username = s.Username
|
||||||
|
credential = s.Credential
|
||||||
|
}
|
||||||
|
|
||||||
is := &livekit.ICEServer{
|
is := &livekit.ICEServer{
|
||||||
Urls: []string{
|
Urls: []string{
|
||||||
fmt.Sprintf("%s:%s:%d?transport=%s", scheme, s.Host, s.Port, transport),
|
fmt.Sprintf("%s:%s:%d?transport=%s", scheme, s.Host, s.Port, transport),
|
||||||
},
|
},
|
||||||
Username: s.Username,
|
Username: username,
|
||||||
Credential: s.Credential,
|
Credential: credential,
|
||||||
}
|
}
|
||||||
iceServers = append(iceServers, is)
|
iceServers = append(iceServers, is)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user