Skip to content

Commit

Permalink
fix: resolve all addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
mrekucci committed Nov 10, 2023
1 parent d6cb66e commit 704f9e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
21 changes: 12 additions & 9 deletions pkg/p2p/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"github.com/multiformats/go-multistream"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/atomic"
"golang.org/x/exp/maps"
)

// loggerName is the tree path name of the logger for this package.
Expand Down Expand Up @@ -652,24 +653,26 @@ func (s *Service) AddProtocol(p p2p.ProtocolSpec) (err error) {
return nil
}

func (s *Service) Addresses() (addreses []ma.Multiaddr, err error) {
func (s *Service) Addresses() ([]ma.Multiaddr, error) {
addresses := make(map[string]ma.Multiaddr)
for _, addr := range s.host.Addrs() {
a, err := buildUnderlayAddress(addr, s.host.ID())
if err != nil {
return nil, err
}

addreses = append(addreses, a)
addresses[a.String()] = a
}
if s.natAddrResolver != nil && len(addreses) > 0 {
a, err := s.natAddrResolver.Resolve(addreses[0])
if err != nil {
return nil, err
if s.natAddrResolver != nil {
for _, addr := range addresses {
a, err := s.natAddrResolver.Resolve(addr)
if err != nil {
return nil, err
}
addresses[a.String()] = a
}
addreses = append(addreses, a)
}

return addreses, nil
return maps.Values(addresses), nil
}

func (s *Service) NATManager() basichost.NATManager {
Expand Down
20 changes: 13 additions & 7 deletions pkg/p2p/libp2p/static_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,26 @@ func (r *staticAddressResolver) Resolve(observedAddress ma.Multiaddr) (ma.Multia
return observedAddress, nil
}

var multiProto string
var elements []string
if r.multiProto != "" {
multiProto = r.multiProto
elements = append(elements, r.multiProto)
} else {
multiProto = strings.Join(observedAddrSplit[:3], "/")
elements = append(elements, observedAddrSplit[:3]...)
}

var port string
elements = append(elements, observedAddrSplit[3])

if r.port != "" {
port = r.port
elements = append(elements, r.port)
} else {
port = observedAddrSplit[4]
elements = append(elements, observedAddrSplit[4])
}
a, err := ma.NewMultiaddr(multiProto + "/" + observedAddrSplit[3] + "/" + port)

if len(observedAddrSplit) > 5 {
elements = append(elements, observedAddrSplit[5:]...)
}

a, err := ma.NewMultiaddr(strings.Join(elements, "/"))
if err != nil {
return nil, err
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/p2p/libp2p/static_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func TestStaticAddressResolver(t *testing.T) {
observableAddress: "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd",
want: "/ip4/192.168.1.34/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd",
},
{
name: "replace ip v4 with quic",
natAddr: "192.168.1.34:",
observableAddress: "/ip4/127.0.0.1/udp/7071/quic-v1/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd",
want: "/ip4/192.168.1.34/udp/7071/quic-v1/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd",
},
{
name: "replace ip v6",
natAddr: "[2001:db8::8a2e:370:1111]:",
Expand Down Expand Up @@ -105,13 +111,13 @@ func TestStaticAddressResolver(t *testing.T) {
if err != nil {
t.Fatal(err)
}
got, err := r.Resolve(observableAddress)
have, err := r.Resolve(observableAddress)
if err != nil {
t.Fatal(err)
}

if got.String() != tc.want {
t.Errorf("got %s, want %s", got, tc.want)
if have.String() != tc.want {
t.Errorf("Resolve(%s):\nhave %s\nwant %s", observableAddress, have, tc.want)
}
})
}
Expand Down

0 comments on commit 704f9e3

Please sign in to comment.