From 5bec58179e4a1c22beef23e7bbf92f4d836c7b49 Mon Sep 17 00:00:00 2001 From: sukun Date: Thu, 3 Oct 2024 10:45:17 +0530 Subject: [PATCH] reset streams on panic --- p2p/protocol/autonatv2/client.go | 1 + p2p/protocol/autonatv2/server.go | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/p2p/protocol/autonatv2/client.go b/p2p/protocol/autonatv2/client.go index b9e88ec414..a6c9eaa7a2 100644 --- a/p2p/protocol/autonatv2/client.go +++ b/p2p/protocol/autonatv2/client.go @@ -253,6 +253,7 @@ func (ac *client) handleDialBack(s network.Stream) { defer func() { if rerr := recover(); rerr != nil { fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack()) + s.Reset() } }() diff --git a/p2p/protocol/autonatv2/server.go b/p2p/protocol/autonatv2/server.go index 1db1d16f2c..230009582f 100644 --- a/p2p/protocol/autonatv2/server.go +++ b/p2p/protocol/autonatv2/server.go @@ -21,6 +21,7 @@ import ( "math/rand" ma "github.com/multiformats/go-multiaddr" + madns "github.com/multiformats/go-multiaddr-dns" manet "github.com/multiformats/go-multiaddr/net" ) @@ -90,13 +91,6 @@ func (as *server) Close() { // handleDialRequest is the dial-request protocol stream handler func (as *server) handleDialRequest(s network.Stream) { - defer func() { - if rerr := recover(); rerr != nil { - fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack()) - } - }() - - log.Debugf("received dial-request from: %s, addr: %s", s.Conn().RemotePeer(), s.Conn().RemoteMultiaddr()) evt := as.serveDialRequest(s) log.Debugf("completed dial-request from %s, response status: %s, dial status: %s, err: %s", s.Conn().RemotePeer(), evt.ResponseStatus, evt.DialStatus, evt.Error) @@ -106,6 +100,15 @@ func (as *server) handleDialRequest(s network.Stream) { } func (as *server) serveDialRequest(s network.Stream) EventDialRequestCompleted { + // catching this panic is fine. In the worst case, we will stop serving autonatv2 requests by + // rate limitin + defer func() { + if rerr := recover(); rerr != nil { + fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack()) + s.Reset() + } + }() + if err := s.Scope().SetService(ServiceName); err != nil { s.Reset() log.Debugf("failed to attach stream to %s service: %w", ServiceName, err) @@ -179,6 +182,11 @@ func (as *server) serveDialRequest(s network.Stream) EventDialRequestCompleted { if !as.allowPrivateAddrs && !manet.IsPublicAddr(a) { continue } + // Don't dial any address with a dns component. + // We may leak some DNS configuration information by DNS resolution. + if madns.Matches(a) { + continue + } if !as.dialerHost.Network().CanDial(p, a) { continue }