added ipv6 compatability to other commands

This commit is contained in:
2021-06-25 17:22:01 +04:00
parent 21b9efc3a6
commit 657577f583
5 changed files with 81 additions and 136 deletions

View File

@@ -2,22 +2,16 @@ package client
import (
"encoding/json"
"errors"
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"os"
)
type IP struct {
Query string
}
// UpdateIpTable Does the following to update it's IP table
func UpdateIpTable(IpAddress string) error {
@@ -30,12 +24,12 @@ func UpdateIpTable(IpAddress string) error {
var resp []byte
version := ip4or6(IpAddress)
version := p2p.Ip4or6(IpAddress)
if version == "version 6" {
resp, err = UploadMultipartFile(client,"http://["+IpAddress+"]:8088/IpTable","json",config.IPTable)
if err != nil {
return err
}
resp, err = UploadMultipartFile(client,"http://["+IpAddress+"]:8088/IpTable","json",config.IPTable)
if err != nil {
return err
}
} else {
resp, err = UploadMultipartFile(client,"http://"+IpAddress+":8088/IpTable","json",config.IPTable)
if err != nil {
@@ -43,6 +37,10 @@ func UpdateIpTable(IpAddress string) error {
}
}
if resp == nil {
return nil
}
//resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
// config.IPTable,
// "json")
@@ -70,6 +68,7 @@ func UpdateIpTable(IpAddress string) error {
// 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 {
@@ -80,69 +79,55 @@ func UpdateIpTableListClient() error {
// duplication
Addresses, err := p2p.ReadIpTable()
var DoNotRead p2p.IpAddresses
//var DoNotRead p2p.IpAddresses
// Run loop 3 times
for i := 0; i < 3; i++ {
// Gets information from IP table
Addresses, err = p2p.ReadIpTable()
if err != nil {
return err
}
// Appending current machine public IP address as should not be there in IP Table
var PublicIP p2p.IpAddress
ipv6, err := GetCurrentIPV6()
if err != nil {
return err
}
ip, err := CurrentPublicIP()
if err != nil {
return err
}
PublicIP.Ipv4 = ip
PublicIP.Ipv6 = ipv6
DoNotRead.IpAddress = append(DoNotRead.IpAddress, PublicIP)
//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
//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 PublicIP.Ipv4 == Addresses.IpAddress[j].Ipv4 {
Exists = true
if Addresses.IpAddress[j].Ipv6 != "" {
err = UpdateIpTable(Addresses.IpAddress[j].Ipv6)
} else {
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4)
}
// Check if IP addresses is there in the struct DoNotRead
for k := range DoNotRead.IpAddress {
if DoNotRead.IpAddress[k].Ipv4 == Addresses.IpAddress[j].Ipv4 {
Exists = true
break
}
}
// If the struct exists then continues
if Exists {
continue
}
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4)
if err != nil {
return err
}
//Appends server1 IP address to variable DoNotRead
DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
}
}
// Removing duplicates in the IP table
if err := p2p.RemoveDuplicates(); err != nil {
return err
}
return nil
}
@@ -235,6 +220,10 @@ func UploadMultipartFile(client http.Client, uri, key, path string) ([]byte, err
}()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
@@ -245,62 +234,3 @@ func UploadMultipartFile(client http.Client, uri, key, path string) ([]byte, err
}
// CurrentPublicIP Get Current Public IP address
func CurrentPublicIP() (string,error) {
req, err := http.Get("http://ip-api.com/json/")
if err != nil {
return "",err
}
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return "",err
}
var ip IP
json.Unmarshal(body, &ip)
return ip.Query, nil
}
// GetCurrentIPV6 gets the current IPV6 address based on the interface
// specified in the config file
func GetCurrentIPV6()(string,error){
Config, err := config.ConfigInit()
if err != nil {
return "",err
}
byNameInterface, err := net.InterfaceByName(Config.NetworkInterface)
if err != nil {
return "",err
}
addresses, err := byNameInterface.Addrs()
if err != nil {
return "",err
}
if addresses[1].String() == "" {
return "",errors.New("IPV6 address not detected")
}
IP,_,err := net.ParseCIDR(addresses[1].String())
if err != nil {
return "",err
}
return IP.String(), nil
}
//Helper function to check if the IP address is IPV4 or
//IPV6 (https://socketloop.com/tutorials/golang-check-if-ip-address-is-version-4-or-6)
func ip4or6(s string) string {
for i := 0; i < len(s); i++ {
switch s[i] {
case '.':
return "version 4"
case ':':
return "version 6"
}
}
return "unknown"
}

View File

@@ -3,6 +3,7 @@ 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,7 +13,13 @@ import (
// and other basic information which helps set a
// cluster of computer
func GetSpecs(IP string)(*server.SysInfo,error) {
URL := "http://" + IP + ":" + serverPort + "/server_info"
var URL string
version := p2p.Ip4or6(IP)
if version == "version 6" {
URL = "http://[" + IP + "]:" + serverPort + "/server_info"
} else {
URL = "http://" + IP + ":" + serverPort + "/server_info"
}
resp, err := http.Get(URL)
if err != nil {
return nil,err

View File

@@ -3,6 +3,7 @@ package client
import (
"encoding/json"
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
"io/ioutil"
"net/http"
@@ -22,7 +23,14 @@ var client = http.Client{}
//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
URL := "http://" + IP + ":" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName
var URL string
version := p2p.Ip4or6(IP)
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
}
resp, err := http.Get(URL)
if err != nil {
return nil,err
@@ -48,7 +56,13 @@ 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 {
URL := "http://" + IP + ":" + serverPort + "/RemoveContainer?id=" + ID
var URL string
version := p2p.Ip4or6(IP)
if version == "version 6" {
URL = "http://[" + IP + "]:" + serverPort + "/RemoveContainer?id=" + ID
} else {
URL = "http://" + IP + ":" + serverPort + "/RemoveContainer?id=" + ID
}
resp, err := http.Get(URL)
if err != nil {
return err
@@ -69,7 +83,13 @@ func RemoveContianer(IP string,ID string) error {
// ViewContainers This function displays all containers available on server side
func ViewContainers(IP string)(*docker.DockerContainers, error){
// Passes URL with route /ShowImages
URL := "http://" + IP + ":" + serverPort + "/ShowImages"
var URL string
version := p2p.Ip4or6(IP)
if version == "version 6" {
URL = "http://[" + IP + "]:" + serverPort + "/ShowImages"
} else {
URL = "http://" + IP + ":" + serverPort + "/ShowImages"
}
resp, err := http.Get(URL)
if err != nil {
return nil,err

View File

@@ -1,7 +1,6 @@
package client
import (
"fmt"
"testing"
)
@@ -13,13 +12,3 @@ func TestUpdateIpTableListClient(t *testing.T) {
}
}
// Testing is a IPV6 address is returned
func TestGetCurrentIPV6(t *testing.T) {
res, err := GetCurrentIPV6()
if err != nil {
t.Error(err)
}
fmt.Println(res)
}