From 83653866e63c988f9a743fa213a85e72ca00d858 Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 20 Dec 2023 04:35:04 +0000 Subject: [PATCH] added support for baremetal mode --- cmd/flags.go | 1 + config/config.go | 1 + config/generate/generate.go | 239 ++++++++++++++++--------------- config/generate/generateFiles.go | 64 +++++++++ p2p/iptable.go | 1 + server/server.go | 25 +++- 6 files changed, 206 insertions(+), 125 deletions(-) diff --git a/cmd/flags.go b/cmd/flags.go index 4a6a16f..e46767f 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -20,6 +20,7 @@ var ( UpdateServerList bool ServerList bool SetDefaultConfig bool + BareMetalPublicKey string NetworkInterface bool ViewPlugin bool TrackedContainers bool diff --git a/config/config.go b/config/config.go index 772c844..66ec1bf 100644 --- a/config/config.go +++ b/config/config.go @@ -33,6 +33,7 @@ type Config struct { IPTableKey string PublicKeyFile string PrivateKeyFile string + BareMetal string CustomConfig interface{} //NetworkInterface string //NetworkInterfaceIPV6Index int diff --git a/config/generate/generate.go b/config/generate/generate.go index 2d21838..393c29f 100644 --- a/config/generate/generate.go +++ b/config/generate/generate.go @@ -1,22 +1,22 @@ package generate import ( - "github.com/Akilan1999/p2p-rendering-computation/config" - "math/rand" - "os" - "time" + "github.com/Akilan1999/p2p-rendering-computation/config" + "math/rand" + "os" + "time" ) var ( - defaults = map[string]interface{}{} - configPaths []string - defaultEnvName = "P2PRC" + defaults = map[string]interface{}{} + configPaths []string + defaultEnvName = "P2PRC" ) // GetPathP2PRC Getting P2PRC Directory from environment variable func GetPathP2PRC() (string, error) { - curDir := os.Getenv(defaultEnvName) - return curDir + "/", nil + curDir := os.Getenv(defaultEnvName) + return curDir + "/", nil } // SetEnvName Sets the environment name @@ -24,149 +24,150 @@ func GetPathP2PRC() (string, error) { // your environment variable // This is useful when extending the use case of P2PRC func SetEnvName(EnvName string) error { - if EnvName != "" { - defaultEnvName = EnvName - } - // Handling error to be implemented only if needed - return nil + if EnvName != "" { + defaultEnvName = EnvName + } + // Handling error to be implemented only if needed + return nil } // GetCurrentPath Getting P2PRC Directory from environment variable func GetCurrentPath() (string, error) { - curDir := os.Getenv("PWD") - return curDir + "/", nil + curDir := os.Getenv("PWD") + return curDir + "/", nil } // SetDefaults This function to be called only during a // make install func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, NoBoilerPlate bool, ConfigUpdate ...*config.Config) (*config.Config, error) { - //Setting current directory to default path - defaultPath, err := GetCurrentPath() - if err != nil { - return nil, err - } + //Setting current directory to default path + defaultPath, err := GetCurrentPath() + if err != nil { + return nil, err + } - // Set Env name - err = config.SetEnvName(envName) - if err != nil { - return nil, err - } + // Set Env name + err = config.SetEnvName(envName) + if err != nil { + return nil, err + } - ////Creates ip_table.json in the json directory - //err = Copy("p2p/ip_table.json", "p2p/iptable/ip_table.json") - //if err != nil { - // return err - //} - // - ////Creates a copy of trackcontainers.json in the appropriate directory - //err = Copy("client/trackcontainers.json", "client/trackcontainers/trackcontainers.json") - //if err != nil { - // return err - //} - // - ////Creates a copy of trackcontainers.json in the appropriate directory - //err = Copy("client/grouptrackcontainers.json", "client/trackcontainers/grouptrackcontainers.json") - //if err != nil { - // return err - //} + ////Creates ip_table.json in the json directory + //err = Copy("p2p/ip_table.json", "p2p/iptable/ip_table.json") + //if err != nil { + // return err + //} + // + ////Creates a copy of trackcontainers.json in the appropriate directory + //err = Copy("client/trackcontainers.json", "client/trackcontainers/trackcontainers.json") + //if err != nil { + // return err + //} + // + ////Creates a copy of trackcontainers.json in the appropriate directory + //err = Copy("client/grouptrackcontainers.json", "client/trackcontainers/grouptrackcontainers.json") + //if err != nil { + // return err + //} - var Defaults config.Config + var Defaults config.Config - if len(ConfigUpdate) == 0 { - //Setting default paths for the config file - Defaults.IPTable = defaultPath + "p2p/iptable/ip_table.json" - Defaults.DefaultDockerFile = defaultPath + "server/docker/containers/docker-ubuntu-sshd/" - Defaults.DockerContainers = defaultPath + "server/docker/containers/" - Defaults.SpeedTestFile = defaultPath + "p2p/50.bin" - Defaults.IPV6Address = "" - Defaults.PluginPath = defaultPath + "plugin/deploy" - Defaults.TrackContainersPath = defaultPath + "client/trackcontainers/trackcontainers.json" - Defaults.GroupTrackContainersPath = defaultPath + "client/trackcontainers/grouptrackcontainers.json" - Defaults.ServerPort = "8088" - Defaults.FRPServerPort = "True" - Defaults.CustomConfig = CustomConfig - Defaults.BehindNAT = "True" - Defaults.DockerRunLogs = "/tmp/" - // Generate a random key to be added to IPTable - Defaults.IPTableKey = String(12) - // Random name generator - hostname, err := os.Hostname() - if err != nil { - return nil, err - } + if len(ConfigUpdate) == 0 { + //Setting default paths for the config file + Defaults.IPTable = defaultPath + "p2p/iptable/ip_table.json" + Defaults.DefaultDockerFile = defaultPath + "server/docker/containers/docker-ubuntu-sshd/" + Defaults.DockerContainers = defaultPath + "server/docker/containers/" + Defaults.SpeedTestFile = defaultPath + "p2p/50.bin" + Defaults.IPV6Address = "" + Defaults.PluginPath = defaultPath + "plugin/deploy" + Defaults.TrackContainersPath = defaultPath + "client/trackcontainers/trackcontainers.json" + Defaults.GroupTrackContainersPath = defaultPath + "client/trackcontainers/grouptrackcontainers.json" + Defaults.ServerPort = "8088" + Defaults.FRPServerPort = "True" + Defaults.CustomConfig = CustomConfig + Defaults.BehindNAT = "True" + Defaults.DockerRunLogs = "/tmp/" + // Generate a random key to be added to IPTable + Defaults.IPTableKey = String(12) + // Random name generator + hostname, err := os.Hostname() + if err != nil { + return nil, err + } - // Generate Public and private keys and set path - Defaults.PublicKeyFile = defaultPath + "p2prc.publicKey" - Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey" + // Generate Public and private keys and set path + Defaults.PublicKeyFile = defaultPath + "p2prc.PublicKeyBareMetal" + Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey" + Defaults.BareMetal = "False" - PrivateKeyExists, err := FileExists(Defaults.PrivateKeyFile) - if err != nil { - return nil, err - } + PrivateKeyExists, err := FileExists(Defaults.PrivateKeyFile) + if err != nil { + return nil, err + } - if !PrivateKeyExists { - // Generate SSH keys - err = MakeSSHKeyPair(Defaults.PublicKeyFile, Defaults.PrivateKeyFile) - if err != nil { - return nil, err - } - } + if !PrivateKeyExists { + // Generate SSH keys + err = MakeSSHKeyPair(Defaults.PublicKeyFile, Defaults.PrivateKeyFile) + if err != nil { + return nil, err + } + } - Defaults.MachineName = hostname + "-" + String(7) - } else { - Defaults = *ConfigUpdate[0] - } + Defaults.MachineName = hostname + "-" + String(7) + } else { + Defaults = *ConfigUpdate[0] + } - //defaults["NetworkInterface"] = "wlp0s20f3" - //defaults["NetworkInterfaceIPV6Index"] = "2" + //defaults["NetworkInterface"] = "wlp0s20f3" + //defaults["NetworkInterfaceIPV6Index"] = "2" - //Paths to search for config file - configPaths = append(configPaths, defaultPath) + //Paths to search for config file + configPaths = append(configPaths, defaultPath) - if fileExists(defaultPath+"config.json") && forceDefault { - err := os.Remove(defaultPath + "config.json") - if err != nil { - return nil, err - } - } + if fileExists(defaultPath+"config.json") && forceDefault { + err := os.Remove(defaultPath + "config.json") + if err != nil { + return nil, err + } + } - // write defaults to the config file - err = Defaults.WriteConfig() - if err != nil { - return nil, err - } + // write defaults to the config file + err = Defaults.WriteConfig() + if err != nil { + return nil, err + } - //Calling configuration file - Config, err := config.ConfigInit(defaults, nil, envName) - if err != nil { - return nil, err - } + //Calling configuration file + Config, err := config.ConfigInit(defaults, nil, envName) + if err != nil { + return nil, err + } - if !NoBoilerPlate { - err = GenerateFiles() - if err != nil { - return nil, err - } - } + if !NoBoilerPlate { + err = GenerateFiles() + if err != nil { + return nil, err + } + } - return Config, nil + return Config, nil } // Generating a random string const charset = "abcdefghijklmnopqrstuvwxyz" + - "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" var seededRand *rand.Rand = rand.New( - rand.NewSource(time.Now().UnixNano())) + rand.NewSource(time.Now().UnixNano())) func StringWithCharset(length int, charset string) string { - b := make([]byte, length) - for i := range b { - b[i] = charset[seededRand.Intn(len(charset))] - } - return string(b) + b := make([]byte, length) + for i := range b { + b[i] = charset[seededRand.Intn(len(charset))] + } + return string(b) } func String(length int) string { - return StringWithCharset(length, charset) + return StringWithCharset(length, charset) } diff --git a/config/generate/generateFiles.go b/config/generate/generateFiles.go index 3bb6da8..b3417b8 100644 --- a/config/generate/generateFiles.go +++ b/config/generate/generateFiles.go @@ -223,3 +223,67 @@ func FileExists(path string) (bool, error) { } return false, err } + +// AuthorizedKey struct represents the structure of an authorized key +type AuthorizedKey struct { + Username string + Key string +} + +// AddPublicKeyBareMetal Adds the parameter public key to the auth list +// Generated by ChatGPT +// Prompts: +// - Generate a go program to read authorised key and map it to a struct and write it back to the same location. +// - Can you follow the original format +// (TURNED OUT TO BE PRETTY SHITTY GENERATED CODE) +//func AddPublicKeyBareMetal(PublicKey string) error { +// // Specify the path to the authorized_keys file +// dirname, err := os.UserHomeDir() +// if err != nil { +// return err +// } +// +// authorizedKeysPath := dirname + "/.ssh/" + "authorized_keys" +// +// // Read the contents of the authorized_keys file +// file, err := os.Open(authorizedKeysPath) +// if err != nil { +// return err +// } +// defer file.Close() +// +// // Create a slice to store AuthorizedKey structs +// var authorizedKeys []AuthorizedKey +// +// // Read each line of the file and parse the data +// scanner := bufio.NewScanner(file) +// for scanner.Scan() { +// line := scanner.Text() +// parts := strings.Fields(line) +// if len(parts) >= 2 { +// authorizedKey := AuthorizedKey{ +// Username: parts[0], +// Key: parts[1], +// } +// authorizedKeys = append(authorizedKeys, authorizedKey) +// } +// } +// +// if err := scanner.Err(); err != nil { +// return err +// } +// +// // Modify the slice of structs if needed +// // authorizedKeys[0].Username = "newUsername" +// // authorizedKeys[0].Key = "newKey" +// authorizedKeys = append(authorizedKeys, AuthorizedKey{Username: "", Key: PublicKey}) +// +// // Write the modified data back to the authorized_keys file +// newFile, err := os.Create(authorizedKeysPath) +// if err != nil { +// return err +// } +// defer newFile.Close() +// +// return nil +//} diff --git a/p2p/iptable.go b/p2p/iptable.go index e185e14..0f67d42 100644 --- a/p2p/iptable.go +++ b/p2p/iptable.go @@ -27,6 +27,7 @@ type IpAddress struct { Download float64 `json:"Download"` Upload float64 `json:"Upload"` ServerPort string `json:"ServerPort"` + BareMetalSSHPort string `json:"BareMetalSSHPort"` NAT string `json:"NAT"` EscapeImplementation string `json:"EscapeImplementation"` CustomInformation string `json:"CustomInformation"` diff --git a/server/server.go b/server/server.go index 5f19a6c..088dd94 100644 --- a/server/server.go +++ b/server/server.go @@ -224,6 +224,19 @@ func Server() (*gin.Engine, error) { ProxyIpAddr.Name = config.MachineName ProxyIpAddr.NAT = "False" ProxyIpAddr.EscapeImplementation = "FRP" + + // Sorry Jan it's a string + // Yes I could convert it + // to a boolean operator + // But I am way too tired + if config.BareMetal == "True" { + _, SSHPort, err := MapPort("22") + if err != nil { + return nil, err + } + ProxyIpAddr.BareMetalSSHPort = SSHPort + } + //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) // append the following to the ip table @@ -252,11 +265,11 @@ func Server() (*gin.Engine, error) { return r, nil } -func MapPort(port string) (string, error) { +func MapPort(port string) (string, string, error) { //Get Server port based on the config file config, err := config.ConfigInit(nil, nil) if err != nil { - return "", err + return "", "", err } // update IPTable with new port and ip address and update ip table @@ -267,7 +280,7 @@ func MapPort(port string) (string, error) { table, err := p2p.ReadIpTable() if err != nil { - return "", err + return "", "", err } var lowestLatency int64 @@ -287,14 +300,14 @@ func MapPort(port string) (string, error) { if lowestLatency != 10000000 { serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) if err != nil { - return "", err + return "", "", err } // Create 3 second delay to allow FRP server to start time.Sleep(1 * time.Second) // Starts FRP as a client with proxyPort, err := frp.StartFRPClientForServer(lowestLatencyIpAddress.Ipv4, serverPort, port, "") if err != nil { - return "", err + return "", "", err } // updating with the current proxy address @@ -306,5 +319,5 @@ func MapPort(port string) (string, error) { //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) } - return ProxyIpAddr.Ipv4 + ":" + ProxyIpAddr.ServerPort, nil + return ProxyIpAddr.Ipv4 + ":" + ProxyIpAddr.ServerPort, ProxyIpAddr.ServerPort, nil }