Skip to content

Commit 3deee92

Browse files
authored
fix: enable dctur when interface address is public (#2931)
* fix: allow punching undialable host public ip fixes #2913 * chore: use interface listen addrs to enable dctur * fix: filter public addresses * chore: remove unused function * chore: formatting --------- Co-authored-by: Daniel N <[email protected]>
1 parent 65bc28b commit 3deee92

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

p2p/protocol/holepunch/svc.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/libp2p/go-msgio/pbio"
2020

2121
ma "github.com/multiformats/go-multiaddr"
22+
manet "github.com/multiformats/go-multiaddr/net"
2223
)
2324

2425
// Protocol is the libp2p protocol for Hole Punching.
@@ -106,7 +107,7 @@ func (s *Service) watchForPublicAddr() {
106107
t := time.NewTimer(duration)
107108
defer t.Stop()
108109
for {
109-
if containsPublicAddr(s.ids.OwnObservedAddrs()) {
110+
if len(s.getPublicAddrs()) > 0 {
110111
log.Debug("Host now has a public address. Starting holepunch protocol.")
111112
s.host.SetStreamHandler(Protocol, s.handleNewStream)
112113
break
@@ -171,7 +172,7 @@ func (s *Service) incomingHolePunch(str network.Stream) (rtt time.Duration, remo
171172
if !isRelayAddress(str.Conn().RemoteMultiaddr()) {
172173
return 0, nil, nil, fmt.Errorf("received hole punch stream: %s", str.Conn().RemoteMultiaddr())
173174
}
174-
ownAddrs = removeRelayAddrs(s.ids.OwnObservedAddrs())
175+
ownAddrs = s.getPublicAddrs()
175176
if s.filter != nil {
176177
ownAddrs = s.filter.FilterLocal(str.Conn().RemotePeer(), ownAddrs)
177178
}
@@ -274,6 +275,29 @@ func (s *Service) handleNewStream(str network.Stream) {
274275
s.tracer.HolePunchFinished("receiver", 1, addrs, ownAddrs, getDirectConnection(s.host, rp))
275276
}
276277

278+
// getPublicAddrs returns public observed and interface addresses
279+
func (s *Service) getPublicAddrs() []ma.Multiaddr {
280+
addrs := removeRelayAddrs(s.ids.OwnObservedAddrs())
281+
282+
interfaceListenAddrs, err := s.host.Network().InterfaceListenAddresses()
283+
if err != nil {
284+
log.Debugf("failed to get to get InterfaceListenAddresses: %s", err)
285+
} else {
286+
addrs = append(addrs, interfaceListenAddrs...)
287+
}
288+
289+
addrs = ma.Unique(addrs)
290+
291+
publicAddrs := make([]ma.Multiaddr, 0, len(addrs))
292+
293+
for _, addr := range addrs {
294+
if manet.IsPublicAddr(addr) {
295+
publicAddrs = append(publicAddrs, addr)
296+
}
297+
}
298+
return publicAddrs
299+
}
300+
277301
// DirectConnect is only exposed for testing purposes.
278302
// TODO: find a solution for this.
279303
func (s *Service) DirectConnect(p peer.ID) error {

p2p/protocol/holepunch/util.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,8 @@ import (
88
"github.com/libp2p/go-libp2p/core/peer"
99

1010
ma "github.com/multiformats/go-multiaddr"
11-
manet "github.com/multiformats/go-multiaddr/net"
1211
)
1312

14-
func containsPublicAddr(addrs []ma.Multiaddr) bool {
15-
for _, addr := range addrs {
16-
if isRelayAddress(addr) || !manet.IsPublicAddr(addr) {
17-
continue
18-
}
19-
return true
20-
}
21-
return false
22-
}
23-
2413
func removeRelayAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
2514
result := make([]ma.Multiaddr, 0, len(addrs))
2615
for _, addr := range addrs {

0 commit comments

Comments
 (0)