diff --git a/cmd/coordinator/main.go b/cmd/coordinator/main.go index 94223f31..40dc25d5 100644 --- a/cmd/coordinator/main.go +++ b/cmd/coordinator/main.go @@ -29,7 +29,7 @@ func main() { defer logging.Flush() glog.Infof("[coordinator] version: %v", Version) - glog.V(4).Infof("Coordinator configs %v", conf) + glog.V(4).Infof("[coordinator] Local configuration %+v", conf) c := coordinator.New(conf) c.Start() diff --git a/go.mod b/go.mod index 14f3bc7a..4c13bad7 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/gorilla/websocket v1.5.0 github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 - github.com/kkyr/fig v0.3.0 + github.com/kkyr/fig v0.3.1-0.20220103220255-711af35e3ee2 github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/pelletier/go-toml v1.9.4 // indirect github.com/pion/ice/v2 v2.2.3 // indirect diff --git a/go.sum b/go.sum index d99c4e79..357eed7e 100644 --- a/go.sum +++ b/go.sum @@ -165,6 +165,8 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8 github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkyr/fig v0.3.0 h1:5bd1amYKp/gsK2bGEUJYzcCrQPKOZp6HZD9K21v9Guo= github.com/kkyr/fig v0.3.0/go.mod h1:fEnrLjwg/iwSr8ksJF4DxrDmCUir5CaVMLORGYMcz30= +github.com/kkyr/fig v0.3.1-0.20220103220255-711af35e3ee2 h1:ZSDGtCWL8CSbDE/ZHdTivgDl8CwuHb8TpMeSKRGAhfk= +github.com/kkyr/fig v0.3.1-0.20220103220255-711af35e3ee2/go.mod h1:fEnrLjwg/iwSr8ksJF4DxrDmCUir5CaVMLORGYMcz30= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= diff --git a/pkg/config/coordinator/config.go b/pkg/config/coordinator/config.go index 6e290b14..63cd288c 100644 --- a/pkg/config/coordinator/config.go +++ b/pkg/config/coordinator/config.go @@ -5,7 +5,7 @@ import ( "github.com/giongto35/cloud-game/v2/pkg/config/emulator" "github.com/giongto35/cloud-game/v2/pkg/config/monitoring" "github.com/giongto35/cloud-game/v2/pkg/config/shared" - webrtcConfig "github.com/giongto35/cloud-game/v2/pkg/config/webrtc" + "github.com/giongto35/cloud-game/v2/pkg/config/webrtc" "github.com/giongto35/cloud-game/v2/pkg/games" flag "github.com/spf13/pflag" ) @@ -24,7 +24,7 @@ type Config struct { } Emulator emulator.Emulator Recording shared.Recording - Webrtc webrtcConfig.Webrtc + Webrtc webrtc.Webrtc } // Analytics is optional Google Analytics @@ -41,6 +41,7 @@ func NewConfig() (conf Config) { if err != nil { panic(err) } + conf.Webrtc.AddIceServersEnv() return } diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 0d665222..5c78d12a 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -6,12 +6,13 @@ import ( "github.com/kkyr/fig" ) +const EnvPrefix = "CLOUD_GAME" + // LoadConfig loads a configuration file into the given struct. // The path param specifies a custom path to the configuration file. // Reads and puts environment variables with the prefix CLOUD_GAME_. // Params from the config should be in uppercase separated with _. func LoadConfig(config interface{}, path string) error { - envPrefix := "CLOUD_GAME" dirs := []string{path} if path == "" { dirs = append(dirs, ".", "configs", "../../../configs") @@ -19,8 +20,12 @@ func LoadConfig(config interface{}, path string) error { dirs = append(dirs, home+"/.cr") } } - if err := fig.Load(config, fig.Dirs(dirs...), fig.UseEnv(envPrefix)); err != nil { + if err := fig.Load(config, fig.Dirs(dirs...), fig.UseEnv(EnvPrefix)); err != nil { return err } return nil } + +func LoadConfigEnv(config interface{}) error { + return fig.Load(config, fig.IgnoreFile(), fig.UseEnv(EnvPrefix)) +} diff --git a/pkg/config/webrtc/config.go b/pkg/config/webrtc/config.go index 5b7bb3af..a72beceb 100644 --- a/pkg/config/webrtc/config.go +++ b/pkg/config/webrtc/config.go @@ -1,6 +1,12 @@ package webrtc -import "github.com/giongto35/cloud-game/v2/pkg/config/encoder" +import ( + "log" + "strings" + + "github.com/giongto35/cloud-game/v2/pkg/config" + "github.com/giongto35/cloud-game/v2/pkg/config/encoder" +) type Webrtc struct { DisableDefaultInterceptors bool @@ -25,3 +31,23 @@ type Config struct { Encoder encoder.Encoder Webrtc Webrtc } + +func (w *Webrtc) AddIceServersEnv() { + cfg := Config{Webrtc: Webrtc{IceServers: []IceServer{{}, {}, {}, {}, {}}}} + _ = config.LoadConfigEnv(&cfg) + for i, ice := range cfg.Webrtc.IceServers { + if ice.Url == "" { + continue + } + if strings.HasPrefix(ice.Url, "turn:") || strings.HasPrefix(ice.Url, "turns:") { + if ice.Username == "" || ice.Credential == "" { + log.Fatalf("TURN or TURNS servers should have both username and credential: %+v", ice) + } + } + if i > len(w.IceServers)-1 { + w.IceServers = append(w.IceServers, ice) + } else { + w.IceServers[i] = ice + } + } +} diff --git a/pkg/config/worker/config.go b/pkg/config/worker/config.go index 9ef461dd..7b2cbb54 100644 --- a/pkg/config/worker/config.go +++ b/pkg/config/worker/config.go @@ -12,7 +12,7 @@ import ( "github.com/giongto35/cloud-game/v2/pkg/config/monitoring" "github.com/giongto35/cloud-game/v2/pkg/config/shared" "github.com/giongto35/cloud-game/v2/pkg/config/storage" - webrtcConfig "github.com/giongto35/cloud-game/v2/pkg/config/webrtc" + "github.com/giongto35/cloud-game/v2/pkg/config/webrtc" "github.com/giongto35/cloud-game/v2/pkg/os" flag "github.com/spf13/pflag" ) @@ -23,7 +23,7 @@ type Config struct { Recording shared.Recording Storage storage.Storage Worker Worker - Webrtc webrtcConfig.Webrtc + Webrtc webrtc.Webrtc } type Worker struct { @@ -46,7 +46,12 @@ var configPath string func NewConfig() (conf Config) { _ = config.LoadConfig(&conf, configPath) conf.expandSpecialTags() - conf.fixValues() + // with ICE lite we clear ICE servers + if !conf.Webrtc.IceLite { + conf.Webrtc.AddIceServersEnv() + } else { + conf.Webrtc.IceServers = []webrtc.IceServer{} + } return } @@ -77,14 +82,6 @@ func (c *Config) expandSpecialTags() { } } -// fixValues tries to fix some values otherwise hard to set externally. -func (c *Config) fixValues() { - // with ICE lite we clear ICE servers - if c.Webrtc.IceLite { - c.Webrtc.IceServers = []webrtcConfig.IceServer{} - } -} - // GetAddr returns defined in the config server address. func (w *Worker) GetAddr() string { return w.Server.GetAddr() }