diff --git a/client/clientIPTable/Iptable.go b/client/clientIPTable/Iptable.go index e39fe87..fa8c1be 100644 --- a/client/clientIPTable/Iptable.go +++ b/client/clientIPTable/Iptable.go @@ -1,139 +1,139 @@ 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" + "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 - } + config, err := config.ConfigInit() + if err != nil { + 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 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 - } + if resp == nil { + return nil + } - //resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable", - // config.IPTable, - // "json") - // - //if err != nil { - // return err - //} + //resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable", + // config.IPTable, + // "json") + // + //if err != nil { + // return err + //} - var ipStruct p2p.IpAddresses - json.Unmarshal(resp, &ipStruct) + 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 - } - } + // 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 + return nil } // UpdateIpTableListClient updates IP tables (Default 3 hops) based on server information available // on the ip tables func UpdateIpTableListClient() error { - // Get config information - Config, err := config.ConfigInit() - if err != nil { - return err - } + // Get config information + Config, err := config.ConfigInit() + if err != nil { + return err + } - // Ensure that the IP Table has Node pingable - err = p2p.LocalSpeedTestIpTable() - if err != nil { - return err - } + // 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 + // IP addresses to not append to struct due to + // duplication - Addresses, err := p2p.ReadIpTable() - var DoNotRead p2p.IpAddresses + Addresses, err := p2p.ReadIpTable() + var DoNotRead p2p.IpAddresses - currentIPV4, err := p2p.CurrentPublicIP() - if err != nil { - return err - } + currentIPV4, err := p2p.CurrentPublicIP() + if err != nil { + return err + } - // Run loop 2 times - for i := 0; i < 2; i++ { - // Gets information from IP table - Addresses, err = p2p.ReadIpTable() - if err != nil { - return err - } + // Run loop 2 times + for i := 0; i < 2; i++ { + // Gets information from IP table + Addresses, err = p2p.ReadIpTable() + if err != nil { + return err + } - //DoNotRead.IpAddress = append(DoNotRead.IpAddress, PublicIP) + //DoNotRead.IpAddress = append(DoNotRead.IpAddress, PublicIP) - // Updates IP table based on server IP table - for j := range Addresses.IpAddress { + // Updates IP table based on server IP table + for j := range Addresses.IpAddress { - Exists := false - // If the address is local then add to the local list - if currentIPV4 == Addresses.IpAddress[j].Ipv4 && Addresses.IpAddress[j].ServerPort == Config.ServerPort { - Exists = true - } + Exists := false + // If the address is local then add to the local list + if currentIPV4 == Addresses.IpAddress[j].Ipv4 && Addresses.IpAddress[j].ServerPort == Config.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].ServerPort == Addresses.IpAddress[j].ServerPort) || (DoNotRead.IpAddress[k].Ipv6 != "" && Addresses.IpAddress[j].Ipv6 == DoNotRead.IpAddress[k].Ipv6) { - Exists = true - break - } - } + // 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].ServerPort == Addresses.IpAddress[j].ServerPort) || (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 the struct exists then continues + if Exists { + continue + } - if Addresses.IpAddress[j].Ipv6 != "" { - go UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort) - } else if Addresses.IpAddress[j].Ipv4 != currentIPV4 { - go UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort) - } + if Addresses.IpAddress[j].Ipv6 != "" { + go UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort) + } else if Addresses.IpAddress[j].Ipv4 != currentIPV4 { + go UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort) + } - //Appends server1 IP address to variable DoNotRead - DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j]) - } - } + //Appends server1 IP address to variable DoNotRead + DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j]) + } + } - return nil + return nil } // SendPostRequest Sends a file as a @@ -183,58 +183,58 @@ func UpdateIpTableListClient() 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) - if err != nil { - return nil, err - } + 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()) + mwriter := multipart.NewWriter(writer) + req.Header.Add("Content-Type", mwriter.FormDataContentType()) - errchan := make(chan error) + errchan := make(chan error) - go func() { - defer close(errchan) - defer writer.Close() - defer mwriter.Close() + go func() { + defer close(errchan) + defer writer.Close() + defer mwriter.Close() - w, err := mwriter.CreateFormFile(key, path) - if err != nil { - errchan <- err - return - } + 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() + 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 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 - } - }() + if err := mwriter.Close(); err != nil { + errchan <- err + return + } + }() - resp, err := client.Do(req) - if err != nil { - return nil, err - } + resp, err := client.Do(req) + if err != nil { + return nil, err + } - content, err := ioutil.ReadAll(resp.Body) + content, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } + if err != nil { + return nil, err + } - return content, nil + return content, nil } diff --git a/p2p/ip_table.json b/p2p/ip_table.json index 704faf9..bb1b3f4 100644 --- a/p2p/ip_table.json +++ b/p2p/ip_table.json @@ -1,13 +1,12 @@ { "ip_address": [ { - "ipv4": "64.227.168.102", - "ipv6": "", - "latency": 0, - "download": 0, - "upload": 0, - "serverport": "8088", - "proxyport": "7000" + "IPV4": "64.227.168.102", + "IPV6": "", + "Latency": 0, + "ServerPort": "8088", + "NAT": "False", + "EscapeImplementation": "None" } ] } diff --git a/p2p/iptable.go b/p2p/iptable.go index e0b1692..2438b23 100644 --- a/p2p/iptable.go +++ b/p2p/iptable.go @@ -18,14 +18,15 @@ type IpAddresses struct { } type IpAddress struct { - Name string `json:"Name"` - Ipv4 string `json:"ipv4"` - Ipv6 string `json:"ipv6"` - Latency time.Duration `json:"latency"` - Download float64 `json:"download"` - Upload float64 `json:"upload"` - ServerPort string `json:"serverport"` - ProxyPort string `json:"proxyport"` + Name string `json:"Name"` + Ipv4 string `json:"IPV4"` + Ipv6 string `json:"IPV6"` + Latency time.Duration `json:"Latency"` + Download float64 `json:"Download"` + Upload float64 `json:"Upload"` + ServerPort string `json:"ServerPort"` + NAT string `json:"NAT"` + EscapeImplementation string `json:"EscapeImplementation"` } type IP struct { @@ -73,9 +74,8 @@ func ReadIpTable() (*IpAddresses, error) { PublicIP.Ipv6 = ipv6 PublicIP.ServerPort = config.ServerPort PublicIP.Name = config.MachineName - if config.FRPServerPort != "0" { - PublicIP.ProxyPort = config.FRPServerPort - } + PublicIP.NAT = config.BehindNAT + PublicIP.EscapeImplementation = "None" // Updates current machine IP address to the IP table ipAddresses.IpAddress = append(ipAddresses.IpAddress, PublicIP) @@ -123,9 +123,9 @@ func PrintIpTable() error { } for i := 0; i < len(table.IpAddress); i++ { - fmt.Printf("\nIP Address: %s\nIPV6: %s\nLatency: %s\nServerPort: %s\n-----------"+ + fmt.Printf("\nIP Address: %s\nIPV6: %s\nLatency: %s\nServerPort: %s\nbehindNAT: %s\nEscapeImplementation: %s\n-----------"+ "-----------------\n", table.IpAddress[i].Ipv4, table.IpAddress[i].Ipv6, - table.IpAddress[i].Latency, table.IpAddress[i].ServerPort) + table.IpAddress[i].Latency, table.IpAddress[i].ServerPort, table.IpAddress[i].NAT, table.IpAddress[i].EscapeImplementation) } //PrettyPrint(table) @@ -140,11 +140,16 @@ func (table *IpAddresses) RemoveDuplicates() error { for i, _ := range table.IpAddress { Exists := false 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].ProxyPort == "0" { - Exists = true - break - } + // Statements checked for + // - duplicate IPV4 addresses [:] + // - duplicate IPV6 addresses [] + // - Node is behind NAT and no escape implementation provided + if (NoDuplicates.IpAddress[k].Ipv4 != "" && NoDuplicates.IpAddress[k].Ipv4 == table.IpAddress[i].Ipv4 && + NoDuplicates.IpAddress[k].ServerPort == table.IpAddress[i].ServerPort) || + (NoDuplicates.IpAddress[k].Ipv6 != "" && NoDuplicates.IpAddress[k].Ipv6 == table.IpAddress[i].Ipv6) || + (NoDuplicates.IpAddress[k].NAT == "True" && NoDuplicates.IpAddress[i].EscapeImplementation == "None") { + Exists = true + break } } if Exists { diff --git a/p2p/speedtest.go b/p2p/speedtest.go index ac26beb..4957181 100644 --- a/p2p/speedtest.go +++ b/p2p/speedtest.go @@ -36,7 +36,6 @@ func (ip *IpAddresses) SpeedTest() error { //Set value to the list ActiveIP.IpAddress = append(ActiveIP.IpAddress, value) - } ip.IpAddress = ActiveIP.IpAddress @@ -67,7 +66,7 @@ func (ip *IpAddresses) SpeedTestUpdatedIPTable() error { for k := range ip.IpAddress { // Checks if both the IPV4 addresses are the same or the IPV6 address is not // an empty string and IPV6 address are the same - if (ip.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 && targets.IpAddress[i].ProxyPort == "0") || (targets.IpAddress[i].Ipv6 != "" && ip.IpAddress[k].Ipv6 == targets.IpAddress[i].Ipv6 && targets.IpAddress[i].ProxyPort == "0") { + if (ip.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 && targets.IpAddress[i].NAT == "True") || (targets.IpAddress[i].Ipv6 != "" && ip.IpAddress[k].Ipv6 == targets.IpAddress[i].Ipv6) { Exists = true break } diff --git a/p2p/upnp.go b/p2p/upnp.go index 0a58107..83b9e81 100644 --- a/p2p/upnp.go +++ b/p2p/upnp.go @@ -1,76 +1,74 @@ package p2p import ( - "fmt" + "fmt" "gitlab.com/NebulousLabs/go-upnp" ) // Port forwarding to the router -func ForwardPort(port int) error{ - // connect to router - d, err := upnp.Discover() - if err != nil { - return err - } +func ForwardPort(port int) error { + // connect to router + d, err := upnp.Discover() + if err != nil { + return err + } - // discover external IP - ip, err := d.ExternalIP() - if err != nil { - return err - } - fmt.Println("Your external IP is:", ip) + // discover external IP + ip, err := d.ExternalIP() + if err != nil { + return err + } + fmt.Println("Your external IP is:", ip) - // forward a port - err = d.Forward(50498, "upnp test") - if err != nil { - return err - } + // forward a port + err = d.Forward(50498, "upnp test") + if err != nil { + return err + } - // record router's location - loc := d.Location() + // record router's location + loc := d.Location() - // connect to router directly - d, err = upnp.Load(loc) - if err != nil { - return err - } + // connect to router directly + d, err = upnp.Load(loc) + if err != nil { + return err + } - return nil + return nil } -// unForwardPort from router -func UnForwardPort(port int) error{ - // connect to router - d, err := upnp.Discover() - if err != nil { - return err - } +// UnForwardPort from router +func UnForwardPort(port int) error { + // connect to router + d, err := upnp.Discover() + if err != nil { + return err + } + // discover external IP + ip, err := d.ExternalIP() + if err != nil { + return err + } - // discover external IP - ip, err := d.ExternalIP() - if err != nil { - return err - } + fmt.Println("Your external IP is:", ip) - fmt.Println("Your external IP is:", ip) + // un-forward a port + err = d.Clear(50498) + if err != nil { + return err + } + // record router's location + loc := d.Location() - // un-forward a port - err = d.Clear(50498) - if err != nil { - return err - } + // connect to router directly + d, err = upnp.Load(loc) + if err != nil { + return err + } - // record router's location - loc := d.Location() - - // connect to router directly - d, err = upnp.Load(loc) - if err != nil { - return err - } - - return nil + return nil } diff --git a/p2p/upnp_test.go b/p2p/upnp_test.go new file mode 100644 index 0000000..ef53493 --- /dev/null +++ b/p2p/upnp_test.go @@ -0,0 +1,15 @@ +package p2p + +import ( + "fmt" + "testing" +) + +// Tests if the current has UPNP support +func TestForwardUPNPPort(t *testing.T) { + err := ForwardPort(6586) + if err != nil { + fmt.Println(err) + t.Fail() + } +} diff --git a/server/server.go b/server/server.go index dfff38b..06b495e 100644 --- a/server/server.go +++ b/server/server.go @@ -209,8 +209,8 @@ func Server() error { // updating with the current proxy address ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 ProxyIpAddr.ServerPort = proxyPort - ProxyIpAddr.ProxyPort = lowestLatencyIpAddress.ProxyPort ProxyIpAddr.Name = config.MachineName + ProxyIpAddr.EscapeImplementation = "FRP" // append the following to the ip table table.IpAddress = append(table.IpAddress, ProxyIpAddr)