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
room.json
laplace
scripts/
scripts/
server/
client/
p2p/

1
.idea/vcs.xml generated
View File

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

View File

View File

View File

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

View File

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

View File

@@ -1,138 +1,16 @@
package core
import (
"github.com/fatedier/frp/client"
"github.com/fatedier/frp/pkg/config"
"github.com/phayes/freeport"
"io/ioutil"
"net/http"
"strconv"
"github.com/Akilan1999/p2p-rendering-computation/p2p/frp"
"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
// - 1 port for server
// - 2 port for barrierKVM
func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error) {
// 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 {
return
@@ -141,13 +19,13 @@ func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error
time.Sleep(1 * time.Second)
// 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 {
return
}
// 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 {
return
@@ -156,7 +34,7 @@ func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error
time.Sleep(1 * time.Second)
//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 {
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
replace github.com/Akilan1999/p2p-rendering-computation => /Users/akilan/Documents/p2p-rendering-computation
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/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 // indirect
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
import (
"encoding/json"
"flag"
"fmt"
"github.com/Akilan1999/remotegameplay/config"
"github.com/Akilan1999/remotegameplay/core"
"log"
"math/rand"
"net/http"
"os/exec"
"time"
"encoding/json"
"flag"
"fmt"
"github.com/Akilan1999/remotegameplay/config"
"github.com/Akilan1999/remotegameplay/core"
"log"
"math/rand"
"net/http"
"os/exec"
"time"
)
func main() {
addr := flag.String("addr", "localhost", "Listen address")
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")
keyFile := flag.String("keyFile", "files/server.key", "TLS key file")
headless := flag.Bool("headless", false, "Creating screenshare using headless mode")
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")
addr := flag.String("addr", "localhost", "Listen address")
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")
keyFile := flag.String("keyFile", "files/server.key", "TLS key file")
headless := flag.Bool("headless", false, "Creating screenshare using headless mode")
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")
flag.Parse()
flag.Parse()
// Action performed when the config file is called
if *setconfig {
config.SetDefaults()
return
}
rand.Seed(time.Now().UnixNano())
server := core.GetHttp()
rand.Seed(time.Now().UnixNano())
server := core.GetHttp()
err := config.SetDefaults()
if err != nil {
return
}
// running implementation to escape NAT
Server, barrireKVM, err := core.EscapeNAT(*port)
if err != nil {
log.Fatalln(err)
}
// 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)
}
Config.IPAddress = "64.227.168.102"
Config.NATEscapeServerPort = Server
Config.NATEscapeBarrierPort = barrireKVM
Config, err := config.ConfigInit()
if err != nil {
log.Fatalln(err)
}
err = Config.WriteConfig()
if err != nil {
log.Fatalln(err)
}
Config.IPAddress = "64.227.168.102"
Config.NATEscapeServerPort = Server
Config.NATEscapeBarrierPort = barrireKVM
// Print out room information
if *roomInfo {
room, err := core.ReadRoomsFile()
if err != nil {
log.Fatalln(err)
}
fmt.Println(Config)
PrettyPrint(room)
return
}
err = Config.WriteConfig()
if err != nil {
log.Fatalln(err)
}
// Running in headless mode
if *headless {
Config, err := config.ConfigInit()
if err != nil {
log.Fatalln(err)
}
// Print out room information
if *roomInfo {
room, err := core.ReadRoomsFile()
if err != nil {
log.Fatalln(err)
}
// Returns the URl address type
Addr := Ip4or6(Config.IPAddress)
PrettyPrint(room)
return
}
// If address is provided
if *addr != "" {
Addr = *addr
// Add brackets if the ip address is ipv6
Addr = Ip4or6(Addr)
}
// Running in headless mode
if *headless {
Config, err := config.ConfigInit()
if err != nil {
log.Fatalln(err)
}
var TaskExecute string
// Returns the URl address type
Addr := Ip4or6(Config.IPAddress)
if *BinaryToExcute != "" {
TaskExecute = *BinaryToExcute
} else {
// Read binary from config file
TaskExecute = Config.ScriptToExecute
}
// If address is provided
if *addr != "" {
Addr = *addr
// Add brackets if the ip address is ipv6
Addr = Ip4or6(Addr)
}
// 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")
if err := cmd.Start(); err != nil {
log.Fatalln(err)
}
var TaskExecute string
// Makes program sleep for 2 seconds to allow chromium browser to open
time.Sleep(3 * time.Second)
if *BinaryToExcute != "" {
TaskExecute = *BinaryToExcute
} else {
// Read binary from config file
TaskExecute = Config.ScriptToExecute
}
// Task to be executed
err = RunTask(TaskExecute)
if err != nil {
fmt.Println(err)
return
}
// 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")
if err := cmd.Start(); err != nil {
log.Fatalln(err)
}
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
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
}
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)
}
}
}
// kills laplace server
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 {
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 (
// Reference: https://stackoverflow.com/questions/24512112/how-to-print-struct-variables-in-console
func PrettyPrint(data interface{}) {
var p []byte
// var err := error
p, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s \n", p)
var p []byte
// var err := error
p, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s \n", p)
}
// 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 {
for i := 0; i < len(s); i++ {
switch s[i] {
case '.':
return s
case ':':
return "[" + s + "]"
}
}
return "[" + s + "]"
for i := 0; i < len(s); i++ {
switch s[i] {
case '.':
return s
case ':':
return "[" + s + "]"
}
}
return "[" + s + "]"
}
func RunTask(task string) error {
// Halts the process
cmd := exec.Command("sh", task)
if err := cmd.Start(); err != nil {
return err
}
// Halts the process
cmd := exec.Command("sh", task)
if err := cmd.Start(); err != nil {
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