diff --git a/client/Iptable.go b/client/Iptable.go index 7195de6..c8bc76b 100644 --- a/client/Iptable.go +++ b/client/Iptable.go @@ -1,34 +1,42 @@ package client import ( - "fmt" + "bytes" + "encoding/json" "git.sr.ht/~akilan1999/p2p-rendering-computation/p2p" - "net/url" + "io" + "io/ioutil" + "mime/multipart" + "net/http" + "os" + "path/filepath" ) // Does the following to update it's IP table func UpdateIpTable(IpAddress string) error { - // Gets information from IP table - Addresses, err := p2p.ReadIpTable() + + resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable", + "/etc/p2p-rendering/ip_table.json", + "json") + if err != nil { return err } - u, err := url.Parse("http://" + IpAddress + ":8088/IpTable") + var ipStruct p2p.IpAddresses + json.Unmarshal(resp, &ipStruct) + + // Updates IP table based on information provided + // by the server + err = ipStruct.SpeedTestUpdatedIPTable() if err != nil { - panic(err) + return err } - params := url.Values{} - params.Add("data", fmt.Sprint(Addresses)) - u.RawQuery = params.Encode() - - fmt.Print(u.RawQuery) - return nil } -//updates IP tables (Default 3 hops) based on server information avaliable +//updates IP tables (Default 3 hops) based on server information available //on the ip tables func UpdateIpTableListClient() error { // Ensure that the IP Table has Node pingable @@ -49,11 +57,11 @@ func UpdateIpTableListClient() error { } // Updates IP table based on server IP table - for j, _ := range Addresses.IpAddress { + for j := range Addresses.IpAddress { // Check if IP addresses is there in the struct DoNotRead Exists := false - for k, _ := range DoNotRead.IpAddress { + for k := range DoNotRead.IpAddress { if DoNotRead.IpAddress[k].Ipv4 == Addresses.IpAddress[j].Ipv4 { Exists = true break @@ -77,3 +85,48 @@ func UpdateIpTableListClient() error { return nil } + +// Sends a file as a POST request. +// Reference (https://stackoverflow.com/questions/51234464/upload-a-file-with-post-request-golang) +func SendPostRequest (url string, filename string, filetype string) ([]byte,error) { + file, err := os.Open(filename) + if err != nil { + return nil,err + } + defer file.Close() + + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + part, err := writer.CreateFormFile(filetype, filepath.Base(file.Name())) + + if err != nil { + return nil,err + } + + io.Copy(part, file) + writer.Close() + request, err := http.NewRequest("POST", url, body) + + if err != nil { + return nil,err + } + + request.Header.Add("Content-Type", writer.FormDataContentType()) + client := &http.Client{} + + response, err := client.Do(request) + + if err != nil { + return nil,err + } + defer response.Body.Close() + + content, err := ioutil.ReadAll(response.Body) + + if err != nil { + return nil,err + } + + return content, nil +} \ No newline at end of file diff --git a/docs/Client.md b/docs/Client.md deleted file mode 100644 index f8099e1..0000000 --- a/docs/Client.md +++ /dev/null @@ -1,29 +0,0 @@ -# Client Module -This module is incharge of communicating with the server and receiving the appropriate information back from the server. -Note: To [read more about the functions](https://pkg.go.dev/git.sr.ht/~akilan1999/p2p-rendering-computation@v0.0.0-20210404191839-6a046babcb02/client) - -## Functions of the Client Module -- [Interact with the Server Api](#functions-of-the-client-module) -- [Decision maker on how the ip table is created or updated](#decision-maker-on-how-the-ip-table-is-created-or-updated) - - -## Interact with the Server Api -This sections talks about the functionality implemented till now. -- The client can start docker containers using the function StartContainer. -This functions calls the route: -``` -http://:/startcontainer -``` -TODO: Outputs and how it's printed - -## Decision maker on how the IP table is created or updated -- Does a local speedtest to verify and see if the server IP's in the IP table -are pingable. -- Downloads the Servers IP table. -- Tries to ping the servers IP Table addresses. -- If it's pingable then it's added as a new entry in the IP table. -- The following steps occurs in the clients IP table. -- To ensure that the same servers are not being called to update the IP table. There is -a temporary list of IP address which have already been called in relation to updating the -IP table. -- Based on the current implementation there will 3 hops done to update the IP table. diff --git a/docs/DesignVIrtualization b/docs/DesignVIrtualization deleted file mode 100644 index 2b931e0..0000000 --- a/docs/DesignVIrtualization +++ /dev/null @@ -1,11 +0,0 @@ -Virtualization Design -====================== - -The virtualization tool will be treated as a separate module. In our implementation -we will use docker as it's easier to configure and set. - -Methods to be created -- Build OS Image from DockerFile -- Run Image Built -- Possibility to kill image by server admin or client side user. -- Track stats of the docker container by server admin or client side user. diff --git a/docs/Installation.md b/docs/Installation.md deleted file mode 100644 index db0067a..0000000 --- a/docs/Installation.md +++ /dev/null @@ -1,60 +0,0 @@ -# Installation - -Over here we will cover the basic steps to get the server and client side running. - -### Install Go lang -The entire the implementation of this project is done using Go lang. -Thus, we need go lang to compile to code to a binary file. -[Instructions to install Go lang](https://golang.org/doc/install) -### Install Docker -In this project the choice of virtualization is Docker due to it's wide usage -in the developer community. In the server module we use the Docker Go API to create and -interact with the containers. - -[Instructions to install docker](https://docs.docker.com/get-docker/) -```` -Do ensure that the docker command does not need sudo to run -```` - -### Build Project -To set up the internal dependencies and build the entire go code -into a single binary -``` -make build -``` - -### Test if binary works -``` -./p2p-rendering-computation --help -``` -#### Output: -``` -NAME: - p2p-rendering-computation - p2p cli application to create and access VMs in other servers - -USAGE: - p2p-rendering-computation [global options] command [command options] [arguments...] - -VERSION: - 1.0.0 - -COMMANDS: - help, h Shows a list of commands or help for one command - -GLOBAL OPTIONS: - --Mode value Specifies mode of running (default: "client") [$P2P_MODE] - --ListServers List servers which can render tasks (default: false) [$LIST_SERVERS] - --CreateVM value Creates Docker container on the selected server [$CREATE_VM] - --FilePath Testing for absolute path (default: false) - --help, -h show help (default: false) - --version, -v print the version (default: false) - -``` - -### Add command to .bashrc or .zshrc -``` -export PATH=~//p2p-redering-computation:${PATH} -``` - - -#### Note: The steps on how to use the commands will be added later. \ No newline at end of file diff --git a/docs/P2P.md b/docs/P2P.md deleted file mode 100644 index e6a33b3..0000000 --- a/docs/P2P.md +++ /dev/null @@ -1,29 +0,0 @@ -# P2P (Peer to Peer module) -In this repository the P2P module has been designed from sratch at the point of this implementation. -[More about function implementation](https://pkg.go.dev/git.sr.ht/~akilan1999/p2p-rendering-computation@v0.0.0-20210404191839-6a046babcb02/p2p) - -## Terminology - 1. IPTable: Refers to a json file which stores information about the current servers avaliable with the speedtest results ran from the Node that triggered it. - ``` - { - "ip_address": [ - { - "ipv4": "localhost", - "latency": 14981051, - "download": 8142.122540206258, - "upload": 3578.766512629995, - } - ] -} - ``` - - -## Responsibility -- To perform speed test to determine best node to connect -- To ensure the IP table has nodes which are pingable -- Using techniques such as [UPNP](https://en.wikipedia.org/wiki/Universal_Plug_and_Play). Still under development -- Port Forwarding (To be introducted in a future release) - - -## Note: -If you are running in server mode it is recommended to use [DMZ](https://routerguide.net/when-and-how-to-setup-dmz-host-for-home-use/) to bypass the [NAT](https://en.wikipedia.org/wiki/Network_address_translation). diff --git a/docs/Problems.md b/docs/Problems.md deleted file mode 100644 index 0320f61..0000000 --- a/docs/Problems.md +++ /dev/null @@ -1,35 +0,0 @@ -# Problems in implementation - -### Number of Devices in the network: -The current implementation has the major flaw which is that -the server has to be in port 8088 to detected in the public -network. As we know most personal networks have a single IP -address. This means we cannot have duplicate ports. A fix can -be to mention the open port on the IP table file. -(Ex: Possible feild to be added) -``` -{ - "ip_address": [ - { - "ipv4": "localhost", - "latency": 14981051, - "download": 8142.122540206258, - "upload": 3578.766512629995, - "port": 8088 - } - ] -} -``` - -### Broadcast of container specs -At the moment the container specs are not broadcasted rather -just the machines specs and this module has yet to be tested -rigorously. - -### Intergration with GPU -Certain machines have GPUs present in them which provide a -huge advantage for those who want to do rendering and certain -sort of computation. The Aim is to only allow it to be compatible -with Nvidia. But an better idea would be to provide compatability -with multiple GPU providers. - diff --git a/docs/README.md b/docs/README.md deleted file mode 100644 index ca51cb8..0000000 --- a/docs/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# Table of contents -1. [Architecture](#NotImplemented) - 1. [Client](Client.md) - 2. [P2P](P2P.md) - 3. [Server](#NotImplemented) -2. [Installation](Installation.md) -3. [Problems](Problems.md) diff --git a/p2p/speedtest.go b/p2p/speedtest.go index 8eabb56..2e5403e 100644 --- a/p2p/speedtest.go +++ b/p2p/speedtest.go @@ -6,7 +6,6 @@ import "fmt" func (ip *IpAddresses)SpeedTest() error{ for i, _ := range ip.IpAddress { - // Ping Test err := ip.IpAddress[i].PingTest() if err != nil { @@ -40,13 +39,29 @@ func (ip *IpAddresses)SpeedTest() error{ // Called when ip tables from client/server is also passed on func (ip *IpAddresses)SpeedTestUpdatedIPTable() error{ targets, err := ReadIpTable() - if err != nil { return err } + var DoNotRead IpAddresses // Appends all IP addresses for i, _ := range targets.IpAddress { + + Exists := false + for k := range DoNotRead.IpAddress { + if DoNotRead.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 { + Exists = true + break + } + } + + // If the struct exists then continues + if Exists { + continue + } + + fmt.Print("lol") + ip.IpAddress = append(ip.IpAddress, targets.IpAddress[i]) } @@ -73,3 +88,4 @@ func LocalSpeedTestIpTable() error { return nil } + diff --git a/server/server.go b/server/server.go index 2e8b12a..b81c154 100644 --- a/server/server.go +++ b/server/server.go @@ -1,15 +1,17 @@ package server import ( + "encoding/json" "fmt" "git.sr.ht/~akilan1999/p2p-rendering-computation/p2p" "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" "github.com/gin-gonic/gin" + "io/ioutil" "net/http" //"fmt" ) -func Server() { +func Server() error{ r := gin.Default() // Gets default information of the server @@ -33,15 +35,33 @@ func Server() { }) //Gets Ip Table from server node - r.GET("/IpTable", func(c *gin.Context) { + r.POST("/IpTable", func(c *gin.Context) { - //jsonData, err := ioutil.ReadAll(c.Request.Body) - //if err != nil { - // c.String(http.StatusOK, fmt.Sprint(err)) - //} + // 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) // Runs speed test to return only servers in the IP table pingable - err := p2p.LocalSpeedTestIpTable() + err = IPTable.SpeedTestUpdatedIPTable() if err != nil { c.String(http.StatusOK, fmt.Sprint(err)) } @@ -86,5 +106,10 @@ func Server() { })*/ // Port running on - r.Run(":8088") + err := r.Run(":8088") + if err != nil { + return err + } + + return nil }