Unverified Commit c30beee1 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #1 from cannium/port-mapping-update

Port mapping update
parents 329756a8 3fa58ced
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/fd/go-nat" "github.com/libp2p/go-nat"
) )
func main() { func main() {
...@@ -52,7 +52,7 @@ func main() { ...@@ -52,7 +52,7 @@ func main() {
} }
}() }()
defer nat.DeletePortMapping("txp", 3080) defer nat.DeletePortMapping("tcp", 3080)
http.ListenAndServe(":3080", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { http.ListenAndServe(":3080", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "text/plain") rw.Header().Set("Content-Type", "text/plain")
......
...@@ -93,6 +93,13 @@ func (n *natpmpNAT) AddPortMapping(protocol string, internalPort int, descriptio ...@@ -93,6 +93,13 @@ func (n *natpmpNAT) AddPortMapping(protocol string, internalPort int, descriptio
} }
} }
// try to map external port the same as internal port
_, err = n.c.AddPortMapping(protocol, internalPort, internalPort, timeoutInSeconds)
if err == nil {
n.ports[internalPort] = internalPort
return internalPort, nil
}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
externalPort := randomPort() externalPort := randomPort()
_, err = n.c.AddPortMapping(protocol, internalPort, externalPort, timeoutInSeconds) _, err = n.c.AddPortMapping(protocol, internalPort, externalPort, timeoutInSeconds)
......
...@@ -176,6 +176,12 @@ func (u *upnp_NAT) AddPortMapping(protocol string, internalPort int, description ...@@ -176,6 +176,12 @@ func (u *upnp_NAT) AddPortMapping(protocol string, internalPort int, description
} }
} }
// try to map external port the same as internal port
err = u.c.AddPortMapping("", uint16(internalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds)
if err == nil {
return internalPort, nil
}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
externalPort := randomPort() externalPort := randomPort()
err = u.c.AddPortMapping("", uint16(externalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds) err = u.c.AddPortMapping("", uint16(externalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment