From 787d7338d2626d365cc7317487bccae02da81db4 Mon Sep 17 00:00:00 2001 From: Ivan Schasny Date: Wed, 7 Jun 2023 11:02:05 +0100 Subject: [PATCH] feat: correct list of addresses for delegated routing --- cmd/ipfs/daemon.go | 2 +- core/core_test.go | 2 +- core/node/libp2p/routingopt.go | 34 ++++++++++++++++++++++++++--- core/node/libp2p/routingopt_test.go | 34 +++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 core/node/libp2p/routingopt_test.go diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 1e03a8264a8..a61f1473b15 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -429,7 +429,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment cfg.Routing.Routers, cfg.Routing.Methods, cfg.Identity.PeerID, - cfg.Addresses.Swarm, + cfg.Addresses, cfg.Identity.PrivKey, ) default: diff --git a/core/core_test.go b/core/core_test.go index 5e8c1336c34..2d7e8927cf3 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -256,7 +256,7 @@ func GetNode(t *testing.T, reframeURLs ...string) *IpfsNode { cfg.Routing.Routers, cfg.Routing.Methods, cfg.Identity.PeerID, - cfg.Addresses.Swarm, + cfg.Addresses, cfg.Identity.PrivKey, ), }, diff --git a/core/node/libp2p/routingopt.go b/core/node/libp2p/routingopt.go index b363f25b7da..da940837f22 100644 --- a/core/node/libp2p/routingopt.go +++ b/core/node/libp2p/routingopt.go @@ -47,7 +47,7 @@ func constructDefaultHTTPRouters(cfg *config.Config) ([]*routinghelpers.Parallel var routers []*routinghelpers.ParallelRouter // Append HTTP routers for additional speed for _, endpoint := range defaultHTTPRouters { - httpRouter, err := irouting.ConstructHTTPRouter(endpoint, cfg.Identity.PeerID, cfg.Addresses.Swarm, cfg.Identity.PrivKey) + httpRouter, err := irouting.ConstructHTTPRouter(endpoint, cfg.Identity.PeerID, httpAddrsFromConfig(cfg.Addresses), cfg.Identity.PrivKey) if err != nil { return nil, err } @@ -123,7 +123,7 @@ func constructDHTRouting(mode dht.ModeOpt) RoutingOption { } // ConstructDelegatedRouting is used when Routing.Type = "custom" -func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, peerID string, addrs []string, privKey string) RoutingOption { +func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, peerID string, addrs config.Addresses, privKey string) RoutingOption { return func(args RoutingOptionArgs) (routing.Routing, error) { return irouting.Parse(routers, methods, &irouting.ExtraDHTParams{ @@ -135,7 +135,7 @@ func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, p }, &irouting.ExtraHTTPParams{ PeerID: peerID, - Addrs: addrs, + Addrs: httpAddrsFromConfig(addrs), PrivKeyB64: privKey, }) } @@ -151,3 +151,31 @@ var ( DHTServerOption = constructDHTRouting(dht.ModeServer) NilRouterOption = constructNilRouting ) + +// httpAddrsFromConfig creates a list of addresses from the provided configuration to be used by HTTP delegated routers. +func httpAddrsFromConfig(cfgAddrs config.Addresses) []string { + // Swarm addrs are announced by default + addrs := cfgAddrs.Swarm + // if Announce addrs are specified - override Swarm + if len(cfgAddrs.Announce) > 0 { + addrs = cfgAddrs.Announce + } else if len(cfgAddrs.NoAnnounce) > 0 { + // if Announce adds are not specified - filter Swarm addrs with NoAnnounce list + maddrs := map[string]struct{}{} + for _, addr := range addrs { + maddrs[addr] = struct{}{} + } + for _, addr := range cfgAddrs.NoAnnounce { + delete(maddrs, addr) + } + addrs = make([]string, 0, len(maddrs)) + for k := range maddrs { + addrs = append(addrs, k) + } + } + // append AppendAnnounce addrs to the result list + if len(cfgAddrs.AppendAnnounce) > 0 { + addrs = append(addrs, cfgAddrs.AppendAnnounce...) + } + return addrs +} diff --git a/core/node/libp2p/routingopt_test.go b/core/node/libp2p/routingopt_test.go new file mode 100644 index 00000000000..7a19b9d84db --- /dev/null +++ b/core/node/libp2p/routingopt_test.go @@ -0,0 +1,34 @@ +package libp2p + +import ( + "testing" + + config "github.com/ipfs/kubo/config" + "github.com/stretchr/testify/require" +) + +func TestHttpAddrsFromConfig(t *testing.T) { + require.Equal(t, []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"}, + httpAddrsFromConfig(config.Addresses{ + Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"}, + }), "Swarm addrs should be taken by default") + + require.Equal(t, []string{"/ip4/192.168.0.1/tcp/4001"}, + httpAddrsFromConfig(config.Addresses{ + Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"}, + Announce: []string{"/ip4/192.168.0.1/tcp/4001"}, + }), "Announce addrs should override Swarm if specified") + + require.Equal(t, []string{"/ip4/0.0.0.0/udp/4001/quic"}, + httpAddrsFromConfig(config.Addresses{ + Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"}, + NoAnnounce: []string{"/ip4/0.0.0.0/tcp/4001"}, + }), "Swarm addrs should not contain NoAnnounce addrs") + + require.Equal(t, []string{"/ip4/192.168.0.1/tcp/4001", "/ip4/192.168.0.2/tcp/4001"}, + httpAddrsFromConfig(config.Addresses{ + Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"}, + Announce: []string{"/ip4/192.168.0.1/tcp/4001"}, + AppendAnnounce: []string{"/ip4/192.168.0.2/tcp/4001"}, + }), "AppendAnnounce addrs should be included if specified") +}