From 0b0ae2df06d2ef5df2797036258e583a4ed8d97c Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 15:52:24 +0000 Subject: [PATCH] added optmized approach to update IPTable --- .../AddCustomInformationToIPTable.go | 2 +- client/clientIPTable/Iptable.go | 9 +- config/config.go | 3 +- config/generate/generate.go | 3 +- p2p/iptable.go | 3 + server/server.go | 116 ++++++++++-------- 6 files changed, 78 insertions(+), 58 deletions(-) diff --git a/client/clientIPTable/AddCustomInformationToIPTable.go b/client/clientIPTable/AddCustomInformationToIPTable.go index 56763b3..a6b8c4e 100644 --- a/client/clientIPTable/AddCustomInformationToIPTable.go +++ b/client/clientIPTable/AddCustomInformationToIPTable.go @@ -31,7 +31,7 @@ func AddCustomInformationToIPTable(text string) error { if found { table.WriteIpTable() // update IPTable after modified entry - UpdateIpTableListClient() + go UpdateIpTableListClient() } else { return errors.New("start server with p2prc -s as the server is currently not running") } diff --git a/client/clientIPTable/Iptable.go b/client/clientIPTable/Iptable.go index d6da7e0..204bbf0 100644 --- a/client/clientIPTable/Iptable.go +++ b/client/clientIPTable/Iptable.go @@ -64,10 +64,11 @@ func UpdateIpTable(IpAddress string, serverPort string, wg *sync.WaitGroup) erro } } - err = ipStruct.WriteIpTable() - if err != nil { - return err - } + // Not required to update IP table as speed test updates the IP Table + //err = ipStruct.WriteIpTable() + //if err != nil { + // return err + //} wg.Done() diff --git a/config/config.go b/config/config.go index 163a597..883638b 100644 --- a/config/config.go +++ b/config/config.go @@ -36,7 +36,8 @@ type Config struct { PrivateKeyFile string PemFile string KeyFile string - BareMetal string + BareMetal bool + UnsafeMode bool CustomConfig interface{} //NetworkInterface string //NetworkInterfaceIPV6Index int diff --git a/config/generate/generate.go b/config/generate/generate.go index cdccc37..89c1885 100644 --- a/config/generate/generate.go +++ b/config/generate/generate.go @@ -93,7 +93,8 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No // Generate Public and private keys and set path Defaults.PublicKeyFile = defaultPath + "p2prc.PublicKeyBareMetal" Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey" - Defaults.BareMetal = "False" + Defaults.BareMetal = false + Defaults.UnsafeMode = false // Generate certificate files for SSL err = GenerateCertificate() diff --git a/p2p/iptable.go b/p2p/iptable.go index 372d88d..9cdf891 100644 --- a/p2p/iptable.go +++ b/p2p/iptable.go @@ -21,6 +21,7 @@ type IpAddresses struct { type IpAddress struct { Name string `json:"Name"` + MachineUsername string `json:"MachineUsername"` Ipv4 string `json:"IPV4"` Ipv6 string `json:"IPV6"` Latency time.Duration `json:"Latency"` @@ -31,6 +32,8 @@ type IpAddress struct { NAT string `json:"NAT"` EscapeImplementation string `json:"EscapeImplementation"` ProxyServer string `json:"ProxyServer"` + UnSafeMode bool `json:"UnSafeMode"` + PublicKey string `json:"PublicKey"` CustomInformation string `json:"CustomInformation"` //CustomInformationKey []byte `json:"CustomInformationKey"` } diff --git a/server/server.go b/server/server.go index 8140e48..6e3f50e 100644 --- a/server/server.go +++ b/server/server.go @@ -2,7 +2,6 @@ package server import ( b64 "encoding/base64" - "encoding/json" "errors" "fmt" "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" @@ -11,8 +10,8 @@ import ( "github.com/Akilan1999/p2p-rendering-computation/p2p/frp" "github.com/Akilan1999/p2p-rendering-computation/server/docker" "github.com/gin-gonic/gin" - "io/ioutil" "net/http" + "os/user" "strconv" "time" ) @@ -69,45 +68,45 @@ func Server() (*gin.Engine, error) { //Gets Ip Table from server node r.POST("/IpTable", func(c *gin.Context) { // Getting IPV4 address of client - var ClientHost p2p.IpAddress - - if p2p.Ip4or6(c.ClientIP()) == "version 6" { - ClientHost.Ipv6 = c.ClientIP() - } else { - ClientHost.Ipv4 = c.ClientIP() - } - - // Variable to store IP table information - var IPTable p2p.IpAddresses - - // Receive file from POST request - body, err := c.FormFile("json") - if err != nil { - c.String(http.StatusOK, fmt.Sprint(err)) - } - - // Open file - open, err := body.Open() - if err != nil { - c.String(http.StatusOK, fmt.Sprint(err)) - } - - // Open received file - file, err := ioutil.ReadAll(open) - if err != nil { - c.String(http.StatusOK, fmt.Sprint(err)) - } - - json.Unmarshal(file, &IPTable) - - //Add Client IP address to IPTable struct - IPTable.IpAddress = append(IPTable.IpAddress, ClientHost) - - // Runs speed test to return only servers in the IP table pingable - err = IPTable.SpeedTestUpdatedIPTable() - if err != nil { - c.String(http.StatusOK, fmt.Sprint(err)) - } + //var ClientHost p2p.IpAddress + // + //if p2p.Ip4or6(c.ClientIP()) == "version 6" { + // ClientHost.Ipv6 = c.ClientIP() + //} else { + // ClientHost.Ipv4 = c.ClientIP() + //} + // + //// Variable to store IP table information + //var IPTable p2p.IpAddresses + // + //// Receive file from POST request + //body, err := c.FormFile("json") + //if err != nil { + // c.String(http.StatusOK, fmt.Sprint(err)) + //} + // + //// Open file + //open, err := body.Open() + //if err != nil { + // c.String(http.StatusOK, fmt.Sprint(err)) + //} + // + //// Open received file + //file, err := ioutil.ReadAll(open) + //if err != nil { + // c.String(http.StatusOK, fmt.Sprint(err)) + //} + // + //json.Unmarshal(file, &IPTable) + // + ////Add Client IP address to IPTable struct + //IPTable.IpAddress = append(IPTable.IpAddress, ClientHost) + // + //// Runs speed test to return only servers in the IP table pingable + //err = IPTable.SpeedTestUpdatedIPTable() + //if err != nil { + // c.String(http.StatusOK, fmt.Sprint(err)) + //} // Reads IP addresses from ip table IpAddresses, err := p2p.ReadIpTable() @@ -294,7 +293,7 @@ func Server() (*gin.Engine, error) { ProxyIpAddr.ProxyServer = "False" ProxyIpAddr.EscapeImplementation = "FRP" - if config.BareMetal == "True" { + if config.BareMetal { _, SSHPort, err := MapPort("22", "") if err != nil { return nil, err @@ -303,9 +302,6 @@ func Server() (*gin.Engine, error) { } //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) - - // append the following to the ip table - table.IpAddress = append(table.IpAddress, ProxyIpAddr) // write information back to the IP Table } @@ -321,14 +317,34 @@ func Server() (*gin.Engine, error) { ProxyIpAddr.ProxyServer = "True" } ProxyIpAddr.EscapeImplementation = "" - if config.BareMetal == "True" { + if config.BareMetal { ProxyIpAddr.BareMetalSSHPort = "22" } - table.IpAddress = append(table.IpAddress, ProxyIpAddr) - } + // Get machine username + currentUser, err := user.Current() + if err != nil { + return nil, err + } + // Add username p2prc binary is being run under + ProxyIpAddr.MachineUsername = currentUser.Username + ProxyIpAddr.UnSafeMode = config.UnsafeMode + // Adds the public key information to the IPTable + // improving transmission of IPTables without the + // entire public key is future work to be improved + // in the P2PRC protocol level (Improving by adding + // UDP with TCP reliability layer). + ProxyIpAddr.PublicKey, err = config.GetPublicKey() + + if err != nil { + return nil, err + } + + // append the following to the ip table + table.IpAddress = append(table.IpAddress, ProxyIpAddr) + // Writing results to the IPTable err = table.WriteIpTable() if err != nil { @@ -406,11 +422,8 @@ func MapPort(port string, domainName string) (string, string, error) { return "", "", err } - fmt.Println(domainName) - // Doing the proxy mapping for the domain name if domainName != "" { - fmt.Println("called --------------------------------") fmt.Println("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort + "/AddProxy?port=" + proxyPort + "&domain_name=" + domainName + "&ip_address=" + lowestLatencyIpAddress.Ipv4) URL := "http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort + "/AddProxy?port=" + proxyPort + "&domain_name=" + domainName + "&ip_address=" + lowestLatencyIpAddress.Ipv4 //} else { @@ -426,6 +439,7 @@ func MapPort(port string, domainName string) (string, string, error) { ProxyIpAddr.Name = config.MachineName ProxyIpAddr.NAT = "False" ProxyIpAddr.EscapeImplementation = "FRP" + //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) } else { return "", "", errors.New("proxy IP not found")