提交 c762bbfc 作者: Juan Benet

Merge pull request #1682 from ipfs/fix/nat-spam

Fix/nat spam
...@@ -29,6 +29,9 @@ var log = eventlog.Logger("nat") ...@@ -29,6 +29,9 @@ var log = eventlog.Logger("nat")
// Port mappings are renewed every (MappingDuration / 3) // Port mappings are renewed every (MappingDuration / 3)
const MappingDuration = time.Second * 60 const MappingDuration = time.Second * 60
// CacheTime is the time a mapping will cache an external address for
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 {
...@@ -159,6 +162,9 @@ type mapping struct { ...@@ -159,6 +162,9 @@ type mapping struct {
extport int extport int
intaddr ma.Multiaddr intaddr ma.Multiaddr
proc goprocess.Process proc goprocess.Process
cached ma.Multiaddr
cacheTime time.Time
} }
func (m *mapping) NAT() *NAT { func (m *mapping) NAT() *NAT {
...@@ -198,6 +204,10 @@ func (m *mapping) InternalAddr() ma.Multiaddr { ...@@ -198,6 +204,10 @@ func (m *mapping) InternalAddr() ma.Multiaddr {
} }
func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
if time.Now().Sub(m.cacheTime) < CacheTime {
return m.cached, nil
}
if m.ExternalPort() == 0 { // dont even try right now. if m.ExternalPort() == 0 { // dont even try right now.
return nil, ErrNoMapping return nil, ErrNoMapping
} }
...@@ -224,6 +234,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { ...@@ -224,6 +234,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
} }
maddr2 := ipmaddr.Encapsulate(tcp) maddr2 := ipmaddr.Encapsulate(tcp)
m.cached = maddr2
m.cacheTime = time.Now()
return maddr2, nil return maddr2, nil
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论