Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
5d1b8295
Commit
5d1b8295
authored
Sep 10, 2015
by
Jeromy
Browse files
cache nat port mappings for a short period of time
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
parent
78eb2172
Changes
1
Hide whitespace changes
Inline
Side-by-side
nat/nat.go
View file @
5d1b8295
...
@@ -35,11 +35,9 @@ const CacheTime = time.Second * 15
...
@@ -35,11 +35,9 @@ const CacheTime = time.Second * 15
// DiscoverNAT looks for a NAT device in the network and
// DiscoverNAT looks for a NAT device in the network and
// returns an object that can manage port mappings.
// returns an object that can manage port mappings.
func
DiscoverNAT
()
*
NAT
{
func
DiscoverNAT
()
*
NAT
{
log
.
Error
(
"DISCOVER NAT"
)
nat
,
err
:=
nat
.
DiscoverGateway
()
nat
,
err
:=
nat
.
DiscoverGateway
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Debug
(
"DiscoverGateway error:"
,
err
)
log
.
Debug
(
"DiscoverGateway error:"
,
err
)
log
.
Error
(
"DISCOVER GATEWAY ERROR: "
,
err
)
return
nil
return
nil
}
}
addr
,
err
:=
nat
.
GetDeviceAddress
()
addr
,
err
:=
nat
.
GetDeviceAddress
()
...
@@ -48,7 +46,6 @@ func DiscoverNAT() *NAT {
...
@@ -48,7 +46,6 @@ func DiscoverNAT() *NAT {
}
else
{
}
else
{
log
.
Debug
(
"DiscoverGateway address:"
,
addr
)
log
.
Debug
(
"DiscoverGateway address:"
,
addr
)
}
}
log
.
Error
(
"NEW NAT!"
)
return
newNAT
(
nat
)
return
newNAT
(
nat
)
}
}
...
@@ -282,7 +279,6 @@ func (nat *NAT) rmMapping(m *mapping) {
...
@@ -282,7 +279,6 @@ func (nat *NAT) rmMapping(m *mapping) {
// Clients should not store the mapped results, but rather always
// Clients should not store the mapped results, but rather always
// poll our object for the latest mappings.
// poll our object for the latest mappings.
func
(
nat
*
NAT
)
NewMapping
(
maddr
ma
.
Multiaddr
)
(
Mapping
,
error
)
{
func
(
nat
*
NAT
)
NewMapping
(
maddr
ma
.
Multiaddr
)
(
Mapping
,
error
)
{
log
.
Error
(
"NEW MAPPING!"
)
if
nat
==
nil
{
if
nat
==
nil
{
return
nil
,
fmt
.
Errorf
(
"no nat available"
)
return
nil
,
fmt
.
Errorf
(
"no nat available"
)
}
}
...
@@ -330,7 +326,6 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
...
@@ -330,7 +326,6 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
}
}
func
(
nat
*
NAT
)
establishMapping
(
m
*
mapping
)
{
func
(
nat
*
NAT
)
establishMapping
(
m
*
mapping
)
{
log
.
Error
(
"establishMapping!"
)
oldport
:=
m
.
ExternalPort
()
oldport
:=
m
.
ExternalPort
()
log
.
Debugf
(
"Attempting port map: %s/%d"
,
m
.
Protocol
(),
m
.
InternalPort
())
log
.
Debugf
(
"Attempting port map: %s/%d"
,
m
.
Protocol
(),
m
.
InternalPort
())
newport
,
err
:=
nat
.
nat
.
AddPortMapping
(
m
.
Protocol
(),
m
.
InternalPort
(),
"http"
,
MappingDuration
)
newport
,
err
:=
nat
.
nat
.
AddPortMapping
(
m
.
Protocol
(),
m
.
InternalPort
(),
"http"
,
MappingDuration
)
...
@@ -431,7 +426,6 @@ func (nat *NAT) MappedAddrs() map[ma.Multiaddr]ma.Multiaddr {
...
@@ -431,7 +426,6 @@ func (nat *NAT) MappedAddrs() map[ma.Multiaddr]ma.Multiaddr {
// This set of mappings _may not_ be correct, as NAT devices are finicky.
// This set of mappings _may not_ be correct, as NAT devices are finicky.
// Consider this with _best effort_ semantics.
// Consider this with _best effort_ semantics.
func
(
nat
*
NAT
)
ExternalAddrs
()
[]
ma
.
Multiaddr
{
func
(
nat
*
NAT
)
ExternalAddrs
()
[]
ma
.
Multiaddr
{
log
.
Error
(
"EXTERNAL ADDRS"
)
mappings
:=
nat
.
Mappings
()
mappings
:=
nat
.
Mappings
()
addrs
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
mappings
))
addrs
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
mappings
))
for
_
,
m
:=
range
mappings
{
for
_
,
m
:=
range
mappings
{
...
...
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