added feature to add root node

This commit is contained in:
2025-04-28 22:22:29 +01:00
parent b9f5dec9de
commit 51b253df15
9 changed files with 136 additions and 158 deletions

View File

@@ -2,94 +2,108 @@ package abstractions
import "C" import "C"
import ( import (
"github.com/Akilan1999/p2p-rendering-computation/client" "github.com/Akilan1999/p2p-rendering-computation/client"
"github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable"
Config "github.com/Akilan1999/p2p-rendering-computation/config" Config "github.com/Akilan1999/p2p-rendering-computation/config"
"github.com/Akilan1999/p2p-rendering-computation/config/generate" "github.com/Akilan1999/p2p-rendering-computation/config/generate"
"github.com/Akilan1999/p2p-rendering-computation/p2p" "github.com/Akilan1999/p2p-rendering-computation/p2p"
"github.com/Akilan1999/p2p-rendering-computation/server" "github.com/Akilan1999/p2p-rendering-computation/server"
"github.com/Akilan1999/p2p-rendering-computation/server/docker" "github.com/Akilan1999/p2p-rendering-computation/server/docker"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"os" "os"
) )
// Init Initialises p2prc // Init Initialises p2prc
func Init(customConfig interface{}) (config *Config.Config, err error) { func Init(customConfig interface{}) (config *Config.Config, err error) {
// Get config file path // Get config file path
// Checks P2PRC path initially // Checks P2PRC path initially
// - Get PATH if environment varaible // - Get PATH if environment varaible
path, err := Config.GetPathP2PRC("P2PRC") path, err := Config.GetPathP2PRC("P2PRC")
if err != nil { if err != nil {
return return
} }
// check if the config file exists // check if the config file exists
if _, err = os.Stat(path + "config.json"); err != nil { if _, err = os.Stat(path + "config.json"); err != nil {
// Initialize with base p2prc config files // Initialize with base p2prc config files
// set the config file with default paths // set the config file with default paths
config, err = generate.SetDefaults("P2PRC", false, customConfig, false) config, err = generate.SetDefaults("P2PRC", false, customConfig, false)
if err != nil { if err != nil {
return return
} }
} else { } else {
// If the configs are available then use them over generating new ones. // If the configs are available then use them over generating new ones.
config, err = Config.ConfigInit(nil, nil) config, err = Config.ConfigInit(nil, nil)
if err != nil { if err != nil {
return return
} }
} }
return return
} }
// Start p2prc in a server mode // Start p2prc in a server mode
func Start() (*gin.Engine, error) { func Start() (*gin.Engine, error) {
engine, err := server.Server() engine, err := server.Server()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return engine, nil return engine, nil
} }
// MapPort Creates a reverse proxy connection and maps the appropriate port // MapPort Creates a reverse proxy connection and maps the appropriate port
func MapPort(port string, domainName string, serverAddress string) (response *client.ResponseMAPPort, err error) { func MapPort(port string, domainName string, serverAddress string) (response *client.ResponseMAPPort, err error) {
response, err = client.MAPPort(port, domainName, serverAddress) response, err = client.MAPPort(port, domainName, serverAddress)
return return
} }
// StartContainer Starts docker container on the remote machine // StartContainer Starts docker container on the remote machine
func StartContainer(IP string) (container *docker.DockerVM, err error) { func StartContainer(IP string) (container *docker.DockerVM, err error) {
container, err = client.StartContainer(IP, 0, false, "", "") container, err = client.StartContainer(IP, 0, false, "", "")
return return
} }
// RemoveContainer Removes docker container based on the IP address and ID // RemoveContainer Removes docker container based on the IP address and ID
// provided // provided
func RemoveContainer(IP string, ID string) error { 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 // GetSpecs Get spec information about the remote server
func GetSpecs(IP string) (specs *server.SysInfo, err error) { func GetSpecs(IP string) (specs *server.SysInfo, err error) {
specs, err = client.GetSpecs(IP) specs, err = client.GetSpecs(IP)
return return
} }
// ViewIPTable View information of nodes in the network // ViewIPTable View information of nodes in the network
func ViewIPTable() (table *p2p.IpAddresses, err error) { func ViewIPTable() (table *p2p.IpAddresses, err error) {
table, err = p2p.ReadIpTable() table, err = p2p.ReadIpTable()
return return
} }
// UpdateIPTable Force updates IP tables based on new // UpdateIPTable Force updates IP tables based on new
// new nodes discovered in the network // new nodes discovered in the network
func UpdateIPTable() (err error) { func UpdateIPTable() (err error) {
return clientIPTable.UpdateIpTableListClient() return clientIPTable.UpdateIpTableListClient()
} }
// AddCustomInformation allows to pass custom information // AddCustomInformation allows to pass custom information
// through the network to which can be listened on // through the network to which can be listened on
// all peers in the network to execute a task. // all peers in the network to execute a task.
func AddCustomInformation(information string) error { 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)
} }

View File

@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/Akilan1999/p2p-rendering-computation/abstractions"
"github.com/Akilan1999/p2p-rendering-computation/client" "github.com/Akilan1999/p2p-rendering-computation/client"
"github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable"
"github.com/Akilan1999/p2p-rendering-computation/config/generate" "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 // Displays all images available on the server side
if ViewImages != "" { if ViewImages != "" {
imageRes, err := client.ViewContainers(ViewImages) imageRes, err := client.ViewContainers(ViewImages)
standardOutput(err, imageRes)
if err != nil {
fmt.Print(err)
}
client.PrettyPrint(imageRes)
} }
// Function called to stop and remove server from Docker // 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") fmt.Println("provide container ID via --ID or --id")
} else { } else {
err := client.RemoveContianer(RemoveVM, ID) err := client.RemoveContianer(RemoveVM, ID)
if err != nil { standardOutput(err, nil)
fmt.Print(err)
}
} }
} }
@@ -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 // Calls function to do Api call to start the container on the server side
imageRes, err := client.StartContainer(CreateVM, PortsInt, GPU, ContainerName, BaseImage) imageRes, err := client.StartContainer(CreateVM, PortsInt, GPU, ContainerName, BaseImage)
if err != nil { standardOutput(err, imageRes)
fmt.Print(err)
}
client.PrettyPrint(imageRes)
} }
//Call if specs flag is called //Call if specs flag is called
if Specs != "" { if Specs != "" {
specs, err := client.GetSpecs(Specs) specs, err := client.GetSpecs(Specs)
if err != nil { standardOutput(err, specs)
return err
}
// Pretty print
client.PrettyPrint(specs)
} }
//Sets default paths to the config file //Sets default paths to the config file
if SetDefaultConfig { if SetDefaultConfig {
_, err := generate.SetDefaults("P2PRC", false, nil, false) _, err := generate.SetDefaults("P2PRC", false, nil, false)
if err != nil { standardOutput(err, nil)
fmt.Print(err)
}
} }
//If the network interface flag is called //If the network interface flag is called
// Then all the network interfaces are displayed // Then all the network interfaces are displayed
if NetworkInterface { if NetworkInterface {
err := p2p.ViewNetworkInterface() err := p2p.ViewNetworkInterface()
if err != nil { standardOutput(err, nil)
fmt.Print(err)
}
} }
// If the view plugin flag is called then display all // If the view plugin flag is called then display all
// plugins available // plugins available
if ViewPlugin { if ViewPlugin {
plugins, err := plugin.DetectPlugins() plugins, err := plugin.DetectPlugins()
if err != nil { standardOutput(err, plugins)
fmt.Print(err)
}
client.PrettyPrint(plugins)
} }
// If the flag Tracked Container is called or the flag // If the flag Tracked Container is called or the flag
// --tc // --tc
if TrackedContainers { if TrackedContainers {
err, trackedContainers := client.ViewTrackedContainers() err, trackedContainers := client.ViewTrackedContainers()
if err != nil { standardOutput(err, trackedContainers)
fmt.Print(err)
}
client.PrettyPrint(trackedContainers)
} }
//Executing plugin when the plugin flag is called //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 // To execute plugin requires the container ID or group ID provided when being executed
if ID != "" { if ID != "" {
err := plugin.CheckRunPlugin(ExecutePlugin, ID) err := plugin.CheckRunPlugin(ExecutePlugin, ID)
if err != nil { standardOutput(err, nil)
fmt.Println(err)
} else {
fmt.Println("Success")
}
} else {
fmt.Println("provide container ID via --ID or --id")
} }
//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 // Creates new group and outputs JSON file
if CreateGroup { if CreateGroup {
group, err := client.CreateGroup() group, err := client.CreateGroup()
if err != nil { standardOutput(err, group)
return err
}
client.PrettyPrint(group)
} }
// Actions to be performed when the // Actions to be performed when the
@@ -208,26 +179,14 @@ var CliAction = func(ctx *cli.Context) error {
// --rmcgroup --id <contianer id> // --rmcgroup --id <contianer id>
if RemoveContainerGroup && ID != "" { if RemoveContainerGroup && ID != "" {
group, err := client.RemoveContainerGroup(ID, Group) group, err := client.RemoveContainerGroup(ID, Group)
if err != nil { standardOutput(err, group)
fmt.Println(err)
} else {
client.PrettyPrint(group)
}
} else if ID != "" { // Add container to group based on group ID provided } else if ID != "" { // Add container to group based on group ID provided
// --id <Container ID> // --id <Container ID>
group, err := client.AddContainerToGroup(ID, Group) group, err := client.AddContainerToGroup(ID, Group)
if err != nil { standardOutput(err, group)
fmt.Println(err)
} else {
client.PrettyPrint(group)
}
} else { // View all information about current group } else { // View all information about current group
group, err := client.GetGroup(Group) group, err := client.GetGroup(Group)
if err != nil { standardOutput(err, group)
fmt.Println(err)
} else {
client.PrettyPrint(group)
}
} }
} }
@@ -236,63 +195,52 @@ var CliAction = func(ctx *cli.Context) error {
// --rmgroup // --rmgroup
if RemoveGroup != "" { if RemoveGroup != "" {
err := client.RemoveGroup(RemoveGroup) err := client.RemoveGroup(RemoveGroup)
if err != nil { standardOutput(err, nil)
fmt.Println(err)
} else {
fmt.Println("Group Removed")
}
} }
// Execute Function to view all groups // Execute Function to view all groups
if Groups { if Groups {
groups, err := client.ReadGroup() groups, err := client.ReadGroup()
if err != nil { standardOutput(err, groups)
fmt.Println(err)
} else {
client.PrettyPrint(groups)
}
} }
//-------------------------------- //--------------------------------
if PullPlugin != "" { if PullPlugin != "" {
err := plugin.DownloadPlugin(PullPlugin) err := plugin.DownloadPlugin(PullPlugin)
if err != nil { standardOutput(err, nil)
fmt.Println(err)
} else {
fmt.Println("Success")
}
} }
if RemovePlugin != "" { if RemovePlugin != "" {
err := plugin.DeletePlugin(RemovePlugin) err := plugin.DeletePlugin(RemovePlugin)
if err != nil { standardOutput(err, nil)
fmt.Println(err)
} else {
fmt.Println("Success")
}
} }
if AddMetaData != "" { if AddMetaData != "" {
err := clientIPTable.AddCustomInformationToIPTable(AddMetaData) err := clientIPTable.AddCustomInformationToIPTable(AddMetaData)
if err != nil { standardOutput(err, nil)
fmt.Println(err)
} else {
fmt.Println("Success")
}
} }
if MAPPort != "" { if MAPPort != "" {
address, err := client.MAPPort(MAPPort, DomainName, RemoteAddress) address, err := client.MAPPort(MAPPort, DomainName, RemoteAddress)
if err != nil { standardOutput(err, address)
return err }
}
if err != nil { // Action called to add root node to network
fmt.Println(err) if AddRootNode && IP != "" && Ports != "" {
} else { err := abstractions.AddRootNode(IP, Ports)
client.PrettyPrint(address) standardOutput(err, nil)
}
} }
return 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)
}
}

View File

@@ -41,6 +41,8 @@ var (
PullPlugin string PullPlugin string
RemovePlugin string RemovePlugin string
AddMetaData string AddMetaData string
AddRootNode bool
IP string
) )
var AppConfigFlags = []cli.Flag{ var AppConfigFlags = []cli.Flag{
@@ -272,4 +274,18 @@ var AppConfigFlags = []cli.Flag{
EnvVars: []string{"ADDMETADATA"}, EnvVars: []string{"ADDMETADATA"},
Destination: &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,
},
} }

View File

@@ -29,8 +29,8 @@ type Config struct {
ServerPort string ServerPort string
ProxyPort string ProxyPort string
GroupTrackContainersPath string GroupTrackContainersPath string
FRPServerPort string FRPServerPort bool
BehindNAT string BehindNAT bool
IPTableKey string IPTableKey string
PublicKeyFile string PublicKeyFile string
PrivateKeyFile string PrivateKeyFile string

View File

@@ -78,9 +78,9 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No
Defaults.GroupTrackContainersPath = defaultPath + "client/trackcontainers/grouptrackcontainers.json" Defaults.GroupTrackContainersPath = defaultPath + "client/trackcontainers/grouptrackcontainers.json"
Defaults.ServerPort = "8088" Defaults.ServerPort = "8088"
Defaults.ProxyPort = "" Defaults.ProxyPort = ""
Defaults.FRPServerPort = "True" Defaults.FRPServerPort = true
Defaults.CustomConfig = CustomConfig Defaults.CustomConfig = CustomConfig
Defaults.BehindNAT = "True" Defaults.BehindNAT = true
Defaults.DockerRunLogs = "/tmp/" Defaults.DockerRunLogs = "/tmp/"
// Generate a random key to be added to IPTable // Generate a random key to be added to IPTable
Defaults.IPTableKey = String(12) Defaults.IPTableKey = String(12)

View File

@@ -45,9 +45,9 @@ func GenerateIPTableFile(rootNodes []p2p.IpAddress) (err error) {
if len(rootNodes) <= 0 { if len(rootNodes) <= 0 {
rootnode.Name = "Node1" rootnode.Name = "Node1"
rootnode.ServerPort = "8078" rootnode.ServerPort = "8078"
rootnode.NAT = "False" rootnode.NAT = false
rootnode.Ipv4 = "217.76.63.222" rootnode.Ipv4 = "217.76.63.222"
rootnode.ProxyServer = "True" rootnode.ProxyServer = true
rootnodes.IpAddress = append(rootnodes.IpAddress, rootnode) rootnodes.IpAddress = append(rootnodes.IpAddress, rootnode)
} else { } else {

View File

@@ -29,9 +29,9 @@ type IpAddress struct {
Upload float64 `json:"Upload"` Upload float64 `json:"Upload"`
ServerPort string `json:"ServerPort"` ServerPort string `json:"ServerPort"`
BareMetalSSHPort string `json:"BareMetalSSHPort"` BareMetalSSHPort string `json:"BareMetalSSHPort"`
NAT string `json:"NAT"` NAT bool `json:"NAT"`
EscapeImplementation string `json:"EscapeImplementation"` EscapeImplementation string `json:"EscapeImplementation"`
ProxyServer string `json:"ProxyServer"` ProxyServer bool `json:"ProxyServer"`
UnSafeMode bool `json:"UnSafeMode"` UnSafeMode bool `json:"UnSafeMode"`
PublicKey string `json:"PublicKey"` PublicKey string `json:"PublicKey"`
CustomInformation string `json:"CustomInformation"` 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 Exists = true
} }

View File

@@ -95,7 +95,7 @@ func (ip *IpAddresses) SpeedTestUpdatedIPTable() error {
} }
// Checks if both the IPV4 addresses are the same or the IPV6 address is not // Checks if both the IPV4 addresses are the same or the IPV6 address is not
// an empty string and IPV6 address are the same // 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 Exists = true
break break
} }

View File

@@ -255,7 +255,7 @@ func Server() (*gin.Engine, error) {
// TODO check if IPV6 or Proxy port is specified // TODO check if IPV6 or Proxy port is specified
// if not update current entry as proxy address // if not update current entry as proxy address
// with appropriate port on IP Table // with appropriate port on IP Table
if config.BehindNAT == "True" { if config.BehindNAT {
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -267,7 +267,7 @@ func Server() (*gin.Engine, error) {
for i, _ := range table.IpAddress { for i, _ := range table.IpAddress {
// Checks if the ping is the lowest and if the following node is acting as a proxy // 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].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() lowestLatency = table.IpAddress[i].Latency.Milliseconds()
lowestLatencyIpAddress = table.IpAddress[i] lowestLatencyIpAddress = table.IpAddress[i]
} }
@@ -291,8 +291,8 @@ func Server() (*gin.Engine, error) {
ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4
ProxyIpAddr.ServerPort = proxyPort ProxyIpAddr.ServerPort = proxyPort
ProxyIpAddr.Name = config.MachineName ProxyIpAddr.Name = config.MachineName
ProxyIpAddr.NAT = "False" ProxyIpAddr.NAT = false
ProxyIpAddr.ProxyServer = "False" ProxyIpAddr.ProxyServer = false
ProxyIpAddr.EscapeImplementation = "FRP" ProxyIpAddr.EscapeImplementation = "FRP"
if config.BareMetal { if config.BareMetal {
@@ -314,9 +314,9 @@ func Server() (*gin.Engine, error) {
} }
ProxyIpAddr.ServerPort = config.ServerPort ProxyIpAddr.ServerPort = config.ServerPort
ProxyIpAddr.Name = config.MachineName ProxyIpAddr.Name = config.MachineName
ProxyIpAddr.NAT = "False" ProxyIpAddr.NAT = false
if config.ProxyPort != "" { if config.ProxyPort != "" {
ProxyIpAddr.ProxyServer = "True" ProxyIpAddr.ProxyServer = true
} }
ProxyIpAddr.EscapeImplementation = "" ProxyIpAddr.EscapeImplementation = ""
if config.BareMetal { if config.BareMetal {
@@ -425,9 +425,9 @@ func MapPort(port string, domainName string) (string, string, error) {
for i, _ := range table.IpAddress { for i, _ := range table.IpAddress {
// Checks if the ping is the lowest and if the following node is acting as a proxy // 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].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 // 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() lowestLatency = table.IpAddress[i].Latency.Milliseconds()
lowestLatencyIpAddress = table.IpAddress[i] lowestLatencyIpAddress = table.IpAddress[i]
continue continue
@@ -466,7 +466,7 @@ func MapPort(port string, domainName string) (string, string, error) {
ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4
ProxyIpAddr.ServerPort = proxyPort ProxyIpAddr.ServerPort = proxyPort
ProxyIpAddr.Name = config.MachineName ProxyIpAddr.Name = config.MachineName
ProxyIpAddr.NAT = "False" ProxyIpAddr.NAT = false
ProxyIpAddr.EscapeImplementation = "FRP" ProxyIpAddr.EscapeImplementation = "FRP"
//ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey)