fixed configuration

This commit is contained in:
2023-02-24 02:52:15 +00:00
parent 8d22b39d0b
commit 574317d286
13 changed files with 787 additions and 423 deletions

5
.gitignore vendored
View File

@@ -3,4 +3,7 @@ env/
remotegameplay remotegameplay
room.json room.json
laplace laplace
scripts/ scripts/
server/
client/
p2p/

1
.idea/vcs.xml generated
View File

@@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/server/docker/containers/docker-ubuntu-sshd" vcs="Git" />
</component> </component>
</project> </project>

View File

View File

View File

@@ -1,168 +1,159 @@
package config package config
import ( import (
"github.com/spf13/viper" "github.com/spf13/viper"
"os" "os"
"os/user" "os/user"
) )
var ( var (
defaultPath string defaultPath string
defaults = map[string]interface{}{ defaults = map[string]interface{}{}
"SystemUsername": "", configName = "config"
"BarrierHostName": "", configType = "json"
} configFile = "config.json"
configName = "config" configPaths []string
configType = "json"
configFile = "config.json"
configPaths []string
) )
type Config struct { type Config struct {
SystemUsername string SystemUsername string
BarrierHostName string BarrierHostName string
Rooms string Rooms string
IPAddress string IPAddress string
ScriptToExecute string ScriptToExecute string
SSHPassword string SSHPassword string
NATEscapeServerPort string NATEscapeServerPort string
NATEscapeBarrierPort string NATEscapeBarrierPort string
} }
// Exists reports whether the named file or directory exists. // Exists reports whether the named file or directory exists.
func fileExists(name string) bool { func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil { if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false return false
} }
} }
return true return true
} }
// SetDefaults This function to be called only during a // SetDefaults This function to be called only during a
// make install // make install
func SetDefaults() error { func SetDefaults() error {
//Getting Current Directory from environment variable //Getting Current Directory from environment variable
curDir := os.Getenv("REMOTEGAMING") curDir := os.Getenv("REMOTEGAMING")
//Setting current directory to default path //Setting current directory to default path
defaultPath = curDir + "/" defaultPath = curDir + "/"
// Get system username // Get system username
user, err := user.Current() user, err := user.Current()
if err != nil { if err != nil {
return err return err
} }
// Get Hostname // Get Hostname
name, err := os.Hostname() name, err := os.Hostname()
if err != nil { if err != nil {
return err return err
} }
//Setting default paths for the config file //Setting default paths for the config file
defaults["SystemUsername"] = user.Username defaults["SystemUsername"] = user.Username
defaults["BarrierHostName"] = name defaults["BarrierHostName"] = name
defaults["Rooms"] = defaultPath + "room.json" defaults["Rooms"] = defaultPath + "room.json"
defaults["IPAddress"] = "0.0.0.0" defaults["ScriptToExecute"] = ""
defaults["ScriptToExecute"] = "" defaults["SSHPassword"] = ""
defaults["SSHPassword"] = ""
//Paths to search for config file //Paths to search for config file
configPaths = append(configPaths, defaultPath) configPaths = append(configPaths, defaultPath)
//Create rooms.json file //Create rooms.json file
os.Create(defaultPath + "room.json") os.Create(defaultPath + "room.json")
// If the config file exists remove and make a new one // If the config file exists remove and make a new one
if fileExists(defaultPath + "config.json") { if fileExists(defaultPath + "config.json") {
err = os.Remove(defaultPath + "config.json") err = os.Remove(defaultPath + "config.json")
if err != nil { if err != nil {
return err return err
} }
} }
//Calling configuration file //Calling configuration file
_, err = ConfigInit() _, err = ConfigInit()
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
func ConfigInit() (*Config, error) { func ConfigInit() (*Config, error) {
curDir := os.Getenv("REMOTEGAMING") curDir := os.Getenv("REMOTEGAMING")
//Setting current directory to default path //Setting current directory to default path
defaultPath = curDir + "/" defaultPath = curDir + "/"
//Paths to search for config file //Paths to search for config file
configPaths = append(configPaths, defaultPath) configPaths = append(configPaths, defaultPath)
//Add all possible configurations paths //Add all possible configurations paths
for _, v := range configPaths { for _, v := range configPaths {
viper.AddConfigPath(v) viper.AddConfigPath(v)
} }
//Read config file //Read config file
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
// If the error thrown is config file not found // If the error thrown is config file not found
//Sets default configuration to viper //Sets default configuration to viper
for k, v := range defaults { for k, v := range defaults {
viper.SetDefault(k, v) viper.SetDefault(k, v)
} }
viper.SetConfigName(configName)
viper.SetConfigFile(configFile)
viper.SetConfigType(configType)
if err = viper.WriteConfig(); err != nil { viper.SetConfigName(configName)
return nil, err viper.SetConfigFile(configFile)
} viper.SetConfigType(configType)
}
// Adds configuration to the struct if err = viper.WriteConfig(); err != nil {
var config Config return nil, err
if err := viper.Unmarshal(&config); err != nil { }
return nil, err }
}
return &config, nil // Adds configuration to the struct
var config Config
if err := viper.Unmarshal(&config); err != nil {
return nil, err
}
return &config, nil
} }
func (c *Config) WriteConfig() error { func (c *Config) WriteConfig() error {
//Getting Current Directory from environment variable //Getting Current Directory from environment variable
curDir := os.Getenv("REMOTEGAMING") curDir := os.Getenv("REMOTEGAMING")
//Setting current directory to default path //Setting current directory to default path
defaultPath = curDir + "/" defaultPath = curDir + "/"
//Setting default paths for the config file //Setting default paths for the config file
defaults["SystemUsername"] = c.SystemUsername defaults["SystemUsername"] = c.SystemUsername
defaults["BarrierHostName"] = c.BarrierHostName defaults["BarrierHostName"] = c.BarrierHostName
defaults["Rooms"] = c.Rooms defaults["Rooms"] = c.Rooms
defaults["IPAddress"] = c.IPAddress defaults["IPAddress"] = c.IPAddress
defaults["ScriptToExecute"] = c.ScriptToExecute defaults["ScriptToExecute"] = c.ScriptToExecute
defaults["SSHPassword"] = c.SSHPassword defaults["SSHPassword"] = c.SSHPassword
defaults["NATEscapeServerPort"] = c.NATEscapeServerPort defaults["NATEscapeServerPort"] = c.NATEscapeServerPort
defaults["NATEscapeBarrierPort"] = c.NATEscapeBarrierPort defaults["NATEscapeBarrierPort"] = c.NATEscapeBarrierPort
//Paths to search for config file // If the config file exists remove and make a new one
configPaths = append(configPaths, defaultPath) //if fileExists(defaultPath + "config.json") {
err := os.Remove(defaultPath + "config.json")
if err != nil {
return err
}
//}
//Create rooms.json file //Calling configuration file
os.Create(defaultPath + "room.json") _, err = ConfigInit()
if err != nil {
// If the config file exists remove and make a new one return err
if fileExists(defaultPath + "config.json") { }
err := os.Remove(defaultPath + "config.json") return nil
if err != nil {
return err
}
}
//Calling configuration file
_, err := ConfigInit()
if err != nil {
return err
}
return nil
} }

View File

@@ -1,19 +1,19 @@
package config package config
import ( import (
"testing" "testing"
) )
func TestConfigInit(t *testing.T) { func TestConfigInit(t *testing.T) {
_, err := ConfigInit() _, err := ConfigRead()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
} }
func TestSetDefaults(t *testing.T) { func TestSetDefaults(t *testing.T) {
err := SetDefaults() err := SetDefaults()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
} }

View File

@@ -1,138 +1,16 @@
package core package core
import ( import (
"github.com/fatedier/frp/client" "github.com/Akilan1999/p2p-rendering-computation/p2p/frp"
"github.com/fatedier/frp/pkg/config"
"github.com/phayes/freeport"
"io/ioutil"
"net/http"
"strconv"
"time" "time"
) )
type Server struct {
address string
port int
}
// Client This struct stores
// client information with server
// proxy connected
type Client struct {
Name string
Server *Server
ClientMappings []ClientMapping
}
// ClientMapping Stores client mapping ports
// to proxy server
type ClientMapping struct {
LocalIP string
LocalPort int
RemotePort int
}
// StartFRPClientForServer Starts Server using FRP server
// returns back a port
func StartFRPClientForServer(ipaddress string, serverport string, localport string) (string, error) {
// Setup server information
var s Server
s.address = ipaddress
// convert serverport to int
portInt, err := strconv.Atoi(serverport)
if err != nil {
return "", err
}
s.port = portInt
// Setup client information
var c Client
c.Name = "ServerPort"
c.Server = &s
// converts localport to int
portInt, err = strconv.Atoi(localport)
if err != nil {
return "", err
}
//random serverport
//randPort := rangeIn(10000, 99999)
OpenPorts, err := freeport.GetFreePorts(1)
if err != nil {
return "", err
}
c.ClientMappings = []ClientMapping{
{
LocalIP: "localhost",
LocalPort: portInt,
RemotePort: OpenPorts[0],
},
}
// Start client server
go c.StartFRPClient()
return strconv.Itoa(OpenPorts[0]), nil
}
// StartFRPClient Starts FRP client
func (c *Client) StartFRPClient() error {
cfg := config.GetDefaultClientConf()
var proxyConfs map[string]config.ProxyConf
var visitorCfgs map[string]config.VisitorConf
proxyConfs = make(map[string]config.ProxyConf)
cfg.ServerAddr = c.Server.address
cfg.ServerPort = c.Server.port
for i, _ := range c.ClientMappings {
var tcpcnf config.TCPProxyConf
tcpcnf.LocalIP = c.ClientMappings[i].LocalIP
tcpcnf.LocalPort = c.ClientMappings[i].LocalPort
tcpcnf.RemotePort = c.ClientMappings[i].RemotePort
proxyConfs[tcpcnf.ProxyName] = &tcpcnf
}
cli, err := client.NewService(cfg, proxyConfs, visitorCfgs, "")
if err != nil {
return err
}
cli.Run()
return nil
}
func GetFRPServerPort(host string) (string, error) {
resp, err := http.Get(host + "/FRPPort")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// EscapeNAT Func to escape NAT // EscapeNAT Func to escape NAT
// - 1 port for server // - 1 port for server
// - 2 port for barrierKVM // - 2 port for barrierKVM
func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error) { func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error) {
// Get free port from P2PRC server node // Get free port from P2PRC server node
serverPort, err := GetFRPServerPort("http://64.227.168.102:8088") serverPort, err := frp.GetFRPServerPort("http://64.227.168.102:8088")
if err != nil { if err != nil {
return return
@@ -141,13 +19,13 @@ func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
// port for the remote gameplay server // port for the remote gameplay server
ServerPort, err = StartFRPClientForServer("64.227.168.102", serverPort, Port) ServerPort, err = frp.StartFRPClientForServer("64.227.168.102", serverPort, Port)
if err != nil { if err != nil {
return return
} }
// Get free port from P2PRC server node // Get free port from P2PRC server node
serverPort, err = GetFRPServerPort("http://64.227.168.102:8088") serverPort, err = frp.GetFRPServerPort("http://64.227.168.102:8088")
if err != nil { if err != nil {
return return
@@ -156,7 +34,7 @@ func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
//port for the barrierKVM server //port for the barrierKVM server
barrierKVMport, err = StartFRPClientForServer("64.227.168.102", serverPort, "24800") barrierKVMport, err = frp.StartFRPClientForServer("64.227.168.102", serverPort, "24800")
if err != nil { if err != nil {
return return
} }

View File

@@ -1,16 +0,0 @@
package core
import (
"fmt"
"testing"
)
func TestEscapeNAT(t *testing.T) {
nat, s, err := EscapeNAT("8090")
if err != nil {
fmt.Println(err)
t.Fail()
}
fmt.Println(nat)
fmt.Println(s)
}

7
go.mod
View File

@@ -2,9 +2,12 @@ module github.com/Akilan1999/remotegameplay
go 1.16 go 1.16
replace github.com/Akilan1999/p2p-rendering-computation => /Users/akilan/Documents/p2p-rendering-computation
require ( require (
github.com/fatedier/frp v0.47.0 github.com/Akilan1999/p2p-rendering-computation v0.0.0-00010101000000-000000000000
github.com/fatedier/frp v0.47.0 // indirect
github.com/gorilla/websocket v1.5.0 github.com/gorilla/websocket v1.5.0
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 // indirect
github.com/spf13/viper v1.8.1 github.com/spf13/viper v1.8.1
) )

484
go.sum

File diff suppressed because it is too large Load Diff

290
main.go
View File

@@ -1,183 +1,191 @@
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"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
"os/exec" "os/exec"
"time" "time"
) )
func main() { func main() {
addr := flag.String("addr", "localhost", "Listen address") addr := flag.String("addr", "localhost", "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")
flag.Parse() flag.Parse()
// Action performed when the config file is called rand.Seed(time.Now().UnixNano())
if *setconfig { server := core.GetHttp()
config.SetDefaults()
return
}
rand.Seed(time.Now().UnixNano()) err := config.SetDefaults()
server := core.GetHttp() if err != nil {
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.NATEscapeServerPort = Server
Config.NATEscapeBarrierPort = barrireKVM
err = Config.WriteConfig() Config.IPAddress = "64.227.168.102"
if err != nil { Config.NATEscapeServerPort = Server
log.Fatalln(err) Config.NATEscapeBarrierPort = barrireKVM
}
// Print out room information fmt.Println(Config)
if *roomInfo {
room, err := core.ReadRoomsFile()
if err != nil {
log.Fatalln(err)
}
PrettyPrint(room) err = Config.WriteConfig()
return if err != nil {
} log.Fatalln(err)
}
// Running in headless mode // Print out room information
if *headless { if *roomInfo {
Config, err := config.ConfigInit() room, err := core.ReadRoomsFile()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
// Returns the URl address type PrettyPrint(room)
Addr := Ip4or6(Config.IPAddress) return
}
// If address is provided // Running in headless mode
if *addr != "" { if *headless {
Addr = *addr Config, err := config.ConfigInit()
// Add brackets if the ip address is ipv6 if err != nil {
Addr = Ip4or6(Addr) log.Fatalln(err)
} }
var TaskExecute string // Returns the URl address type
Addr := Ip4or6(Config.IPAddress)
if *BinaryToExcute != "" { // If address is provided
TaskExecute = *BinaryToExcute if *addr != "" {
} else { Addr = *addr
// Read binary from config file // Add brackets if the ip address is ipv6
TaskExecute = Config.ScriptToExecute Addr = Ip4or6(Addr)
} }
// Starting screen share headless var TaskExecute string
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 {
log.Fatalln(err)
}
// Makes program sleep for 2 seconds to allow chromium browser to open if *BinaryToExcute != "" {
time.Sleep(3 * time.Second) TaskExecute = *BinaryToExcute
} else {
// Read binary from config file
TaskExecute = Config.ScriptToExecute
}
// Task to be executed // Starting screen share headless
err = RunTask(TaskExecute) 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 != nil { if err := cmd.Start(); err != nil {
fmt.Println(err) log.Fatalln(err)
return }
}
return // Makes program sleep for 2 seconds to allow chromium browser to open
time.Sleep(3 * time.Second)
} // Task to be executed
err = RunTask(TaskExecute)
if err != nil {
fmt.Println(err)
return
}
// kills laplace server return
if *killServer {
cmd := exec.Command("pkill", "remotegameplay")
if err := cmd.Run(); err != nil {
fmt.Println(err)
}
return
}
// kills chromium server
if *killChromium {
cmd := exec.Command("pkill", "chromium")
if err := cmd.Run(); err != nil {
fmt.Println(err)
}
return
}
if *tls { }
log.Println("Listening on TLS:", *addr+":"+*port)
if err := http.ListenAndServeTLS(*addr+":"+*port, *certFile, *keyFile, server); err != nil { // kills laplace server
log.Fatalln(err) if *killServer {
} cmd := exec.Command("pkill", "remotegameplay")
} else { if err := cmd.Run(); err != nil {
log.Println("Listening:", *addr+":"+*port) fmt.Println(err)
if err := http.ListenAndServe(*addr+":"+*port, server); err != nil { }
log.Fatalln(err) return
} }
} // kills chromium server
if *killChromium {
cmd := exec.Command("pkill", "chromium")
if err := cmd.Run(); err != nil {
fmt.Println(err)
}
return
}
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)
}
}
// Start P2PRC server
//_, err = abstractions.Start()
//if err != nil {
// 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
} }

15
p2p/iptable/ip_table.json Normal file
View File

@@ -0,0 +1,15 @@
{
"ip_address": [
{
"Name": "Node1",
"IPV4": "64.227.168.102",
"IPV6": "",
"Latency": 0,
"Download": 0,
"Upload": 0,
"ServerPort": "8088",
"NAT": "False",
"EscapeImplementation": ""
}
]
}

Submodule server/docker/containers/docker-ubuntu-sshd added at fbee2ad6a9