[#43] pkg/network: Do not panic at multiaddr to net.Addr conversion

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-24 10:37:42 +03:00
parent 8585e147d0
commit f930993e3a
7 changed files with 27 additions and 10 deletions

View file

@ -23,13 +23,13 @@ func (a Address) String() string {
}
// NetAddr returns network endpoint address in string format.
func (a Address) NetAddr() string {
func (a Address) NetAddr() (string, error) {
ip, err := manet.ToNetAddr(a.ma)
if err != nil {
panic(errors.Wrap(err, "could not get net addr"))
return "", errors.Wrap(err, "could not get net addr")
}
return ip.String()
return ip.String(), nil
}
// AddressFromString restores address from a string representation.
@ -47,5 +47,5 @@ func AddressFromString(s string) (*Address, error) {
// IsLocalAddress returns true if network endpoint from local address
// source is equal to network endpoint of passed address.
func IsLocalAddress(src LocalAddressSource, addr *Address) bool {
return src.LocalAddress().NetAddr() == addr.NetAddr()
return src.LocalAddress().ma.Equal(addr.ma)
}

View file

@ -24,5 +24,7 @@ func TestAddress_NetAddr(t *testing.T) {
addr, err := AddressFromString(ma.String())
require.NoError(t, err)
require.Equal(t, ip+":"+port, addr.NetAddr())
netAddr, err := addr.NetAddr()
require.NoError(t, err)
require.Equal(t, ip+":"+port, netAddr)
}

View file

@ -22,7 +22,10 @@ func (h *remoteHeader) head(ctx context.Context, prm *Prm, handler func(*object.
return errors.Wrapf(err, "(%T) could not receive private key", h)
}
addr := h.node.NetAddr()
addr, err := h.node.NetAddr()
if err != nil {
return err
}
c, err := client.New(key,
client.WithAddress(addr),

View file

@ -38,7 +38,10 @@ func (t *remoteTarget) Close() (*transformer.AccessIdentifiers, error) {
return nil, errors.Wrapf(err, "(%T) could not receive private key", t)
}
addr := t.addr.NetAddr()
addr, err := t.addr.NetAddr()
if err != nil {
return nil, err
}
c, err := client.New(key,
client.WithAddress(addr),

View file

@ -32,7 +32,10 @@ func (r *remoteRangeWriter) WriteTo(w io.Writer) (int64, error) {
return 0, errors.Wrapf(err, "(%T) could not receive private key", r)
}
addr := r.node.NetAddr()
addr, err := r.node.NetAddr()
if err != nil {
return 0, err
}
c, err := client.New(key,
client.WithAddress(addr),

View file

@ -23,7 +23,10 @@ func (h *remoteHasher) hashRange(ctx context.Context, prm *Prm, handler func([][
return errors.Wrapf(err, "(%T) could not receive private key", h)
}
addr := h.node.NetAddr()
addr, err := h.node.NetAddr()
if err != nil {
return err
}
c, err := client.New(key,
client.WithAddress(addr),

View file

@ -24,7 +24,10 @@ func (s *remoteStream) stream(ctx context.Context, ch chan<- []*object.ID) error
return errors.Wrapf(err, "(%T) could not receive private key", s)
}
addr := s.addr.NetAddr()
addr, err := s.addr.NetAddr()
if err != nil {
return err
}
c, err := client.New(key,
client.WithAddress(addr),