Skip to content

Commit f686b4c

Browse files
Merge pull request #3948 from ipfs/feat/addresses-announce
core: make announced swarm addresses configurable
2 parents 8ad3b11 + 952f658 commit f686b4c

File tree

7 files changed

+140
-8
lines changed

7 files changed

+140
-8
lines changed

cmd/ipfs/daemon.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,15 +497,29 @@ func printSwarmAddrs(node *core.IpfsNode) {
497497
fmt.Println("Swarm not listening, running in offline mode.")
498498
return
499499
}
500+
501+
var lisAddrs []string
502+
ifaceAddrs, err := node.PeerHost.Network().InterfaceListenAddresses()
503+
if err != nil {
504+
log.Errorf("failed to read listening addresses: %s", err)
505+
}
506+
for _, addr := range ifaceAddrs {
507+
lisAddrs = append(lisAddrs, addr.String())
508+
}
509+
sort.Sort(sort.StringSlice(lisAddrs))
510+
for _, addr := range lisAddrs {
511+
fmt.Printf("Swarm listening on %s\n", addr)
512+
}
513+
500514
var addrs []string
501515
for _, addr := range node.PeerHost.Addrs() {
502516
addrs = append(addrs, addr.String())
503517
}
504518
sort.Sort(sort.StringSlice(addrs))
505-
506519
for _, addr := range addrs {
507-
fmt.Printf("Swarm listening on %s\n", addr)
520+
fmt.Printf("Swarm announcing %s\n", addr)
508521
}
522+
509523
}
510524

511525
// serveHTTPGateway collects options, creates listener, prints status message and starts serving requests

core/core.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
pnet "gx/ipfs/QmNMCAuxnQFHLGWcvay3DmVFrKuY6Y2nsc9vzsf4gVouJV/go-libp2p-pnet"
4646
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
4747
routing "gx/ipfs/QmPjTrrSfE6TzLv6ya6VWhGcCgPrUAdcgrDcQyRDX2VyW1/go-libp2p-routing"
48+
mafilter "gx/ipfs/QmQBB2dQLmQHJgs2gqZ3iqL2XiuCtUCvXzWt5kMXDf5Zcr/go-maddr-filter"
4849
ipnet "gx/ipfs/QmQq9YzmdFdWNTDdArueGyD7L5yyiRQigrRHJnTGkxcEjT/go-libp2p-interface-pnet"
4950
dht "gx/ipfs/QmRKEzkaiwud2LnwJ9CgBrKw122ddKPTMtLizV3DNimVRD/go-libp2p-kad-dht"
5051
p2phost "gx/ipfs/QmRNyPNJGNCaZyYonJj7owciWTsMd9gRfEKmZY3o6xwN3h/go-libp2p-host"
@@ -212,8 +213,17 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
212213
}()
213214
}
214215

216+
addrsFactory, err := makeAddrsFactory(cfg.Addresses)
217+
if err != nil {
218+
return err
219+
}
220+
221+
hostopts := &ConstructPeerHostOpts{
222+
AddrsFactory: addrsFactory,
223+
DisableNatPortMap: cfg.Swarm.DisableNatPortMap,
224+
}
215225
peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter,
216-
addrfilter, tpt, protec, &ConstructPeerHostOpts{DisableNatPortMap: cfg.Swarm.DisableNatPortMap})
226+
addrfilter, tpt, protec, hostopts)
217227
if err != nil {
218228
return err
219229
}
@@ -263,6 +273,52 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
263273
return n.Bootstrap(DefaultBootstrapConfig)
264274
}
265275

276+
func makeAddrsFactory(cfg config.Addresses) (p2pbhost.AddrsFactory, error) {
277+
var annAddrs []ma.Multiaddr
278+
for _, addr := range cfg.Announce {
279+
maddr, err := ma.NewMultiaddr(addr)
280+
if err != nil {
281+
return nil, err
282+
}
283+
annAddrs = append(annAddrs, maddr)
284+
}
285+
286+
filters := mafilter.NewFilters()
287+
noAnnAddrs := map[string]bool{}
288+
for _, addr := range cfg.NoAnnounce {
289+
f, err := mamask.NewMask(addr)
290+
if err == nil {
291+
filters.AddDialFilter(f)
292+
continue
293+
}
294+
maddr, err := ma.NewMultiaddr(addr)
295+
if err != nil {
296+
return nil, err
297+
}
298+
noAnnAddrs[maddr.String()] = true
299+
}
300+
301+
return func(allAddrs []ma.Multiaddr) []ma.Multiaddr {
302+
var addrs []ma.Multiaddr
303+
if len(annAddrs) > 0 {
304+
addrs = annAddrs
305+
} else {
306+
addrs = allAddrs
307+
}
308+
309+
var out []ma.Multiaddr
310+
for _, maddr := range addrs {
311+
// check for exact matches
312+
ok, _ := noAnnAddrs[maddr.String()]
313+
// check for /ipcidr matches
314+
if !ok && !filters.AddrBlocked(maddr) {
315+
out = append(out, maddr)
316+
}
317+
}
318+
return out
319+
}, nil
320+
}
321+
266322
func makeSmuxTransport(mplexExp bool) smux.Transport {
267323
mstpt := mssmux.NewBlankTransport()
268324

@@ -705,6 +761,7 @@ func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
705761

706762
type ConstructPeerHostOpts struct {
707763
DisableNatPortMap bool
764+
AddrsFactory p2pbhost.AddrsFactory
708765
}
709766

710767
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)
@@ -730,6 +787,9 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr
730787
if !opts.DisableNatPortMap {
731788
hostOpts = append(hostOpts, p2pbhost.NATPortMap)
732789
}
790+
if opts.AddrsFactory != nil {
791+
hostOpts = append(hostOpts, opts.AddrsFactory)
792+
}
733793

734794
host := p2pbhost.New(network, hostOpts...)
735795

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,11 @@
446446
"hash": "Qma7Kuwun7w8SZphjEPDVxvGfetBkqdNGmigDA13sJdLex",
447447
"name": "go-ipld-git",
448448
"version": "0.1.3"
449+
},
450+
{
451+
"hash": "QmQBB2dQLmQHJgs2gqZ3iqL2XiuCtUCvXzWt5kMXDf5Zcr",
452+
"name": "go-maddr-filter",
453+
"version": "1.1.4"
449454
}
450455
],
451456
"gxVersion": "0.10.0",

repo/config/addresses.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package config
22

33
// Addresses stores the (string) multiaddr addresses for the node.
44
type Addresses struct {
5-
Swarm []string // addresses for the swarm network
6-
API string // address for the local API (RPC)
7-
Gateway string // address to listen on for IPFS HTTP object gateway
5+
Swarm []string // addresses for the swarm to listen on
6+
Announce []string // swarm addresses to announce to the network
7+
NoAnnounce []string // swarm addresses not to announce to the network
8+
API string // address for the local API (RPC)
9+
Gateway string // address to listen on for IPFS HTTP object gateway
810
}

repo/config/init.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
3636
// "/ip4/0.0.0.0/udp/4002/utp", // disabled for now.
3737
"/ip6/::/tcp/4001",
3838
},
39-
API: "/ip4/127.0.0.1/tcp/5001",
40-
Gateway: "/ip4/127.0.0.1/tcp/8080",
39+
Announce: []string{},
40+
NoAnnounce: []string{},
41+
API: "/ip4/127.0.0.1/tcp/5001",
42+
Gateway: "/ip4/127.0.0.1/tcp/8080",
4143
},
4244

4345
Datastore: datastore,

test/sharness/t0060-daemon.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ test_expect_success "ipfs daemon output looks good" '
3939
STARTFILE="ipfs cat /ipfs/$HASH_WELCOME_DOCS/readme" &&
4040
echo "Initializing daemon..." >expected_daemon &&
4141
sed "s/^/Swarm listening on /" local_addrs >>expected_daemon &&
42+
sed "s/^/Swarm announcing /" local_addrs >>expected_daemon &&
4243
echo "API server listening on '$API_MADDR'" >>expected_daemon &&
4344
echo "Gateway (readonly) server listening on '$GWAY_MADDR'" >>expected_daemon &&
4445
echo "Daemon is ready" >>expected_daemon &&

test/sharness/t0140-swarm.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,52 @@ test_expect_success "cant trigger a dial backoff with swarm connect" '
4949

5050
test_kill_ipfs_daemon
5151

52+
announceCfg='["/ip4/127.0.0.1/tcp/4001", "/ip4/1.2.3.4/tcp/1234"]'
53+
test_expect_success "test_config_set succeeds" "
54+
ipfs config --json Addresses.Announce '$announceCfg'
55+
"
56+
57+
test_launch_ipfs_daemon
58+
59+
test_expect_success 'Addresses.Announce affects addresses' '
60+
ipfs swarm addrs local >actual &&
61+
grep "/ip4/1.2.3.4/tcp/1234" actual &&
62+
ipfs id -f"<addrs>" | xargs -n1 echo >actual &&
63+
grep "/ip4/1.2.3.4/tcp/1234" actual
64+
'
65+
66+
test_kill_ipfs_daemon
67+
68+
noAnnounceCfg='["/ip4/1.2.3.4/tcp/1234"]'
69+
test_expect_success "test_config_set succeeds" "
70+
ipfs config --json Addresses.NoAnnounce '$noAnnounceCfg'
71+
"
72+
73+
test_launch_ipfs_daemon
74+
75+
test_expect_success "Addresses.NoAnnounce affects addresses" '
76+
ipfs swarm addrs local >actual &&
77+
grep -v "/ip4/1.2.3.4/tcp/1234" actual &&
78+
ipfs id -f"<addrs>" | xargs -n1 echo >actual &&
79+
grep -v "/ip4/1.2.3.4/tcp/1234" actual
80+
'
81+
82+
test_kill_ipfs_daemon
83+
84+
noAnnounceCfg='["/ip4/1.2.3.4/ipcidr/16"]'
85+
test_expect_success "test_config_set succeeds" "
86+
ipfs config --json Addresses.NoAnnounce '$noAnnounceCfg'
87+
"
88+
89+
test_launch_ipfs_daemon
90+
91+
test_expect_success "Addresses.NoAnnounce with /ipcidr affects addresses" '
92+
ipfs swarm addrs local >actual &&
93+
grep -v "/ip4/1.2.3.4/tcp/1234" actual &&
94+
ipfs id -f"<addrs>" | xargs -n1 echo >actual &&
95+
grep -v "/ip4/1.2.3.4/tcp/1234" actual
96+
'
97+
98+
test_kill_ipfs_daemon
99+
52100
test_done

0 commit comments

Comments
 (0)