diff --git a/pkg/config/config.go b/pkg/config/config.go index 1ebfd614..e7b64dc6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -15,10 +15,10 @@ const AUDIO_CHANNELS = 2 const AUDIO_MS = 20 const AUDIO_FRAME = AUDIO_RATE * AUDIO_MS / 1000 * AUDIO_CHANNELS -var Port = flag.String("port", "8000", "Port of the game") var FrontendSTUNTURN = flag.String("stunturn", DefaultSTUNTURN, "Frontend STUN TURN servers") var Mode = flag.String("mode", "dev", "Environment") var StunTurnTemplate = `[{"urls":"stun:stun.l.google.com:19302"},{"urls":"stun:%s:3478"},{"urls":"turn:%s:3478","username":"root","credential":"root"}]` +var HttpPort = flag.String("httpPort", "8000", "User agent port of the app") var HttpsPort = flag.Int("httpsPort", 443, "Https Port") var HttpsKey = flag.String("httpsKey", "", "Https Key") var HttpsChain = flag.String("httpsChain", "", "Https Chain") diff --git a/pkg/config/worker/config.go b/pkg/config/worker/config.go index bd8bb002..b8b27f97 100644 --- a/pkg/config/worker/config.go +++ b/pkg/config/worker/config.go @@ -8,6 +8,7 @@ import ( type Config struct { Port int CoordinatorAddress string + HttpPort int // video Scale int @@ -23,6 +24,7 @@ func NewDefaultConfig() Config { return Config{ Port: 8800, CoordinatorAddress: "localhost:8000", + HttpPort: 9000, Scale: 1, EnableAspectRatio: false, Width: 320, @@ -39,6 +41,7 @@ func NewDefaultConfig() Config { func (c *Config) AddFlags(fs *pflag.FlagSet) *Config { fs.IntVarP(&c.Port, "port", "", 8800, "Worker server port") fs.StringVarP(&c.CoordinatorAddress, "coordinatorhost", "", c.CoordinatorAddress, "Worker URL to connect") + fs.IntVarP(&c.HttpPort, "httpPort", "", c.HttpPort, "Set external HTTP port") fs.StringVarP(&c.Zone, "zone", "z", c.Zone, "Zone of the worker") fs.IntVarP(&c.Scale, "scale", "s", c.Scale, "Set output viewport scale factor") diff --git a/pkg/coordinator/coordinator.go b/pkg/coordinator/coordinator.go index 68b7df7e..1e31931c 100644 --- a/pkg/coordinator/coordinator.go +++ b/pkg/coordinator/coordinator.go @@ -146,7 +146,7 @@ func (o *Coordinator) initializeCoordinator() { httpSrv.Handler = certManager.HTTPHandler(httpSrv.Handler) } - httpSrv.Addr = ":8000" + httpSrv.Addr = ":" + *config.HttpPort err := httpSrv.ListenAndServe() if err != nil { log.Fatalf("httpSrv.ListenAndServe() failed with %s", err) diff --git a/pkg/coordinator/handlers.go b/pkg/coordinator/handlers.go index 6b0c2f36..af5e5ff7 100644 --- a/pkg/coordinator/handlers.go +++ b/pkg/coordinator/handlers.go @@ -121,6 +121,8 @@ func (o *Server) WSO(w http.ResponseWriter, r *http.Request) { pingServer := o.getPingServer(zone) + wc.Printf("Set ping server address: %s", pingServer) + // In case worker and coordinator in the same host if !util.IsPublicIP(address) && *config.Mode == config.ProdEnv { // Don't accept private IP for worker's address in prod mode diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go index d822831a..59b9f23e 100644 --- a/pkg/worker/worker.go +++ b/pkg/worker/worker.go @@ -109,9 +109,9 @@ func (o *Worker) spawnServer(port int) { } certManager = &autocert.Manager{ - Prompt: autocert.AcceptTOS, - Cache: autocert.DirCache("assets/cache"), - Client: &acme.Client{DirectoryURL: leurl}, + Prompt: autocert.AcceptTOS, + Cache: autocert.DirCache("assets/cache"), + Client: &acme.Client{DirectoryURL: leurl}, } httpsSrv.TLSConfig = &tls.Config{GetCertificate: certManager.GetCertificate} @@ -154,22 +154,25 @@ func (o *Worker) initializeWorker() { }() go worker.Run() - port := 9000 - // It's recommend to run one worker on one instance. This logic is to make sure more than 1 workers still work + port := o.cfg.HttpPort + // It's recommend to run one worker on one instance. + // This logic is to make sure more than 1 workers still work + portsNum := 100 for { - log.Println("Listening at port: localhost:", port) - // err := http.ListenAndServe(":"+strconv.Itoa(port), nil) + portsNum-- l, err := net.Listen("tcp", ":"+strconv.Itoa(port)) if err != nil { port++ continue } - if port == 9100 { + + if portsNum < 1 { + log.Printf("Couldn't find an open port in range %v-%v\n", o.cfg.HttpPort, port) // Cannot find port return } - l.Close() + _ = l.Close() o.spawnServer(port) }