mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-01-23 02:34:42 +00:00
Remove DebugHost and Environment config params
This commit is contained in:
parent
02a061a580
commit
2a283e24db
10 changed files with 35 additions and 172 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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]")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue