Commit 3fa58ced authored by Can ZHANG's avatar Can ZHANG

Try to map external port the same as internal port

parent 66a3ad4c
...@@ -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