Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-nat
Commits
c30beee1
Unverified
Commit
c30beee1
authored
Sep 17, 2018
by
Steven Allen
Committed by
GitHub
Sep 17, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1 from cannium/port-mapping-update
Port mapping update
parents
329756a8
3fa58ced
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
2 deletions
+15
-2
_examples/nat-tester.go
_examples/nat-tester.go
+2
-2
natpmp.go
natpmp.go
+7
-0
upnp.go
upnp.go
+6
-0
No files found.
_examples/nat-tester.go
View file @
c30beee1
...
@@ -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
(
"t
x
p"
,
3080
)
defer
nat
.
DeletePortMapping
(
"t
c
p"
,
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"
)
...
...
natpmp.go
View file @
c30beee1
...
@@ -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
)
...
...
upnp.go
View file @
c30beee1
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment