added minor fixed to reverse proxy
This commit is contained in:
@@ -1,135 +1,138 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UpdateIpTable Does the following to update it's IP table
|
// UpdateIpTable Does the following to update it's IP table
|
||||||
func UpdateIpTable(IpAddress string, serverPort string) error {
|
func UpdateIpTable(IpAddress string, serverPort string) error {
|
||||||
|
|
||||||
config, err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
|
|
||||||
|
var resp []byte
|
||||||
|
|
||||||
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
version := p2p.Ip4or6(IpAddress)
|
if resp == nil {
|
||||||
if version == "version 6" {
|
return nil
|
||||||
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 {
|
//resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
|
||||||
return nil
|
// config.IPTable,
|
||||||
}
|
// "json")
|
||||||
|
//
|
||||||
|
//if err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
|
|
||||||
//resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
|
var ipStruct p2p.IpAddresses
|
||||||
// config.IPTable,
|
json.Unmarshal(resp, &ipStruct)
|
||||||
// "json")
|
|
||||||
//
|
|
||||||
//if err != nil {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
|
|
||||||
var ipStruct p2p.IpAddresses
|
// Updates IP table based on information provided
|
||||||
json.Unmarshal(resp, &ipStruct)
|
// by the server
|
||||||
|
if len(ipStruct.IpAddress) > 0 {
|
||||||
|
err = ipStruct.SpeedTestUpdatedIPTable()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Updates IP table based on information provided
|
return nil
|
||||||
// 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
|
// UpdateIpTableListClient updates IP tables (Default 3 hops) based on server information available
|
||||||
//on the ip tables
|
//on the ip tables
|
||||||
func UpdateIpTableListClient() error {
|
func UpdateIpTableListClient() error {
|
||||||
|
|
||||||
// Ensure that the IP Table has Node pingable
|
// Ensure that the IP Table has Node pingable
|
||||||
err := p2p.LocalSpeedTestIpTable()
|
err := p2p.LocalSpeedTestIpTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// IP addresses to not append to struct due to
|
// IP addresses to not append to struct due to
|
||||||
// duplication
|
// duplication
|
||||||
|
|
||||||
Addresses, err := p2p.ReadIpTable()
|
Addresses, err := p2p.ReadIpTable()
|
||||||
//var DoNotRead p2p.IpAddresses
|
//var DoNotRead p2p.IpAddresses
|
||||||
|
|
||||||
// Run loop 3 times
|
currentIPV4, err := p2p.CurrentPublicIP()
|
||||||
for i := 0; i < 3; i++ {
|
if err != nil {
|
||||||
// Gets information from IP table
|
return err
|
||||||
//Addresses, err = p2p.ReadIpTable()
|
}
|
||||||
//if err != nil {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, PublicIP)
|
|
||||||
|
|
||||||
// Updates IP table based on server IP table
|
// Run loop 1 times
|
||||||
for j := range Addresses.IpAddress {
|
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)
|
||||||
|
|
||||||
//Exists := false
|
// Updates IP table based on server IP table
|
||||||
//
|
for j := range Addresses.IpAddress {
|
||||||
//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 != "" {
|
//Exists := false
|
||||||
err = UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort)
|
//
|
||||||
} else {
|
//if PublicIP.Ipv4 == Addresses.IpAddress[j].Ipv4 || (PublicIP.Ipv6 != "" && Addresses.IpAddress[j].Ipv6 == PublicIP.Ipv6){
|
||||||
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort)
|
// 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 err != nil {
|
if Addresses.IpAddress[j].Ipv6 != "" {
|
||||||
return err
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
//Appends server1 IP address to variable DoNotRead
|
if err != nil {
|
||||||
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
//Appends server1 IP address to variable DoNotRead
|
||||||
|
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendPostRequest Sends a file as a
|
// SendPostRequest Sends a file as a
|
||||||
@@ -179,59 +182,58 @@ func UpdateIpTableListClient() error {
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
func UploadMultipartFile(client http.Client, uri, key, path string) ([]byte, error) {
|
func UploadMultipartFile(client http.Client, uri, key, path string) ([]byte, error) {
|
||||||
body, writer := io.Pipe()
|
body, writer := io.Pipe()
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, uri, body)
|
req, err := http.NewRequest(http.MethodPost, uri, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mwriter := multipart.NewWriter(writer)
|
mwriter := multipart.NewWriter(writer)
|
||||||
req.Header.Add("Content-Type", mwriter.FormDataContentType())
|
req.Header.Add("Content-Type", mwriter.FormDataContentType())
|
||||||
|
|
||||||
errchan := make(chan error)
|
errchan := make(chan error)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer close(errchan)
|
defer close(errchan)
|
||||||
defer writer.Close()
|
defer writer.Close()
|
||||||
defer mwriter.Close()
|
defer mwriter.Close()
|
||||||
|
|
||||||
w, err := mwriter.CreateFormFile(key, path)
|
w, err := mwriter.CreateFormFile(key, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errchan <- err
|
errchan <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
in, err := os.Open(path)
|
in, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errchan <- err
|
errchan <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer in.Close()
|
defer in.Close()
|
||||||
|
|
||||||
if written, err := io.Copy(w, in); err != nil {
|
if written, err := io.Copy(w, in); err != nil {
|
||||||
errchan <- fmt.Errorf("error copying %s (%d bytes written): %v", path, written, err)
|
errchan <- fmt.Errorf("error copying %s (%d bytes written): %v", path, written, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := mwriter.Close(); err != nil {
|
if err := mwriter.Close(); err != nil {
|
||||||
errchan <- err
|
errchan <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := ioutil.ReadAll(resp.Body)
|
content, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return content, nil
|
return content, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -138,8 +138,10 @@ func (table *IpAddresses) RemoveDuplicates() error {
|
|||||||
Exists := false
|
Exists := false
|
||||||
for k := range NoDuplicates.IpAddress {
|
for k := range NoDuplicates.IpAddress {
|
||||||
if (NoDuplicates.IpAddress[k].Ipv4 != "" && NoDuplicates.IpAddress[k].Ipv4 == table.IpAddress[i].Ipv4) || (NoDuplicates.IpAddress[k].Ipv6 != "" && NoDuplicates.IpAddress[k].Ipv6 == table.IpAddress[i].Ipv6) {
|
if (NoDuplicates.IpAddress[k].Ipv4 != "" && NoDuplicates.IpAddress[k].Ipv4 == table.IpAddress[i].Ipv4) || (NoDuplicates.IpAddress[k].Ipv6 != "" && NoDuplicates.IpAddress[k].Ipv6 == table.IpAddress[i].Ipv6) {
|
||||||
Exists = true
|
if NoDuplicates.IpAddress[k].ProxyPort == "0" {
|
||||||
break
|
Exists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if Exists {
|
if Exists {
|
||||||
|
|||||||
147
p2p/speedtest.go
147
p2p/speedtest.go
@@ -1,111 +1,110 @@
|
|||||||
package p2p
|
package p2p
|
||||||
|
|
||||||
// SpeedTest Runs a speed test and does updates IP tables accordingly
|
// SpeedTest Runs a speed test and does updates IP tables accordingly
|
||||||
func (ip *IpAddresses)SpeedTest() error{
|
func (ip *IpAddresses) SpeedTest() error {
|
||||||
|
|
||||||
//temp variable to store elements IP addresses and other information
|
//temp variable to store elements IP addresses and other information
|
||||||
// of IP addresses that are pingable
|
// of IP addresses that are pingable
|
||||||
var ActiveIP IpAddresses
|
var ActiveIP IpAddresses
|
||||||
|
|
||||||
// Index to remove from struct
|
// Index to remove from struct
|
||||||
for _, value := range ip.IpAddress {
|
for _, value := range ip.IpAddress {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
//if len(ip.IpAddress) == 1 {
|
//if len(ip.IpAddress) == 1 {
|
||||||
// i = 0
|
// i = 0
|
||||||
//}
|
//}
|
||||||
|
|
||||||
// Ping Test
|
// Ping Test
|
||||||
err = value.PingTest()
|
err = value.PingTest()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
//Upload Speed Test
|
//Upload Speed Test
|
||||||
//err = value.UploadSpeed()
|
//err = value.UploadSpeed()
|
||||||
//if err != nil {
|
//if err != nil {
|
||||||
// return err
|
// return err
|
||||||
//}
|
//}
|
||||||
//
|
//
|
||||||
//err = value.DownloadSpeed()
|
//err = value.DownloadSpeed()
|
||||||
//if err != nil {
|
//if err != nil {
|
||||||
// return err
|
// return err
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//Set value to the list
|
//Set value to the list
|
||||||
|
|
||||||
ActiveIP.IpAddress = append(ActiveIP.IpAddress, value)
|
ActiveIP.IpAddress = append(ActiveIP.IpAddress, value)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ip.IpAddress = ActiveIP.IpAddress
|
ip.IpAddress = ActiveIP.IpAddress
|
||||||
|
|
||||||
err := ip.WriteIpTable()
|
err := ip.WriteIpTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SpeedTestUpdatedIPTable Called when ip tables from httpclient/server is also passed on
|
// SpeedTestUpdatedIPTable Called when ip tables from httpclient/server is also passed on
|
||||||
func (ip *IpAddresses)SpeedTestUpdatedIPTable() error{
|
func (ip *IpAddresses) SpeedTestUpdatedIPTable() error {
|
||||||
targets, err := ReadIpTable()
|
targets, err := ReadIpTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// To ensure struct has no duplicates IP addresses
|
// To ensure struct has no duplicates IP addresses
|
||||||
//DoNotRead := targets
|
//DoNotRead := targets
|
||||||
|
|
||||||
// Appends all IP addresses
|
// Appends all IP addresses
|
||||||
for i, _ := range targets.IpAddress {
|
for i, _ := range targets.IpAddress {
|
||||||
|
|
||||||
// To ensure that there are no duplicate IP addresses
|
// To ensure that there are no duplicate IP addresses
|
||||||
//Exists := false
|
//Exists := false
|
||||||
//for k := range ip.IpAddress {
|
//for k := range ip.IpAddress {
|
||||||
// // Checks if both the IPV4 addresses are the same or the IPV6 address is not
|
// // Checks if both the IPV4 addresses are the same or the IPV6 address is not
|
||||||
// // an empty string and IPV6 address are the same
|
// // an empty string and IPV6 address are the same
|
||||||
// if ip.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 || (targets.IpAddress[i].Ipv6 != "" && ip.IpAddress[k].Ipv6 == targets.IpAddress[i].Ipv6) {
|
// if ip.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 || (targets.IpAddress[i].Ipv6 != "" && ip.IpAddress[k].Ipv6 == targets.IpAddress[i].Ipv6) {
|
||||||
// Exists = true
|
// Exists = true
|
||||||
// break
|
// break
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
//
|
//
|
||||||
//// If the struct exists then continues
|
//// If the struct exists then continues
|
||||||
//if Exists {
|
//if Exists {
|
||||||
// continue
|
// continue
|
||||||
//}
|
//}
|
||||||
|
|
||||||
ip.IpAddress = append(ip.IpAddress, targets.IpAddress[i])
|
ip.IpAddress = append(ip.IpAddress, targets.IpAddress[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ip.SpeedTest()
|
err = ip.SpeedTest()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocalSpeedTestIpTable Runs speed test in iptables locally only
|
// LocalSpeedTestIpTable Runs speed test in iptables locally only
|
||||||
func LocalSpeedTestIpTable() error {
|
func LocalSpeedTestIpTable() error {
|
||||||
targets, err := ReadIpTable()
|
targets, err := ReadIpTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = targets.SpeedTest()
|
err = targets.SpeedTest()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Helper function to remove element from an array of a struct
|
// Helper function to remove element from an array of a struct
|
||||||
//func remove(s []IpAddress, i int) []IpAddress {
|
//func remove(s []IpAddress, i int) []IpAddress {
|
||||||
// s[len(s)-1], s[i] = s[i], s[len(s)-1]
|
// s[len(s)-1], s[i] = s[i], s[len(s)-1]
|
||||||
|
|||||||
Reference in New Issue
Block a user