Report unreachable cloudflare v3 registrations

This commit is contained in:
世界
2026-03-24 13:59:13 +08:00
parent e6a7efc49a
commit 2340db6fcf
2 changed files with 42 additions and 0 deletions

View File

@@ -142,6 +142,10 @@ func (m *DatagramV3Muxer) handleRegistration(ctx context.Context, data []byte) {
if closeAfterIdle == 0 {
closeAfterIdle = 210 * time.Second
}
if !destination.Addr().IsValid() || destination.Addr().IsUnspecified() || destination.Port() == 0 {
m.sendRegistrationResponse(requestID, v3ResponseDestinationUnreachable, "")
return
}
m.sessionAccess.Lock()
if existing, exists := m.sessions[requestID]; exists {

View File

@@ -0,0 +1,38 @@
//go:build with_cloudflare_tunnel
package cloudflare
import (
"context"
"encoding/binary"
"testing"
"github.com/sagernet/sing-box/adapter/inbound"
C "github.com/sagernet/sing-box/constant"
)
func TestDatagramV3RegistrationDestinationUnreachable(t *testing.T) {
sender := &captureDatagramSender{}
inboundInstance := &Inbound{
Adapter: inbound.NewAdapter(C.TypeCloudflareTunnel, "test"),
flowLimiter: &FlowLimiter{},
}
muxer := NewDatagramV3Muxer(inboundInstance, sender, nil)
requestID := RequestID{}
requestID[15] = 1
payload := make([]byte, 1+2+2+16+4)
payload[0] = 0
binary.BigEndian.PutUint16(payload[1:3], 0)
binary.BigEndian.PutUint16(payload[3:5], 30)
copy(payload[5:21], requestID[:])
copy(payload[21:25], []byte{0, 0, 0, 0})
muxer.handleRegistration(context.Background(), payload)
if len(sender.sent) != 1 {
t.Fatalf("expected one registration response, got %d", len(sender.sent))
}
if sender.sent[0][0] != byte(DatagramV3TypeRegistrationResponse) || sender.sent[0][1] != v3ResponseDestinationUnreachable {
t.Fatalf("unexpected datagram response: %v", sender.sent[0])
}
}