Browse Source

autonatv2: recover from panics (#2992)

* autonatv2: catch panics

* reset streams on panic
marco/go-multiaddr-dns-v040
sukun 1 month ago
committed by GitHub
parent
commit
6c2d56bbcd
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      p2p/protocol/autonatv2/autonat.go
  2. 1
      p2p/protocol/autonatv2/autonat_test.go
  3. 9
      p2p/protocol/autonatv2/client.go
  4. 10
      p2p/protocol/autonatv2/server.go

4
p2p/protocol/autonatv2/autonat.go

@ -25,9 +25,9 @@ const (
DialProtocol = "/libp2p/autonat/2/dial-request"
maxMsgSize = 8192
streamTimeout = time.Minute
streamTimeout = 15 * time.Second
dialBackStreamTimeout = 5 * time.Second
dialBackDialTimeout = 30 * time.Second
dialBackDialTimeout = 10 * time.Second
dialBackMaxMsgSize = 1024
minHandshakeSizeBytes = 30_000 // for amplification attack prevention
maxHandshakeSizeBytes = 100_000

1
p2p/protocol/autonatv2/autonat_test.go

@ -657,5 +657,4 @@ func TestAreAddrsConsistency(t *testing.T) {
}
})
}
}

9
p2p/protocol/autonatv2/client.go

@ -3,6 +3,8 @@ package autonatv2
import (
"context"
"fmt"
"os"
"runtime/debug"
"sync"
"time"
@ -248,6 +250,13 @@ func newDialRequest(reqs []Request, nonce uint64) pb.Message {
// handleDialBack receives the nonce on the dial-back stream
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()
}()
if err := s.Scope().SetService(ServiceName); err != nil {
log.Debugf("failed to attach stream to service %s: %w", ServiceName, err)
s.Reset()

10
p2p/protocol/autonatv2/server.go

@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"io"
"os"
"runtime/debug"
"sync"
"time"
@ -88,6 +90,14 @@ 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())
s.Reset()
}
}()
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)

Loading…
Cancel
Save