Pārlūkot izejas kodu

Set udp dontfrag by default

世界 3 gadi atpakaļ
vecāks
revīzija
0c975db0a6
4 mainītis faili ar 13 papildinājumiem un 3 dzēšanām
  1. 4 0
      common/dialer/default.go
  2. 7 2
      inbound/default_udp.go
  3. 1 0
      option/inbound.go
  4. 1 1
      option/outbound.go

+ 4 - 0
common/dialer/default.go

@@ -112,6 +112,10 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia
 	if options.TCPFastOpen {
 	if options.TCPFastOpen {
 		warnTFOOnUnsupportedPlatform.Check()
 		warnTFOOnUnsupportedPlatform.Check()
 	}
 	}
+	if !options.UDPFragment {
+		dialer.Control = control.Append(dialer.Control, control.DisableUDPFragment())
+		listener.Control = control.Append(listener.Control, control.DisableUDPFragment())
+	}
 	var bindUDPAddr string
 	var bindUDPAddr string
 	udpDialer := dialer
 	udpDialer := dialer
 	var bindAddress netip.Addr
 	var bindAddress netip.Addr

+ 7 - 2
inbound/default_udp.go

@@ -10,6 +10,7 @@ import (
 	"github.com/sagernet/sing-dns"
 	"github.com/sagernet/sing-dns"
 	"github.com/sagernet/sing/common"
 	"github.com/sagernet/sing/common"
 	"github.com/sagernet/sing/common/buf"
 	"github.com/sagernet/sing/common/buf"
+	"github.com/sagernet/sing/common/control"
 	E "github.com/sagernet/sing/common/exceptions"
 	E "github.com/sagernet/sing/common/exceptions"
 	M "github.com/sagernet/sing/common/metadata"
 	M "github.com/sagernet/sing/common/metadata"
 	N "github.com/sagernet/sing/common/network"
 	N "github.com/sagernet/sing/common/network"
@@ -17,11 +18,15 @@ import (
 
 
 func (a *myInboundAdapter) ListenUDP() (net.PacketConn, error) {
 func (a *myInboundAdapter) ListenUDP() (net.PacketConn, error) {
 	bindAddr := M.SocksaddrFrom(netip.Addr(a.listenOptions.Listen), a.listenOptions.ListenPort)
 	bindAddr := M.SocksaddrFrom(netip.Addr(a.listenOptions.Listen), a.listenOptions.ListenPort)
-	udpConn, err := net.ListenUDP(M.NetworkFromNetAddr(N.NetworkUDP, bindAddr.Addr), bindAddr.UDPAddr())
+	var lc net.ListenConfig
+	if !a.listenOptions.UDPFragment {
+		lc.Control = control.Append(lc.Control, control.DisableUDPFragment())
+	}
+	udpConn, err := lc.ListenPacket(a.ctx, M.NetworkFromNetAddr(N.NetworkUDP, bindAddr.Addr), bindAddr.String())
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
-	a.udpConn = udpConn
+	a.udpConn = udpConn.(*net.UDPConn)
 	a.udpAddr = bindAddr
 	a.udpAddr = bindAddr
 	a.logger.Info("udp server started at ", udpConn.LocalAddr())
 	a.logger.Info("udp server started at ", udpConn.LocalAddr())
 	return udpConn, err
 	return udpConn, err

+ 1 - 0
option/inbound.go

@@ -114,6 +114,7 @@ type ListenOptions struct {
 	Listen        ListenAddress `json:"listen"`
 	Listen        ListenAddress `json:"listen"`
 	ListenPort    uint16        `json:"listen_port,omitempty"`
 	ListenPort    uint16        `json:"listen_port,omitempty"`
 	TCPFastOpen   bool          `json:"tcp_fast_open,omitempty"`
 	TCPFastOpen   bool          `json:"tcp_fast_open,omitempty"`
+	UDPFragment   bool          `json:"udp_fragment,omitempty"`
 	UDPTimeout    int64         `json:"udp_timeout,omitempty"`
 	UDPTimeout    int64         `json:"udp_timeout,omitempty"`
 	ProxyProtocol bool          `json:"proxy_protocol,omitempty"`
 	ProxyProtocol bool          `json:"proxy_protocol,omitempty"`
 	Detour        string        `json:"detour,omitempty"`
 	Detour        string        `json:"detour,omitempty"`

+ 1 - 1
option/outbound.go

@@ -108,12 +108,12 @@ type DialerOptions struct {
 	Detour         string         `json:"detour,omitempty"`
 	Detour         string         `json:"detour,omitempty"`
 	BindInterface  string         `json:"bind_interface,omitempty"`
 	BindInterface  string         `json:"bind_interface,omitempty"`
 	BindAddress    *ListenAddress `json:"bind_address,omitempty"`
 	BindAddress    *ListenAddress `json:"bind_address,omitempty"`
-	BindAddress6   *ListenAddress `json:"bind_address6,omitempty"`
 	ProtectPath    string         `json:"protect_path,omitempty"`
 	ProtectPath    string         `json:"protect_path,omitempty"`
 	RoutingMark    int            `json:"routing_mark,omitempty"`
 	RoutingMark    int            `json:"routing_mark,omitempty"`
 	ReuseAddr      bool           `json:"reuse_addr,omitempty"`
 	ReuseAddr      bool           `json:"reuse_addr,omitempty"`
 	ConnectTimeout Duration       `json:"connect_timeout,omitempty"`
 	ConnectTimeout Duration       `json:"connect_timeout,omitempty"`
 	TCPFastOpen    bool           `json:"tcp_fast_open,omitempty"`
 	TCPFastOpen    bool           `json:"tcp_fast_open,omitempty"`
+	UDPFragment    bool           `json:"udp_fragment,omitempty"`
 	DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
 	DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
 	FallbackDelay  Duration       `json:"fallback_delay,omitempty"`
 	FallbackDelay  Duration       `json:"fallback_delay,omitempty"`
 }
 }