diff --git a/.github/workflows/cd/cloudretro.io/coordinator.env b/.github/workflows/cd/cloudretro.io/coordinator.env index 6ac274e9..bfee32c2 100644 --- a/.github/workflows/cd/cloudretro.io/coordinator.env +++ b/.github/workflows/cd/cloudretro.io/coordinator.env @@ -3,4 +3,3 @@ CLOUD_GAME_COORDINATOR_ANALYTICS_INJECT=true CLOUD_GAME_COORDINATOR_SERVER_ADDRESS= CLOUD_GAME_COORDINATOR_SERVER_HTTPS=true CLOUD_GAME_COORDINATOR_SERVER_TLS_DOMAIN=cloudretro.io -CLOUD_GAME_ENVIRONMENT=prod diff --git a/configs/config.yaml b/configs/config.yaml index 4512a203..7e3a9569 100644 --- a/configs/config.yaml +++ b/configs/config.yaml @@ -2,13 +2,7 @@ # Application configuration file # -# application environment (dev, staging, prod) -# deprecated -environment: dev - coordinator: - # address if the server want to connect directly to debug - debugHost: # games library library: # some directory which is gonna be the root folder for the library diff --git a/pkg/config/coordinator/config.go b/pkg/config/coordinator/config.go index 89076169..0fab5ae1 100644 --- a/pkg/config/coordinator/config.go +++ b/pkg/config/coordinator/config.go @@ -12,7 +12,6 @@ import ( type Config struct { Coordinator struct { - DebugHost string Library games.Config Monitoring monitoring.Config Origin struct { @@ -22,10 +21,9 @@ type Config struct { Server shared.Server Analytics Analytics } - Emulator emulator.Emulator - Environment shared.Environment - Recording shared.Recording - Webrtc webrtcConfig.Webrtc + Emulator emulator.Emulator + Recording shared.Recording + Webrtc webrtcConfig.Webrtc } // Analytics is optional Google Analytics @@ -46,7 +44,6 @@ func NewConfig() (conf Config) { } func (c *Config) ParseFlags() { - c.Environment.WithFlags() c.Coordinator.Server.WithFlags() flag.IntVar(&c.Coordinator.Monitoring.Port, "monitoring.port", c.Coordinator.Monitoring.Port, "Monitoring server port") flag.StringVarP(&configPath, "conf", "c", configPath, "Set custom configuration file path") diff --git a/pkg/config/shared/config.go b/pkg/config/shared/config.go index d6d0c95b..c5731fb3 100644 --- a/pkg/config/shared/config.go +++ b/pkg/config/shared/config.go @@ -1,11 +1,6 @@ package shared -import ( - "github.com/giongto35/cloud-game/v2/pkg/environment" - flag "github.com/spf13/pflag" -) - -type Environment environment.Env +import flag "github.com/spf13/pflag" type Server struct { Address string @@ -39,11 +34,3 @@ func (s *Server) GetAddr() string { } return s.Address } - -func (env *Environment) Get() environment.Env { - return (environment.Env)(*env) -} - -func (env *Environment) WithFlags() { - flag.StringVar((*string)(env), "env", string(*env), "Specify environment type: [dev, staging, prod]") -} diff --git a/pkg/config/worker/config.go b/pkg/config/worker/config.go index 99c41752..b3131da2 100644 --- a/pkg/config/worker/config.go +++ b/pkg/config/worker/config.go @@ -13,18 +13,17 @@ import ( "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/environment" + "github.com/giongto35/cloud-game/v2/pkg/os" flag "github.com/spf13/pflag" ) type Config struct { - Encoder encoder.Encoder - Emulator emulator.Emulator - Environment shared.Environment - Recording shared.Recording - Storage storage.Storage - Worker Worker - Webrtc webrtcConfig.Webrtc + Encoder encoder.Encoder + Emulator emulator.Emulator + Recording shared.Recording + Storage storage.Storage + Worker Worker + Webrtc webrtcConfig.Webrtc } type Worker struct { @@ -54,7 +53,6 @@ func NewConfig() (conf Config) { // Define own flags with default value set to the current config param. // Don't forget to call flag.Parse(). func (c *Config) ParseFlags() { - c.Environment.WithFlags() c.Worker.Server.WithFlags() flag.IntVar(&c.Worker.Monitoring.Port, "monitoring.port", c.Worker.Monitoring.Port, "Monitoring server port") flag.StringVar(&c.Worker.Network.CoordinatorAddress, "coordinatorhost", c.Worker.Network.CoordinatorAddress, "Worker URL to connect") @@ -70,7 +68,7 @@ func (c *Config) expandSpecialTags() { if *dir == "" || !strings.Contains(*dir, tag) { continue } - userHomeDir, err := environment.GetUserHome() + userHomeDir, err := os.GetUserHome() if err != nil { log.Fatalln("couldn't read user home directory", err) } diff --git a/pkg/coordinator/handlers.go b/pkg/coordinator/handlers.go index 16fed446..9bc25bac 100644 --- a/pkg/coordinator/handlers.go +++ b/pkg/coordinator/handlers.go @@ -5,18 +5,17 @@ import ( "errors" "log" "math" + "net" "net/http" "strings" "github.com/giongto35/cloud-game/v2/pkg/config/coordinator" "github.com/giongto35/cloud-game/v2/pkg/cws" "github.com/giongto35/cloud-game/v2/pkg/cws/api" - "github.com/giongto35/cloud-game/v2/pkg/environment" "github.com/giongto35/cloud-game/v2/pkg/games" "github.com/giongto35/cloud-game/v2/pkg/ice" "github.com/giongto35/cloud-game/v2/pkg/network/websocket" "github.com/giongto35/cloud-game/v2/pkg/service" - "github.com/giongto35/cloud-game/v2/pkg/util" "github.com/gofrs/uuid" ) @@ -98,31 +97,9 @@ func (s *Server) WSO(w http.ResponseWriter, r *http.Request) { wc.Zone = connRt.Zone wc.PingServer = connRt.PingAddr - // Register to workersClients map the client connection - address := util.GetRemoteAddress(c) - public := util.IsPublicIP(address) - - wc.Printf("addr: %v | zone: %v | pub: %v | ping: %v", address, wc.Zone, public, wc.PingServer) - - // In case worker and coordinator in the same host - if !public && s.cfg.Environment.Get() == environment.Production { - // Don't accept private IP for worker's address in prod mode - // However, if the worker in the same host with coordinator, we can get public IP of worker - wc.Printf("[!] Address %s is invalid", address) - - address = util.GetHostPublicIP() - wc.Printf("Find public address: %s", address) - - if address == "" || !util.IsPublicIP(address) { - // Skip this worker because we cannot find public IP - wc.Println("[!] Unable to find public address, reject worker") - return - } - } - - // Create a workerClient instance - wc.Address = address - wc.StunTurnServer = ice.ToJson(s.cfg.Webrtc.IceServers, ice.Replacement{From: "server-ip", To: address}) + addr := getIP(c.RemoteAddr()) + wc.Printf("addr: %v | zone: %v | ping: %v", addr, wc.Zone, wc.PingServer) + wc.StunTurnServer = ice.ToJson(s.cfg.Webrtc.IceServers, ice.Replacement{From: "server-ip", To: addr}) // Attach to Server instance with workerID, add defer s.workerClients[workerID] = wc @@ -229,25 +206,12 @@ func (s *Server) WS(w http.ResponseWriter, r *http.Request) { } func (s *Server) getBestWorkerClient(client *BrowserClient, zone string) (*WorkerClient, error) { - conf := s.cfg.Coordinator - if conf.DebugHost != "" { - client.Println("Connecting to debug host instead prod servers", conf.DebugHost) - wc := s.getWorkerFromAddress(conf.DebugHost) - if wc != nil { - return wc, nil - } - // if there is not debugHost, continue usual flow - client.Println("Not found, connecting to all available servers") - } - workerClients := s.getAvailableWorkers() - serverID, err := s.findBestServerFromBrowser(workerClients, client, zone) if err != nil { log.Println(err) return nil, err } - return s.workerClients[serverID], nil } @@ -263,17 +227,6 @@ func (s *Server) getAvailableWorkers() map[string]*WorkerClient { return workerClients } -// getWorkerFromAddress returns the worker has given address -func (s *Server) getWorkerFromAddress(address string) *WorkerClient { - for _, w := range s.workerClients { - if w.HasGameSlot() && w.Address == address { - return w - } - } - - return nil -} - // findBestServerFromBrowser returns the best server for a session // All workers addresses are sent to user and user will ping to get latency func (s *Server) findBestServerFromBrowser(workerClients map[string]*WorkerClient, client *BrowserClient, zone string) (string, error) { @@ -382,3 +335,13 @@ func createInitPackage(stunturn string, games []games.GameMetadata) string { encodedList, _ := json.Marshal(initPackage) return string(encodedList) } + +func getIP(a net.Addr) (addr string) { + if parts := strings.Split(a.String(), ":"); len(parts) == 2 { + addr = parts[0] + } + if addr == "" { + return "localhost" + } + return +} diff --git a/pkg/coordinator/worker.go b/pkg/coordinator/worker.go index 83b661f6..2b18650e 100644 --- a/pkg/coordinator/worker.go +++ b/pkg/coordinator/worker.go @@ -13,7 +13,6 @@ type WorkerClient struct { *cws.Client WorkerID string - Address string // ip address of worker // public server used for ping check PingServer string StunTurnServer string diff --git a/pkg/environment/env.go b/pkg/environment/env.go deleted file mode 100644 index 57a443cc..00000000 --- a/pkg/environment/env.go +++ /dev/null @@ -1,28 +0,0 @@ -package environment - -import "os/user" - -type Env string - -const ( - Dev Env = "dev" - Staging Env = "staging" - Production Env = "prod" -) - -func (env *Env) AnyOf(what ...Env) bool { - for _, cur := range what { - if *env == cur { - return true - } - } - return false -} - -func GetUserHome() (string, error) { - me, err := user.Current() - if err != nil { - return "", err - } - return me.HomeDir, nil -} diff --git a/pkg/os/os.go b/pkg/os/os.go index ea4e0ecd..e5141520 100644 --- a/pkg/os/os.go +++ b/pkg/os/os.go @@ -3,6 +3,7 @@ package os import ( "os" "os/signal" + "os/user" "syscall" ) @@ -21,3 +22,11 @@ func ExpectTermination() chan struct{} { }() return done } + +func GetUserHome() (string, error) { + me, err := user.Current() + if err != nil { + return "", err + } + return me.HomeDir, nil +} diff --git a/pkg/util/netutil.go b/pkg/util/netutil.go deleted file mode 100644 index 855a7e78..00000000 --- a/pkg/util/netutil.go +++ /dev/null @@ -1,55 +0,0 @@ -package util - -import ( - "net" - "strings" - - "github.com/gorilla/websocket" -) - -// GetRemoteAddress returns public address of websocket connection -func GetRemoteAddress(conn *websocket.Conn) string { - var remoteAddr string - // log.Println("Address :", conn.RemoteAddr().String()) - if parts := strings.Split(conn.RemoteAddr().String(), ":"); len(parts) == 2 { - remoteAddr = parts[0] - } - if remoteAddr == "" { - return "localhost" - } - - return remoteAddr -} - -// IsPublicIP checks if address is public address -func IsPublicIP(address string) bool { - ip := net.ParseIP(address) - if ip.IsLoopback() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() { - return false - } - if ip4 := ip.To4(); ip4 != nil { - switch { - case ip4[0] == 10: - return false - case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31: - return false - case ip4[0] == 192 && ip4[1] == 168: - return false - default: - return true - } - } - return false -} - -// GetHostPublicIP to get the public ip address. Only work if not behind NAT -func GetHostPublicIP() string { - conn, err := net.Dial("udp", "8.8.8.8:80") - if err != nil { - return "" - } - defer conn.Close() - localAddr := conn.LocalAddr().String() - idx := strings.LastIndex(localAddr, ":") - return localAddr[0:idx] -}