changed way network learns about nodes
This commit is contained in:
@@ -1,239 +0,0 @@
|
|||||||
package client
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"mime/multipart"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// UpdateIpTable Does the following to update it's IP table
|
|
||||||
func UpdateIpTable(IpAddress string, serverPort string) error {
|
|
||||||
|
|
||||||
config, err := config.ConfigInit()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
client := http.Client{}
|
|
||||||
|
|
||||||
var resp []byte
|
|
||||||
|
|
||||||
version := p2p.Ip4or6(IpAddress)
|
|
||||||
if version == "version 6" {
|
|
||||||
resp, err = UploadMultipartFile(client, "http://["+IpAddress+"]:"+serverPort+"/IpTable", "json", config.IPTable)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
resp, err = UploadMultipartFile(client, "http://"+IpAddress+":"+serverPort+"/IpTable", "json", config.IPTable)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
|
|
||||||
// config.IPTable,
|
|
||||||
// "json")
|
|
||||||
//
|
|
||||||
//if err != nil {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
|
|
||||||
var ipStruct p2p.IpAddresses
|
|
||||||
json.Unmarshal(resp, &ipStruct)
|
|
||||||
|
|
||||||
// Updates IP table based on information provided
|
|
||||||
// by the server
|
|
||||||
if len(ipStruct.IpAddress) > 0 {
|
|
||||||
err = ipStruct.SpeedTestUpdatedIPTable()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateIpTableListClient 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
|
|
||||||
err := p2p.LocalSpeedTestIpTable()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// IP addresses to not append to struct due to
|
|
||||||
// duplication
|
|
||||||
|
|
||||||
Addresses, err := p2p.ReadIpTable()
|
|
||||||
//var DoNotRead p2p.IpAddresses
|
|
||||||
|
|
||||||
currentIPV4, err := p2p.CurrentPublicIP()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run loop 1 times
|
|
||||||
for i := 0; i < 1; i++ {
|
|
||||||
// Gets information from IP table
|
|
||||||
//Addresses, err = p2p.ReadIpTable()
|
|
||||||
//if err != nil {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, PublicIP)
|
|
||||||
|
|
||||||
// Updates IP table based on server IP table
|
|
||||||
for j := range Addresses.IpAddress {
|
|
||||||
|
|
||||||
//Exists := false
|
|
||||||
//
|
|
||||||
//if PublicIP.Ipv4 == Addresses.IpAddress[j].Ipv4 || (PublicIP.Ipv6 != "" && Addresses.IpAddress[j].Ipv6 == PublicIP.Ipv6){
|
|
||||||
// Exists = true
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// Check if IP addresses is there in the struct DoNotRead
|
|
||||||
//for k := range DoNotRead.IpAddress {
|
|
||||||
// if DoNotRead.IpAddress[k].Ipv4 == Addresses.IpAddress[j].Ipv4 || (DoNotRead.IpAddress[k].Ipv6 != "" && Addresses.IpAddress[j].Ipv6 == DoNotRead.IpAddress[k].Ipv6) {
|
|
||||||
// Exists = true
|
|
||||||
// break
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// If the struct exists then continues
|
|
||||||
//if Exists {
|
|
||||||
// continue
|
|
||||||
//}
|
|
||||||
|
|
||||||
if Addresses.IpAddress[j].Ipv6 != "" {
|
|
||||||
err = UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort)
|
|
||||||
} else if Addresses.IpAddress[j].Ipv4 != currentIPV4 {
|
|
||||||
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Appends server1 IP address to variable DoNotRead
|
|
||||||
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendPostRequest 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
|
|
||||||
//}
|
|
||||||
|
|
||||||
func UploadMultipartFile(client http.Client, uri, key, path string) ([]byte, error) {
|
|
||||||
body, writer := io.Pipe()
|
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, uri, body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
mwriter := multipart.NewWriter(writer)
|
|
||||||
req.Header.Add("Content-Type", mwriter.FormDataContentType())
|
|
||||||
|
|
||||||
errchan := make(chan error)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(errchan)
|
|
||||||
defer writer.Close()
|
|
||||||
defer mwriter.Close()
|
|
||||||
|
|
||||||
w, err := mwriter.CreateFormFile(key, path)
|
|
||||||
if err != nil {
|
|
||||||
errchan <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
in, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
errchan <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer in.Close()
|
|
||||||
|
|
||||||
if written, err := io.Copy(w, in); err != nil {
|
|
||||||
errchan <- fmt.Errorf("error copying %s (%d bytes written): %v", path, written, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := mwriter.Close(); err != nil {
|
|
||||||
errchan <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
content, err := ioutil.ReadAll(resp.Body)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return content, nil
|
|
||||||
|
|
||||||
}
|
|
||||||
239
client/clientIPTable/Iptable.go
Normal file
239
client/clientIPTable/Iptable.go
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
package clientIPTable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
||||||
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// UpdateIpTable Does the following to update it's IP table
|
||||||
|
func UpdateIpTable(IpAddress string, serverPort string) error {
|
||||||
|
|
||||||
|
config, err := config.ConfigInit()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
client := http.Client{}
|
||||||
|
|
||||||
|
var resp []byte
|
||||||
|
|
||||||
|
version := p2p.Ip4or6(IpAddress)
|
||||||
|
if version == "version 6" {
|
||||||
|
resp, err = UploadMultipartFile(client, "http://["+IpAddress+"]:"+serverPort+"/IpTable", "json", config.IPTable)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resp, err = UploadMultipartFile(client, "http://"+IpAddress+":"+serverPort+"/IpTable", "json", config.IPTable)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
|
||||||
|
// config.IPTable,
|
||||||
|
// "json")
|
||||||
|
//
|
||||||
|
//if err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
|
|
||||||
|
var ipStruct p2p.IpAddresses
|
||||||
|
json.Unmarshal(resp, &ipStruct)
|
||||||
|
|
||||||
|
// Updates IP table based on information provided
|
||||||
|
// by the server
|
||||||
|
if len(ipStruct.IpAddress) > 0 {
|
||||||
|
err = ipStruct.SpeedTestUpdatedIPTable()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateIpTableListClient 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
|
||||||
|
err := p2p.LocalSpeedTestIpTable()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// IP addresses to not append to struct due to
|
||||||
|
// duplication
|
||||||
|
|
||||||
|
Addresses, err := p2p.ReadIpTable()
|
||||||
|
//var DoNotRead p2p.IpAddresses
|
||||||
|
|
||||||
|
currentIPV4, err := p2p.CurrentPublicIP()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run loop 1 times
|
||||||
|
for i := 0; i < 1; i++ {
|
||||||
|
// Gets information from IP table
|
||||||
|
//Addresses, err = p2p.ReadIpTable()
|
||||||
|
//if err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, PublicIP)
|
||||||
|
|
||||||
|
// Updates IP table based on server IP table
|
||||||
|
for j := range Addresses.IpAddress {
|
||||||
|
|
||||||
|
//Exists := false
|
||||||
|
//
|
||||||
|
//if PublicIP.Ipv4 == Addresses.IpAddress[j].Ipv4 || (PublicIP.Ipv6 != "" && Addresses.IpAddress[j].Ipv6 == PublicIP.Ipv6){
|
||||||
|
// Exists = true
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//// Check if IP addresses is there in the struct DoNotRead
|
||||||
|
//for k := range DoNotRead.IpAddress {
|
||||||
|
// if DoNotRead.IpAddress[k].Ipv4 == Addresses.IpAddress[j].Ipv4 || (DoNotRead.IpAddress[k].Ipv6 != "" && Addresses.IpAddress[j].Ipv6 == DoNotRead.IpAddress[k].Ipv6) {
|
||||||
|
// Exists = true
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//// If the struct exists then continues
|
||||||
|
//if Exists {
|
||||||
|
// continue
|
||||||
|
//}
|
||||||
|
|
||||||
|
if Addresses.IpAddress[j].Ipv6 != "" {
|
||||||
|
err = UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort)
|
||||||
|
} else if Addresses.IpAddress[j].Ipv4 != currentIPV4 {
|
||||||
|
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Appends server1 IP address to variable DoNotRead
|
||||||
|
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendPostRequest 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
|
||||||
|
//}
|
||||||
|
|
||||||
|
func UploadMultipartFile(client http.Client, uri, key, path string) ([]byte, error) {
|
||||||
|
body, writer := io.Pipe()
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, uri, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mwriter := multipart.NewWriter(writer)
|
||||||
|
req.Header.Add("Content-Type", mwriter.FormDataContentType())
|
||||||
|
|
||||||
|
errchan := make(chan error)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(errchan)
|
||||||
|
defer writer.Close()
|
||||||
|
defer mwriter.Close()
|
||||||
|
|
||||||
|
w, err := mwriter.CreateFormFile(key, path)
|
||||||
|
if err != nil {
|
||||||
|
errchan <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
in, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
errchan <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer in.Close()
|
||||||
|
|
||||||
|
if written, err := io.Copy(w, in); err != nil {
|
||||||
|
errchan <- fmt.Errorf("error copying %s (%d bytes written): %v", path, written, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := mwriter.Close(); err != nil {
|
||||||
|
errchan <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return content, nil
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package client
|
package clientIPTable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -11,4 +11,3 @@ func TestUpdateIpTableListClient(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3,6 +3,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/client"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/client"
|
||||||
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/client/clientIPTable"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/generate"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/generate"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
||||||
@@ -22,7 +23,7 @@ var CliAction = func(ctx *cli.Context) error {
|
|||||||
|
|
||||||
//Listing servers and also updates IP tables (Default 3 hops)
|
//Listing servers and also updates IP tables (Default 3 hops)
|
||||||
if UpdateServerList {
|
if UpdateServerList {
|
||||||
err := client.UpdateIpTableListClient()
|
err := clientIPTable.UpdateIpTableListClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Print(err)
|
fmt.Print(err)
|
||||||
}
|
}
|
||||||
|
|||||||
373
server/server.go
373
server/server.go
@@ -1,231 +1,232 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/client/clientIPTable"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p/frp"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p/frp"
|
||||||
"github.com/gin-gonic/gin"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"io/ioutil"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"io/ioutil"
|
||||||
"strconv"
|
"net/http"
|
||||||
"time"
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Server() error {
|
func Server() error {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
// update IPTable with new port and ip address and update ip table
|
// update IPTable with new port and ip address and update ip table
|
||||||
var ProxyIpAddr p2p.IpAddress
|
var ProxyIpAddr p2p.IpAddress
|
||||||
var lowestLatencyIpAddress p2p.IpAddress
|
var lowestLatencyIpAddress p2p.IpAddress
|
||||||
|
|
||||||
// Gets default information of the server
|
// Gets default information of the server
|
||||||
r.GET("/server_info", func(c *gin.Context) {
|
r.GET("/server_info", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, ServerInfo())
|
c.JSON(http.StatusOK, ServerInfo())
|
||||||
})
|
})
|
||||||
|
|
||||||
// Speed test with 50 mbps
|
// Speed test with 50 mbps
|
||||||
r.GET("/50", func(c *gin.Context) {
|
r.GET("/50", func(c *gin.Context) {
|
||||||
// Get Path from config
|
// Get Path from config
|
||||||
config, err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusOK, fmt.Sprint(err))
|
c.String(http.StatusOK, fmt.Sprint(err))
|
||||||
}
|
}
|
||||||
c.File(config.SpeedTestFile)
|
c.File(config.SpeedTestFile)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Route build to do a speed test
|
// Route build to do a speed test
|
||||||
r.GET("/upload", func(c *gin.Context) {
|
r.GET("/upload", func(c *gin.Context) {
|
||||||
file, _ := c.FormFile("file")
|
file, _ := c.FormFile("file")
|
||||||
|
|
||||||
// Upload the file to specific dst.
|
// Upload the file to specific dst.
|
||||||
// c.SaveUploadedFile(file, 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
|
//Gets Ip Table from server node
|
||||||
r.POST("/IpTable", func(c *gin.Context) {
|
r.POST("/IpTable", func(c *gin.Context) {
|
||||||
// Getting IPV4 address of client
|
// Getting IPV4 address of client
|
||||||
var ClientHost p2p.IpAddress
|
var ClientHost p2p.IpAddress
|
||||||
|
|
||||||
if p2p.Ip4or6(c.ClientIP()) == "version 6" {
|
if p2p.Ip4or6(c.ClientIP()) == "version 6" {
|
||||||
ClientHost.Ipv6 = c.ClientIP()
|
ClientHost.Ipv6 = c.ClientIP()
|
||||||
} else {
|
} else {
|
||||||
ClientHost.Ipv4 = c.ClientIP()
|
ClientHost.Ipv4 = c.ClientIP()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variable to store IP table information
|
// Variable to store IP table information
|
||||||
var IPTable p2p.IpAddresses
|
var IPTable p2p.IpAddresses
|
||||||
|
|
||||||
// Receive file from POST request
|
// Receive file from POST request
|
||||||
body, err := c.FormFile("json")
|
body, err := c.FormFile("json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusOK, fmt.Sprint(err))
|
c.String(http.StatusOK, fmt.Sprint(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open file
|
// Open file
|
||||||
open, err := body.Open()
|
open, err := body.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusOK, fmt.Sprint(err))
|
c.String(http.StatusOK, fmt.Sprint(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open received file
|
// Open received file
|
||||||
file, err := ioutil.ReadAll(open)
|
file, err := ioutil.ReadAll(open)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusOK, fmt.Sprint(err))
|
c.String(http.StatusOK, fmt.Sprint(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
json.Unmarshal(file, &IPTable)
|
json.Unmarshal(file, &IPTable)
|
||||||
|
|
||||||
//Add Client IP address to IPTable struct
|
//Add Client IP address to IPTable struct
|
||||||
IPTable.IpAddress = append(IPTable.IpAddress, ClientHost)
|
IPTable.IpAddress = append(IPTable.IpAddress, ClientHost)
|
||||||
|
|
||||||
// Runs speed test to return only servers in the IP table pingable
|
// Runs speed test to return only servers in the IP table pingable
|
||||||
err = IPTable.SpeedTestUpdatedIPTable()
|
err = IPTable.SpeedTestUpdatedIPTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusOK, fmt.Sprint(err))
|
c.String(http.StatusOK, fmt.Sprint(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads IP addresses from ip table
|
// Reads IP addresses from ip table
|
||||||
IpAddresses, err := p2p.ReadIpTable()
|
IpAddresses, err := p2p.ReadIpTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusOK, fmt.Sprint(err))
|
c.String(http.StatusOK, fmt.Sprint(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, IpAddresses)
|
c.JSON(http.StatusOK, IpAddresses)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Starts docker container in server
|
// Starts docker container in server
|
||||||
r.GET("/startcontainer", func(c *gin.Context) {
|
r.GET("/startcontainer", func(c *gin.Context) {
|
||||||
// Get Number of ports to open and whether to use GPU or not
|
// Get Number of ports to open and whether to use GPU or not
|
||||||
Ports := c.DefaultQuery("ports", "0")
|
Ports := c.DefaultQuery("ports", "0")
|
||||||
GPU := c.DefaultQuery("GPU", "false")
|
GPU := c.DefaultQuery("GPU", "false")
|
||||||
ContainerName := c.DefaultQuery("ContainerName", "")
|
ContainerName := c.DefaultQuery("ContainerName", "")
|
||||||
var PortsInt int
|
var PortsInt int
|
||||||
|
|
||||||
// Convert Get Request value to int
|
// Convert Get Request value to int
|
||||||
fmt.Sscanf(Ports, "%d", &PortsInt)
|
fmt.Sscanf(Ports, "%d", &PortsInt)
|
||||||
|
|
||||||
// Creates container and returns-back result to
|
// Creates container and returns-back result to
|
||||||
// access container
|
// access container
|
||||||
resp, err := docker.BuildRunContainer(PortsInt, GPU, ContainerName)
|
resp, err := docker.BuildRunContainer(PortsInt, GPU, ContainerName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensures that FRP is triggered only if a proxy address is provided
|
// Ensures that FRP is triggered only if a proxy address is provided
|
||||||
if ProxyIpAddr.Ipv4 != "" && c.Request.Host != "localhost:8088" && c.Request.Host != "0.0.0.0:8088" {
|
if ProxyIpAddr.Ipv4 != "" && c.Request.Host != "localhost:8088" && c.Request.Host != "0.0.0.0:8088" {
|
||||||
resp, err = frp.StartFRPCDockerContainer(ProxyIpAddr.Ipv4, lowestLatencyIpAddress.ServerPort, resp)
|
resp, err = frp.StartFRPCDockerContainer(ProxyIpAddr.Ipv4, lowestLatencyIpAddress.ServerPort, resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
||||||
}
|
}
|
||||||
fmt.Println(resp)
|
fmt.Println(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, resp)
|
c.JSON(http.StatusOK, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
//Remove container
|
//Remove container
|
||||||
r.GET("/RemoveContainer", func(c *gin.Context) {
|
r.GET("/RemoveContainer", func(c *gin.Context) {
|
||||||
ID := c.DefaultQuery("id", "0")
|
ID := c.DefaultQuery("id", "0")
|
||||||
if err := docker.StopAndRemoveContainer(ID); err != nil {
|
if err := docker.StopAndRemoveContainer(ID); err != nil {
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
||||||
}
|
}
|
||||||
c.String(http.StatusOK, "success")
|
c.String(http.StatusOK, "success")
|
||||||
})
|
})
|
||||||
|
|
||||||
//Show images available
|
//Show images available
|
||||||
r.GET("/ShowImages", func(c *gin.Context) {
|
r.GET("/ShowImages", func(c *gin.Context) {
|
||||||
resp, err := docker.ViewAllContainers()
|
resp, err := docker.ViewAllContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, resp)
|
c.JSON(http.StatusOK, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Request for port no from Server with address
|
// Request for port no from Server with address
|
||||||
r.GET("/FRPPort", func(c *gin.Context) {
|
r.GET("/FRPPort", func(c *gin.Context) {
|
||||||
port, err := frp.StartFRPProxyFromRandom()
|
port, err := frp.StartFRPProxyFromRandom()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
c.String(http.StatusOK, strconv.Itoa(port))
|
c.String(http.StatusOK, strconv.Itoa(port))
|
||||||
})
|
})
|
||||||
|
|
||||||
//Get Server port based on the config file
|
//Get Server port based on the config file
|
||||||
config, err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is a proxy port specified
|
// If there is a proxy port specified
|
||||||
// then starts the FRP server
|
// then starts the FRP server
|
||||||
if config.FRPServerPort != "0" {
|
if config.FRPServerPort != "0" {
|
||||||
go frp.StartFRPProxyFromRandom()
|
go frp.StartFRPProxyFromRandom()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check if IPV6 or Proxy port is specified
|
// TODO check if IPV6 or Proxy port is specified
|
||||||
// if not update current entry as proxy address
|
// if not update current entry as proxy address
|
||||||
// with appropriate port on IP Table
|
// with appropriate port on IP Table
|
||||||
if config.BehindNAT == "True" {
|
if config.BehindNAT == "True" {
|
||||||
table, err := p2p.ReadIpTable()
|
table, err := p2p.ReadIpTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var lowestLatency int64
|
var lowestLatency int64
|
||||||
// random large number
|
// random large number
|
||||||
lowestLatency = 10000000
|
lowestLatency = 10000000
|
||||||
|
|
||||||
for i, _ := range table.IpAddress {
|
for i, _ := range table.IpAddress {
|
||||||
// Checks if the ping is the lowest and if the following node is acting as a proxy
|
// Checks if the ping is the lowest and if the following node is acting as a proxy
|
||||||
//if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && table.IpAddress[i].ProxyPort != "" {
|
//if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && table.IpAddress[i].ProxyPort != "" {
|
||||||
if table.IpAddress[i].Latency.Milliseconds() < lowestLatency {
|
if table.IpAddress[i].Latency.Milliseconds() < lowestLatency {
|
||||||
lowestLatency = table.IpAddress[i].Latency.Milliseconds()
|
lowestLatency = table.IpAddress[i].Latency.Milliseconds()
|
||||||
lowestLatencyIpAddress = table.IpAddress[i]
|
lowestLatencyIpAddress = table.IpAddress[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is an identified node
|
// If there is an identified node
|
||||||
if lowestLatency != 10000000 {
|
if lowestLatency != 10000000 {
|
||||||
serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort)
|
serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Create 3 second delay to allow FRP server to start
|
// Create 3 second delay to allow FRP server to start
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
// Starts FRP as a client with
|
// Starts FRP as a client with
|
||||||
proxyPort, err := frp.StartFRPClientForServer(lowestLatencyIpAddress.Ipv4, serverPort, config.ServerPort)
|
proxyPort, err := frp.StartFRPClientForServer(lowestLatencyIpAddress.Ipv4, serverPort, config.ServerPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// updating with the current proxy address
|
// updating with the current proxy address
|
||||||
ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4
|
ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4
|
||||||
ProxyIpAddr.ServerPort = proxyPort
|
ProxyIpAddr.ServerPort = proxyPort
|
||||||
ProxyIpAddr.ProxyPort = lowestLatencyIpAddress.ProxyPort
|
ProxyIpAddr.ProxyPort = lowestLatencyIpAddress.ProxyPort
|
||||||
ProxyIpAddr.Name = config.MachineName
|
ProxyIpAddr.Name = config.MachineName
|
||||||
|
|
||||||
// append the following to the ip table
|
// append the following to the ip table
|
||||||
table.IpAddress = append(table.IpAddress, ProxyIpAddr)
|
table.IpAddress = append(table.IpAddress, ProxyIpAddr)
|
||||||
// write information back to the IP Table
|
// write information back to the IP Table
|
||||||
table.WriteIpTable()
|
table.WriteIpTable()
|
||||||
// update ip table
|
// update ip table
|
||||||
go table.SpeedTest()
|
go clientIPTable.UpdateIpTableListClient()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run gin server on the specified port
|
// Run gin server on the specified port
|
||||||
err = r.Run(":" + config.ServerPort)
|
err = r.Run(":" + config.ServerPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user