Add ICE config from the CLOUD_GAME_WEBRTC_ICESERVERS_0_ env vars (#377)

This commit is contained in:
sergystepanov 2022-08-17 23:06:27 +03:00 committed by GitHub
parent 54ee9a7af3
commit 8e49b05226
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 18 deletions

View file

@ -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()

2
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

View file

@ -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
}

View file

@ -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))
}

View file

@ -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
}
}
}

View file

@ -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() }