diff --git a/abstractions/base.go b/abstractions/base.go index f009c56..e32ba6f 100644 --- a/abstractions/base.go +++ b/abstractions/base.go @@ -2,94 +2,108 @@ package abstractions import "C" import ( - "github.com/Akilan1999/p2p-rendering-computation/client" - "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" - Config "github.com/Akilan1999/p2p-rendering-computation/config" - "github.com/Akilan1999/p2p-rendering-computation/config/generate" - "github.com/Akilan1999/p2p-rendering-computation/p2p" - "github.com/Akilan1999/p2p-rendering-computation/server" - "github.com/Akilan1999/p2p-rendering-computation/server/docker" - "github.com/gin-gonic/gin" - "os" + "github.com/Akilan1999/p2p-rendering-computation/client" + "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" + Config "github.com/Akilan1999/p2p-rendering-computation/config" + "github.com/Akilan1999/p2p-rendering-computation/config/generate" + "github.com/Akilan1999/p2p-rendering-computation/p2p" + "github.com/Akilan1999/p2p-rendering-computation/server" + "github.com/Akilan1999/p2p-rendering-computation/server/docker" + "github.com/gin-gonic/gin" + "os" ) // Init Initialises p2prc func Init(customConfig interface{}) (config *Config.Config, err error) { - // Get config file path - // Checks P2PRC path initially - // - Get PATH if environment varaible - path, err := Config.GetPathP2PRC("P2PRC") - if err != nil { - return - } - // check if the config file exists - if _, err = os.Stat(path + "config.json"); err != nil { - // Initialize with base p2prc config files - // set the config file with default paths - config, err = generate.SetDefaults("P2PRC", false, customConfig, false) - if err != nil { - return - } - } else { - // If the configs are available then use them over generating new ones. - config, err = Config.ConfigInit(nil, nil) - if err != nil { - return - } - } + // Get config file path + // Checks P2PRC path initially + // - Get PATH if environment varaible + path, err := Config.GetPathP2PRC("P2PRC") + if err != nil { + return + } + // check if the config file exists + if _, err = os.Stat(path + "config.json"); err != nil { + // Initialize with base p2prc config files + // set the config file with default paths + config, err = generate.SetDefaults("P2PRC", false, customConfig, false) + if err != nil { + return + } + } else { + // If the configs are available then use them over generating new ones. + config, err = Config.ConfigInit(nil, nil) + if err != nil { + return + } + } - return + return } // Start p2prc in a server mode func Start() (*gin.Engine, error) { - engine, err := server.Server() - if err != nil { - return nil, err - } - return engine, nil + engine, err := server.Server() + if err != nil { + return nil, err + } + return engine, nil } // MapPort Creates a reverse proxy connection and maps the appropriate port func MapPort(port string, domainName string, serverAddress string) (response *client.ResponseMAPPort, err error) { - response, err = client.MAPPort(port, domainName, serverAddress) - return + response, err = client.MAPPort(port, domainName, serverAddress) + return } // StartContainer Starts docker container on the remote machine func StartContainer(IP string) (container *docker.DockerVM, err error) { - container, err = client.StartContainer(IP, 0, false, "", "") - return + container, err = client.StartContainer(IP, 0, false, "", "") + return } // RemoveContainer Removes docker container based on the IP address and ID // provided func RemoveContainer(IP string, ID string) error { - return client.RemoveContianer(IP, ID) + return client.RemoveContianer(IP, ID) } // GetSpecs Get spec information about the remote server func GetSpecs(IP string) (specs *server.SysInfo, err error) { - specs, err = client.GetSpecs(IP) - return + specs, err = client.GetSpecs(IP) + return } // ViewIPTable View information of nodes in the network func ViewIPTable() (table *p2p.IpAddresses, err error) { - table, err = p2p.ReadIpTable() - return + table, err = p2p.ReadIpTable() + return } // UpdateIPTable Force updates IP tables based on new // new nodes discovered in the network func UpdateIPTable() (err error) { - return clientIPTable.UpdateIpTableListClient() + return clientIPTable.UpdateIpTableListClient() } // AddCustomInformation allows to pass custom information // through the network to which can be listened on // all peers in the network to execute a task. func AddCustomInformation(information string) error { - return clientIPTable.AddCustomInformationToIPTable(information) + return clientIPTable.AddCustomInformationToIPTable(information) +} + +// AddRootNode Adds root node to the network by using defaults except for +// ip address and port no. Supports only IPV4 as of now. +func AddRootNode(rootIP string, portNo string) error { + var rootNode []p2p.IpAddress + + rootNode = append(rootNode, p2p.IpAddress{ + Name: "", + Ipv4: rootIP, + ServerPort: portNo, + NAT: false, + }) + return generate.GenerateIPTableFile(rootNode) } diff --git a/cmd/action.go b/cmd/action.go index 19e8d96..f9d9578 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/Akilan1999/p2p-rendering-computation/abstractions" "github.com/Akilan1999/p2p-rendering-computation/client" "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" "github.com/Akilan1999/p2p-rendering-computation/config/generate" @@ -87,11 +88,7 @@ var CliAction = func(ctx *cli.Context) error { // Displays all images available on the server side if ViewImages != "" { imageRes, err := client.ViewContainers(ViewImages) - - if err != nil { - fmt.Print(err) - } - client.PrettyPrint(imageRes) + standardOutput(err, imageRes) } // Function called to stop and remove server from Docker @@ -100,9 +97,7 @@ var CliAction = func(ctx *cli.Context) error { fmt.Println("provide container ID via --ID or --id") } else { err := client.RemoveContianer(RemoveVM, ID) - if err != nil { - fmt.Print(err) - } + standardOutput(err, nil) } } @@ -119,58 +114,40 @@ var CliAction = func(ctx *cli.Context) error { // Calls function to do Api call to start the container on the server side imageRes, err := client.StartContainer(CreateVM, PortsInt, GPU, ContainerName, BaseImage) - if err != nil { - fmt.Print(err) - } - client.PrettyPrint(imageRes) + standardOutput(err, imageRes) } //Call if specs flag is called if Specs != "" { specs, err := client.GetSpecs(Specs) - if err != nil { - return err - } - - // Pretty print - client.PrettyPrint(specs) + standardOutput(err, specs) } //Sets default paths to the config file if SetDefaultConfig { _, err := generate.SetDefaults("P2PRC", false, nil, false) - if err != nil { - fmt.Print(err) - } + standardOutput(err, nil) } //If the network interface flag is called // Then all the network interfaces are displayed if NetworkInterface { err := p2p.ViewNetworkInterface() - if err != nil { - fmt.Print(err) - } + standardOutput(err, nil) } // If the view plugin flag is called then display all // plugins available if ViewPlugin { plugins, err := plugin.DetectPlugins() - if err != nil { - fmt.Print(err) - } - client.PrettyPrint(plugins) + standardOutput(err, plugins) } // If the flag Tracked Container is called or the flag // --tc if TrackedContainers { err, trackedContainers := client.ViewTrackedContainers() - if err != nil { - fmt.Print(err) - } - client.PrettyPrint(trackedContainers) + standardOutput(err, trackedContainers) } //Executing plugin when the plugin flag is called @@ -179,14 +156,11 @@ var CliAction = func(ctx *cli.Context) error { // To execute plugin requires the container ID or group ID provided when being executed if ID != "" { err := plugin.CheckRunPlugin(ExecutePlugin, ID) - if err != nil { - fmt.Println(err) - } else { - fmt.Println("Success") - } - } else { - fmt.Println("provide container ID via --ID or --id") + standardOutput(err, nil) } + //else { + // fmt.Println("provide container ID via --ID or --id") + //} } @@ -194,10 +168,7 @@ var CliAction = func(ctx *cli.Context) error { // Creates new group and outputs JSON file if CreateGroup { group, err := client.CreateGroup() - if err != nil { - return err - } - client.PrettyPrint(group) + standardOutput(err, group) } // Actions to be performed when the @@ -208,26 +179,14 @@ var CliAction = func(ctx *cli.Context) error { // --rmcgroup --id if RemoveContainerGroup && ID != "" { group, err := client.RemoveContainerGroup(ID, Group) - if err != nil { - fmt.Println(err) - } else { - client.PrettyPrint(group) - } + standardOutput(err, group) } else if ID != "" { // Add container to group based on group ID provided // --id group, err := client.AddContainerToGroup(ID, Group) - if err != nil { - fmt.Println(err) - } else { - client.PrettyPrint(group) - } + standardOutput(err, group) } else { // View all information about current group group, err := client.GetGroup(Group) - if err != nil { - fmt.Println(err) - } else { - client.PrettyPrint(group) - } + standardOutput(err, group) } } @@ -236,63 +195,52 @@ var CliAction = func(ctx *cli.Context) error { // --rmgroup if RemoveGroup != "" { err := client.RemoveGroup(RemoveGroup) - if err != nil { - fmt.Println(err) - } else { - fmt.Println("Group Removed") - } + standardOutput(err, nil) } // Execute Function to view all groups if Groups { groups, err := client.ReadGroup() - if err != nil { - fmt.Println(err) - } else { - client.PrettyPrint(groups) - } + standardOutput(err, groups) } //-------------------------------- if PullPlugin != "" { err := plugin.DownloadPlugin(PullPlugin) - if err != nil { - fmt.Println(err) - } else { - fmt.Println("Success") - } + standardOutput(err, nil) } if RemovePlugin != "" { err := plugin.DeletePlugin(RemovePlugin) - if err != nil { - fmt.Println(err) - } else { - fmt.Println("Success") - } + standardOutput(err, nil) } if AddMetaData != "" { err := clientIPTable.AddCustomInformationToIPTable(AddMetaData) - if err != nil { - fmt.Println(err) - } else { - fmt.Println("Success") - } + standardOutput(err, nil) } if MAPPort != "" { address, err := client.MAPPort(MAPPort, DomainName, RemoteAddress) - if err != nil { - return err - } - if err != nil { - fmt.Println(err) - } else { - client.PrettyPrint(address) - } + standardOutput(err, address) + } + + // Action called to add root node to network + if AddRootNode && IP != "" && Ports != "" { + err := abstractions.AddRootNode(IP, Ports) + standardOutput(err, nil) } return nil } + +func standardOutput(err error, i interface{}) { + if err != nil { + fmt.Println(err) + } else if i != nil { + fmt.Println("Success") + } else { + client.PrettyPrint(i) + } +} diff --git a/cmd/flags.go b/cmd/flags.go index 486f9fb..34423b4 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -41,6 +41,8 @@ var ( PullPlugin string RemovePlugin string AddMetaData string + AddRootNode bool + IP string ) var AppConfigFlags = []cli.Flag{ @@ -272,4 +274,18 @@ var AppConfigFlags = []cli.Flag{ EnvVars: []string{"ADDMETADATA"}, Destination: &AddMetaData, }, + &cli.BoolFlag{ + Name: "AddRootNode", + Aliases: []string{"arn"}, + Usage: "Adds initial root node to talk to", + EnvVars: []string{"ADDROOTNODE"}, + Destination: &AddRootNode, + }, + &cli.StringFlag{ + Name: "IPAddress", + Aliases: []string{"ip"}, + Usage: "IP address", + EnvVars: []string{"IP"}, + Destination: &IP, + }, } diff --git a/config/config.go b/config/config.go index 883638b..b05af12 100644 --- a/config/config.go +++ b/config/config.go @@ -29,8 +29,8 @@ type Config struct { ServerPort string ProxyPort string GroupTrackContainersPath string - FRPServerPort string - BehindNAT string + FRPServerPort bool + BehindNAT bool IPTableKey string PublicKeyFile string PrivateKeyFile string diff --git a/config/generate/generate.go b/config/generate/generate.go index b926457..7d8228c 100644 --- a/config/generate/generate.go +++ b/config/generate/generate.go @@ -78,9 +78,9 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No Defaults.GroupTrackContainersPath = defaultPath + "client/trackcontainers/grouptrackcontainers.json" Defaults.ServerPort = "8088" Defaults.ProxyPort = "" - Defaults.FRPServerPort = "True" + Defaults.FRPServerPort = true Defaults.CustomConfig = CustomConfig - Defaults.BehindNAT = "True" + Defaults.BehindNAT = true Defaults.DockerRunLogs = "/tmp/" // Generate a random key to be added to IPTable Defaults.IPTableKey = String(12) diff --git a/config/generate/generateFiles.go b/config/generate/generateFiles.go index c97c22b..9b2081f 100644 --- a/config/generate/generateFiles.go +++ b/config/generate/generateFiles.go @@ -45,9 +45,9 @@ func GenerateIPTableFile(rootNodes []p2p.IpAddress) (err error) { if len(rootNodes) <= 0 { rootnode.Name = "Node1" rootnode.ServerPort = "8078" - rootnode.NAT = "False" + rootnode.NAT = false rootnode.Ipv4 = "217.76.63.222" - rootnode.ProxyServer = "True" + rootnode.ProxyServer = true rootnodes.IpAddress = append(rootnodes.IpAddress, rootnode) } else { diff --git a/p2p/iptable.go b/p2p/iptable.go index 9cdf891..082ece2 100644 --- a/p2p/iptable.go +++ b/p2p/iptable.go @@ -29,9 +29,9 @@ type IpAddress struct { Upload float64 `json:"Upload"` ServerPort string `json:"ServerPort"` BareMetalSSHPort string `json:"BareMetalSSHPort"` - NAT string `json:"NAT"` + NAT bool `json:"NAT"` EscapeImplementation string `json:"EscapeImplementation"` - ProxyServer string `json:"ProxyServer"` + ProxyServer bool `json:"ProxyServer"` UnSafeMode bool `json:"UnSafeMode"` PublicKey string `json:"PublicKey"` CustomInformation string `json:"CustomInformation"` @@ -161,7 +161,7 @@ func (table *IpAddresses) RemoveDuplicates() error { } } - if table.IpAddress[i].NAT == "True" && table.IpAddress[i].EscapeImplementation == "None" { + if table.IpAddress[i].NAT && table.IpAddress[i].EscapeImplementation == "None" { Exists = true } diff --git a/p2p/speedtest.go b/p2p/speedtest.go index 0468b07..e6b80e9 100644 --- a/p2p/speedtest.go +++ b/p2p/speedtest.go @@ -95,7 +95,7 @@ func (ip *IpAddresses) SpeedTestUpdatedIPTable() error { } // Checks if both the IPV4 addresses are the same or the IPV6 address is not // an empty string and IPV6 address are the same - if (ip.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 && targets.IpAddress[i].NAT == "True") || (targets.IpAddress[i].Ipv6 != "" && ip.IpAddress[k].Ipv6 == targets.IpAddress[i].Ipv6) { + if (ip.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 && targets.IpAddress[i].NAT) || (targets.IpAddress[i].Ipv6 != "" && ip.IpAddress[k].Ipv6 == targets.IpAddress[i].Ipv6) { Exists = true break } diff --git a/server/server.go b/server/server.go index 8086d67..0e3ec1c 100644 --- a/server/server.go +++ b/server/server.go @@ -255,7 +255,7 @@ func Server() (*gin.Engine, error) { // TODO check if IPV6 or Proxy port is specified // if not update current entry as proxy address // with appropriate port on IP Table - if config.BehindNAT == "True" { + if config.BehindNAT { if err != nil { return nil, err } @@ -267,7 +267,7 @@ func Server() (*gin.Engine, error) { for i, _ := range table.IpAddress { // Checks if the ping is the lowest and if the following node is acting as a proxy //if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && table.IpAddress[i].ProxyPort != "" { - if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && table.IpAddress[i].NAT != "" { + if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && !table.IpAddress[i].NAT { lowestLatency = table.IpAddress[i].Latency.Milliseconds() lowestLatencyIpAddress = table.IpAddress[i] } @@ -291,8 +291,8 @@ func Server() (*gin.Engine, error) { ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 ProxyIpAddr.ServerPort = proxyPort ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" - ProxyIpAddr.ProxyServer = "False" + ProxyIpAddr.NAT = false + ProxyIpAddr.ProxyServer = false ProxyIpAddr.EscapeImplementation = "FRP" if config.BareMetal { @@ -314,9 +314,9 @@ func Server() (*gin.Engine, error) { } ProxyIpAddr.ServerPort = config.ServerPort ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" + ProxyIpAddr.NAT = false if config.ProxyPort != "" { - ProxyIpAddr.ProxyServer = "True" + ProxyIpAddr.ProxyServer = true } ProxyIpAddr.EscapeImplementation = "" if config.BareMetal { @@ -425,9 +425,9 @@ func MapPort(port string, domainName string) (string, string, error) { for i, _ := range table.IpAddress { // Checks if the ping is the lowest and if the following node is acting as a proxy //if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && table.IpAddress[i].ProxyPort != "" { - if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && table.IpAddress[i].NAT != "" { + if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && !table.IpAddress[i].NAT { // Filter based on nodes with proxy enabled - if domainName != "" && table.IpAddress[i].ProxyServer == "True" { + if domainName != "" && table.IpAddress[i].ProxyServer { lowestLatency = table.IpAddress[i].Latency.Milliseconds() lowestLatencyIpAddress = table.IpAddress[i] continue @@ -466,7 +466,7 @@ func MapPort(port string, domainName string) (string, string, error) { ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 ProxyIpAddr.ServerPort = proxyPort ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" + ProxyIpAddr.NAT = false ProxyIpAddr.EscapeImplementation = "FRP" //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey)