From 0b0ae2df06d2ef5df2797036258e583a4ed8d97c Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 15:52:24 +0000 Subject: [PATCH 01/11] 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") From b55134d194fd4078dcb8d624b3b5f29ab83baa8d Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 16:00:12 +0000 Subject: [PATCH 02/11] fixed untracked files --- cert.pem | 18 --- key.pem | 28 ----- p2p/testingMetrics.go | 179 ----------------------------- plugin/TestAnsible/description.txt | 1 - plugin/TestAnsible/hosts | 12 -- plugin/TestAnsible/site.yml | 8 -- 6 files changed, 246 deletions(-) delete mode 100644 cert.pem delete mode 100644 key.pem delete mode 100644 p2p/testingMetrics.go delete mode 100644 plugin/TestAnsible/description.txt delete mode 100644 plugin/TestAnsible/hosts delete mode 100644 plugin/TestAnsible/site.yml diff --git a/cert.pem b/cert.pem deleted file mode 100644 index f9a2028..0000000 --- a/cert.pem +++ /dev/null @@ -1,18 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIC9DCCAdygAwIBAgIQXOymZW4XLjqscy843BeeBzANBgkqhkiG9w0BAQsFADAS -MRAwDgYDVQQKEwdBY21lIENvMB4XDTI0MTAyMDIxMjc1NloXDTI1MTAyMDIxMjc1 -NlowEjEQMA4GA1UEChMHQWNtZSBDbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC -AQoCggEBAMFErOjO5LYkiMev4Xma6DX7rmjcyQRau84IyOflajE7LFyUnEDxZJkE -JU57mm06uOLr6XUytkFOFrgeCsWAbraiGhKDH5srBvvNAgah/8RgkIFXASDslEy5 -bI9RZZLe8jJP0cVGvgnZi6aZsDYZm9wlYSgY+9f9QddCb1NQ+/0mu9cluq6v/n3G -q5PhXHt9CnHoYs8hJ+fbJATAogugvfh3zYbkrBUPY73hQ2/dwHy7mdfAieo6i3GR -NP2/YQTHBwBqm6teOlBZ8VfQOyzU8czz1mhbD8GVh5CF/ZWE6tHxxjWG+B53VW/e -yH0LlQQFttNn2bB58W9Bgfov5CBj/30CAwEAAaNGMEQwDgYDVR0PAQH/BAQDAgWg -MBMGA1UdJQQMMAoGCCsGAQUFBwMBMAwGA1UdEwEB/wQCMAAwDwYDVR0RBAgwBocE -TWH5GDANBgkqhkiG9w0BAQsFAAOCAQEAEs4zLQt9n1BLibCmoKwzYDSUK/KQQdcA -8LREoTH6bD+TryzWSQ1zF+WZHbru9pGNO9gkEbmcYwziTVoPmFwblvoc/YoXFp2B -fQiIAZ5+TMtEfdj6agW8+fz00eMKvo1HkbO3RhDj+KYxlED7DL4Tn/ESx7wKtPsX -lSUBK4Chqmj3hB8OCIeDUoMwnCaA1plLfBgcFixtWyYEGv8gXgwrpVqXLfdnGCOI -biFWeLMSXiqZOykJl6fJbOGjkmTsLSDHcyhV2x1+FHviDJYF4WckrkhSmBw+Pspz -OT41o6uP/MFimbFAhCfcPRwHj1vMzfunKvb7UGPJPCWE5oWA7sWsQw== ------END CERTIFICATE----- diff --git a/key.pem b/key.pem deleted file mode 100644 index 170c493..0000000 --- a/key.pem +++ /dev/null @@ -1,28 +0,0 @@ ------BEGIN PRIVATE KEY----- -MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDBRKzozuS2JIjH -r+F5mug1+65o3MkEWrvOCMjn5WoxOyxclJxA8WSZBCVOe5ptOrji6+l1MrZBTha4 -HgrFgG62ohoSgx+bKwb7zQIGof/EYJCBVwEg7JRMuWyPUWWS3vIyT9HFRr4J2Yum -mbA2GZvcJWEoGPvX/UHXQm9TUPv9JrvXJbqur/59xquT4Vx7fQpx6GLPISfn2yQE -wKILoL34d82G5KwVD2O94UNv3cB8u5nXwInqOotxkTT9v2EExwcAapurXjpQWfFX -0Dss1PHM89ZoWw/BlYeQhf2VhOrR8cY1hvged1Vv3sh9C5UEBbbTZ9mwefFvQYH6 -L+QgY/99AgMBAAECggEAag7BxFAk+j86QDqoRQ9Y9qbSowDl3tnbzcjMV1Vx3OpY -AcYbqRfO2JmJz3PM3rwm0YAccRAPK+8qUIdL6e3804RJQndqVMBb6LorrnAf5ygG -pLOWDvdEuoXWqR9F2j82MZ3Tw6p6hvU2yYn1RPrwqPJqSpLLQU/dXb87UEFUxyoQ -7bNBNzsulZNd70JJtvmieXU1tf88EdiIxv/Ua9ZRepkkLDJlt3BwsGK+JdVhOhN2 -zUjhlQ4HrErdY6C0rEX7gKznSaThaZb6RYZOLqBfe5nG/uQjw+OfltLEz4nR5xn2 -2F07T0a5hP4WvCgNQRBzsXGYNumJEuSkcadEw8hEkQKBgQD2RAnUlnQ99xqvz3QK -STptQcJOF07DQmEs6A8E7BHue/Z4+gYNtbyQ7NefDYBuGWOzUOXOJs6k//CCgcDe -ZzmtRPs3Sro8AfK0FS09r/OpXL+LPJjhSJ+YEjoVzERSDCvVNshuIO92zlDPITg8 -J9ZsPzXrm+Yh1f5gvSgvmgJodwKBgQDI6FsH7mdjW3bp1tGnF/SzqOBMutyZp7/9 -b7dtJhZNwdi4765JVtLAXDei5kht7NzVlH7GJGcrIen5emKBRS6JjDlhFcFw669V -biOiBrL/3Gu9irjLh2abSBE5r7GClelDLgmJ/JrpkRJRcBtfoPLsYIHtViNXz2CE -w2yf3D+IqwKBgDV7l0r1Fe+zL7eI7WGif67kvicphrB/RQ10pHZkMozBnUiuvAvU -MUykMzvWudKvbuitpAr+gJhzAS/GsDVc47jOp2RdtU58+dHEW5vvD5LzkqHRmj4r -AIYNR1Iel6PeAtL+X88TFg6ORhyF0Wm3GSlPHQ27QOKOT/2Ady7HWsiHAoGAAZ2U -tdCd9jeTEjhPAUf5CMTP4O4TvgySR6hxUihfP4rSglIzFZXkJfXX4LB3XqLr96+1 -AUTjq34fUolAZ4e14xdEktpI7u6GP2qeULw7DkGogCKwb7qMAa2nrc4S4FjyQj8J -hpmIaV5oRk1fevphWbNA+DacHCWnwgmMQxDTVfcCgYAxbQQFyRJ1B+WJtvE66cqA -T/DvgzevwqE4na+NNc6hApZt+kNoLODLUi6ikYimNzdtGnr4AIitvGR3J9qzFC+I -+d2F1ioSV4fpVD7idYDVkncME+pXzzGweg91k8ucHqcdU1k70lYTokGgm3DyClcP -76O6IS1URnFFXco9DGuDcA== ------END PRIVATE KEY----- diff --git a/p2p/testingMetrics.go b/p2p/testingMetrics.go deleted file mode 100644 index 7ab6d7e..0000000 --- a/p2p/testingMetrics.go +++ /dev/null @@ -1,179 +0,0 @@ -package p2p - -import ( - "bytes" - "errors" - "github.com/Akilan1999/p2p-rendering-computation/config" - "io" - "io/ioutil" - "log" - "mime/multipart" - "net/http" - "os" - "time" -) - -// var dlSizes = [...]int{350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000} -// var ulSizes = [...]int{100, 300, 500, 800, 1000, 1500, 2500, 3000, 3500, 4000} //kB -var httpclient = http.Client{} - -// DownloadTest executes the test to measure download speed -//func (s *IpAddress) DownloadTest(savingMode bool) error { -// dlURL := "http://" + s.Ipv4 + ":8088/server_info" -// eg := errgroup.Group{} -// -// // Warming up -// sTime := time.Now() -// for i := 0; i < 2; i++ { -// eg.Go(func() error { -// return dlWarmUp("http://" + s.Ipv4 + ":8088/server_info") -// }) -// } -// if err := eg.Wait(); err != nil { -// return err -// } -// fTime := time.Now() -// // 1.125MB for each request (750 * 750 * 2) -// wuSpeed := 1.125 * 8 * 2 / fTime.Sub(sTime.Add(s.Latency)).Seconds() -// -// // Decide workload by warm up speed -// workload := 0 -// weight := 0 -// skip := false -// if savingMode { -// workload = 6 -// weight = 3 -// } else if 10.0 < wuSpeed { -// workload = 16 -// weight = 4 -// } else if 4.0 < wuSpeed { -// workload = 8 -// weight = 4 -// } else if 2.5 < wuSpeed { -// workload = 4 -// weight = 4 -// } else { -// skip = true -// } -// -// // Main speedtest -// dlSpeed := wuSpeed -// if skip == false { -// sTime = time.Now() -// for i := 0; i < workload; i++ { -// eg.Go(func() error { -// return downloadRequest(dlURL, weight) -// }) -// } -// if err := eg.Wait(); err != nil { -// return err -// } -// fTime = time.Now() -// -// reqMB := dlSizes[weight] * dlSizes[weight] * 2 / 1000 / 1000 -// dlSpeed = float64(reqMB) * 8 * float64(workload) / fTime.Sub(sTime).Seconds() -// } -// -// s.Download = dlSpeed -// return nil -//} - -// Download Speed -func (s *IpAddress) DownloadSpeed() error { - start := time.Now() - resp, err := httpclient.Get("http://" + s.Ipv4 + ":8088/50") - if err != nil { - return err - } - defer resp.Body.Close() - ioutil.ReadAll(resp.Body) - t := time.Since(start) - //fmt.Println(s.Seconds()) - // size * time (seconds) - s.Download = (50 / t.Seconds()) * 8 - return nil -} - -func (s *IpAddress) UploadSpeed() error { - start := time.Now() - - // Get upload file path from config file - // Get Path from config - config, err := config.ConfigInit(nil, nil) - if err != nil { - return err - } - - b, w := createMultipartFormData("file", config.SpeedTestFile) - - req, err := http.NewRequest("GET", "http://"+s.Ipv4+":"+s.ServerPort+"/upload", &b) - if err != nil { - return err - } - // Don't forget to set the content type, this will contain the boundary. - req.Header.Set("Content-Type", w.FormDataContentType()) - defer req.Body.Close() - ioutil.ReadAll(req.Body) - t := time.Since(start) - //fmt.Println(s.Seconds()) - // size * time (seconds) - s.Upload = (50 / t.Seconds()) * 8 - return nil -} - -// Upload helper function for uploading -// (https://stackoverflow.com/questions/20205796/post-data-using-the-content-type-multipart-form-data -func createMultipartFormData(fieldName, fileName string) (bytes.Buffer, *multipart.Writer) { - var b bytes.Buffer - var err error - w := multipart.NewWriter(&b) - var fw io.Writer - file := mustOpen(fileName) - if fw, err = w.CreateFormFile(fieldName, file.Name()); err != nil { - log.Fatalf("Error creating writer: %v", err) - } - if _, err = io.Copy(fw, file); err != nil { - log.Fatalf("Error with io.Copy: %v", err) - //t.Errorf("Error with io.Copy: %v", err) - } - w.Close() - return b, w -} - -func mustOpen(f string) *os.File { - r, err := os.Open(f) - if err != nil { - log.Fatalf("Error with mustOpen: %v", err) - } - return r -} - -// PingTest executes test to measure latency -func (s *IpAddress) PingTest() error { - //pingURL := strings.Split(s.URL, "/upload")[0] + "/latency.txt" - var pingURL string - if s.Ipv6 != "" { - pingURL = "http://[" + s.Ipv6 + "]:" + s.ServerPort + "/server_info" - s.Ipv4 = "" - } else { - pingURL = "http://" + s.Ipv4 + ":" + s.ServerPort + "/server_info" - } - - l := time.Duration(100000000000) // 10sec - for i := 0; i < 3; i++ { - sTime := time.Now() - resp, err := http.Get(pingURL) - fTime := time.Now() - if err != nil || resp.StatusCode != 200 { - return errors.New("Node not found") - } - if fTime.Sub(sTime) < l { - l = fTime.Sub(sTime) - } - resp.Body.Close() - } - - s.Latency = time.Duration(int64(l.Nanoseconds() / 2)) - - return nil -} diff --git a/plugin/TestAnsible/description.txt b/plugin/TestAnsible/description.txt deleted file mode 100644 index b0a0a8f..0000000 --- a/plugin/TestAnsible/description.txt +++ /dev/null @@ -1 +0,0 @@ -Test if it's detected \ No newline at end of file diff --git a/plugin/TestAnsible/hosts b/plugin/TestAnsible/hosts deleted file mode 100644 index 07e4869..0000000 --- a/plugin/TestAnsible/hosts +++ /dev/null @@ -1,12 +0,0 @@ ---- -all: - vars: - ansible_python_interpreter: /usr/bin/python3 -main: - hosts: - host1: - ansible_host: 0.0.0.0 # Replace with your remote IP - ansible_port: 44003 # Replace with your remote SSH port - ansible_user: master # Replace wtih your username - ansible_ssh_pass: password - ansible_sudo_pass: password \ No newline at end of file diff --git a/plugin/TestAnsible/site.yml b/plugin/TestAnsible/site.yml deleted file mode 100644 index f263373..0000000 --- a/plugin/TestAnsible/site.yml +++ /dev/null @@ -1,8 +0,0 @@ ---- - -- hosts: all - - tasks: - - name: simple-ansibleplaybook - debug: - msg: Your are running 'simple-ansibleplaybook' example From f833eba32b813d98d9e947455640171477b0a8c4 Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 16:09:39 +0000 Subject: [PATCH 03/11] fixed untracked files --- .gitignore | 2 +- p2p/testingMetrics.go | 179 +++++++++++++++++++++++++++++ plugin/TestAnsible/description.txt | 1 + plugin/TestAnsible/hosts | 12 ++ plugin/TestAnsible/site.yml | 8 ++ 5 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 p2p/testingMetrics.go create mode 100644 plugin/TestAnsible/description.txt create mode 100644 plugin/TestAnsible/hosts create mode 100644 plugin/TestAnsible/site.yml diff --git a/.gitignore b/.gitignore index 8f34978..9903888 100644 --- a/.gitignore +++ b/.gitignore @@ -33,7 +33,7 @@ server/docker/containers/ export/ # Any testing file -test* +*test # Ignore public and private keys p2prc.publicKey diff --git a/p2p/testingMetrics.go b/p2p/testingMetrics.go new file mode 100644 index 0000000..7ab6d7e --- /dev/null +++ b/p2p/testingMetrics.go @@ -0,0 +1,179 @@ +package p2p + +import ( + "bytes" + "errors" + "github.com/Akilan1999/p2p-rendering-computation/config" + "io" + "io/ioutil" + "log" + "mime/multipart" + "net/http" + "os" + "time" +) + +// var dlSizes = [...]int{350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000} +// var ulSizes = [...]int{100, 300, 500, 800, 1000, 1500, 2500, 3000, 3500, 4000} //kB +var httpclient = http.Client{} + +// DownloadTest executes the test to measure download speed +//func (s *IpAddress) DownloadTest(savingMode bool) error { +// dlURL := "http://" + s.Ipv4 + ":8088/server_info" +// eg := errgroup.Group{} +// +// // Warming up +// sTime := time.Now() +// for i := 0; i < 2; i++ { +// eg.Go(func() error { +// return dlWarmUp("http://" + s.Ipv4 + ":8088/server_info") +// }) +// } +// if err := eg.Wait(); err != nil { +// return err +// } +// fTime := time.Now() +// // 1.125MB for each request (750 * 750 * 2) +// wuSpeed := 1.125 * 8 * 2 / fTime.Sub(sTime.Add(s.Latency)).Seconds() +// +// // Decide workload by warm up speed +// workload := 0 +// weight := 0 +// skip := false +// if savingMode { +// workload = 6 +// weight = 3 +// } else if 10.0 < wuSpeed { +// workload = 16 +// weight = 4 +// } else if 4.0 < wuSpeed { +// workload = 8 +// weight = 4 +// } else if 2.5 < wuSpeed { +// workload = 4 +// weight = 4 +// } else { +// skip = true +// } +// +// // Main speedtest +// dlSpeed := wuSpeed +// if skip == false { +// sTime = time.Now() +// for i := 0; i < workload; i++ { +// eg.Go(func() error { +// return downloadRequest(dlURL, weight) +// }) +// } +// if err := eg.Wait(); err != nil { +// return err +// } +// fTime = time.Now() +// +// reqMB := dlSizes[weight] * dlSizes[weight] * 2 / 1000 / 1000 +// dlSpeed = float64(reqMB) * 8 * float64(workload) / fTime.Sub(sTime).Seconds() +// } +// +// s.Download = dlSpeed +// return nil +//} + +// Download Speed +func (s *IpAddress) DownloadSpeed() error { + start := time.Now() + resp, err := httpclient.Get("http://" + s.Ipv4 + ":8088/50") + if err != nil { + return err + } + defer resp.Body.Close() + ioutil.ReadAll(resp.Body) + t := time.Since(start) + //fmt.Println(s.Seconds()) + // size * time (seconds) + s.Download = (50 / t.Seconds()) * 8 + return nil +} + +func (s *IpAddress) UploadSpeed() error { + start := time.Now() + + // Get upload file path from config file + // Get Path from config + config, err := config.ConfigInit(nil, nil) + if err != nil { + return err + } + + b, w := createMultipartFormData("file", config.SpeedTestFile) + + req, err := http.NewRequest("GET", "http://"+s.Ipv4+":"+s.ServerPort+"/upload", &b) + if err != nil { + return err + } + // Don't forget to set the content type, this will contain the boundary. + req.Header.Set("Content-Type", w.FormDataContentType()) + defer req.Body.Close() + ioutil.ReadAll(req.Body) + t := time.Since(start) + //fmt.Println(s.Seconds()) + // size * time (seconds) + s.Upload = (50 / t.Seconds()) * 8 + return nil +} + +// Upload helper function for uploading +// (https://stackoverflow.com/questions/20205796/post-data-using-the-content-type-multipart-form-data +func createMultipartFormData(fieldName, fileName string) (bytes.Buffer, *multipart.Writer) { + var b bytes.Buffer + var err error + w := multipart.NewWriter(&b) + var fw io.Writer + file := mustOpen(fileName) + if fw, err = w.CreateFormFile(fieldName, file.Name()); err != nil { + log.Fatalf("Error creating writer: %v", err) + } + if _, err = io.Copy(fw, file); err != nil { + log.Fatalf("Error with io.Copy: %v", err) + //t.Errorf("Error with io.Copy: %v", err) + } + w.Close() + return b, w +} + +func mustOpen(f string) *os.File { + r, err := os.Open(f) + if err != nil { + log.Fatalf("Error with mustOpen: %v", err) + } + return r +} + +// PingTest executes test to measure latency +func (s *IpAddress) PingTest() error { + //pingURL := strings.Split(s.URL, "/upload")[0] + "/latency.txt" + var pingURL string + if s.Ipv6 != "" { + pingURL = "http://[" + s.Ipv6 + "]:" + s.ServerPort + "/server_info" + s.Ipv4 = "" + } else { + pingURL = "http://" + s.Ipv4 + ":" + s.ServerPort + "/server_info" + } + + l := time.Duration(100000000000) // 10sec + for i := 0; i < 3; i++ { + sTime := time.Now() + resp, err := http.Get(pingURL) + fTime := time.Now() + if err != nil || resp.StatusCode != 200 { + return errors.New("Node not found") + } + if fTime.Sub(sTime) < l { + l = fTime.Sub(sTime) + } + resp.Body.Close() + } + + s.Latency = time.Duration(int64(l.Nanoseconds() / 2)) + + return nil +} diff --git a/plugin/TestAnsible/description.txt b/plugin/TestAnsible/description.txt new file mode 100644 index 0000000..b0a0a8f --- /dev/null +++ b/plugin/TestAnsible/description.txt @@ -0,0 +1 @@ +Test if it's detected \ No newline at end of file diff --git a/plugin/TestAnsible/hosts b/plugin/TestAnsible/hosts new file mode 100644 index 0000000..07e4869 --- /dev/null +++ b/plugin/TestAnsible/hosts @@ -0,0 +1,12 @@ +--- +all: + vars: + ansible_python_interpreter: /usr/bin/python3 +main: + hosts: + host1: + ansible_host: 0.0.0.0 # Replace with your remote IP + ansible_port: 44003 # Replace with your remote SSH port + ansible_user: master # Replace wtih your username + ansible_ssh_pass: password + ansible_sudo_pass: password \ No newline at end of file diff --git a/plugin/TestAnsible/site.yml b/plugin/TestAnsible/site.yml new file mode 100644 index 0000000..f263373 --- /dev/null +++ b/plugin/TestAnsible/site.yml @@ -0,0 +1,8 @@ +--- + +- hosts: all + + tasks: + - name: simple-ansibleplaybook + debug: + msg: Your are running 'simple-ansibleplaybook' example From b3f5d63e16ec8dcf5cf9b88e685cf4528a51e687 Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 16:20:30 +0000 Subject: [PATCH 04/11] fixed adding public key default path --- config/generate/generate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/generate/generate.go b/config/generate/generate.go index 89c1885..b926457 100644 --- a/config/generate/generate.go +++ b/config/generate/generate.go @@ -91,7 +91,7 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No } // Generate Public and private keys and set path - Defaults.PublicKeyFile = defaultPath + "p2prc.PublicKeyBareMetal" + Defaults.PublicKeyFile = defaultPath + "p2prc.publicKey" Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey" Defaults.BareMetal = false Defaults.UnsafeMode = false From afacc339ec40b6131cf8d290811f3612f83e7848 Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 16:29:21 +0000 Subject: [PATCH 05/11] reverted IPTable logic --- server/server.go | 743 ++++++++++++++++++++++++----------------------- 1 file changed, 372 insertions(+), 371 deletions(-) diff --git a/server/server.go b/server/server.go index 6e3f50e..b0eabda 100644 --- a/server/server.go +++ b/server/server.go @@ -1,449 +1,450 @@ package server import ( - b64 "encoding/base64" - "errors" - "fmt" - "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" - "github.com/Akilan1999/p2p-rendering-computation/config" - "github.com/Akilan1999/p2p-rendering-computation/p2p" - "github.com/Akilan1999/p2p-rendering-computation/p2p/frp" - "github.com/Akilan1999/p2p-rendering-computation/server/docker" - "github.com/gin-gonic/gin" - "net/http" - "os/user" - "strconv" - "time" + b64 "encoding/base64" + "errors" + "fmt" + "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" + "github.com/Akilan1999/p2p-rendering-computation/config" + "github.com/Akilan1999/p2p-rendering-computation/p2p" + "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" ) type ReverseProxy struct { - IPAddress string - Port string + IPAddress string + Port string } // ReverseProxies Reverse to the map such as ReverseProxies[]ReverseProxy type var ReverseProxies map[string]ReverseProxy func Server() (*gin.Engine, error) { - r := gin.Default() + r := gin.Default() - // "The make function allocates and initializes a hash map data - //structure and returns a map value that points to it. - //The specifics of that data structure are an implementation - //detail of the runtime and are not specified by the language itself." - // source: https://go.dev/blog/maps - ReverseProxies = make(map[string]ReverseProxy) + // "The make function allocates and initializes a hash map data + //structure and returns a map value that points to it. + //The specifics of that data structure are an implementation + //detail of the runtime and are not specified by the language itself." + // source: https://go.dev/blog/maps + ReverseProxies = make(map[string]ReverseProxy) - //Get Server port based on the config file - config, err := config.ConfigInit(nil, nil) - if err != nil { - return nil, err - } + //Get Server port based on the config file + config, err := config.ConfigInit(nil, nil) + if err != nil { + return nil, err + } - // update IPTable with new port and ip address and update ip table - var ProxyIpAddr p2p.IpAddress - var lowestLatencyIpAddress p2p.IpAddress + // update IPTable with new port and ip address and update ip table + var ProxyIpAddr p2p.IpAddress + var lowestLatencyIpAddress p2p.IpAddress - // Gets default information of the server - r.GET("/server_info", func(c *gin.Context) { - c.JSON(http.StatusOK, ServerInfo()) - }) + // Gets default information of the server + r.GET("/server_info", func(c *gin.Context) { + c.JSON(http.StatusOK, ServerInfo()) + }) - // Speed test with 50 mbps - r.GET("/50", func(c *gin.Context) { - // Get Path from config - c.File(config.SpeedTestFile) - }) + // Speed test with 50 mbps + r.GET("/50", func(c *gin.Context) { + // Get Path from config + c.File(config.SpeedTestFile) + }) - // Route build to do a speed test - r.GET("/upload", func(c *gin.Context) { - file, _ := c.FormFile("file") + // Route build to do a speed test + r.GET("/upload", func(c *gin.Context) { + file, _ := c.FormFile("file") - // Upload the file to specific dst. - // c.SaveUploadedFile(file, dst) + // Upload the file to specific dst. + // c.SaveUploadedFile(file, dst) - c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) - }) + c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) + }) - //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)) - //} + //Gets Ip Table from server node + r.POST("/IpTable", func(c *gin.Context) { + // Getting IPV4 address of client + var ClientHost p2p.IpAddress - // Reads IP addresses from ip table - IpAddresses, err := p2p.ReadIpTable() - if err != nil { - c.String(http.StatusOK, fmt.Sprint(err)) - } + if p2p.Ip4or6(c.ClientIP()) == "version 6" { + ClientHost.Ipv6 = c.ClientIP() + } else { + ClientHost.Ipv4 = c.ClientIP() + } - c.JSON(http.StatusOK, IpAddresses) - }) + // Variable to store IP table information + var IPTable p2p.IpAddresses - // Starts docker container in server - r.GET("/startcontainer", func(c *gin.Context) { - // Get Number of ports to open and whether to use GPU or not - Ports := c.DefaultQuery("ports", "0") - GPU := c.DefaultQuery("GPU", "false") - ContainerName := c.DefaultQuery("ContainerName", "") - BaseImage := c.DefaultQuery("BaseImage", "") - PublicKey := c.DefaultQuery("PublicKey", "") - var PortsInt int + // Receive file from POST request + body, err := c.FormFile("json") + if err != nil { + c.String(http.StatusOK, fmt.Sprint(err)) + } - if PublicKey == "" { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", "Publickey not passed")) - } + // Open file + open, err := body.Open() + if err != nil { + c.String(http.StatusOK, fmt.Sprint(err)) + } - PublicKeyDecoded, err := b64.StdEncoding.DecodeString(PublicKey) - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } + // Open received file + file, err := ioutil.ReadAll(open) + if err != nil { + c.String(http.StatusOK, fmt.Sprint(err)) + } - // Convert Get Request value to int - fmt.Sscanf(Ports, "%d", &PortsInt) + json.Unmarshal(file, &IPTable) - fmt.Println(string(PublicKeyDecoded[:])) + //Add Client IP address to IPTable struct + IPTable.IpAddress = append(IPTable.IpAddress, ClientHost) - // Creates container and returns-back result to - // access container - resp, err := docker.BuildRunContainer(PortsInt, GPU, ContainerName, BaseImage, string(PublicKeyDecoded[:])) + // 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)) + } - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } + // Reads IP addresses from ip table + IpAddresses, err := p2p.ReadIpTable() + if err != nil { + c.String(http.StatusOK, fmt.Sprint(err)) + } - // Ensures that FRP is triggered only if a proxy address is provided - if ProxyIpAddr.Ipv4 != "" && c.Request.Host != "localhost:"+config.ServerPort && c.Request.Host != "0.0.0.0:"+config.ServerPort { - resp, err = frp.StartFRPCDockerContainer(ProxyIpAddr.Ipv4, lowestLatencyIpAddress.ServerPort, resp) - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } - } + c.JSON(http.StatusOK, IpAddresses) + }) - c.JSON(http.StatusOK, resp) - }) + // Starts docker container in server + r.GET("/startcontainer", func(c *gin.Context) { + // Get Number of ports to open and whether to use GPU or not + Ports := c.DefaultQuery("ports", "0") + GPU := c.DefaultQuery("GPU", "false") + ContainerName := c.DefaultQuery("ContainerName", "") + BaseImage := c.DefaultQuery("BaseImage", "") + PublicKey := c.DefaultQuery("PublicKey", "") + var PortsInt int - //Remove container - r.GET("/RemoveContainer", func(c *gin.Context) { - ID := c.DefaultQuery("id", "0") - if err := docker.StopAndRemoveContainer(ID); err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } - c.String(http.StatusOK, "success") - }) + if PublicKey == "" { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", "Publickey not passed")) + } - //Show images available - r.GET("/ShowImages", func(c *gin.Context) { - resp, err := docker.ViewAllContainers() - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } - c.JSON(http.StatusOK, resp) - }) + PublicKeyDecoded, err := b64.StdEncoding.DecodeString(PublicKey) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } - // Request for port no from Server with address - r.GET("/FRPPort", func(c *gin.Context) { - port, err := frp.StartFRPProxyFromRandom() - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } + // Convert Get Request value to int + fmt.Sscanf(Ports, "%d", &PortsInt) - c.String(http.StatusOK, strconv.Itoa(port)) - }) + fmt.Println(string(PublicKeyDecoded[:])) - r.GET("/MAPPort", func(c *gin.Context) { - Ports := c.DefaultQuery("port", "0") - DomainName := c.DefaultQuery("domain_name", "") - url, _, err := MapPort(Ports, DomainName) - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } + // Creates container and returns-back result to + // access container + resp, err := docker.BuildRunContainer(PortsInt, GPU, ContainerName, BaseImage, string(PublicKeyDecoded[:])) - c.String(http.StatusOK, url) - }) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } - r.GET("/AddProxy", func(c *gin.Context) { - DomainName := c.DefaultQuery("domain_name", "") - ip_address := c.DefaultQuery("ip_address", "") - Ports := c.DefaultQuery("port", "") + // Ensures that FRP is triggered only if a proxy address is provided + if ProxyIpAddr.Ipv4 != "" && c.Request.Host != "localhost:"+config.ServerPort && c.Request.Host != "0.0.0.0:"+config.ServerPort { + resp, err = frp.StartFRPCDockerContainer(ProxyIpAddr.Ipv4, lowestLatencyIpAddress.ServerPort, resp) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } + } - if DomainName == "" || ip_address == "" || Ports == "" { - c.String(http.StatusInternalServerError, fmt.Sprintf("All get parameters npt provided"+ - " do ensure domain_name, ip_Address and port no is provided")) - } + c.JSON(http.StatusOK, resp) + }) - err := SaveRegistration(DomainName, ip_address+":"+Ports) - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf(err.Error())) - } + //Remove container + r.GET("/RemoveContainer", func(c *gin.Context) { + ID := c.DefaultQuery("id", "0") + if err := docker.StopAndRemoveContainer(ID); err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } + c.String(http.StatusOK, "success") + }) - //_, ok := ReverseProxies[DomainName] - //// To check if the subdomain entry exists - //if ok { - // c.String(http.StatusInternalServerError, fmt.Sprintf("The domain entry already exists as a reverse"+ - // " proxy entry")) - //} - // - //// added proxy as a map entry - //ReverseProxies[DomainName] = ReverseProxy{IPAddress: ip_address, Port: Ports} - c.String(http.StatusOK, "Sucess") + //Show images available + r.GET("/ShowImages", func(c *gin.Context) { + resp, err := docker.ViewAllContainers() + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } + c.JSON(http.StatusOK, resp) + }) - }) + // Request for port no from Server with address + r.GET("/FRPPort", func(c *gin.Context) { + port, err := frp.StartFRPProxyFromRandom() + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } - //r.GET("/RemoveProxy", func(c *gin.Context) { - // DomainName := c.DefaultQuery("domain_name", "") - // - // _, ok := ReverseProxies[DomainName] - // if !ok { - // c.String(http.StatusInternalServerError, fmt.Sprintf("Domain name does exist in entries of proxies")) - // } else { - // delete(ReverseProxies, DomainName) - // } - // - //}) + c.String(http.StatusOK, strconv.Itoa(port)) + }) - // If there is a proxy port specified - // then starts the FRP server - //if config.FRPServerPort != "0" { - // go frp.StartFRPProxyFromRandom() - //} + r.GET("/MAPPort", func(c *gin.Context) { + Ports := c.DefaultQuery("port", "0") + DomainName := c.DefaultQuery("domain_name", "") + url, _, err := MapPort(Ports, DomainName) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } - // Remove nodes currently not pingable - clientIPTable.RemoveOfflineNodes() + c.String(http.StatusOK, url) + }) - table, err := p2p.ReadIpTable() + r.GET("/AddProxy", func(c *gin.Context) { + DomainName := c.DefaultQuery("domain_name", "") + ip_address := c.DefaultQuery("ip_address", "") + Ports := c.DefaultQuery("port", "") - // 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 err != nil { - return nil, err - } + if DomainName == "" || ip_address == "" || Ports == "" { + c.String(http.StatusInternalServerError, fmt.Sprintf("All get parameters npt provided"+ + " do ensure domain_name, ip_Address and port no is provided")) + } - var lowestLatency int64 - // random large number - lowestLatency = 10000000 + err := SaveRegistration(DomainName, ip_address+":"+Ports) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf(err.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 != "" { - lowestLatency = table.IpAddress[i].Latency.Milliseconds() - lowestLatencyIpAddress = table.IpAddress[i] - } - } + //_, ok := ReverseProxies[DomainName] + //// To check if the subdomain entry exists + //if ok { + // c.String(http.StatusInternalServerError, fmt.Sprintf("The domain entry already exists as a reverse"+ + // " proxy entry")) + //} + // + //// added proxy as a map entry + //ReverseProxies[DomainName] = ReverseProxy{IPAddress: ip_address, Port: Ports} + c.String(http.StatusOK, "Sucess") - // If there is an identified node - if lowestLatency != 10000000 { - serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) - if err != nil { - return nil, 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, config.ServerPort, "") - if err != nil { - return nil, err - } + }) - // updating with the current proxy address - ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 - ProxyIpAddr.ServerPort = proxyPort - ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" - ProxyIpAddr.ProxyServer = "False" - ProxyIpAddr.EscapeImplementation = "FRP" + //r.GET("/RemoveProxy", func(c *gin.Context) { + // DomainName := c.DefaultQuery("domain_name", "") + // + // _, ok := ReverseProxies[DomainName] + // if !ok { + // c.String(http.StatusInternalServerError, fmt.Sprintf("Domain name does exist in entries of proxies")) + // } else { + // delete(ReverseProxies, DomainName) + // } + // + //}) - if config.BareMetal { - _, SSHPort, err := MapPort("22", "") - if err != nil { - return nil, err - } - ProxyIpAddr.BareMetalSSHPort = SSHPort - } + // If there is a proxy port specified + // then starts the FRP server + //if config.FRPServerPort != "0" { + // go frp.StartFRPProxyFromRandom() + //} - //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) - // write information back to the IP Table - } + // Remove nodes currently not pingable + clientIPTable.RemoveOfflineNodes() - } else { - ProxyIpAddr.Ipv4, err = p2p.CurrentPublicIP() - if err != nil { - fmt.Println(err) - } - ProxyIpAddr.ServerPort = config.ServerPort - ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" - if config.ProxyPort != "" { - ProxyIpAddr.ProxyServer = "True" - } - ProxyIpAddr.EscapeImplementation = "" - if config.BareMetal { - ProxyIpAddr.BareMetalSSHPort = "22" - } + table, err := p2p.ReadIpTable() - } + // 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 err != nil { + return nil, err + } - // 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() + var lowestLatency int64 + // random large number + lowestLatency = 10000000 - if err != nil { - return nil, err - } + 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 != "" { + lowestLatency = table.IpAddress[i].Latency.Milliseconds() + lowestLatencyIpAddress = table.IpAddress[i] + } + } - // append the following to the ip table - table.IpAddress = append(table.IpAddress, ProxyIpAddr) + // If there is an identified node + if lowestLatency != 10000000 { + serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) + if err != nil { + return nil, 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, config.ServerPort, "") + if err != nil { + return nil, err + } - // Writing results to the IPTable - err = table.WriteIpTable() - if err != nil { - return nil, err - } + // updating with the current proxy address + ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 + ProxyIpAddr.ServerPort = proxyPort + ProxyIpAddr.Name = config.MachineName + ProxyIpAddr.NAT = "False" + ProxyIpAddr.ProxyServer = "False" + ProxyIpAddr.EscapeImplementation = "FRP" - // update ip table - go func() error { - err := clientIPTable.UpdateIpTableListClient() - if err != nil { - fmt.Println(err) - return err - } - return nil - }() + if config.BareMetal { + _, SSHPort, err := MapPort("22", "") + if err != nil { + return nil, err + } + ProxyIpAddr.BareMetalSSHPort = SSHPort + } - if config.ProxyPort != "" { - go ProxyRun(config.ProxyPort) - } + //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) + // write information back to the IP Table + } - // Run gin server on the specified port - go r.Run(":" + config.ServerPort) + } else { + ProxyIpAddr.Ipv4, err = p2p.CurrentPublicIP() + if err != nil { + fmt.Println(err) + } + ProxyIpAddr.ServerPort = config.ServerPort + ProxyIpAddr.Name = config.MachineName + ProxyIpAddr.NAT = "False" + if config.ProxyPort != "" { + ProxyIpAddr.ProxyServer = "True" + } + ProxyIpAddr.EscapeImplementation = "" + if config.BareMetal { + ProxyIpAddr.BareMetalSSHPort = "22" + } - return r, nil + } + + // 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 { + return nil, err + } + + // update ip table + go func() error { + err := clientIPTable.UpdateIpTableListClient() + if err != nil { + fmt.Println(err) + return err + } + return nil + }() + + if config.ProxyPort != "" { + go ProxyRun(config.ProxyPort) + } + + // Run gin server on the specified port + go r.Run(":" + config.ServerPort) + + return r, nil } func MapPort(port string, domainName string) (string, string, error) { - //Get Server port based on the config file - config, err := config.ConfigInit(nil, nil) - if err != nil { - return "", "", err - } + //Get Server port based on the config file + config, err := config.ConfigInit(nil, nil) + if err != nil { + return "", "", err + } - // update IPTable with new port and ip address and update ip table - var ProxyIpAddr p2p.IpAddress - var lowestLatencyIpAddress p2p.IpAddress + // update IPTable with new port and ip address and update ip table + var ProxyIpAddr p2p.IpAddress + var lowestLatencyIpAddress p2p.IpAddress - clientIPTable.RemoveOfflineNodes() + clientIPTable.RemoveOfflineNodes() - table, err := p2p.ReadIpTable() - if err != nil { - return "", "", err - } + table, err := p2p.ReadIpTable() + if err != nil { + return "", "", err + } - var lowestLatency int64 - // random large number - lowestLatency = 10000000 + var lowestLatency int64 + // random large number + lowestLatency = 10000000 - 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 != "" { - // Filter based on nodes with proxy enabled - if domainName != "" && table.IpAddress[i].ProxyServer == "True" { - lowestLatency = table.IpAddress[i].Latency.Milliseconds() - lowestLatencyIpAddress = table.IpAddress[i] - continue - } - lowestLatency = table.IpAddress[i].Latency.Milliseconds() - lowestLatencyIpAddress = table.IpAddress[i] - } - } + 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 != "" { + // Filter based on nodes with proxy enabled + if domainName != "" && table.IpAddress[i].ProxyServer == "True" { + lowestLatency = table.IpAddress[i].Latency.Milliseconds() + lowestLatencyIpAddress = table.IpAddress[i] + continue + } + lowestLatency = table.IpAddress[i].Latency.Milliseconds() + lowestLatencyIpAddress = table.IpAddress[i] + } + } - // If there is an identified node - if lowestLatency != 10000000 { - serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) - if err != nil { - 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 - } + // If there is an identified node + if lowestLatency != 10000000 { + serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) + if err != nil { + 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 + } - // Doing the proxy mapping for the domain name - if domainName != "" { - 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 { - // URL = "http://" + IP + ":" + serverPort + "/server_info" - //} - http.Get(URL) - //SaveRegistration(domainName, lowestLatencyIpAddress.Ipv4+":"+proxyPort) - } + // Doing the proxy mapping for the domain name + if domainName != "" { + 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 { + // URL = "http://" + IP + ":" + serverPort + "/server_info" + //} + http.Get(URL) + //SaveRegistration(domainName, lowestLatencyIpAddress.Ipv4+":"+proxyPort) + } - // updating with the current proxy address - ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 - ProxyIpAddr.ServerPort = proxyPort - ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" - ProxyIpAddr.EscapeImplementation = "FRP" + // updating with the current proxy address + ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 + ProxyIpAddr.ServerPort = proxyPort + ProxyIpAddr.Name = config.MachineName + ProxyIpAddr.NAT = "False" + ProxyIpAddr.EscapeImplementation = "FRP" - //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) - } else { - return "", "", errors.New("proxy IP not found") - } + //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) + } else { + return "", "", errors.New("proxy IP not found") + } - return ProxyIpAddr.Ipv4 + ":" + ProxyIpAddr.ServerPort, ProxyIpAddr.ServerPort, nil + return ProxyIpAddr.Ipv4 + ":" + ProxyIpAddr.ServerPort, ProxyIpAddr.ServerPort, nil } From 2bd39cc45034069e251d1b8a5cf083a521348d0a Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 16:31:01 +0000 Subject: [PATCH 06/11] fixed JSON compile time error --- server/server.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/server.go b/server/server.go index b0eabda..0c64fdd 100644 --- a/server/server.go +++ b/server/server.go @@ -2,6 +2,7 @@ package server import ( b64 "encoding/base64" + "encoding/json" "errors" "fmt" "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" From eb0a7d6b34108a6627f14e3936324d7f3e1e7431 Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 16:59:12 +0000 Subject: [PATCH 07/11] for update IPTable reduce logic --- server/server.go | 82 +++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/server/server.go b/server/server.go index 0c64fdd..7d9898e 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,7 +10,6 @@ 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" @@ -69,46 +67,46 @@ 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)) - } + //// 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)) + //} // Reads IP addresses from ip table IpAddresses, err := p2p.ReadIpTable() From 623d7b93e03e0931a6f62a6cc00aefbf0f0b7cbd Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 17:04:28 +0000 Subject: [PATCH 08/11] for update IPTable reduce logic --- server/server.go | 744 ++++++++++++++++++++++++----------------------- 1 file changed, 373 insertions(+), 371 deletions(-) diff --git a/server/server.go b/server/server.go index 7d9898e..0e56ed1 100644 --- a/server/server.go +++ b/server/server.go @@ -1,449 +1,451 @@ package server import ( - b64 "encoding/base64" - "errors" - "fmt" - "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" - "github.com/Akilan1999/p2p-rendering-computation/config" - "github.com/Akilan1999/p2p-rendering-computation/p2p" - "github.com/Akilan1999/p2p-rendering-computation/p2p/frp" - "github.com/Akilan1999/p2p-rendering-computation/server/docker" - "github.com/gin-gonic/gin" - "net/http" - "os/user" - "strconv" - "time" + b64 "encoding/base64" + "encoding/json" + "errors" + "fmt" + "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" + "github.com/Akilan1999/p2p-rendering-computation/config" + "github.com/Akilan1999/p2p-rendering-computation/p2p" + "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" ) type ReverseProxy struct { - IPAddress string - Port string + IPAddress string + Port string } // ReverseProxies Reverse to the map such as ReverseProxies[]ReverseProxy type var ReverseProxies map[string]ReverseProxy func Server() (*gin.Engine, error) { - r := gin.Default() + r := gin.Default() - // "The make function allocates and initializes a hash map data - //structure and returns a map value that points to it. - //The specifics of that data structure are an implementation - //detail of the runtime and are not specified by the language itself." - // source: https://go.dev/blog/maps - ReverseProxies = make(map[string]ReverseProxy) + // "The make function allocates and initializes a hash map data + //structure and returns a map value that points to it. + //The specifics of that data structure are an implementation + //detail of the runtime and are not specified by the language itself." + // source: https://go.dev/blog/maps + ReverseProxies = make(map[string]ReverseProxy) - //Get Server port based on the config file - config, err := config.ConfigInit(nil, nil) - if err != nil { - return nil, err - } + //Get Server port based on the config file + config, err := config.ConfigInit(nil, nil) + if err != nil { + return nil, err + } - // update IPTable with new port and ip address and update ip table - var ProxyIpAddr p2p.IpAddress - var lowestLatencyIpAddress p2p.IpAddress + // update IPTable with new port and ip address and update ip table + var ProxyIpAddr p2p.IpAddress + var lowestLatencyIpAddress p2p.IpAddress - // Gets default information of the server - r.GET("/server_info", func(c *gin.Context) { - c.JSON(http.StatusOK, ServerInfo()) - }) + // Gets default information of the server + r.GET("/server_info", func(c *gin.Context) { + c.JSON(http.StatusOK, ServerInfo()) + }) - // Speed test with 50 mbps - r.GET("/50", func(c *gin.Context) { - // Get Path from config - c.File(config.SpeedTestFile) - }) + // Speed test with 50 mbps + r.GET("/50", func(c *gin.Context) { + // Get Path from config + c.File(config.SpeedTestFile) + }) - // Route build to do a speed test - r.GET("/upload", func(c *gin.Context) { - file, _ := c.FormFile("file") + // Route build to do a speed test + r.GET("/upload", func(c *gin.Context) { + file, _ := c.FormFile("file") - // Upload the file to specific dst. - // c.SaveUploadedFile(file, dst) + // Upload the file to specific dst. + // c.SaveUploadedFile(file, dst) - c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) - }) + c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) + }) - //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)) - //} + //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() + //} - // Reads IP addresses from ip table - IpAddresses, err := p2p.ReadIpTable() - if err != nil { - c.String(http.StatusOK, fmt.Sprint(err)) - } + // Variable to store IP table information + var IPTable p2p.IpAddresses - c.JSON(http.StatusOK, IpAddresses) - }) + // Receive file from POST request + body, err := c.FormFile("json") + if err != nil { + c.String(http.StatusOK, fmt.Sprint(err)) + } - // Starts docker container in server - r.GET("/startcontainer", func(c *gin.Context) { - // Get Number of ports to open and whether to use GPU or not - Ports := c.DefaultQuery("ports", "0") - GPU := c.DefaultQuery("GPU", "false") - ContainerName := c.DefaultQuery("ContainerName", "") - BaseImage := c.DefaultQuery("BaseImage", "") - PublicKey := c.DefaultQuery("PublicKey", "") - var PortsInt int + // Open file + open, err := body.Open() + if err != nil { + c.String(http.StatusOK, fmt.Sprint(err)) + } - if PublicKey == "" { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", "Publickey not passed")) - } + // Open received file + file, err := ioutil.ReadAll(open) + if err != nil { + c.String(http.StatusOK, fmt.Sprint(err)) + } - PublicKeyDecoded, err := b64.StdEncoding.DecodeString(PublicKey) - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } + json.Unmarshal(file, &IPTable) - // Convert Get Request value to int - fmt.Sscanf(Ports, "%d", &PortsInt) + //Add Client IP address to IPTable struct + //IPTable.IpAddress = append(IPTable.IpAddress, ClientHost) - fmt.Println(string(PublicKeyDecoded[:])) + // 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)) + } - // Creates container and returns-back result to - // access container - resp, err := docker.BuildRunContainer(PortsInt, GPU, ContainerName, BaseImage, string(PublicKeyDecoded[:])) + // Reads IP addresses from ip table + IpAddresses, err := p2p.ReadIpTable() + if err != nil { + c.String(http.StatusOK, fmt.Sprint(err)) + } - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } + c.JSON(http.StatusOK, IpAddresses) + }) - // Ensures that FRP is triggered only if a proxy address is provided - if ProxyIpAddr.Ipv4 != "" && c.Request.Host != "localhost:"+config.ServerPort && c.Request.Host != "0.0.0.0:"+config.ServerPort { - resp, err = frp.StartFRPCDockerContainer(ProxyIpAddr.Ipv4, lowestLatencyIpAddress.ServerPort, resp) - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } - } + // Starts docker container in server + r.GET("/startcontainer", func(c *gin.Context) { + // Get Number of ports to open and whether to use GPU or not + Ports := c.DefaultQuery("ports", "0") + GPU := c.DefaultQuery("GPU", "false") + ContainerName := c.DefaultQuery("ContainerName", "") + BaseImage := c.DefaultQuery("BaseImage", "") + PublicKey := c.DefaultQuery("PublicKey", "") + var PortsInt int - c.JSON(http.StatusOK, resp) - }) + if PublicKey == "" { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", "Publickey not passed")) + } - //Remove container - r.GET("/RemoveContainer", func(c *gin.Context) { - ID := c.DefaultQuery("id", "0") - if err := docker.StopAndRemoveContainer(ID); err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } - c.String(http.StatusOK, "success") - }) + PublicKeyDecoded, err := b64.StdEncoding.DecodeString(PublicKey) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } - //Show images available - r.GET("/ShowImages", func(c *gin.Context) { - resp, err := docker.ViewAllContainers() - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } - c.JSON(http.StatusOK, resp) - }) + // Convert Get Request value to int + fmt.Sscanf(Ports, "%d", &PortsInt) - // Request for port no from Server with address - r.GET("/FRPPort", func(c *gin.Context) { - port, err := frp.StartFRPProxyFromRandom() - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } + fmt.Println(string(PublicKeyDecoded[:])) - c.String(http.StatusOK, strconv.Itoa(port)) - }) + // Creates container and returns-back result to + // access container + resp, err := docker.BuildRunContainer(PortsInt, GPU, ContainerName, BaseImage, string(PublicKeyDecoded[:])) - r.GET("/MAPPort", func(c *gin.Context) { - Ports := c.DefaultQuery("port", "0") - DomainName := c.DefaultQuery("domain_name", "") - url, _, err := MapPort(Ports, DomainName) - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) - } + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } - c.String(http.StatusOK, url) - }) + // Ensures that FRP is triggered only if a proxy address is provided + if ProxyIpAddr.Ipv4 != "" && c.Request.Host != "localhost:"+config.ServerPort && c.Request.Host != "0.0.0.0:"+config.ServerPort { + resp, err = frp.StartFRPCDockerContainer(ProxyIpAddr.Ipv4, lowestLatencyIpAddress.ServerPort, resp) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } + } - r.GET("/AddProxy", func(c *gin.Context) { - DomainName := c.DefaultQuery("domain_name", "") - ip_address := c.DefaultQuery("ip_address", "") - Ports := c.DefaultQuery("port", "") + c.JSON(http.StatusOK, resp) + }) - if DomainName == "" || ip_address == "" || Ports == "" { - c.String(http.StatusInternalServerError, fmt.Sprintf("All get parameters npt provided"+ - " do ensure domain_name, ip_Address and port no is provided")) - } + //Remove container + r.GET("/RemoveContainer", func(c *gin.Context) { + ID := c.DefaultQuery("id", "0") + if err := docker.StopAndRemoveContainer(ID); err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } + c.String(http.StatusOK, "success") + }) - err := SaveRegistration(DomainName, ip_address+":"+Ports) - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf(err.Error())) - } + //Show images available + r.GET("/ShowImages", func(c *gin.Context) { + resp, err := docker.ViewAllContainers() + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } + c.JSON(http.StatusOK, resp) + }) - //_, ok := ReverseProxies[DomainName] - //// To check if the subdomain entry exists - //if ok { - // c.String(http.StatusInternalServerError, fmt.Sprintf("The domain entry already exists as a reverse"+ - // " proxy entry")) - //} - // - //// added proxy as a map entry - //ReverseProxies[DomainName] = ReverseProxy{IPAddress: ip_address, Port: Ports} - c.String(http.StatusOK, "Sucess") + // Request for port no from Server with address + r.GET("/FRPPort", func(c *gin.Context) { + port, err := frp.StartFRPProxyFromRandom() + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } - }) + c.String(http.StatusOK, strconv.Itoa(port)) + }) - //r.GET("/RemoveProxy", func(c *gin.Context) { - // DomainName := c.DefaultQuery("domain_name", "") - // - // _, ok := ReverseProxies[DomainName] - // if !ok { - // c.String(http.StatusInternalServerError, fmt.Sprintf("Domain name does exist in entries of proxies")) - // } else { - // delete(ReverseProxies, DomainName) - // } - // - //}) + r.GET("/MAPPort", func(c *gin.Context) { + Ports := c.DefaultQuery("port", "0") + DomainName := c.DefaultQuery("domain_name", "") + url, _, err := MapPort(Ports, DomainName) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } - // If there is a proxy port specified - // then starts the FRP server - //if config.FRPServerPort != "0" { - // go frp.StartFRPProxyFromRandom() - //} + c.String(http.StatusOK, url) + }) - // Remove nodes currently not pingable - clientIPTable.RemoveOfflineNodes() + r.GET("/AddProxy", func(c *gin.Context) { + DomainName := c.DefaultQuery("domain_name", "") + ip_address := c.DefaultQuery("ip_address", "") + Ports := c.DefaultQuery("port", "") - table, err := p2p.ReadIpTable() + if DomainName == "" || ip_address == "" || Ports == "" { + c.String(http.StatusInternalServerError, fmt.Sprintf("All get parameters npt provided"+ + " do ensure domain_name, ip_Address and port no is provided")) + } - // 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 err != nil { - return nil, err - } + err := SaveRegistration(DomainName, ip_address+":"+Ports) + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf(err.Error())) + } - var lowestLatency int64 - // random large number - lowestLatency = 10000000 + //_, ok := ReverseProxies[DomainName] + //// To check if the subdomain entry exists + //if ok { + // c.String(http.StatusInternalServerError, fmt.Sprintf("The domain entry already exists as a reverse"+ + // " proxy entry")) + //} + // + //// added proxy as a map entry + //ReverseProxies[DomainName] = ReverseProxy{IPAddress: ip_address, Port: Ports} + c.String(http.StatusOK, "Sucess") - 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 != "" { - lowestLatency = table.IpAddress[i].Latency.Milliseconds() - lowestLatencyIpAddress = table.IpAddress[i] - } - } + }) - // If there is an identified node - if lowestLatency != 10000000 { - serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) - if err != nil { - return nil, 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, config.ServerPort, "") - if err != nil { - return nil, err - } + //r.GET("/RemoveProxy", func(c *gin.Context) { + // DomainName := c.DefaultQuery("domain_name", "") + // + // _, ok := ReverseProxies[DomainName] + // if !ok { + // c.String(http.StatusInternalServerError, fmt.Sprintf("Domain name does exist in entries of proxies")) + // } else { + // delete(ReverseProxies, DomainName) + // } + // + //}) - // updating with the current proxy address - ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 - ProxyIpAddr.ServerPort = proxyPort - ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" - ProxyIpAddr.ProxyServer = "False" - ProxyIpAddr.EscapeImplementation = "FRP" + // If there is a proxy port specified + // then starts the FRP server + //if config.FRPServerPort != "0" { + // go frp.StartFRPProxyFromRandom() + //} - if config.BareMetal { - _, SSHPort, err := MapPort("22", "") - if err != nil { - return nil, err - } - ProxyIpAddr.BareMetalSSHPort = SSHPort - } + // Remove nodes currently not pingable + clientIPTable.RemoveOfflineNodes() - //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) - // write information back to the IP Table - } + table, err := p2p.ReadIpTable() - } else { - ProxyIpAddr.Ipv4, err = p2p.CurrentPublicIP() - if err != nil { - fmt.Println(err) - } - ProxyIpAddr.ServerPort = config.ServerPort - ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" - if config.ProxyPort != "" { - ProxyIpAddr.ProxyServer = "True" - } - ProxyIpAddr.EscapeImplementation = "" - if config.BareMetal { - ProxyIpAddr.BareMetalSSHPort = "22" - } + // 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 err != nil { + return nil, err + } - } + var lowestLatency int64 + // random large number + lowestLatency = 10000000 - // 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() + 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 != "" { + lowestLatency = table.IpAddress[i].Latency.Milliseconds() + lowestLatencyIpAddress = table.IpAddress[i] + } + } - if err != nil { - return nil, err - } + // If there is an identified node + if lowestLatency != 10000000 { + serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) + if err != nil { + return nil, 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, config.ServerPort, "") + if err != nil { + return nil, err + } - // append the following to the ip table - table.IpAddress = append(table.IpAddress, ProxyIpAddr) + // updating with the current proxy address + ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 + ProxyIpAddr.ServerPort = proxyPort + ProxyIpAddr.Name = config.MachineName + ProxyIpAddr.NAT = "False" + ProxyIpAddr.ProxyServer = "False" + ProxyIpAddr.EscapeImplementation = "FRP" - // Writing results to the IPTable - err = table.WriteIpTable() - if err != nil { - return nil, err - } + if config.BareMetal { + _, SSHPort, err := MapPort("22", "") + if err != nil { + return nil, err + } + ProxyIpAddr.BareMetalSSHPort = SSHPort + } - // update ip table - go func() error { - err := clientIPTable.UpdateIpTableListClient() - if err != nil { - fmt.Println(err) - return err - } - return nil - }() + //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) + // write information back to the IP Table + } - if config.ProxyPort != "" { - go ProxyRun(config.ProxyPort) - } + } else { + ProxyIpAddr.Ipv4, err = p2p.CurrentPublicIP() + if err != nil { + fmt.Println(err) + } + ProxyIpAddr.ServerPort = config.ServerPort + ProxyIpAddr.Name = config.MachineName + ProxyIpAddr.NAT = "False" + if config.ProxyPort != "" { + ProxyIpAddr.ProxyServer = "True" + } + ProxyIpAddr.EscapeImplementation = "" + if config.BareMetal { + ProxyIpAddr.BareMetalSSHPort = "22" + } - // Run gin server on the specified port - go r.Run(":" + config.ServerPort) + } - return r, nil + // 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 { + return nil, err + } + + // update ip table + go func() error { + err := clientIPTable.UpdateIpTableListClient() + if err != nil { + fmt.Println(err) + return err + } + return nil + }() + + if config.ProxyPort != "" { + go ProxyRun(config.ProxyPort) + } + + // Run gin server on the specified port + go r.Run(":" + config.ServerPort) + + return r, nil } func MapPort(port string, domainName string) (string, string, error) { - //Get Server port based on the config file - config, err := config.ConfigInit(nil, nil) - if err != nil { - return "", "", err - } + //Get Server port based on the config file + config, err := config.ConfigInit(nil, nil) + if err != nil { + return "", "", err + } - // update IPTable with new port and ip address and update ip table - var ProxyIpAddr p2p.IpAddress - var lowestLatencyIpAddress p2p.IpAddress + // update IPTable with new port and ip address and update ip table + var ProxyIpAddr p2p.IpAddress + var lowestLatencyIpAddress p2p.IpAddress - clientIPTable.RemoveOfflineNodes() + clientIPTable.RemoveOfflineNodes() - table, err := p2p.ReadIpTable() - if err != nil { - return "", "", err - } + table, err := p2p.ReadIpTable() + if err != nil { + return "", "", err + } - var lowestLatency int64 - // random large number - lowestLatency = 10000000 + var lowestLatency int64 + // random large number + lowestLatency = 10000000 - 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 != "" { - // Filter based on nodes with proxy enabled - if domainName != "" && table.IpAddress[i].ProxyServer == "True" { - lowestLatency = table.IpAddress[i].Latency.Milliseconds() - lowestLatencyIpAddress = table.IpAddress[i] - continue - } - lowestLatency = table.IpAddress[i].Latency.Milliseconds() - lowestLatencyIpAddress = table.IpAddress[i] - } - } + 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 != "" { + // Filter based on nodes with proxy enabled + if domainName != "" && table.IpAddress[i].ProxyServer == "True" { + lowestLatency = table.IpAddress[i].Latency.Milliseconds() + lowestLatencyIpAddress = table.IpAddress[i] + continue + } + lowestLatency = table.IpAddress[i].Latency.Milliseconds() + lowestLatencyIpAddress = table.IpAddress[i] + } + } - // If there is an identified node - if lowestLatency != 10000000 { - serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) - if err != nil { - 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 - } + // If there is an identified node + if lowestLatency != 10000000 { + serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) + if err != nil { + 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 + } - // Doing the proxy mapping for the domain name - if domainName != "" { - 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 { - // URL = "http://" + IP + ":" + serverPort + "/server_info" - //} - http.Get(URL) - //SaveRegistration(domainName, lowestLatencyIpAddress.Ipv4+":"+proxyPort) - } + // Doing the proxy mapping for the domain name + if domainName != "" { + 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 { + // URL = "http://" + IP + ":" + serverPort + "/server_info" + //} + http.Get(URL) + //SaveRegistration(domainName, lowestLatencyIpAddress.Ipv4+":"+proxyPort) + } - // updating with the current proxy address - ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 - ProxyIpAddr.ServerPort = proxyPort - ProxyIpAddr.Name = config.MachineName - ProxyIpAddr.NAT = "False" - ProxyIpAddr.EscapeImplementation = "FRP" + // updating with the current proxy address + ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 + ProxyIpAddr.ServerPort = proxyPort + ProxyIpAddr.Name = config.MachineName + ProxyIpAddr.NAT = "False" + ProxyIpAddr.EscapeImplementation = "FRP" - //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) - } else { - return "", "", errors.New("proxy IP not found") - } + //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) + } else { + return "", "", errors.New("proxy IP not found") + } - return ProxyIpAddr.Ipv4 + ":" + ProxyIpAddr.ServerPort, ProxyIpAddr.ServerPort, nil + return ProxyIpAddr.Ipv4 + ":" + ProxyIpAddr.ServerPort, ProxyIpAddr.ServerPort, nil } From 22c8fefd8d6f5e1d0e45cf48073388e29f40b2f7 Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 17:55:43 +0000 Subject: [PATCH 09/11] added base mechanism to add public keys from the propogated from the IPTable --- p2p/speedtest.go | 29 ++++++++++ p2p/ssh_autorisation.go | 123 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 p2p/ssh_autorisation.go diff --git a/p2p/speedtest.go b/p2p/speedtest.go index 4957181..0468b07 100644 --- a/p2p/speedtest.go +++ b/p2p/speedtest.go @@ -1,5 +1,9 @@ package p2p +import ( + "github.com/Akilan1999/p2p-rendering-computation/config" +) + // SpeedTest Runs a speed test and does updates IP tables accordingly func (ip *IpAddresses) SpeedTest() error { @@ -50,11 +54,26 @@ func (ip *IpAddresses) SpeedTest() error { // SpeedTestUpdatedIPTable Called when ip tables from httpclient/server is also passed on func (ip *IpAddresses) SpeedTestUpdatedIPTable() error { + + Config, err := config.ConfigInit(nil, nil) + if err != nil { + return err + } + targets, err := ReadIpTable() if err != nil { return err } + // Checks if baremetal mode and unsafe mode + // is enabled. If it is enabled it adds the + // the propagated public key to the list. + + AddPublicKey := false + if Config.BareMetal && Config.UnsafeMode { + AddPublicKey = true + } + // To ensure struct has no duplicates IP addresses //DoNotRead := targets @@ -64,6 +83,16 @@ func (ip *IpAddresses) SpeedTestUpdatedIPTable() error { //To ensure that there are no duplicate IP addresses Exists := false for k := range ip.IpAddress { + if AddPublicKey && ip.IpAddress[k].PublicKey != "" { + // This function call (AddAuthorisationKey) is inefficient but to be optimised later on. + // This is because when if the user is running on as unsafe mode the authorization file + // is opened from the SSH directory and then iterates through every single SSH entry + // to find out if the SSH entry exists or not. This will incur multiple CPU cycles + // for no reason. A better approach would be to have been to store the states on memory and only + // add when needed based on the memory location. This is something is to be discussed + // and look upon later on. + AddAuthorisationKey(ip.IpAddress[k].PublicKey) + } // 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) { diff --git a/p2p/ssh_autorisation.go b/p2p/ssh_autorisation.go new file mode 100644 index 0000000..89b4a86 --- /dev/null +++ b/p2p/ssh_autorisation.go @@ -0,0 +1,123 @@ +// NOTE: Most of the code snippet was generated using ChatGPT +// Prompt used: "generate go program to read and populate ssh authorization file" + +package p2p + +import ( + "bufio" + "errors" + "fmt" + "os" + "path/filepath" + "strings" +) + +// GetAuthorizedKeysPath returns the path to the authorized_keys file +func GetAuthorizedKeysPath() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("could not find home directory: %v", err) + } + return filepath.Join(homeDir, ".ssh", "authorized_keys"), nil +} + +// ReadAuthorizedKeys reads and returns the current contents of the authorized_keys file as a map +func ReadAuthorizedKeys(path string) (map[string]bool, error) { + file, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("could not open authorized_keys file: %v", err) + } + defer file.Close() + + keys := make(map[string]bool) + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + // Skip empty lines and comments + if line != "" && !strings.HasPrefix(line, "#") { + keys[line] = true + } + } + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("error reading authorized_keys file: %v", err) + } + return keys, nil +} + +// AddKeyToAuthorizedKeys adds a new key to the authorized_keys file if it doesn’t already exist +func AddKeyToAuthorizedKeys(path, newKey string) error { + keys, err := ReadAuthorizedKeys(path) + if err != nil { + return err + } + + // Check if the key already exists in the map + if keys[newKey] { + return errors.New("key already exists in authorized_keys") + } + + // Append the new key + file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + return fmt.Errorf("could not open authorized_keys file for writing: %v", err) + } + defer file.Close() + + if _, err := file.WriteString(newKey + "\n"); err != nil { + return fmt.Errorf("could not write to authorized_keys file: %v", err) + } + return nil +} + +func RemoveKeyFromAuthorizedKeys(path, keyToRemove string) error { + keys, err := ReadAuthorizedKeys(path) + if err != nil { + return err + } + + // Check if the key exists in the map + if !keys[keyToRemove] { + return errors.New("key not found in authorized_keys") + } + + // Delete the key from the map + delete(keys, keyToRemove) + + // Write updated keys back to the authorized_keys file + file, err := os.OpenFile(path, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + return fmt.Errorf("could not open authorized_keys file for writing: %v", err) + } + defer file.Close() + + for key := range keys { + if _, err := file.WriteString(key + "\n"); err != nil { + return fmt.Errorf("could not write to authorized_keys file: %v", err) + } + } + + return nil +} + +// AddAuthorisationKey Adds public key provided to the +// authorization file so that nodes can SSH into +// the +func AddAuthorisationKey(PublicKey string) error { + path, err := GetAuthorizedKeysPath() + if err != nil { + return err + } + + // Display existing keys + _, err = ReadAuthorizedKeys(path) + if err != nil { + return err + } + + err = AddKeyToAuthorizedKeys(path, PublicKey) + if err != nil { + return err + } + + return nil +} From 277259a65aa421cd786ac81bc4ab8c9e329dd5c6 Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 13 Nov 2024 19:16:18 +0000 Subject: [PATCH 10/11] removed terminal channel check error prone --- main.go | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 3607d92..fb85b0f 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,13 @@ package main import ( - "log" - "os" + "log" + "os" + "os/signal" + "syscall" - "github.com/Akilan1999/p2p-rendering-computation/cmd" - "github.com/urfave/cli/v2" + "github.com/Akilan1999/p2p-rendering-computation/cmd" + "github.com/urfave/cli/v2" ) // VERSION specifies the version of the platform @@ -16,16 +18,36 @@ var mode string var OS, Pull_location, Run_script string var List_servers, Ip_table bool -func main() { - app := cli.NewApp() - app.Name = "p2p-rendering-computation" - app.Usage = "p2p cli application to create and access VMs in other servers" - app.Version = VERSION - app.Flags = cmd.AppConfigFlags - app.Action = cmd.CliAction +// To be implemented later on +func getFireSignalsChannel() chan os.Signal { + + c := make(chan os.Signal, 1) + signal.Notify(c, + // https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html + syscall.SIGTERM, // "the normal way to politely ask a program to terminate" + syscall.SIGINT, // Ctrl+C + syscall.SIGQUIT, // Ctrl-\ + syscall.SIGKILL, // "always fatal", "SIGKILL and SIGSTOP may not be caught by a program" + syscall.SIGHUP, // "terminal is disconnected" + ) + return c - err := app.Run(os.Args) - if err != nil { - log.Fatal(err) - } +} + +func exit() { + syscall.Kill(syscall.Getpid(), syscall.SIGTERM) +} + +func main() { + app := cli.NewApp() + app.Name = "p2p-rendering-computation" + app.Usage = "p2p cli application to create and access VMs in other servers" + app.Version = VERSION + app.Flags = cmd.AppConfigFlags + app.Action = cmd.CliAction + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } } From de5042294f46da00cca87c39a05e24d5de0c9f71 Mon Sep 17 00:00:00 2001 From: Akilan Selvacoumar <31743758+Akilan1999@users.noreply.github.com> Date: Mon, 25 Nov 2024 14:48:25 +0000 Subject: [PATCH 11/11] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6a810ee..1a17640 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ > Fixing documentation to latest changes. If you have any questions setting up P2PRC either [create an issue](https://github.com/Akilan1999/p2p-rendering-computation/issues/new/choose) or send me an email (me AT akilan dot io). > Currently HEAD is always intended to stay on a working state. It is recommended to always use HEAD in your go.mod file. +> [!WARNING] +> The root node of P2PRC is down. We are working on creating a new root node with more stable performance. The new root node will be added on the 27th of November. +


p2prc