From 0efe1e5093fda53dc26ee6c96b40a2e2c6320bb8 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 22 Aug 2022 04:04:56 -0700 Subject: [PATCH] feat: inject DNS resolver (#1607) --- config/config.go | 2 +- config/constructor_types.go | 30 ++++++++++++++++++------------ config/muxer.go | 2 +- config/reflection_magic.go | 10 ++++++---- config/security.go | 2 +- config/transport.go | 14 ++++++++------ config/transport_test.go | 2 +- defaults.go | 10 ++++++++++ 8 files changed, 46 insertions(+), 26 deletions(-) diff --git a/config/config.go b/config/config.go index 6561d8522b..4ccbc509f8 100644 --- a/config/config.go +++ b/config/config.go @@ -188,7 +188,7 @@ func (cfg *Config) addTransports(h host.Host) error { if err != nil { return err } - tpts, err := makeTransports(h, upgrader, cfg.ConnectionGater, cfg.PSK, cfg.ResourceManager, cfg.Transports) + tpts, err := makeTransports(h, upgrader, cfg.ConnectionGater, cfg.PSK, cfg.ResourceManager, cfg.MultiaddrResolver, cfg.Transports) if err != nil { return err } diff --git a/config/constructor_types.go b/config/constructor_types.go index 7cb24464b4..53a105ba39 100644 --- a/config/constructor_types.go +++ b/config/constructor_types.go @@ -13,6 +13,8 @@ import ( "github.com/libp2p/go-libp2p/core/pnet" "github.com/libp2p/go-libp2p/core/sec" "github.com/libp2p/go-libp2p/core/transport" + + madns "github.com/multiformats/go-multiaddr-dns" ) var ( @@ -30,41 +32,45 @@ var ( rcmgrType = reflect.TypeOf((*network.ResourceManager)(nil)).Elem() // concrete types - peerIDType = reflect.TypeOf((peer.ID)("")) - pskType = reflect.TypeOf((pnet.PSK)(nil)) + peerIDType = reflect.TypeOf((peer.ID)("")) + pskType = reflect.TypeOf((pnet.PSK)(nil)) + resolverType = reflect.TypeOf((*madns.Resolver)(nil)) ) var argTypes = map[reflect.Type]constructor{ - upgraderType: func(_ host.Host, u transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + upgraderType: func(_ host.Host, u transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return u }, - hostType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + hostType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return h }, - networkType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + networkType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return h.Network() }, - pskType: func(_ host.Host, _ transport.Upgrader, psk pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + pskType: func(_ host.Host, _ transport.Upgrader, psk pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return psk }, - connGaterType: func(_ host.Host, _ transport.Upgrader, _ pnet.PSK, cg connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + connGaterType: func(_ host.Host, _ transport.Upgrader, _ pnet.PSK, cg connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return cg }, - peerIDType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + peerIDType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return h.ID() }, - privKeyType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + privKeyType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return h.Peerstore().PrivKey(h.ID()) }, - pubKeyType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + pubKeyType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return h.Peerstore().PubKey(h.ID()) }, - pstoreType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) interface{} { + pstoreType: func(h host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) interface{} { return h.Peerstore() }, - rcmgrType: func(_ host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, rcmgr network.ResourceManager) interface{} { + rcmgrType: func(_ host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, rcmgr network.ResourceManager, _ *madns.Resolver) interface{} { return rcmgr }, + resolverType: func(_ host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, r *madns.Resolver) interface{} { + return r + }, } func newArgTypeSet(types ...reflect.Type) map[reflect.Type]constructor { diff --git a/config/muxer.go b/config/muxer.go index 651dea76b6..30a3fa2f7e 100644 --- a/config/muxer.go +++ b/config/muxer.go @@ -35,7 +35,7 @@ func MuxerConstructor(m interface{}) (MuxC, error) { return nil, err } return func(h host.Host) (network.Multiplexer, error) { - t, err := ctor(h, nil, nil, nil, nil) + t, err := ctor(h, nil, nil, nil, nil, nil) if err != nil { return nil, err } diff --git a/config/reflection_magic.go b/config/reflection_magic.go index 407e585056..bb2f52c6b0 100644 --- a/config/reflection_magic.go +++ b/config/reflection_magic.go @@ -11,6 +11,8 @@ import ( "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/pnet" "github.com/libp2p/go-libp2p/core/transport" + + madns "github.com/multiformats/go-multiaddr-dns" ) var errorType = reflect.TypeOf((*error)(nil)).Elem() @@ -80,7 +82,7 @@ func callConstructor(c reflect.Value, args []reflect.Value) (interface{}, error) return val, err } -type constructor func(host.Host, transport.Upgrader, pnet.PSK, connmgr.ConnectionGater, network.ResourceManager) interface{} +type constructor func(host.Host, transport.Upgrader, pnet.PSK, connmgr.ConnectionGater, network.ResourceManager, *madns.Resolver) interface{} func makeArgumentConstructors(fnType reflect.Type, argTypes map[reflect.Type]constructor) ([]constructor, error) { params := fnType.NumIn() @@ -131,7 +133,7 @@ func makeConstructor( tptType reflect.Type, argTypes map[reflect.Type]constructor, opts ...interface{}, -) (func(host.Host, transport.Upgrader, pnet.PSK, connmgr.ConnectionGater, network.ResourceManager) (interface{}, error), error) { +) (func(host.Host, transport.Upgrader, pnet.PSK, connmgr.ConnectionGater, network.ResourceManager, *madns.Resolver) (interface{}, error), error) { v := reflect.ValueOf(tpt) // avoid panicing on nil/zero value. if v == (reflect.Value{}) { @@ -155,10 +157,10 @@ func makeConstructor( return nil, err } - return func(h host.Host, u transport.Upgrader, psk pnet.PSK, cg connmgr.ConnectionGater, rcmgr network.ResourceManager) (interface{}, error) { + return func(h host.Host, u transport.Upgrader, psk pnet.PSK, cg connmgr.ConnectionGater, rcmgr network.ResourceManager, resolver *madns.Resolver) (interface{}, error) { arguments := make([]reflect.Value, 0, len(argConstructors)+len(opts)) for i, makeArg := range argConstructors { - if arg := makeArg(h, u, psk, cg, rcmgr); arg != nil { + if arg := makeArg(h, u, psk, cg, rcmgr, resolver); arg != nil { arguments = append(arguments, reflect.ValueOf(arg)) } else { // ValueOf an un-typed nil yields a zero reflect diff --git a/config/security.go b/config/security.go index a98d761a89..330cfaf334 100644 --- a/config/security.go +++ b/config/security.go @@ -41,7 +41,7 @@ func SecurityConstructor(security interface{}) (SecC, error) { return nil, err } return func(h host.Host) (sec.SecureTransport, error) { - t, err := ctor(h, nil, nil, nil, nil) + t, err := ctor(h, nil, nil, nil, nil, nil) if err != nil { return nil, err } diff --git a/config/transport.go b/config/transport.go index 9006683a09..850357f5a4 100644 --- a/config/transport.go +++ b/config/transport.go @@ -6,12 +6,14 @@ import ( "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/pnet" "github.com/libp2p/go-libp2p/core/transport" + + madns "github.com/multiformats/go-multiaddr-dns" ) // TptC is the type for libp2p transport constructors. You probably won't ever // implement this function interface directly. Instead, pass your transport // constructor to TransportConstructor. -type TptC func(host.Host, transport.Upgrader, pnet.PSK, connmgr.ConnectionGater, network.ResourceManager) (transport.Transport, error) +type TptC func(host.Host, transport.Upgrader, pnet.PSK, connmgr.ConnectionGater, network.ResourceManager, *madns.Resolver) (transport.Transport, error) var transportArgTypes = argTypes @@ -39,7 +41,7 @@ var transportArgTypes = argTypes func TransportConstructor(tpt interface{}, opts ...interface{}) (TptC, error) { // Already constructed? if t, ok := tpt.(transport.Transport); ok { - return func(_ host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager) (transport.Transport, error) { + return func(_ host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) (transport.Transport, error) { return t, nil }, nil } @@ -47,8 +49,8 @@ func TransportConstructor(tpt interface{}, opts ...interface{}) (TptC, error) { if err != nil { return nil, err } - return func(h host.Host, u transport.Upgrader, psk pnet.PSK, cg connmgr.ConnectionGater, rcmgr network.ResourceManager) (transport.Transport, error) { - t, err := ctor(h, u, psk, cg, rcmgr) + return func(h host.Host, u transport.Upgrader, psk pnet.PSK, cg connmgr.ConnectionGater, rcmgr network.ResourceManager, resolver *madns.Resolver) (transport.Transport, error) { + t, err := ctor(h, u, psk, cg, rcmgr, resolver) if err != nil { return nil, err } @@ -56,10 +58,10 @@ func TransportConstructor(tpt interface{}, opts ...interface{}) (TptC, error) { }, nil } -func makeTransports(h host.Host, u transport.Upgrader, cg connmgr.ConnectionGater, psk pnet.PSK, rcmgr network.ResourceManager, tpts []TptC) ([]transport.Transport, error) { +func makeTransports(h host.Host, u transport.Upgrader, cg connmgr.ConnectionGater, psk pnet.PSK, rcmgr network.ResourceManager, resolver *madns.Resolver, tpts []TptC) ([]transport.Transport, error) { transports := make([]transport.Transport, len(tpts)) for i, tC := range tpts { - t, err := tC(h, u, psk, cg, rcmgr) + t, err := tC(h, u, psk, cg, rcmgr, resolver) if err != nil { return nil, err } diff --git a/config/transport_test.go b/config/transport_test.go index 1af9c169d9..5684521e98 100644 --- a/config/transport_test.go +++ b/config/transport_test.go @@ -36,7 +36,7 @@ func TestConstructorWithOpts(t *testing.T) { return tcp.NewTCPTransport(nil, nil) }, 42, 1337) require.NoError(t, err) - _, err = c(nil, nil, nil, nil, nil) + _, err = c(nil, nil, nil, nil, nil, nil) require.NoError(t, err) require.Equal(t, []int{42, 1337}, options) } diff --git a/defaults.go b/defaults.go index ca70644860..0e7467b5af 100644 --- a/defaults.go +++ b/defaults.go @@ -17,6 +17,7 @@ import ( ws "github.com/libp2p/go-libp2p/p2p/transport/websocket" "github.com/multiformats/go-multiaddr" + madns "github.com/multiformats/go-multiaddr-dns" ) // DefaultSecurity is the default security option. @@ -106,6 +107,11 @@ var DefaultConnectionManager = func(cfg *Config) error { return cfg.Apply(ConnectionManager(mgr)) } +// DefaultMultiaddrResolver creates a default connection manager +var DefaultMultiaddrResolver = func(cfg *Config) error { + return cfg.Apply(MultiaddrResolver(madns.DefaultResolver)) +} + // Complete list of default options and when to fallback on them. // // Please *DON'T* specify default options any other way. Putting this all here @@ -150,6 +156,10 @@ var defaults = []struct { fallback: func(cfg *Config) bool { return cfg.ConnManager == nil }, opt: DefaultConnectionManager, }, + { + fallback: func(cfg *Config) bool { return cfg.MultiaddrResolver == nil }, + opt: DefaultMultiaddrResolver, + }, } // Defaults configures libp2p to use the default options. Can be combined with