changed
duplication detection logic and added better ouput for nodes in the IPTable
This commit is contained in:
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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 [<IPV4>:<Port No>]
|
||||
// - duplicate IPV6 addresses [<IPV6>]
|
||||
// - 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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
106
p2p/upnp.go
106
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
|
||||
|
||||
}
|
||||
|
||||
15
p2p/upnp_test.go
Normal file
15
p2p/upnp_test.go
Normal file
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user