Browse Source

drop unsupported domain address type in packet addr (#3186)

Xiaokang Wang (Shelikhoo) 1 year ago
parent
commit
229b30d877

+ 5 - 0
common/net/packetaddr/connection_adaptor.go

@@ -79,6 +79,11 @@ func (c *packetConnectionAdaptor) ReadFrom(p []byte) (n int, addr gonet.Addr, er
 }
 }
 
 
 func (c *packetConnectionAdaptor) WriteTo(p []byte, addr gonet.Addr) (n int, err error) {
 func (c *packetConnectionAdaptor) WriteTo(p []byte, addr gonet.Addr) (n int, err error) {
+	_, ok := addr.(*gonet.UDPAddr)
+	if !ok {
+		// address other than UDPAddr is not supported, and will be dropped.
+		return 0, nil
+	}
 	payloadLen := len(p)
 	payloadLen := len(p)
 	var buffer *buf.Buffer
 	var buffer *buf.Buffer
 	buffer, err = AttachAddressToPacket(buf.FromBytes(p), addr)
 	buffer, err = AttachAddressToPacket(buf.FromBytes(p), addr)

+ 4 - 0
common/net/packetaddr/packetaddr.go

@@ -2,6 +2,7 @@ package packetaddr
 
 
 import (
 import (
 	"bytes"
 	"bytes"
+	"github.com/v2fly/v2ray-core/v5/common/errors"
 	gonet "net"
 	gonet "net"
 
 
 	"github.com/v2fly/v2ray-core/v5/common/buf"
 	"github.com/v2fly/v2ray-core/v5/common/buf"
@@ -45,6 +46,9 @@ func ExtractAddressFromPacket(data *buf.Buffer) (*buf.Buffer, gonet.Addr, error)
 	if err != nil {
 	if err != nil {
 		return nil, nil, err
 		return nil, nil, err
 	}
 	}
+	if address.Family().IsDomain() {
+		return nil, nil, errors.New("invalid address type")
+	}
 	addr := &gonet.UDPAddr{
 	addr := &gonet.UDPAddr{
 		IP:   address.IP(),
 		IP:   address.IP(),
 		Port: int(port.Value()),
 		Port: int(port.Value()),

+ 6 - 0
transport/internet/udp/dispatcher_packetaddr.go

@@ -27,6 +27,12 @@ func (p PacketAddrDispatcher) Dispatch(ctx context.Context, destination net.Dest
 	if destination.Network != net.Network_UDP {
 	if destination.Network != net.Network_UDP {
 		return
 		return
 	}
 	}
+
+	// Processing of domain address is unsupported as it adds unpredictable overhead, it will be dropped.
+	if destination.Address.Family().IsDomain() {
+		return
+	}
+
 	p.conn.WriteTo(payload.Bytes(), &net.UDPAddr{IP: destination.Address.IP(), Port: int(destination.Port.Value())})
 	p.conn.WriteTo(payload.Bytes(), &net.UDPAddr{IP: destination.Address.IP(), Port: int(destination.Port.Value())})
 }
 }