Skip to content

Commit 163d12d

Browse files
committed
Add DisableNatPortMap option.
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 1fb3b68 commit 163d12d

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

core/core.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
214214
}
215215

216216
peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter,
217-
addrfilter, tpt, protec)
217+
addrfilter, tpt, protec, &ConstructPeerHostOpts{DisableNatPortMap: cfg.Swarm.DisableNatPortMap})
218218
if err != nil {
219219
return err
220220
}
@@ -709,12 +709,16 @@ func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
709709
return listen, nil
710710
}
711711

712-
type HostOption func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport, protc ipnet.Protector) (p2phost.Host, error)
712+
type ConstructPeerHostOpts struct {
713+
DisableNatPortMap bool
714+
}
715+
716+
type HostOption func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport, protc ipnet.Protector, opts *ConstructPeerHostOpts) (p2phost.Host, error)
713717

714718
var DefaultHostOption HostOption = constructPeerHost
715719

716720
// isolates the complex initialization steps
717-
func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport, protec ipnet.Protector) (p2phost.Host, error) {
721+
func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport, protec ipnet.Protector, opts *ConstructPeerHostOpts) (p2phost.Host, error) {
718722

719723
// no addresses to begin with. we'll start later.
720724
swrm, err := swarm.NewSwarmWithProtector(ctx, nil, id, ps, protec, tpt, bwr)
@@ -728,7 +732,12 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr
728732
network.Swarm().Filters.AddDialFilter(f)
729733
}
730734

731-
host := p2pbhost.New(network, p2pbhost.NATPortMap, bwr)
735+
hostOpts := []interface{}{bwr}
736+
if !opts.DisableNatPortMap {
737+
hostOpts = append(hostOpts, p2pbhost.NATPortMap)
738+
}
739+
740+
host := p2pbhost.New(network, hostOpts...)
732741

733742
return host, nil
734743
}

core/mock/mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func NewMockNode() (*core.IpfsNode, error) {
3434
}
3535

3636
func MockHostOption(mn mocknet.Mocknet) core.HostOption {
37-
return func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, _ smux.Transport, _ ipnet.Protector) (host.Host, error) {
37+
return func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, _ smux.Transport, _ ipnet.Protector, _ *core.ConstructPeerHostOpts) (host.Host, error) {
3838
return mn.AddPeerWithPeerstore(id, ps)
3939
}
4040
}

repo/config/swarm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ package config
33
type SwarmConfig struct {
44
AddrFilters []string
55
DisableBandwidthMetrics bool
6+
DisableNatPortMap bool
67
}

0 commit comments

Comments
 (0)