From 4deff38c072229f1aeeb80a4893b28f6f31eca0d Mon Sep 17 00:00:00 2001 From: Akilan Date: Thu, 29 Dec 2022 03:40:36 +0000 Subject: [PATCH] added fix for reverse proxy --- client/ServerSpecs.go | 31 ++++++------- client/container.go | 103 +++++++++++++++++++++--------------------- p2p/frp/client.go | 14 ++++-- server/server.go | 2 + 4 files changed, 78 insertions(+), 72 deletions(-) diff --git a/client/ServerSpecs.go b/client/ServerSpecs.go index 4a5e9cb..35c577d 100644 --- a/client/ServerSpecs.go +++ b/client/ServerSpecs.go @@ -3,7 +3,6 @@ package client import ( "encoding/json" "fmt" - "git.sr.ht/~akilan1999/p2p-rendering-computation/p2p" "git.sr.ht/~akilan1999/p2p-rendering-computation/server" "io/ioutil" "net/http" @@ -12,30 +11,30 @@ import ( // GetSpecs Gets Specs from the server such CPU, GPU usage // and other basic information which helps set a // cluster of computer -func GetSpecs(IP string)(*server.SysInfo,error) { +func GetSpecs(IP string) (*server.SysInfo, error) { var URL string - version := p2p.Ip4or6(IP) + //version := p2p.Ip4or6(IP) //Get port number of the server - serverPort, err := GetServerPort(IP) - if err != nil { - return nil, err - } + //serverPort, err := GetServerPort(IP) + //if err != nil { + // return nil, err + //} - if version == "version 6" { - URL = "http://[" + IP + "]:" + serverPort + "/server_info" - } else { - URL = "http://" + IP + ":" + serverPort + "/server_info" - } + //if version == "version 6" { + URL = "http://" + IP + "/server_info" + //} else { + // URL = "http://" + IP + ":" + serverPort + "/server_info" + //} resp, err := http.Get(URL) if err != nil { - return nil,err + return nil, err } // Convert response to byte value byteValue, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil,err + return nil, err } // Create variable for result response type @@ -44,7 +43,7 @@ func GetSpecs(IP string)(*server.SysInfo,error) { // Adds byte value to docker.DockerVM struct json.Unmarshal(byteValue, &serverSpecsResult) if err != nil { - return nil,err + return nil, err } return &serverSpecsResult, nil @@ -61,4 +60,4 @@ func PrettyPrint(data interface{}) { return } fmt.Printf("%s \n", p) -} \ No newline at end of file +} diff --git a/client/container.go b/client/container.go index c4d979d..35edd09 100644 --- a/client/container.go +++ b/client/container.go @@ -12,39 +12,39 @@ import ( var ( serverPort = "8088" - client = http.Client{} + client = http.Client{} ) // StartContainer Start container using REST api Implementation // From the selected server IP address // TODO: Test cases for this function -//Calls URL ex: http://0.0.0.0:8088/startcontainer?ports=0&GPU=false&ContainerName=docker-ubuntu-sshd -func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*docker.DockerVM ,error) { +// Calls URL ex: http://0.0.0.0:8088/startcontainer?ports=0&GPU=false&ContainerName=docker-ubuntu-sshd +func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*docker.DockerVM, error) { // Passes URL with number of TCP ports to allocated and to give GPU access to the docker container var URL string - version := p2p.Ip4or6(IP) + //version := p2p.Ip4or6(IP) + // + ////Get port number of the server + //serverPort, err := GetServerPort(IP) + //if err != nil { + // return nil, err + //} - //Get port number of the server - serverPort, err := GetServerPort(IP) - if err != nil { - return nil, err - } - - if version == "version 6" { - URL = "http://[" + IP + "]:" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName - } else { - URL = "http://" + IP + ":" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName - } + //if version == "version 6" { + // URL = "http://[" + IP + "]:" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName + //} else { + URL = "http://" + IP + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName + //} resp, err := http.Get(URL) if err != nil { - return nil,err + return nil, err } // Convert response to byte value byteValue, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil,err + return nil, err } // Create variable for result response type @@ -53,7 +53,7 @@ func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*d // Adds byte value to docker.DockerVM struct json.Unmarshal(byteValue, &dockerResult) if err != nil { - return nil,err + return nil, err } // Adds the container to the tracked list @@ -66,21 +66,21 @@ func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*d } // RemoveContianer Stops and removes container from the server -func RemoveContianer(IP string,ID string) error { +func RemoveContianer(IP string, ID string) error { var URL string - version := p2p.Ip4or6(IP) + //version := p2p.Ip4or6(IP) + // + ////Get port number of the server + //serverPort, err := GetServerPort(IP) + //if err != nil { + // return err + //} - //Get port number of the server - serverPort, err := GetServerPort(IP) - if err != nil { - return err - } - - if version == "version 6" { - URL = "http://[" + IP + "]:" + serverPort + "/RemoveContainer?id=" + ID - } else { - URL = "http://" + IP + ":" + serverPort + "/RemoveContainer?id=" + ID - } + //if version == "version 6" { + // URL = "http://[" + IP + "]:" + serverPort + "/RemoveContainer?id=" + ID + //} else { + URL = "http://" + IP + "/RemoveContainer?id=" + ID + //} resp, err := http.Get(URL) if err != nil { return err @@ -102,7 +102,7 @@ func RemoveContianer(IP string,ID string) error { if err != nil { return err } - // Remove container created from the tracked list + // Remove container created from the tracked list err = RemoveTrackedContainer(ID) if err != nil { return err @@ -112,31 +112,31 @@ func RemoveContianer(IP string,ID string) error { } // ViewContainers This function displays all containers available on server side -func ViewContainers(IP string)(*docker.DockerContainers, error){ +func ViewContainers(IP string) (*docker.DockerContainers, error) { // Passes URL with route /ShowImages var URL string - version := p2p.Ip4or6(IP) + //version := p2p.Ip4or6(IP) + // + ////Get port number of the server + //serverPort, err := GetServerPort(IP) + //if err != nil { + // return nil, err + //} - //Get port number of the server - serverPort, err := GetServerPort(IP) - if err != nil { - return nil, err - } - - if version == "version 6" { - URL = "http://[" + IP + "]:" + serverPort + "/ShowImages" - } else { - URL = "http://" + IP + ":" + serverPort + "/ShowImages" - } + //if version == "version 6" { + // URL = "http://[" + IP + "]:" + serverPort + "/ShowImages" + //} else { + URL = "http://" + IP + "/ShowImages" + //} resp, err := http.Get(URL) if err != nil { - return nil,err + return nil, err } // Convert response to byte value byteValue, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil,err + return nil, err } // Create variable for result response type @@ -145,18 +145,18 @@ func ViewContainers(IP string)(*docker.DockerContainers, error){ // Adds byte value to docker.DockerContainers struct json.Unmarshal(byteValue, &Result) if err != nil { - return nil,err + return nil, err } return &Result, nil } // GetServerPort Helper function to do find out server port information -func GetServerPort(IpAddress string) (string,error) { +func GetServerPort(IpAddress string) (string, error) { // Getting information from the clients ip table ipTable, err := p2p.ReadIpTable() if err != nil { - return "",err + return "", err } // Iterate thorough ip table struct and find @@ -170,7 +170,7 @@ func GetServerPort(IpAddress string) (string,error) { } // We return default port - return "8088",nil + return "8088", nil } // PrintStartContainer Prints results Generated container @@ -187,7 +187,6 @@ func GetServerPort(IpAddress string) (string,error) { // } //} - // TODO implementation using RPC calls //func StartContainer(Ip string) (*docker.DockerVM,error){ // client, err := rpc.DialHTTP("tcp",Ip + ":" + serverPort) diff --git a/p2p/frp/client.go b/p2p/frp/client.go index 62a9013..f0ec2f3 100644 --- a/p2p/frp/client.go +++ b/p2p/frp/client.go @@ -4,6 +4,7 @@ import ( "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" "github.com/fatedier/frp/client" "github.com/fatedier/frp/pkg/config" + "github.com/phayes/freeport" "math/rand" "strconv" ) @@ -50,19 +51,23 @@ func StartFRPClientForServer(ipaddress string, port string, localport string) (s } //random port - randPort := rangeIn(10000, 99999) + //randPort := rangeIn(10000, 99999) + OpenPorts, err := freeport.GetFreePorts(1) + if err != nil { + return "", err + } c.ClientMappings = []ClientMapping{ { - LocalIP: ipaddress, + LocalIP: "localhost", LocalPort: portInt, - RemotePort: randPort, + RemotePort: OpenPorts[0], }, } // Start client server go c.StartFRPClient() - return strconv.Itoa(randPort), nil + return strconv.Itoa(OpenPorts[0]), nil } @@ -88,6 +93,7 @@ func StartFRPCDockerContainer(ipaddress string, port string, docker *docker.Dock // Set client mapping var clientMapping ClientMapping portMap := docker.Ports.PortSet[i].ExternalPort + clientMapping.LocalIP = "localhost" clientMapping.LocalPort = portMap clientMapping.RemotePort = portMap // Append to array diff --git a/server/server.go b/server/server.go index 4c7cc98..a3d8d55 100644 --- a/server/server.go +++ b/server/server.go @@ -183,6 +183,8 @@ func Server() error { return err } + fmt.Println(proxyPort) + // updating with the current proxy address ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 ProxyIpAddr.ServerPort = proxyPort