SVLAVR
2026-11-24 18:20:46 +00:00
committed by RPRXX
co-authored by Codex RPRX
parent 15b2499d34
commit e17392a96e
64 changed files with 3107 additions and 3918 deletions
+8 -6
View File
@@ -19,6 +19,7 @@ import (
feature_inbound "github.com/xtls/xray-core/features/inbound"
"github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/proxy/vless/multiplex"
"github.com/xtls/xray-core/proxy/vmess"
"github.com/xtls/xray-core/proxy/vmess/encoding"
"github.com/xtls/xray-core/transport"
@@ -238,7 +239,6 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
if err != nil {
return errors.New("failed to start decoding").Base(err)
}
writer := buf.NewBufferedWriter(buf.NewWriter(connection))
response := &protocol.ResponseHeader{
Command: h.generateCommand(ctx, request),
@@ -249,12 +249,14 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
return errors.New("failed to start decoding response").Base(err)
}
writer.SetFlushNext()
link := &transport.Link{Reader: bodyReader, Writer: bodyWriter}
if err := dispatcher.DispatchLink(ctx, request.Destination(), &transport.Link{
Reader: bodyReader,
Writer: bodyWriter,
},
); err != nil {
if request.Command == protocol.RequestCommandMux {
worker := multiplex.NewServerWorker(ctx, dispatcher.DispatchLink, link)
<-worker.WaitClosed()
return worker.Err()
}
if err := dispatcher.DispatchLink(ctx, request.Destination(), link); err != nil {
return errors.New("failed to dispatch request").Base(err)
}
return nil
-13
View File
@@ -1,13 +0,0 @@
package outbound
import (
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
)
// As a stub command consumer.
func (h *Handler) handleCommand(dest net.Destination, cmd protocol.ResponseCommand) {
switch cmd.(type) {
default:
}
}
+19 -7
View File
@@ -8,6 +8,7 @@ package outbound
import (
protocol "github.com/xtls/xray-core/common/protocol"
vless "github.com/xtls/xray-core/proxy/vless"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
@@ -25,6 +26,7 @@ const (
type Config struct {
state protoimpl.MessageState `protogen:"open.v1"`
Receiver *protocol.ServerEndpoint `protobuf:"bytes,1,opt,name=Receiver,proto3" json:"Receiver,omitempty"`
Multiplex *vless.Multiplex `protobuf:"bytes,2,opt,name=multiplex,proto3" json:"multiplex,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -66,13 +68,21 @@ func (x *Config) GetReceiver() *protocol.ServerEndpoint {
return nil
}
func (x *Config) GetMultiplex() *vless.Multiplex {
if x != nil {
return x.Multiplex
}
return nil
}
var File_proxy_vmess_outbound_config_proto protoreflect.FileDescriptor
const file_proxy_vmess_outbound_config_proto_rawDesc = "" +
"\n" +
"!proxy/vmess/outbound/config.proto\x12\x19xray.proxy.vmess.outbound\x1a!common/protocol/server_spec.proto\"J\n" +
"!proxy/vmess/outbound/config.proto\x12\x19xray.proxy.vmess.outbound\x1a!common/protocol/server_spec.proto\x1a\x19proxy/vless/account.proto\"\x85\x01\n" +
"\x06Config\x12@\n" +
"\bReceiver\x18\x01 \x01(\v2$.xray.common.protocol.ServerEndpointR\bReceiverBm\n" +
"\bReceiver\x18\x01 \x01(\v2$.xray.common.protocol.ServerEndpointR\bReceiver\x129\n" +
"\tmultiplex\x18\x02 \x01(\v2\x1b.xray.proxy.vless.MultiplexR\tmultiplexBm\n" +
"\x1dcom.xray.proxy.vmess.outboundP\x01Z.github.com/xtls/xray-core/proxy/vmess/outbound\xaa\x02\x19Xray.Proxy.Vmess.Outboundb\x06proto3"
var (
@@ -91,14 +101,16 @@ var file_proxy_vmess_outbound_config_proto_msgTypes = make([]protoimpl.MessageIn
var file_proxy_vmess_outbound_config_proto_goTypes = []any{
(*Config)(nil), // 0: xray.proxy.vmess.outbound.Config
(*protocol.ServerEndpoint)(nil), // 1: xray.common.protocol.ServerEndpoint
(*vless.Multiplex)(nil), // 2: xray.proxy.vless.Multiplex
}
var file_proxy_vmess_outbound_config_proto_depIdxs = []int32{
1, // 0: xray.proxy.vmess.outbound.Config.Receiver:type_name -> xray.common.protocol.ServerEndpoint
1, // [1:1] is the sub-list for method output_type
1, // [1:1] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
2, // 1: xray.proxy.vmess.outbound.Config.multiplex:type_name -> xray.proxy.vless.Multiplex
2, // [2:2] is the sub-list for method output_type
2, // [2:2] is the sub-list for method input_type
2, // [2:2] is the sub-list for extension type_name
2, // [2:2] is the sub-list for extension extendee
0, // [0:2] is the sub-list for field type_name
}
func init() { file_proxy_vmess_outbound_config_proto_init() }
+2
View File
@@ -7,7 +7,9 @@ option java_package = "com.xray.proxy.vmess.outbound";
option java_multiple_files = true;
import "common/protocol/server_spec.proto";
import "proxy/vless/account.proto";
message Config {
xray.common.protocol.ServerEndpoint Receiver = 1;
xray.proxy.vless.Multiplex multiplex = 2;
}
+100 -75
View File
@@ -21,6 +21,7 @@ import (
"github.com/xtls/xray-core/common/xudp"
core "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/proxy/vless/multiplex"
"github.com/xtls/xray-core/proxy/vmess"
"github.com/xtls/xray-core/proxy/vmess/encoding"
"github.com/xtls/xray-core/transport"
@@ -32,6 +33,8 @@ import (
type Handler struct {
server *protocol.ServerSpec
policyManager policy.Manager
multiplex *multiplex.Client
stopMultiplex context.CancelFunc
}
// New creates a new VMess outbound handler.
@@ -44,21 +47,55 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
return nil, errors.New("failed to get server spec").Base(err)
}
muxConfig, err := multiplex.NormalizeConfig(config.Multiplex)
if err != nil {
return nil, err
}
v := core.MustFromContext(ctx)
handler := &Handler{
server: server,
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
}
if muxConfig != nil {
dialer, ok := session.FullHandlerFromContext(ctx).(internet.Dialer)
if !ok {
return nil, errors.New("VMess multiplex requires an outbound dialer")
}
poolCtx, stop := context.WithCancel(context.Background())
handler.stopMultiplex = stop
handler.multiplex = multiplex.NewClient(poolCtx, muxConfig, "", func(ctx context.Context) (context.Context, *transport.Link, error) {
return handler.process(ctx, nil, dialer, protocol.RequestCommandMux)
})
}
return handler, nil
}
func (h *Handler) Close() error {
if h.stopMultiplex != nil {
h.stopMultiplex()
}
return nil
}
// Process implements proxy.Outbound.Process().
func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
if handled, err := h.multiplex.Dispatch(ctx, link); handled {
return err
}
_, _, err := h.process(ctx, link, dialer, 0)
return err
}
func (h *Handler) process(ctx context.Context, link *transport.Link, dialer internet.Dialer, command protocol.RequestCommand) (_ context.Context, _ *transport.Link, err error) {
native := command == protocol.RequestCommandMux
if native {
ctx = session.ContextWithOutbounds(ctx, []*session.Outbound{{Target: h.server.Destination}})
}
outbounds := session.OutboundsFromContext(ctx)
ob := outbounds[len(outbounds)-1]
if !ob.Target.IsValid() {
return errors.New("target not specified")
return nil, nil, errors.New("target not specified")
}
ob.Name = "vmess"
ob.CanSpliceCopy = 3
@@ -66,7 +103,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
rec := h.server
var conn stat.Connection
err := retry.ExponentialBackoff(5, 200).On(func() error {
err = retry.ExponentialBackoff(5, 200).On(func() error {
rawConn, err := dialer.Dial(ctx, rec.Destination)
if err != nil {
return err
@@ -76,30 +113,30 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
return nil
})
if err != nil {
return errors.New("failed to find an available destination").Base(err)
return nil, nil, errors.New("failed to find an available destination").Base(err)
}
if !native {
defer conn.Close()
}
defer conn.Close()
target := ob.Target
errors.LogInfo(ctx, "tunneling request to ", target, " via ", rec.Destination.NetAddr())
command := protocol.RequestCommandTCP
if target.Network == net.Network_UDP {
command = protocol.RequestCommandUDP
}
if target.Address.Family().IsDomain() && target.Address.Domain() == "v1.mux.cool" {
command = protocol.RequestCommandMux
}
user := rec.User
singleXUDP := target.Network == net.Network_UDP && target.Port != 53 && target.Port != 443
request := &protocol.RequestHeader{
Version: encoding.Version,
User: user,
Command: command,
User: rec.User,
Command: protocol.RequestCommandTCP,
Address: target.Address,
Port: target.Port,
Option: protocol.RequestOptionChunkStream,
}
switch {
case native || singleXUDP:
request.Command = protocol.RequestCommandMux
case target.Network == net.Network_UDP:
request.Command = protocol.RequestCommandUDP
}
account := request.User.Account.(*vmess.MemoryAccount)
request.Security = account.Security
@@ -107,7 +144,6 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
if request.Security == protocol.SecurityType_AES128_GCM || request.Security == protocol.SecurityType_CHACHA20_POLY1305 {
request.Option.Set(protocol.RequestOptionChunkMasking)
}
if shouldEnablePadding(request.Security) && request.Option.Has(protocol.RequestOptionChunkMasking) {
request.Option.Set(protocol.RequestOptionGlobalPadding)
}
@@ -116,54 +152,60 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
request.Option.Set(protocol.RequestOptionAuthenticatedLength)
}
input := link.Reader
output := link.Writer
hashkdf := hmac.New(sha256.New, []byte("VMessBF"))
hashkdf.Write(account.ID.Bytes())
behaviorSeed := crc64.Checksum(hashkdf.Sum(nil), crc64.MakeTable(crc64.ISO))
var newCtx context.Context
var newCancel context.CancelFunc
clientSession := encoding.NewClientSession(ctx, int64(behaviorSeed))
sessionPolicy := h.policyManager.ForLevel(request.User.Level)
if session.TimeoutOnlyFromContext(ctx) {
newCtx, newCancel = context.WithCancel(context.Background())
ctx = context.Background()
}
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
if native {
context.AfterFunc(ctx, func() { common.Interrupt(stat.TryUnwrapStatsConn(conn)) })
}
defer func() {
if err != nil {
timer.SetTimeout(0)
}
}()
writer := buf.NewBufferedWriter(buf.NewWriter(conn))
if err := clientSession.EncodeRequestHeader(request, writer); err != nil {
return nil, nil, errors.New("failed to encode request").Base(err)
}
session := encoding.NewClientSession(ctx, int64(behaviorSeed))
sessionPolicy := h.policyManager.ForLevel(request.User.Level)
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, func() {
cancel()
if newCancel != nil {
newCancel()
bodyWriter, err := clientSession.EncodeRequestBody(request, writer)
if err != nil {
return nil, nil, errors.New("failed to start encoding").Base(err)
}
openResponse := func() (buf.Reader, error) {
reader := &buf.BufferedReader{Reader: buf.NewReader(conn)}
if _, err := clientSession.DecodeResponseHeader(reader); err != nil {
return nil, errors.New("failed to read header").Base(err)
}
}, sessionPolicy.Timeouts.ConnectionIdle)
return clientSession.DecodeResponseBody(request, reader)
}
if request.Command == protocol.RequestCommandUDP && request.Port != 53 && request.Port != 443 {
request.Command = protocol.RequestCommandMux
request.Address = net.DomainAddress("v1.mux.cool")
request.Port = net.Port(666)
if native {
if err := writer.SetBuffered(false); err != nil {
return nil, nil, err
}
physical, err := multiplex.OpenLink(ctx, cancel, bodyWriter, timer, openResponse)
return ctx, physical, err
}
requestDone := func() error {
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
writer := buf.NewBufferedWriter(buf.NewWriter(conn))
if err := session.EncodeRequestHeader(request, writer); err != nil {
return errors.New("failed to encode request").Base(err)
stream := bodyWriter
if singleXUDP {
stream = xudp.NewPacketWriter(bodyWriter, target, xudp.GetGlobalID(ctx))
}
bodyWriter, err := session.EncodeRequestBody(request, writer)
if err != nil {
return errors.New("failed to start encoding").Base(err)
}
bodyWriter2 := bodyWriter
if request.Command == protocol.RequestCommandMux && request.Port == 666 {
bodyWriter = xudp.NewPacketWriter(bodyWriter, target, xudp.GetGlobalID(ctx))
}
if err := buf.CopyOnceTimeout(input, bodyWriter, time.Millisecond*100); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
if err := buf.CopyOnceTimeout(link.Reader, stream, time.Millisecond*100); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
return errors.New("failed to write first payload").Base(err)
}
@@ -171,50 +213,33 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
return err
}
if err := buf.Copy(input, bodyWriter, buf.UpdateActivity(timer)); err != nil {
err := buf.Copy(link.Reader, stream, buf.UpdateActivity(timer))
if err != nil || account.NoTerminationSignal {
return err
}
if request.Option.Has(protocol.RequestOptionChunkStream) && !account.NoTerminationSignal {
if err := bodyWriter2.WriteMultiBuffer(buf.MultiBuffer{}); err != nil {
return err
}
}
return nil
return bodyWriter.WriteMultiBuffer(buf.MultiBuffer{})
}
responseDone := func() error {
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
reader := &buf.BufferedReader{Reader: buf.NewReader(conn)}
header, err := session.DecodeResponseHeader(reader)
bodyReader, err := openResponse()
if err != nil {
return errors.New("failed to read header").Base(err)
return err
}
h.handleCommand(rec.Destination, header.Command)
bodyReader, err := session.DecodeResponseBody(request, reader)
if err != nil {
return errors.New("failed to start encoding response").Base(err)
}
if request.Command == protocol.RequestCommandMux && request.Port == 666 {
if singleXUDP {
bodyReader = xudp.NewPacketReader(&buf.BufferedReader{Reader: bodyReader})
}
return buf.Copy(bodyReader, output, buf.UpdateActivity(timer))
return buf.Copy(bodyReader, link.Writer, buf.UpdateActivity(timer))
}
if newCtx != nil {
ctx = newCtx
}
responseDonePost := task.OnSuccess(responseDone, task.Close(output))
responseDonePost := task.OnSuccess(responseDone, task.Close(link.Writer))
if err := task.Run(ctx, requestDone, responseDonePost); err != nil {
return errors.New("connection ends").Base(err)
return nil, nil, errors.New("connection ends").Base(err)
}
return nil
return nil, nil, nil
}
var enablePadding atomic.Bool