mirror of
https://github.com/luckfox-eng29/kvm.git
synced 2026-05-28 17:11:20 +02:00
feat(webrtc): add configurable STUN and TURN servers
Add backend config, RPC handlers, and an HTTP endpoint for WebRTC ICE servers. Replace hardcoded frontend STUN usage with server-provided ICE server configuration, and add access settings UI for STUN and TURN entries.
This commit is contained in:
38
webrtc.go
38
webrtc.go
@@ -34,6 +34,33 @@ type SessionConfig struct {
|
||||
Logger *zerolog.Logger
|
||||
}
|
||||
|
||||
const DefaultSTUN = "stun:stun.l.google.com:19302"
|
||||
|
||||
func buildICEServers() []webrtc.ICEServer {
|
||||
if config == nil {
|
||||
LoadConfig()
|
||||
}
|
||||
|
||||
stunURL := config.STUN
|
||||
if stunURL == "" {
|
||||
stunURL = DefaultSTUN
|
||||
}
|
||||
|
||||
servers := []webrtc.ICEServer{{URLs: []string{stunURL}}}
|
||||
for _, turnServer := range config.TurnServers {
|
||||
if turnServer.URL == "" {
|
||||
continue
|
||||
}
|
||||
servers = append(servers, webrtc.ICEServer{
|
||||
URLs: []string{turnServer.URL},
|
||||
Username: turnServer.Username,
|
||||
Credential: turnServer.Credential,
|
||||
})
|
||||
}
|
||||
|
||||
return servers
|
||||
}
|
||||
|
||||
func (s *Session) ExchangeOffer(offerStr string) (string, error) {
|
||||
b, err := base64.StdEncoding.DecodeString(offerStr)
|
||||
if err != nil {
|
||||
@@ -86,16 +113,7 @@ func newSession(sessionConfig SessionConfig) (*Session, error) {
|
||||
scopedLogger = webrtcLogger
|
||||
}
|
||||
|
||||
iceServers := []webrtc.ICEServer{
|
||||
{
|
||||
URLs: []string{"stun:stun.l.google.com:19302"},
|
||||
},
|
||||
}
|
||||
if config.STUN != "" {
|
||||
iceServers = append(iceServers, webrtc.ICEServer{
|
||||
URLs: []string{config.STUN},
|
||||
})
|
||||
}
|
||||
iceServers := buildICEServers()
|
||||
|
||||
api := webrtc.NewAPI(webrtc.WithSettingEngine(webrtcSettingEngine))
|
||||
peerConnection, err := api.NewPeerConnection(webrtc.Configuration{
|
||||
|
||||
Reference in New Issue
Block a user