added upnp
This commit is contained in:
11
p2p/README
11
p2p/README
@@ -1,3 +1,12 @@
|
||||
P2P module
|
||||
===========
|
||||
This part we will implement a UDP hole punching
|
||||
(Techniques to get over NAT)
|
||||
- UPNP implementation
|
||||
- DMZ to be used if UPNP does not work
|
||||
- Port forwarding for future release
|
||||
|
||||
(Discovery of Nodes)
|
||||
- Connect to reliable connection (that has some knowledge)
|
||||
- Check if the connection is still open (If not instruct the server to remove from table)
|
||||
- According speed tests
|
||||
- Send list of servers connection open
|
||||
|
||||
61
p2p/upnp.go
61
p2p/upnp.go
@@ -1,35 +1,29 @@
|
||||
package main
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"gitlab.com/NebulousLabs/go-upnp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Port forwarding to the router
|
||||
func ForwardPort(port int) error{
|
||||
// connect to router
|
||||
d, err := upnp.Discover()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// discover external IP
|
||||
ip, err := d.ExternalIP()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("Your external IP is:", ip)
|
||||
|
||||
// forward a port
|
||||
/* err = d.Forward(63390, "upnp test")
|
||||
err = d.Forward(50498, "upnp test")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} */
|
||||
|
||||
// un-forward a port
|
||||
err = d.Clear(63390)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// record router's location
|
||||
@@ -38,6 +32,45 @@ func main() {
|
||||
// connect to router directly
|
||||
d, err = upnp.Load(loc)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
// connect to router directly
|
||||
d, err = upnp.Load(loc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
21
p2p/upnp_test.go
Normal file
21
p2p/upnp_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user