added ip table compatible with ipv6

This commit is contained in:
2021-06-25 14:30:57 +04:00
parent 1eac752fa7
commit 9f8e6a1f04
5 changed files with 116 additions and 40 deletions

View File

@@ -1,14 +1,23 @@
{
"ip_address": [
{
"ipv4": "0.0.0.0",
"latency": 453359,
"ipv4": "86.99.25.74",
"ipv6": "2001:8f8:172d:ee93:7588:ad57:c351:3309",
"latency": 428261,
"download": 0,
"upload": 0
},
{
"ipv4": "127.0.0.1",
"latency": 410267,
"ipv4": "86.99.25.74",
"ipv6": "2001:8f8:172d:ee93:7588:ad57:c351:3309",
"latency": 374920,
"download": 0,
"upload": 0
},
{
"ipv4": "86.99.25.74",
"ipv6": "2001:8f8:172d:ee93:7588:ad57:c351:3309",
"latency": 341720,
"download": 0,
"upload": 0
}

View File

@@ -2,9 +2,12 @@ package p2p
import (
"encoding/json"
"errors"
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
"io/ioutil"
"net"
"net/http"
"os"
"time"
)
@@ -23,6 +26,10 @@ type IpAddress struct {
Upload float64 `json:"upload"`
}
type IP struct {
Query string
}
// ReadIpTable Read data from Ip tables from json file
func ReadIpTable()(*IpAddresses ,error){
// Get Path from config
@@ -50,11 +57,38 @@ func ReadIpTable()(*IpAddresses ,error){
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &ipAddresses)
var PublicIP IpAddress
ipv6, err := GetCurrentIPV6()
if err != nil {
return nil, err
}
ip, err := CurrentPublicIP()
if err != nil {
return nil, err
}
PublicIP.Ipv4 = ip
PublicIP.Ipv6 = ipv6
// Updates current machine IP address to the IP table
ipAddresses.IpAddress = append(ipAddresses.IpAddress, PublicIP)
//before writing to iptable ensures the duplicates are removed
if err = ipAddresses.RemoveDuplicates(); err != nil {
return nil, err
}
return &ipAddresses, nil
}
// WriteIpTable Write to IP table json file
func (i *IpAddresses) WriteIpTable() error {
//before writing to iptable ensures the duplicates are removed
if err := i.RemoveDuplicates(); err != nil {
return err
}
file, err := json.MarshalIndent(i, "", " ")
if err != nil {
return err
@@ -83,8 +117,8 @@ func PrintIpTable() error {
}
for i := 0; i < len(table.IpAddress); i++ {
fmt.Printf("\nIP Address: %s\nLatency: %s\n-----------" +
"-----------------\n",table.IpAddress[i].Ipv4,
fmt.Printf("\nIP Address: %s\nIPV6: %s\nLatency: %s\n-----------" +
"-----------------\n",table.IpAddress[i].Ipv4,table.IpAddress[i].Ipv6,
table.IpAddress[i].Latency)
}
return nil
@@ -92,17 +126,13 @@ func PrintIpTable() error {
// RemoveDuplicates This is a temporary fix current functions failing to remove
// Duplicate IP addresses from local IP table
func RemoveDuplicates() error {
table, err := ReadIpTable()
if err != nil {
return err
}
func (table IpAddresses)RemoveDuplicates() error {
var NoDuplicates IpAddresses
for i, _:= range table.IpAddress {
Exists := false
for k := range NoDuplicates.IpAddress {
if NoDuplicates.IpAddress[k].Ipv4 == table.IpAddress[i].Ipv4 {
if NoDuplicates.IpAddress[k].Ipv4 == table.IpAddress[i].Ipv4 || (NoDuplicates.IpAddress[k].Ipv6 != "" && NoDuplicates.IpAddress[k].Ipv6 == table.IpAddress[i].Ipv6){
Exists = true
break
}
@@ -114,9 +144,65 @@ func RemoveDuplicates() error {
NoDuplicates.IpAddress = append(NoDuplicates.IpAddress, table.IpAddress[i])
}
if err := NoDuplicates.WriteIpTable(); err != nil {
return nil
return nil
}
// 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
}
return nil
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
}
// Ip4or6 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

@@ -49,7 +49,7 @@ func (ip *IpAddresses)SpeedTest() error{
return nil
}
// SpeedTestUpdatedIPTable Called when ip tables from client/server is also passed on
// SpeedTestUpdatedIPTable Called when ip tables from httpclient/server is also passed on
func (ip *IpAddresses)SpeedTestUpdatedIPTable() error{
targets, err := ReadIpTable()
if err != nil {
@@ -65,7 +65,9 @@ func (ip *IpAddresses)SpeedTestUpdatedIPTable() error{
// To ensure that there are no duplicate IP addresses
Exists := false
for k := range ip.IpAddress {
if ip.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 {
// 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].Ipv6 != "" && ip.IpAddress[k].Ipv6 == targets.IpAddress[i].Ipv6) {
Exists = true
break
}

View File

@@ -14,7 +14,7 @@ import (
//var dlSizes = [...]int{350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000}
//var ulSizes = [...]int{100, 300, 500, 800, 1000, 1500, 2500, 3000, 3500, 4000} //kB
var client = http.Client{}
var httpclient = http.Client{}
// DownloadTest executes the test to measure download speed
//func (s *IpAddress) DownloadTest(savingMode bool) error {
@@ -80,7 +80,7 @@ var client = http.Client{}
// Download Speed
func (s *IpAddress)DownloadSpeed() error {
start := time.Now()
resp, err := client.Get("http://" + s.Ipv4 + ":8088/50")
resp, err := httpclient.Get("http://" + s.Ipv4 + ":8088/50")
if err != nil {
return err
}

View File

@@ -1,21 +0,0 @@
package p2p
import(
"testing"
)
func TestAddRemoveUpnp(t *testing.T){
// forwarding port 23241 via upnp
err := ForwardPort(23241)
if err != nil {
t.Errorf("Error returned: %q", err)
}
// unforwarding port 23241 via upnp
err = UnForwardPort(23241)
if err != nil {
t.Errorf("Error returned: %q", err)
}
}