From 2cf79427696782565e7860f593e433198f17562d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20L=C3=A9vai?= Date: Thu, 3 Oct 2024 10:41:18 +0200 Subject: [PATCH] Make generic servers using password auth work (#13) * Make generic ice servers with password auth work * Make generic STUN servers without queries work --- client/ice-servers.go | 54 ++++++++++++++++++++++++++++++++++++++++++- config/config.go | 31 +++++++++++++------------ 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/client/ice-servers.go b/client/ice-servers.go index ee738b4..ba309b9 100644 --- a/client/ice-servers.go +++ b/client/ice-servers.go @@ -1,6 +1,7 @@ package client import ( + "fmt" "log/slog" "github.com/nimbleape/iceperf-agent/adapters" @@ -19,7 +20,58 @@ import ( ) func formGenericIceServers(config *config.ICEConfig) (adapters.IceServersConfig, error) { - return adapters.IceServersConfig{}, nil + iceServers := []webrtc.ICEServer{} + if config.StunEnabled { + for proto, ports := range config.StunPorts { + query := "" + if !config.StunUseRFC7094URI { + query = fmt.Sprintf("?transport=%s", proto) + } + for _, port := range ports { + stunProto := "stun" + if proto == "tls" { + stunProto = "stuns" + } + url := fmt.Sprintf("%s:%s:%d%s", stunProto, config.StunHost, port, query) + + iceServers = append(iceServers, + webrtc.ICEServer{ + URLs: []string{url}, + Username: config.Username, + Credential: config.Password, + CredentialType: webrtc.ICECredentialTypePassword, + }) + + } + } + } + if config.TurnEnabled { + for proto, ports := range config.TurnPorts { + for _, port := range ports { + turnProto := "turn" + if proto == "tls" { + turnProto = "turns" + } + url := fmt.Sprintf("%s:%s:%d?transport=%s", + turnProto, config.TurnHost, port, proto) + + iceServers = append(iceServers, + webrtc.ICEServer{ + URLs: []string{url}, + Username: config.Username, + Credential: config.Password, + CredentialType: webrtc.ICECredentialTypePassword, + }) + } + } + + } + c := adapters.IceServersConfig{ + IceServers: iceServers, + DoThroughput: config.DoThroughput, + } + + return c, nil } type IceServersConfig struct { diff --git a/config/config.go b/config/config.go index 96721f7..ce8ca2d 100644 --- a/config/config.go +++ b/config/config.go @@ -14,21 +14,22 @@ import ( ) type ICEConfig struct { - Username string `yaml:"username,omitempty"` - Password string `yaml:"password,omitempty"` - ApiKey string `json:"apiKey,omitempty" yaml:"api_key,omitempty"` - AccountSid string `yaml:"account_sid,omitempty"` - RequestUrl string `json:"requestUrl,omitempty" yaml:"request_url,omitempty"` - HttpUsername string `yaml:"http_username"` - HttpPassword string `yaml:"http_password"` - Enabled bool `yaml:"enabled"` - StunHost string `yaml:"stun_host,omitempty"` - TurnHost string `yaml:"turn_host,omitempty"` - TurnPorts map[string][]int `yaml:"turn_ports,omitempty"` - StunPorts map[string][]int `yaml:"stun_ports,omitempty"` - StunEnabled bool `yaml:"stun_enabled"` - TurnEnabled bool `yaml:"turn_enabled"` - DoThroughput bool `yaml:"do_throughput"` + Username string `yaml:"username,omitempty"` + Password string `yaml:"password,omitempty"` + ApiKey string `json:"apiKey,omitempty" yaml:"api_key,omitempty"` + AccountSid string `yaml:"account_sid,omitempty"` + RequestUrl string `json:"requestUrl,omitempty" yaml:"request_url,omitempty"` + HttpUsername string `yaml:"http_username"` + HttpPassword string `yaml:"http_password"` + Enabled bool `yaml:"enabled"` + StunUseRFC7094URI bool `yaml:"stun_use_rfc7094_uri"` + StunHost string `yaml:"stun_host,omitempty"` + TurnHost string `yaml:"turn_host,omitempty"` + TurnPorts map[string][]int `yaml:"turn_ports,omitempty"` + StunPorts map[string][]int `yaml:"stun_ports,omitempty"` + StunEnabled bool `yaml:"stun_enabled"` + TurnEnabled bool `yaml:"turn_enabled"` + DoThroughput bool `yaml:"do_throughput"` } type LokiConfig struct {