diff --git a/proxy/drop.go b/proxy/drop.go new file mode 100644 index 000000000..b6df202ee --- /dev/null +++ b/proxy/drop.go @@ -0,0 +1,24 @@ +package proxy + +import ( + "context" + "math/rand/v2" + "time" + + "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/common/buf" + "github.com/xtls/xray-core/common/signal" +) + +// wait to drain all the possible incoming UDP data +// because re-established QUIC UDP may bypass the sniffer +func DropUDP(ctx context.Context, reader buf.Reader) { + ctx, cancel := context.WithCancel(ctx) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + }, time.Duration(30+rand.N(60))*time.Second) + context.AfterFunc(ctx, func() { + common.Interrupt(reader) + }) + buf.Copy(reader, buf.Discard, buf.UpdateActivity(timer)) +} diff --git a/proxy/vless/multiplex/client.go b/proxy/vless/multiplex/client.go index 570ceb5d0..129211b00 100644 --- a/proxy/vless/multiplex/client.go +++ b/proxy/vless/multiplex/client.go @@ -12,6 +12,7 @@ import ( "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/session" + "github.com/xtls/xray-core/proxy" "github.com/xtls/xray-core/proxy/vless" "github.com/xtls/xray-core/transport" ) @@ -72,6 +73,7 @@ func (c *Client) Dispatch(ctx context.Context, link *transport.Link) (bool, erro if target.Network == net.Network_UDP && target.Port == 443 { switch c.config.GetXudpProxyUDP443() { case "reject", "": + proxy.DropUDP(ctx, link.Reader) return true, errors.New("XUDP rejected UDP/443 traffic") case "skip": return false, nil diff --git a/proxy/vless/multiplex/client_test.go b/proxy/vless/multiplex/client_test.go index bedf36e23..068a23b20 100644 --- a/proxy/vless/multiplex/client_test.go +++ b/proxy/vless/multiplex/client_test.go @@ -26,7 +26,9 @@ func TestClientUDP443Policy(t *testing.T) { opened = true return nil, nil, context.Canceled }) - handled, err := client.Dispatch(ctx, &transport.Link{}) + r, w := pipe.New() + common.Close(w) + handled, err := client.Dispatch(ctx, &transport.Link{Reader: r}) wantRejected := policy == "reject" if opened || handled != wantRejected || (err != nil) != wantRejected { t.Fatalf("Dispatch() = (%t, %v), opened carrier = %t, want rejected = %t without opening a carrier", handled, err, opened, wantRejected) diff --git a/proxy/vless/outbound/outbound.go b/proxy/vless/outbound/outbound.go index 8e0e78e16..6720ae418 100644 --- a/proxy/vless/outbound/outbound.go +++ b/proxy/vless/outbound/outbound.go @@ -191,6 +191,13 @@ func (h *Handler) process(ctx context.Context, link *transport.Link, dialer inte rec := h.server var conn stat.Connection + account := rec.User.Account.(*vless.MemoryAccount) + + if !native && ob.Target.Network == net.Network_UDP && ob.Target.Port == 443 && account.Flow == vless.XRV { + proxy.DropUDP(ctx, link.Reader) + return nil, nil, errors.New("XTLS rejected UDP/443 traffic") + } + if h.testpre > 0 { h.initpre.Do(func() { h.preConns = make(chan *ConnExpire) @@ -272,27 +279,20 @@ func (h *Handler) process(ctx context.Context, link *transport.Link, dialer inte request.Address, request.Port = target.Address, target.Port } - account := request.User.Account.(*vless.MemoryAccount) - requestAddons := &encoding.Addons{ Flow: account.Flow, } var input *bytes.Reader var rawInput **bytes.Buffer - allowUDP443 := false switch requestAddons.Flow { case vless.XRV + "-udp443": - allowUDP443 = true requestAddons.Flow = requestAddons.Flow[:16] fallthrough case vless.XRV: ob.CanSpliceCopy = 2 switch request.Command { case protocol.RequestCommandUDP: - if !allowUDP443 && request.Port == 443 { - return nil, nil, errors.New("XTLS rejected UDP/443 traffic") - } case protocol.RequestCommandTCP, protocol.RequestCommandMux, protocol.RequestCommandRvs: var target net.Conn if commonConn, ok := conn.(*encryption.CommonConn); ok {