Skip to content

Commit

Permalink
reset streams on panic
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Oct 3, 2024
1 parent 96d1106 commit 5bec581
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions p2p/protocol/autonatv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}()

Expand Down
22 changes: 15 additions & 7 deletions p2p/protocol/autonatv2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5bec581

Please sign in to comment.