Skip to content

Commit 5082f83

Browse files
committed
core: add NatPortMap and default it to true
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 1fb3b68 commit 5082f83

File tree

9 files changed

+88
-14
lines changed

9 files changed

+88
-14
lines changed

cmd/ipfs/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ environment variable:
8989
return
9090
}
9191

92-
conf = &config.Config{}
92+
conf = config.NewConfig()
9393
if err := json.NewDecoder(confFile).Decode(conf); err != nil {
9494
res.SetError(err, cmds.ErrNormal)
9595
return

core/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (cfg *BuildCfg) fillDefaults() error {
8686
}
8787

8888
func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
89-
c := cfg.Config{}
89+
c := cfg.NewConfig()
9090
priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader)
9191
if err != nil {
9292
return nil, err
@@ -109,7 +109,7 @@ func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
109109

110110
return &repo.Mock{
111111
D: dstore,
112-
C: c,
112+
C: *c,
113113
}, nil
114114
}
115115

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{NatPortMap: cfg.Swarm.NatPortMap})
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+
NatPortMap 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+
newOpts := []interface{}{bwr}
736+
if opts.NatPortMap {
737+
newOpts = append(newOpts, p2pbhost.NATPortMap)
738+
}
739+
740+
host := p2pbhost.New(network, newOpts)
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/config.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ type Config struct {
3434
Experimental Experiments
3535
}
3636

37+
// NewConfig creates any new Config with all the values set the
38+
// default
39+
func NewConfig() *Config {
40+
conf := &Config{}
41+
conf.SetDefaults()
42+
return conf
43+
}
44+
45+
func (conf *Config) SetDefaults() {
46+
// sets defaults other that are not the go-standard of
47+
// false, 0, or the empty string
48+
conf.Swarm.NatPortMap = true
49+
}
50+
3751
const (
3852
// DefaultPathName is the default config dir name
3953
DefaultPathName = ".ipfs"
@@ -95,11 +109,11 @@ func FromMap(v map[string]interface{}) (*Config, error) {
95109
if err := json.NewEncoder(buf).Encode(v); err != nil {
96110
return nil, err
97111
}
98-
var conf Config
99-
if err := json.NewDecoder(buf).Decode(&conf); err != nil {
112+
conf := NewConfig()
113+
if err := json.NewDecoder(buf).Decode(conf); err != nil {
100114
return nil, fmt.Errorf("Failure to decode config: %s", err)
101115
}
102-
return &conf, nil
116+
return conf, nil
103117
}
104118

105119
func ToMap(conf *Config) (map[string]interface{}, error) {
@@ -113,3 +127,4 @@ func ToMap(conf *Config) (map[string]interface{}, error) {
113127
}
114128
return m, nil
115129
}
130+

repo/config/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
7272
Interval: "12h",
7373
},
7474
}
75+
conf.SetDefaults()
7576

7677
return conf, nil
7778
}

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+
NatPortMap bool // default: true
67
}

repo/fsrepo/serialize/serialize.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ func Load(filename string) (*config.Config, error) {
6363
return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
6464
}
6565

66-
var cfg config.Config
67-
err := ReadConfigFile(filename, &cfg)
66+
cfg := config.NewConfig()
67+
68+
err := ReadConfigFile(filename, cfg)
6869
if err != nil {
6970
return nil, err
7071
}
7172

72-
return &cfg, err
73+
return cfg, err
7374
}

test/sharness/t0023-config-default.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2014 Christian Couder
4+
# MIT Licensed; see the LICENSE file in this repository.
5+
#
6+
7+
test_description="Test init command with default config"
8+
9+
. lib/test-lib.sh
10+
11+
test_init_ipfs
12+
13+
test_expect_success "Swarm.NatPortMap set to true in inital config" '
14+
echo "true" > expected &&
15+
ipfs config Swarm.NatPortMap > actual &&
16+
test_cmp expected actual
17+
'
18+
19+
test_expect_success "Swarm.NatPortMap has line in config file" '
20+
grep -q NatPortMap .ipfs/config
21+
'
22+
23+
test_expect_success "remove Swarm.NatPortMap from config file" '
24+
sed -i "s/NatPortMap/XxxYyyyZzz/" .ipfs/config
25+
'
26+
27+
test_expect_success "load config file by replacing a unrelated key" '
28+
ipfs config --json Swarm.DisableBandwidthMetrics false
29+
'
30+
31+
test_expect_success "Swarm.NatPortMap set to true in new config" '
32+
echo "true" > expected &&
33+
ipfs config Swarm.NatPortMap > actual &&
34+
test_cmp expected actual
35+
'
36+
37+
test_expect_success "reset Swarm group to default values" '
38+
ipfs config --json Swarm {}
39+
'
40+
41+
test_expect_success "Swarm.NatPortMap still set to true in reset config" '
42+
echo "true" > expected &&
43+
ipfs config Swarm.NatPortMap > actual &&
44+
test_cmp expected actual
45+
'
46+
47+
test_done

0 commit comments

Comments
 (0)