added broadcast roomID

This commit is contained in:
2023-02-25 01:35:20 +00:00
parent d46e49ca6e
commit ed898e3866
2 changed files with 180 additions and 180 deletions

View File

@@ -40,14 +40,14 @@ func BroadcastServerToBackend() error {
respIpv4orIPv6 = "http://" + respIpv4orIPv6 respIpv4orIPv6 = "http://" + respIpv4orIPv6
// Get room information // Get room information
_, err = ReadRoomsFile() room, err := ReadRoomsFile()
if err != nil { if err != nil {
return err return err
} }
// Game session url // Game session url
//+ file.ID //+ file.ID
gameSession.Link = respIpv4orIPv6 + ":" + config.NATEscapeServerPort + "/?id=" gameSession.Link = respIpv4orIPv6 + ":" + config.NATEscapeServerPort + "/?id=" + room.ID
// Rate for the game session // Rate for the game session
gameSession.Rate = config.Rate gameSession.Rate = config.Rate
// Server specs to struct GameSession // Server specs to struct GameSession

356
main.go
View File

@@ -1,225 +1,225 @@
package main package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"github.com/Akilan1999/remotegameplay/config" "github.com/Akilan1999/remotegameplay/config"
"github.com/Akilan1999/remotegameplay/core" "github.com/Akilan1999/remotegameplay/core"
gameserver "github.com/Akilan1999/remotegameplay/server" gameserver "github.com/Akilan1999/remotegameplay/server"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
"os/exec" "os/exec"
"time" "time"
) )
func main() { func main() {
addr := flag.String("addr", "0.0.0.0", "Listen address") 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") tls := flag.Bool("tls", false, "Use TLS")
setconfig := flag.Bool("setconfig", false, "Generates a config file") setconfig := flag.Bool("setconfig", false, "Generates a config file")
certFile := flag.String("certFile", "files/server.crt", "TLS cert file") certFile := flag.String("certFile", "files/server.crt", "TLS cert file")
keyFile := flag.String("keyFile", "files/server.key", "TLS key file") keyFile := flag.String("keyFile", "files/server.key", "TLS key file")
headless := flag.Bool("headless", false, "Creating screenshare using headless mode") headless := flag.Bool("headless", false, "Creating screenshare using headless mode")
roomInfo := flag.Bool("roomInfo", false, "Getting room id of headless server") roomInfo := flag.Bool("roomInfo", false, "Getting room id of headless server")
killServer := flag.Bool("killServer", false, "Kills the laplace") killServer := flag.Bool("killServer", false, "Kills the laplace")
killChromium := flag.Bool("killChromium", false, "Kills all chromuim") killChromium := flag.Bool("killChromium", false, "Kills all chromuim")
BinaryToExcute := flag.String("BinaryToExecute", "", "Providing path (i.e Absolute path) of binary to execute") BinaryToExcute := flag.String("BinaryToExecute", "", "Providing path (i.e Absolute path) of binary to execute")
GameServer := flag.Bool("GameServer", false, "Starts the game server by default") GameServer := flag.Bool("GameServer", false, "Starts the game server by default")
Migrate := flag.Bool("Migrate", false, "Sets up the tables for the Sqlite database") Migrate := flag.Bool("Migrate", false, "Sets up the tables for the Sqlite database")
flag.Parse() flag.Parse()
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
server := core.GetHttp() server := core.GetHttp()
if *setconfig { if *setconfig {
err := config.SetDefaults() err := config.SetDefaults()
if err != nil { if err != nil {
return return
} }
return return
} }
// Print out room information // Print out room information
if *roomInfo { if *roomInfo {
room, err := core.ReadRoomsFile() room, err := core.ReadRoomsFile()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
PrettyPrint(room) PrettyPrint(room)
return return
} }
// kills laplace server // kills laplace server
if *killServer { if *killServer {
cmd := exec.Command("pkill", "remotegameplay") cmd := exec.Command("pkill", "remotegameplay")
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
fmt.Println(err) fmt.Println(err)
} }
return return
} }
// kills chromium server // kills chromium server
if *killChromium { if *killChromium {
cmd := exec.Command("pkill", "chromium") cmd := exec.Command("pkill", "chromium")
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
fmt.Println(err) fmt.Println(err)
} }
return return
} }
if *Migrate { if *Migrate {
// Connect to Sqlite // Connect to Sqlite
connect, err := gameserver.Connect() connect, err := gameserver.Connect()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
// Migrate table // Migrate table
session, err := gameserver.CreateTables(connect) session, err := gameserver.CreateTables(connect)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
session.Scan(gameserver.GameSession{}) session.Scan(gameserver.GameSession{})
return return
} }
// running implementation to escape NAT // running implementation to escape NAT
Server, barrireKVM, err := core.EscapeNAT(*port) Server, barrireKVM, err := core.EscapeNAT(*port)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
Config, err := config.ConfigInit() Config, err := config.ConfigInit()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
Config.IPAddress = "64.227.168.102" Config.IPAddress = "64.227.168.102"
Config.NATEscapeServerPort = Server Config.NATEscapeServerPort = Server
Config.NATEscapeBarrierPort = barrireKVM Config.NATEscapeBarrierPort = barrireKVM
err = Config.WriteConfig() err = Config.WriteConfig()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
// Running in headless mode // Running in headless mode
if *headless { if *headless {
// Running starting a browser as a background process // Running starting a browser as a background process
go func() { go func() {
Config, err := config.ConfigInit() Config, err := config.ConfigInit()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
//// Returns the URl address type //// Returns the URl address type
//Addr := Ip4or6(Config.IPAddress) //Addr := Ip4or6(Config.IPAddress)
// //
//// If address is provided //// If address is provided
//if *addr != "" { //if *addr != "" {
// Addr = *addr // Addr = *addr
// // Add brackets if the ip address is ipv6 // // Add brackets if the ip address is ipv6
// Addr = Ip4or6(Addr) // Addr = Ip4or6(Addr)
//} //}
var TaskExecute string var TaskExecute string
if *BinaryToExcute != "" { if *BinaryToExcute != "" {
TaskExecute = *BinaryToExcute TaskExecute = *BinaryToExcute
} else { } else {
// Read binary from config file // Read binary from config file
TaskExecute = Config.ScriptToExecute TaskExecute = Config.ScriptToExecute
} }
fmt.Println(addr) fmt.Println(addr)
// Starting screen share headless // Starting screen share headless
cmd := exec.Command("chromium-browser", "--no-sandbox", "--auto-select-desktop-capture-source=Entire screen", "--url", "https://"+*addr+":"+*port+"/?mode=headless", "--ignore-certificate-errors") cmd := exec.Command("chromium-browser", "--no-sandbox", "--auto-select-desktop-capture-source=Entire screen", "--url", "https://"+*addr+":"+*port+"/?mode=headless", "--ignore-certificate-errors")
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
// Makes program sleep for 2 seconds to allow chromium browser to open // Makes program sleep for 2 seconds to allow chromium browser to open
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
err = core.BroadcastServerToBackend() err = core.BroadcastServerToBackend()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} else { } else {
fmt.Println("success broadcasting to game server") fmt.Println("success broadcasting to game server")
} }
// Task to be executed // Task to be executed
err = RunTask(TaskExecute) err = RunTask(TaskExecute)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
}() }()
} }
if *GameServer { if *GameServer {
gameserver.Server(*port) gameserver.Server(*port)
} else { } else {
if *tls { if *tls {
log.Println("Listening on TLS:", *addr+":"+*port) log.Println("Listening on TLS:", *addr+":"+*port)
if err := http.ListenAndServeTLS(*addr+":"+*port, *certFile, *keyFile, server); err != nil { if err := http.ListenAndServeTLS(*addr+":"+*port, *certFile, *keyFile, server); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
} else { } else {
log.Println("Listening:", *addr+":"+*port) log.Println("Listening:", *addr+":"+*port)
if err := http.ListenAndServe(*addr+":"+*port, server); err != nil { if err := http.ListenAndServe(*addr+":"+*port, server); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
} }
} }
// Start P2PRC server // Start P2PRC server
//_, err = abstractions.Start() //_, err = abstractions.Start()
//if err != nil { //if err != nil {
// return // return
//} //}
} }
// PrettyPrint print the contents of the obj ( // PrettyPrint print the contents of the obj (
// Reference: https://stackoverflow.com/questions/24512112/how-to-print-struct-variables-in-console // Reference: https://stackoverflow.com/questions/24512112/how-to-print-struct-variables-in-console
func PrettyPrint(data interface{}) { func PrettyPrint(data interface{}) {
var p []byte var p []byte
// var err := error // var err := error
p, err := json.MarshalIndent(data, "", "\t") p, err := json.MarshalIndent(data, "", "\t")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
fmt.Printf("%s \n", p) fmt.Printf("%s \n", p)
} }
// Ip4or6 Helper function to check if the IP address is IPV4 or // Ip4or6 Helper function to check if the IP address is IPV4 or
// IPV6 (https://socketloop.com/tutorials/golang-check-if-ip-address-is-version-4-or-6) // IPV6 (https://socketloop.com/tutorials/golang-check-if-ip-address-is-version-4-or-6)
func Ip4or6(s string) string { func Ip4or6(s string) string {
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
switch s[i] { switch s[i] {
case '.': case '.':
return s return s
case ':': case ':':
return "[" + s + "]" return "[" + s + "]"
} }
} }
return "[" + s + "]" return "[" + s + "]"
} }
func RunTask(task string) error { func RunTask(task string) error {
// Halts the process // Halts the process
cmd := exec.Command("sh", task) cmd := exec.Command("sh", task)
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
return err return err
} }
return nil return nil
} }