diff --git a/config/config.go b/config/config.go index 52e9ab2..bc9c895 100644 --- a/config/config.go +++ b/config/config.go @@ -17,18 +17,21 @@ var ( ) type Config struct { - SystemUsername string - BarrierHostName string - Rooms string - IPAddress string - ScriptToExecute string - SSHPassword string - NATEscapeServerPort string - NATEscapeBarrierPort string - BackendURL string - BrowserCommand string - Rate float64 - ScreenName string + SystemUsername string + BarrierHostName string + Rooms string + IPAddress string + ScriptToExecute string + SSHPassword string + NATEscapeGameServerPort string + NATEscapeScreenSharePort string + NATEscapeBarrierPort string + BackendURL string + BrowserCommand string + Rate float64 + ScreenName string + InternalGameServerPort string + InternalScreenSharePort string } // Exists reports whether the named file or directory exists. @@ -74,6 +77,8 @@ func SetDefaults() error { c.Rate = 0.0 c.BackendURL = "https://xplane-webrtc.akilan.io" c.ScreenName = "Entire screen" + c.InternalGameServerPort = "8088" + c.InternalScreenSharePort = "8888" file, _ := json.MarshalIndent(c, "", " ") @@ -179,7 +184,7 @@ func (c *Config) WriteConfig() error { //defaults["IPAddress"] = c.IPAddress //defaults["ScriptToExecute"] = c.ScriptToExecute //defaults["SSHPassword"] = c.SSHPassword - //defaults["NATEscapeServerPort"] = c.NATEscapeServerPort + //defaults["NATEscapeGameServerPort"] = c.NATEscapeGameServerPort //defaults["NATEscapeBarrierPort"] = c.NATEscapeBarrierPort // If the config file exists remove and make a new one diff --git a/core/core.go b/core/core.go index eb30f77..7fccdce 100644 --- a/core/core.go +++ b/core/core.go @@ -1,6 +1,7 @@ package core import ( + "crypto/tls" "encoding/json" "errors" "fmt" @@ -47,7 +48,7 @@ func BroadcastServerToBackend() error { // Game session url //+ file.ID - gameSession.Link = respIpv4orIPv6 + ":" + config.NATEscapeServerPort + "/?id=" + room.ID + gameSession.Link = respIpv4orIPv6 + ":" + config.NATEscapeGameServerPort + "/?id=" + room.ID // Rate for the game session gameSession.Rate = config.Rate // Server specs to struct GameSession @@ -75,6 +76,7 @@ func BroadcastServerToBackend() error { } form.Add("CPU", gameSession.Server.CPU) + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} req, err := http.NewRequest("POST", config.BackendURL+"AddGameSession", strings.NewReader(form.Encode())) if err != nil { return err diff --git a/core/escape-NAT.go b/core/escape-NAT.go index fe94715..df9562f 100644 --- a/core/escape-NAT.go +++ b/core/escape-NAT.go @@ -8,7 +8,7 @@ import ( // EscapeNAT Func to escape NAT // - 1 port for server // - 2 port for barrierKVM -func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error) { +func EscapeNAT(ScreenSharePort, GameplayServerPort string) (ServerPort string, ScreensharePort string, barrierKVMport string, err error) { // Get free port from P2PRC server node serverPort, err := frp.GetFRPServerPort("http://64.227.168.102:8088") @@ -19,7 +19,22 @@ func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error time.Sleep(1 * time.Second) // port for the remote gameplay server - ServerPort, err = frp.StartFRPClientForServer("64.227.168.102", serverPort, Port) + ServerPort, err = frp.StartFRPClientForServer("64.227.168.102", serverPort, GameplayServerPort) + if err != nil { + return + } + + // Get free port from P2PRC server node + ScreensharePort, err = frp.GetFRPServerPort("http://64.227.168.102:8088") + + if err != nil { + return + } + + time.Sleep(1 * time.Second) + + // port for the screenshare port + ScreensharePort, err = frp.StartFRPClientForServer("64.227.168.102", ScreensharePort, ScreenSharePort) if err != nil { return } diff --git a/main.go b/main.go index 1a279e0..fea057b 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( func main() { addr := flag.String("addr", "0.0.0.0", "Listen address") - port := flag.String("port", "8888", "port for running the server") + //port := flag.String("port", "8888", "port for running the server") tls := flag.Bool("tls", false, "Use TLS") setconfig := flag.Bool("setconfig", false, "Generates a config file") certFile := flag.String("certFile", "files/server.crt", "TLS cert file") @@ -25,9 +25,10 @@ func main() { roomInfo := flag.Bool("roomInfo", false, "Getting room id of headless server") killServer := flag.Bool("killServer", false, "Kills the laplace") killChromium := flag.Bool("killChromium", false, "Kills all chromuim") - BinaryToExcute := flag.String("BinaryToExecute", "", "Providing path (i.e Absolute path) of binary to execute") + BinaryToExecute := flag.String("BinaryToExecute", "", "Providing path (i.e Absolute path) of binary to execute") GameServer := flag.Bool("GameServer", false, "Starts the game server by default") Migrate := flag.Bool("Migrate", false, "Sets up the tables for the Sqlite database") + BothServers := flag.Bool("BothServers", false, "Starts the Gameserver and screenshare. Also ensures the screenshare can broadcast the availability to the game server") flag.Parse() @@ -86,19 +87,20 @@ func main() { return } - // running implementation to escape NAT - Server, barrireKVM, err := core.EscapeNAT(*port) - if err != nil { - log.Fatalln(err) - } - Config, err := config.ConfigInit() if err != nil { log.Fatalln(err) } + // running implementation to escape NAT + GameServerPort, ScreenSharePort, barrireKVM, err := core.EscapeNAT(Config.InternalScreenSharePort, Config.InternalGameServerPort) + if err != nil { + log.Fatalln(err) + } + Config.IPAddress = "64.227.168.102" - Config.NATEscapeServerPort = Server + Config.NATEscapeGameServerPort = GameServerPort + Config.NATEscapeScreenSharePort = ScreenSharePort Config.NATEscapeBarrierPort = barrireKVM err = Config.WriteConfig() @@ -106,6 +108,37 @@ func main() { log.Fatalln(err) } + // If both server and screenshare have be triggered (first set the according flags to true) + if *BothServers { + *headless = true + *tls = true + *GameServer = true + } + + if *GameServer { + go gameserver.Server(Config.InternalGameServerPort) + } + + if !*GameServer || *BothServers { + if *tls { + log.Println("Listening on TLS:", *addr+":"+Config.InternalScreenSharePort) + go http.ListenAndServeTLS(*addr+":"+Config.InternalScreenSharePort, *certFile, *keyFile, server) + } else { + log.Println("Listening:", *addr+":"+Config.InternalScreenSharePort) + go http.ListenAndServe(*addr+":"+Config.InternalScreenSharePort, server) + } + } + + // If both server selected set the remote Gameserver to local now. + if *BothServers { + Config.BackendURL = "http://" + Config.IPAddress + ":" + Config.NATEscapeGameServerPort + "/" + err = Config.WriteConfig() + if err != nil { + fmt.Println(err) + return + } + } + // Running in headless mode if *headless { // Running starting a browser as a background process @@ -127,15 +160,15 @@ func main() { var TaskExecute string - if *BinaryToExcute != "" { - TaskExecute = *BinaryToExcute + if *BinaryToExecute != "" { + TaskExecute = *BinaryToExecute } else { // Read binary from config file TaskExecute = Config.ScriptToExecute } // Starting screen share headless - cmd := exec.Command(Config.BrowserCommand, "--no-sandbox", "--auto-select-desktop-capture-source="+Config.ScreenName, "--url", "https://"+*addr+":"+*port+"/?mode=headless", "--ignore-certificate-errors") + cmd := exec.Command(Config.BrowserCommand, "--no-sandbox", "--auto-select-desktop-capture-source="+Config.ScreenName, "--url", "https://"+*addr+":"+Config.InternalScreenSharePort+"/?mode=headless", "--ignore-certificate-errors") if err := cmd.Start(); err != nil { log.Fatalln(err) } @@ -160,20 +193,8 @@ func main() { } - if *GameServer { - gameserver.Server(*port) - } else { - if *tls { - log.Println("Listening on TLS:", *addr+":"+*port) - if err := http.ListenAndServeTLS(*addr+":"+*port, *certFile, *keyFile, server); err != nil { - log.Fatalln(err) - } - } else { - log.Println("Listening:", *addr+":"+*port) - if err := http.ListenAndServe(*addr+":"+*port, server); err != nil { - log.Fatalln(err) - } - } + for { + } // Start P2PRC server