Skip to content

Commit f003ad0

Browse files
author
Lars Gierth
committed
core: make announced swarm addresses configurable
License: MIT Signed-off-by: Lars Gierth <[email protected]>
1 parent 20dae52 commit f003ad0

File tree

6 files changed

+138
-8
lines changed

6 files changed

+138
-8
lines changed

cmd/ipfs/daemon.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,15 +500,29 @@ func printSwarmAddrs(node *core.IpfsNode) {
500500
fmt.Println("Swarm not listening, running in offline mode.")
501501
return
502502
}
503+
504+
var lisAddrs []string
505+
ifaceAddrs, err := node.PeerHost.Network().InterfaceListenAddresses()
506+
if err != nil {
507+
log.Errorf("failed to read listening addresses: %s", err)
508+
}
509+
for _, addr := range ifaceAddrs {
510+
lisAddrs = append(lisAddrs, addr.String())
511+
}
512+
sort.Sort(sort.StringSlice(lisAddrs))
513+
for _, addr := range lisAddrs {
514+
fmt.Printf("Swarm listening on %s\n", addr)
515+
}
516+
503517
var addrs []string
504518
for _, addr := range node.PeerHost.Addrs() {
505519
addrs = append(addrs, addr.String())
506520
}
507521
sort.Sort(sort.StringSlice(addrs))
508-
509522
for _, addr := range addrs {
510-
fmt.Printf("Swarm listening on %s\n", addr)
523+
fmt.Printf("Swarm announcing %s\n", addr)
511524
}
525+
512526
}
513527

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

core/core.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import (
6767
addrutil "gx/ipfs/QmbH3urJHTrZSUETgvQRriWM6mMFqyNSwCqnhknxfSGVWv/go-addr-util"
6868
yamux "gx/ipfs/Qmbn7RYyWzBVXiUp9jZ1dA4VADHy9DtS7iZLwfhEUQvm3U/go-smux-yamux"
6969
ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
70+
mafilter "gx/ipfs/Qmd3bFpvhYdjX7q11bNKsZnxwtWbwTsgkFhzemqLb5L4Wy/go-maddr-filter"
7071
peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
7172
metrics "gx/ipfs/QmdibiN2wzuuXXz4JvqQ1ZGW3eUkoAy1AWznHFau6iePCc/go-libp2p-metrics"
7273
smux "gx/ipfs/QmeZBgYBHvxMukGK5ojg28BCNLB9SeXqT7XXg6o7r2GbJy/go-stream-muxer"
@@ -211,8 +212,17 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
211212
}()
212213
}
213214

215+
addrsFactory, err := makeAddrsFactory(cfg.Addresses)
216+
if err != nil {
217+
return err
218+
}
219+
220+
hostopts := &ConstructPeerHostOpts{
221+
AddrsFactory: addrsFactory,
222+
DisableNatPortMap: cfg.Swarm.DisableNatPortMap,
223+
}
214224
peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter,
215-
addrfilter, tpt, protec, &ConstructPeerHostOpts{DisableNatPortMap: cfg.Swarm.DisableNatPortMap})
225+
addrfilter, tpt, protec, hostopts)
216226
if err != nil {
217227
return err
218228
}
@@ -260,6 +270,50 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
260270
return n.Bootstrap(DefaultBootstrapConfig)
261271
}
262272

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

@@ -708,6 +762,7 @@ func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
708762

709763
type ConstructPeerHostOpts struct {
710764
DisableNatPortMap bool
765+
AddrsFactory p2pbhost.AddrsFactory
711766
}
712767

713768
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)
@@ -733,6 +788,9 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr
733788
if !opts.DisableNatPortMap {
734789
hostOpts = append(hostOpts, p2pbhost.NATPortMap)
735790
}
791+
if opts.AddrsFactory != nil {
792+
hostOpts = append(hostOpts, opts.AddrsFactory)
793+
}
736794

737795
host := p2pbhost.New(network, hostOpts...)
738796

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@
306306
"hash": "QmfJHywXQu98UeZtGJBQrPAR6AtmDjjbe3qjTo9piXHPnx",
307307
"name": "murmur3",
308308
"version": "0.0.0"
309+
},
310+
{
311+
"author": "whyrusleeping",
312+
"hash": "Qmd3bFpvhYdjX7q11bNKsZnxwtWbwTsgkFhzemqLb5L4Wy",
313+
"name": "go-maddr-filter",
314+
"version": "1.1.3"
309315
}
310316
],
311317
"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/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)