Skip to content

Commit

Permalink
core: make announced swarm addresses configurable
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
  • Loading branch information
Lars Gierth committed May 31, 2017
1 parent 20dae52 commit f003ad0
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 8 deletions.
18 changes: 16 additions & 2 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,15 +500,29 @@ func printSwarmAddrs(node *core.IpfsNode) {
fmt.Println("Swarm not listening, running in offline mode.")
return
}

var lisAddrs []string
ifaceAddrs, err := node.PeerHost.Network().InterfaceListenAddresses()
if err != nil {
log.Errorf("failed to read listening addresses: %s", err)
}
for _, addr := range ifaceAddrs {
lisAddrs = append(lisAddrs, addr.String())
}
sort.Sort(sort.StringSlice(lisAddrs))
for _, addr := range lisAddrs {
fmt.Printf("Swarm listening on %s\n", addr)
}

var addrs []string
for _, addr := range node.PeerHost.Addrs() {
addrs = append(addrs, addr.String())
}
sort.Sort(sort.StringSlice(addrs))

for _, addr := range addrs {
fmt.Printf("Swarm listening on %s\n", addr)
fmt.Printf("Swarm announcing %s\n", addr)
}

}

// serveHTTPGateway collects options, creates listener, prints status message and starts serving requests
Expand Down
60 changes: 59 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
addrutil "gx/ipfs/QmbH3urJHTrZSUETgvQRriWM6mMFqyNSwCqnhknxfSGVWv/go-addr-util"
yamux "gx/ipfs/Qmbn7RYyWzBVXiUp9jZ1dA4VADHy9DtS7iZLwfhEUQvm3U/go-smux-yamux"
ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
mafilter "gx/ipfs/Qmd3bFpvhYdjX7q11bNKsZnxwtWbwTsgkFhzemqLb5L4Wy/go-maddr-filter"
peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
metrics "gx/ipfs/QmdibiN2wzuuXXz4JvqQ1ZGW3eUkoAy1AWznHFau6iePCc/go-libp2p-metrics"
smux "gx/ipfs/QmeZBgYBHvxMukGK5ojg28BCNLB9SeXqT7XXg6o7r2GbJy/go-stream-muxer"
Expand Down Expand Up @@ -211,8 +212,17 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
}()
}

addrsFactory, err := makeAddrsFactory(cfg.Addresses)
if err != nil {
return err
}

hostopts := &ConstructPeerHostOpts{
AddrsFactory: addrsFactory,
DisableNatPortMap: cfg.Swarm.DisableNatPortMap,
}
peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter,
addrfilter, tpt, protec, &ConstructPeerHostOpts{DisableNatPortMap: cfg.Swarm.DisableNatPortMap})
addrfilter, tpt, protec, hostopts)
if err != nil {
return err
}
Expand Down Expand Up @@ -260,6 +270,50 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
return n.Bootstrap(DefaultBootstrapConfig)
}

func makeAddrsFactory(cfg config.Addresses) (p2pbhost.AddrsFactory, error) {
var annAddrs []ma.Multiaddr
for _, addr := range cfg.Announce {
maddr, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}
annAddrs = append(annAddrs, maddr)
}

filters := mafilter.NewFilters()
noAnnAddrs := map[string]bool{}
for _, addr := range cfg.NoAnnounce {
f, err := mamask.NewMask(addr)
if err == nil {
filters.AddDialFilter(f)
continue
}
maddr, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}
noAnnAddrs[maddr.String()] = true
}

return func(allAddrs []ma.Multiaddr) []ma.Multiaddr {
var addrs []ma.Multiaddr
if len(annAddrs) > 0 {
addrs = annAddrs[:]
} else {
addrs = allAddrs
}

var out []ma.Multiaddr
for _, maddr := range addrs {
ok, _ := noAnnAddrs[maddr.String()]
if !ok && !filters.AddrBlocked(maddr) {
out = append(out, maddr)
}
}
return out
}, nil
}

func makeSmuxTransport(mplexExp bool) smux.Transport {
mstpt := mssmux.NewBlankTransport()

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

type ConstructPeerHostOpts struct {
DisableNatPortMap bool
AddrsFactory p2pbhost.AddrsFactory
}

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

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

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@
"hash": "QmfJHywXQu98UeZtGJBQrPAR6AtmDjjbe3qjTo9piXHPnx",
"name": "murmur3",
"version": "0.0.0"
},
{
"author": "whyrusleeping",
"hash": "Qmd3bFpvhYdjX7q11bNKsZnxwtWbwTsgkFhzemqLb5L4Wy",
"name": "go-maddr-filter",
"version": "1.1.3"
}
],
"gxVersion": "0.10.0",
Expand Down
8 changes: 5 additions & 3 deletions repo/config/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package config

// Addresses stores the (string) multiaddr addresses for the node.
type Addresses struct {
Swarm []string // addresses for the swarm network
API string // address for the local API (RPC)
Gateway string // address to listen on for IPFS HTTP object gateway
Swarm []string // addresses for the swarm to listen on
Announce []string // swarm addresses to announce to the network
NoAnnounce []string // swarm addresses not to announce to the network
API string // address for the local API (RPC)
Gateway string // address to listen on for IPFS HTTP object gateway
}
6 changes: 4 additions & 2 deletions repo/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
// "/ip4/0.0.0.0/udp/4002/utp", // disabled for now.
"/ip6/::/tcp/4001",
},
API: "/ip4/127.0.0.1/tcp/5001",
Gateway: "/ip4/127.0.0.1/tcp/8080",
Announce: []string{},
NoAnnounce: []string{},
API: "/ip4/127.0.0.1/tcp/5001",
Gateway: "/ip4/127.0.0.1/tcp/8080",
},

Datastore: datastore,
Expand Down
48 changes: 48 additions & 0 deletions test/sharness/t0140-swarm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,52 @@ test_expect_success "cant trigger a dial backoff with swarm connect" '

test_kill_ipfs_daemon

announceCfg='["/ip4/127.0.0.1/tcp/4001", "/ip4/1.2.3.4/tcp/1234"]'
test_expect_success "test_config_set succeeds" "
ipfs config --json Addresses.Announce '$announceCfg'
"

test_launch_ipfs_daemon

test_expect_success 'Addresses.Announce affects addresses' '
ipfs swarm addrs local >actual &&
grep "/ip4/1.2.3.4/tcp/1234" actual &&
ipfs id -f"<addrs>" | xargs -n1 echo >actual &&
grep "/ip4/1.2.3.4/tcp/1234" actual
'

test_kill_ipfs_daemon

noAnnounceCfg='["/ip4/1.2.3.4/tcp/1234"]'
test_expect_success "test_config_set succeeds" "
ipfs config --json Addresses.NoAnnounce '$noAnnounceCfg'
"

test_launch_ipfs_daemon

test_expect_success "Addresses.NoAnnounce affects addresses" '
ipfs swarm addrs local >actual &&
grep -v "/ip4/1.2.3.4/tcp/1234" actual &&
ipfs id -f"<addrs>" | xargs -n1 echo >actual &&
grep -v "/ip4/1.2.3.4/tcp/1234" actual
'

test_kill_ipfs_daemon

noAnnounceCfg='["/ip4/1.2.3.4/ipcidr/16"]'
test_expect_success "test_config_set succeeds" "
ipfs config --json Addresses.NoAnnounce '$noAnnounceCfg'
"

test_launch_ipfs_daemon

test_expect_success "Addresses.NoAnnounce with /ipcidr affects addresses" '
ipfs swarm addrs local >actual &&
grep -v "/ip4/1.2.3.4/tcp/1234" actual &&
ipfs id -f"<addrs>" | xargs -n1 echo >actual &&
grep -v "/ip4/1.2.3.4/tcp/1234" actual
'

test_kill_ipfs_daemon

test_done

0 comments on commit f003ad0

Please sign in to comment.