From bf43c38c59c920df29c373b8b64931e2b70426eb Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 13 Apr 2024 00:25:42 -0400 Subject: [PATCH 01/11] Add VLESS seed configurations --- proxy/vless/encoding/addons.go | 18 +- proxy/vless/encoding/addons.pb.go | 410 ++++++++++++++++++++++++++++-- proxy/vless/encoding/addons.proto | 30 +++ 3 files changed, 420 insertions(+), 38 deletions(-) diff --git a/proxy/vless/encoding/addons.go b/proxy/vless/encoding/addons.go index 1bf1817dbc82..374c45d1f528 100644 --- a/proxy/vless/encoding/addons.go +++ b/proxy/vless/encoding/addons.go @@ -13,8 +13,7 @@ import ( ) func EncodeHeaderAddons(buffer *buf.Buffer, addons *Addons) error { - switch addons.Flow { - case vless.XRV: + if addons.Flow == vless.XRV || len(addons.Seed) > 0 { bytes, err := proto.Marshal(addons) if err != nil { return errors.New("failed to marshal addons protobuf value").Base(err) @@ -25,12 +24,11 @@ func EncodeHeaderAddons(buffer *buf.Buffer, addons *Addons) error { if _, err := buffer.Write(bytes); err != nil { return errors.New("failed to write addons protobuf value").Base(err) } - default: + } else { if err := buffer.WriteByte(0); err != nil { return errors.New("failed to write addons protobuf length").Base(err) } } - return nil } @@ -50,11 +48,6 @@ func DecodeHeaderAddons(buffer *buf.Buffer, reader io.Reader) (*Addons, error) { if err := proto.Unmarshal(buffer.Bytes(), addons); err != nil { return nil, errors.New("failed to unmarshal addons protobuf value").Base(err) } - - // Verification. - switch addons.Flow { - default: - } } return addons, nil @@ -74,11 +67,8 @@ func EncodeBodyAddons(writer io.Writer, request *protocol.RequestHeader, request // DecodeBodyAddons returns a Reader from which caller can fetch decrypted body. func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *Addons) buf.Reader { - switch addons.Flow { - default: - if request.Command == protocol.RequestCommandUDP { - return NewLengthPacketReader(reader) - } + if request.Command == protocol.RequestCommandUDP { + return NewLengthPacketReader(reader) } return buf.NewReader(reader) } diff --git a/proxy/vless/encoding/addons.pb.go b/proxy/vless/encoding/addons.pb.go index f093dda41e4e..8436b8ed438f 100644 --- a/proxy/vless/encoding/addons.pb.go +++ b/proxy/vless/encoding/addons.pb.go @@ -20,13 +20,70 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) +type SeedMode int32 + +const ( + SeedMode_Unknown SeedMode = 0 + SeedMode_PaddingOnly SeedMode = 1 + SeedMode_PaddingPlusDelay SeedMode = 2 + SeedMode_IndependentScheduler SeedMode = 3 +) + +// Enum value maps for SeedMode. +var ( + SeedMode_name = map[int32]string{ + 0: "Unknown", + 1: "PaddingOnly", + 2: "PaddingPlusDelay", + 3: "IndependentScheduler", + } + SeedMode_value = map[string]int32{ + "Unknown": 0, + "PaddingOnly": 1, + "PaddingPlusDelay": 2, + "IndependentScheduler": 3, + } +) + +func (x SeedMode) Enum() *SeedMode { + p := new(SeedMode) + *p = x + return p +} + +func (x SeedMode) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (SeedMode) Descriptor() protoreflect.EnumDescriptor { + return file_proxy_vless_encoding_addons_proto_enumTypes[0].Descriptor() +} + +func (SeedMode) Type() protoreflect.EnumType { + return &file_proxy_vless_encoding_addons_proto_enumTypes[0] +} + +func (x SeedMode) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use SeedMode.Descriptor instead. +func (SeedMode) EnumDescriptor() ([]byte, []int) { + return file_proxy_vless_encoding_addons_proto_rawDescGZIP(), []int{0} +} + type Addons struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Flow string `protobuf:"bytes,1,opt,name=Flow,proto3" json:"Flow,omitempty"` - Seed []byte `protobuf:"bytes,2,opt,name=Seed,proto3" json:"Seed,omitempty"` + Flow string `protobuf:"bytes,1,opt,name=Flow,proto3" json:"Flow,omitempty"` + Seed []byte `protobuf:"bytes,2,opt,name=Seed,proto3" json:"Seed,omitempty"` + Mode SeedMode `protobuf:"varint,3,opt,name=Mode,proto3,enum=xray.proxy.vless.encoding.SeedMode" json:"Mode,omitempty"` + Duration string `protobuf:"bytes,4,opt,name=Duration,proto3" json:"Duration,omitempty"` // "0-8" means apply to number of packets, "1kb-" means start applying once both side exchange 1kb data, counting two-ways + Padding *PaddingConfig `protobuf:"bytes,5,opt,name=Padding,proto3" json:"Padding,omitempty"` + Delay *DelayConfig `protobuf:"bytes,6,opt,name=Delay,proto3" json:"Delay,omitempty"` + SchedulerConfig *SchedulerConfig `protobuf:"bytes,7,opt,name=SchedulerConfig,proto3" json:"SchedulerConfig,omitempty"` } func (x *Addons) Reset() { @@ -75,24 +132,283 @@ func (x *Addons) GetSeed() []byte { return nil } +func (x *Addons) GetMode() SeedMode { + if x != nil { + return x.Mode + } + return SeedMode_Unknown +} + +func (x *Addons) GetDuration() string { + if x != nil { + return x.Duration + } + return "" +} + +func (x *Addons) GetPadding() *PaddingConfig { + if x != nil { + return x.Padding + } + return nil +} + +func (x *Addons) GetDelay() *DelayConfig { + if x != nil { + return x.Delay + } + return nil +} + +func (x *Addons) GetSchedulerConfig() *SchedulerConfig { + if x != nil { + return x.SchedulerConfig + } + return nil +} + +type PaddingConfig struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + RegularMin uint32 `protobuf:"varint,1,opt,name=RegularMin,proto3" json:"RegularMin,omitempty"` + RegularMax uint32 `protobuf:"varint,2,opt,name=RegularMax,proto3" json:"RegularMax,omitempty"` + LongMin uint32 `protobuf:"varint,3,opt,name=LongMin,proto3" json:"LongMin,omitempty"` + LongMax uint32 `protobuf:"varint,4,opt,name=LongMax,proto3" json:"LongMax,omitempty"` +} + +func (x *PaddingConfig) Reset() { + *x = PaddingConfig{} + if protoimpl.UnsafeEnabled { + mi := &file_proxy_vless_encoding_addons_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PaddingConfig) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PaddingConfig) ProtoMessage() {} + +func (x *PaddingConfig) ProtoReflect() protoreflect.Message { + mi := &file_proxy_vless_encoding_addons_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PaddingConfig.ProtoReflect.Descriptor instead. +func (*PaddingConfig) Descriptor() ([]byte, []int) { + return file_proxy_vless_encoding_addons_proto_rawDescGZIP(), []int{1} +} + +func (x *PaddingConfig) GetRegularMin() uint32 { + if x != nil { + return x.RegularMin + } + return 0 +} + +func (x *PaddingConfig) GetRegularMax() uint32 { + if x != nil { + return x.RegularMax + } + return 0 +} + +func (x *PaddingConfig) GetLongMin() uint32 { + if x != nil { + return x.LongMin + } + return 0 +} + +func (x *PaddingConfig) GetLongMax() uint32 { + if x != nil { + return x.LongMax + } + return 0 +} + +type DelayConfig struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + IsRandom bool `protobuf:"varint,1,opt,name=IsRandom,proto3" json:"IsRandom,omitempty"` + MinMillis uint32 `protobuf:"varint,2,opt,name=MinMillis,proto3" json:"MinMillis,omitempty"` + MaxMillis uint32 `protobuf:"varint,3,opt,name=MaxMillis,proto3" json:"MaxMillis,omitempty"` +} + +func (x *DelayConfig) Reset() { + *x = DelayConfig{} + if protoimpl.UnsafeEnabled { + mi := &file_proxy_vless_encoding_addons_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DelayConfig) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DelayConfig) ProtoMessage() {} + +func (x *DelayConfig) ProtoReflect() protoreflect.Message { + mi := &file_proxy_vless_encoding_addons_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DelayConfig.ProtoReflect.Descriptor instead. +func (*DelayConfig) Descriptor() ([]byte, []int) { + return file_proxy_vless_encoding_addons_proto_rawDescGZIP(), []int{2} +} + +func (x *DelayConfig) GetIsRandom() bool { + if x != nil { + return x.IsRandom + } + return false +} + +func (x *DelayConfig) GetMinMillis() uint32 { + if x != nil { + return x.MinMillis + } + return 0 +} + +func (x *DelayConfig) GetMaxMillis() uint32 { + if x != nil { + return x.MaxMillis + } + return 0 +} + +type SchedulerConfig struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + TimeoutMillis uint32 `protobuf:"varint,1,opt,name=TimeoutMillis,proto3" json:"TimeoutMillis,omitempty"` // original traffic will not be sent right away but when scheduler want to send or pending buffer times out +} + +func (x *SchedulerConfig) Reset() { + *x = SchedulerConfig{} + if protoimpl.UnsafeEnabled { + mi := &file_proxy_vless_encoding_addons_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SchedulerConfig) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SchedulerConfig) ProtoMessage() {} + +func (x *SchedulerConfig) ProtoReflect() protoreflect.Message { + mi := &file_proxy_vless_encoding_addons_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SchedulerConfig.ProtoReflect.Descriptor instead. +func (*SchedulerConfig) Descriptor() ([]byte, []int) { + return file_proxy_vless_encoding_addons_proto_rawDescGZIP(), []int{3} +} + +func (x *SchedulerConfig) GetTimeoutMillis() uint32 { + if x != nil { + return x.TimeoutMillis + } + return 0 +} + var File_proxy_vless_encoding_addons_proto protoreflect.FileDescriptor var file_proxy_vless_encoding_addons_proto_rawDesc = []byte{ 0x0a, 0x21, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2f, 0x61, 0x64, 0x64, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x19, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, - 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x22, 0x30, - 0x0a, 0x06, 0x41, 0x64, 0x64, 0x6f, 0x6e, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x46, 0x6c, 0x6f, 0x77, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x46, 0x6c, 0x6f, 0x77, 0x12, 0x12, 0x0a, 0x04, - 0x53, 0x65, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x53, 0x65, 0x65, 0x64, - 0x42, 0x6d, 0x0a, 0x1d, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, - 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, - 0x67, 0x50, 0x01, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, - 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, - 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2f, 0x65, 0x6e, 0x63, 0x6f, 0x64, - 0x69, 0x6e, 0x67, 0xaa, 0x02, 0x19, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, - 0x2e, 0x56, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x22, 0xdd, + 0x02, 0x0a, 0x06, 0x41, 0x64, 0x64, 0x6f, 0x6e, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x46, 0x6c, 0x6f, + 0x77, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x46, 0x6c, 0x6f, 0x77, 0x12, 0x12, 0x0a, + 0x04, 0x53, 0x65, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x53, 0x65, 0x65, + 0x64, 0x12, 0x37, 0x0a, 0x04, 0x4d, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, + 0x23, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, + 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x53, 0x65, 0x65, 0x64, + 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x44, 0x75, + 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x44, 0x75, + 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x42, 0x0a, 0x07, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, + 0x67, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x28, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, + 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, + 0x69, 0x6e, 0x67, 0x2e, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6e, 0x66, 0x69, + 0x67, 0x52, 0x07, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x12, 0x3c, 0x0a, 0x05, 0x44, 0x65, + 0x6c, 0x61, 0x79, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x78, 0x72, 0x61, 0x79, + 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, + 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x43, 0x6f, 0x6e, 0x66, 0x69, + 0x67, 0x52, 0x05, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x12, 0x54, 0x0a, 0x0f, 0x53, 0x63, 0x68, 0x65, + 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x07, 0x20, 0x01, 0x28, + 0x0b, 0x32, 0x2a, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, + 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x53, 0x63, + 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x0f, 0x53, + 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x83, + 0x01, 0x0a, 0x0d, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, + 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x69, 0x6e, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x69, 0x6e, + 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x61, 0x78, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x61, 0x78, + 0x12, 0x18, 0x0a, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x69, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x0d, 0x52, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x69, 0x6e, 0x12, 0x18, 0x0a, 0x07, 0x4c, 0x6f, + 0x6e, 0x67, 0x4d, 0x61, 0x78, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x4c, 0x6f, 0x6e, + 0x67, 0x4d, 0x61, 0x78, 0x22, 0x65, 0x0a, 0x0b, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x43, 0x6f, 0x6e, + 0x66, 0x69, 0x67, 0x12, 0x1a, 0x0a, 0x08, 0x49, 0x73, 0x52, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x49, 0x73, 0x52, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x12, + 0x1c, 0x0a, 0x09, 0x4d, 0x69, 0x6e, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x0d, 0x52, 0x09, 0x4d, 0x69, 0x6e, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x12, 0x1c, 0x0a, + 0x09, 0x4d, 0x61, 0x78, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, + 0x52, 0x09, 0x4d, 0x61, 0x78, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x22, 0x37, 0x0a, 0x0f, 0x53, + 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x24, + 0x0a, 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, + 0x6c, 0x6c, 0x69, 0x73, 0x2a, 0x58, 0x0a, 0x08, 0x53, 0x65, 0x65, 0x64, 0x4d, 0x6f, 0x64, 0x65, + 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x0f, 0x0a, + 0x0b, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x4f, 0x6e, 0x6c, 0x79, 0x10, 0x01, 0x12, 0x14, + 0x0a, 0x10, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x50, 0x6c, 0x75, 0x73, 0x44, 0x65, 0x6c, + 0x61, 0x79, 0x10, 0x02, 0x12, 0x18, 0x0a, 0x14, 0x49, 0x6e, 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, + 0x65, 0x6e, 0x74, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x10, 0x03, 0x42, 0x6d, + 0x0a, 0x1d, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, + 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x50, + 0x01, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, + 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, + 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, + 0x67, 0xaa, 0x02, 0x19, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x56, + 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -107,16 +423,25 @@ func file_proxy_vless_encoding_addons_proto_rawDescGZIP() []byte { return file_proxy_vless_encoding_addons_proto_rawDescData } -var file_proxy_vless_encoding_addons_proto_msgTypes = make([]protoimpl.MessageInfo, 1) -var file_proxy_vless_encoding_addons_proto_goTypes = []any{ - (*Addons)(nil), // 0: xray.proxy.vless.encoding.Addons +var file_proxy_vless_encoding_addons_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_proxy_vless_encoding_addons_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_proxy_vless_encoding_addons_proto_goTypes = []interface{}{ + (SeedMode)(0), // 0: xray.proxy.vless.encoding.SeedMode + (*Addons)(nil), // 1: xray.proxy.vless.encoding.Addons + (*PaddingConfig)(nil), // 2: xray.proxy.vless.encoding.PaddingConfig + (*DelayConfig)(nil), // 3: xray.proxy.vless.encoding.DelayConfig + (*SchedulerConfig)(nil), // 4: xray.proxy.vless.encoding.SchedulerConfig } var file_proxy_vless_encoding_addons_proto_depIdxs = []int32{ - 0, // [0:0] is the sub-list for method output_type - 0, // [0:0] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name + 0, // 0: xray.proxy.vless.encoding.Addons.Mode:type_name -> xray.proxy.vless.encoding.SeedMode + 2, // 1: xray.proxy.vless.encoding.Addons.Padding:type_name -> xray.proxy.vless.encoding.PaddingConfig + 3, // 2: xray.proxy.vless.encoding.Addons.Delay:type_name -> xray.proxy.vless.encoding.DelayConfig + 4, // 3: xray.proxy.vless.encoding.Addons.SchedulerConfig:type_name -> xray.proxy.vless.encoding.SchedulerConfig + 4, // [4:4] is the sub-list for method output_type + 4, // [4:4] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name } func init() { file_proxy_vless_encoding_addons_proto_init() } @@ -137,19 +462,56 @@ func file_proxy_vless_encoding_addons_proto_init() { return nil } } + file_proxy_vless_encoding_addons_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PaddingConfig); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proxy_vless_encoding_addons_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DelayConfig); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proxy_vless_encoding_addons_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SchedulerConfig); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_proxy_vless_encoding_addons_proto_rawDesc, - NumEnums: 0, - NumMessages: 1, + NumEnums: 1, + NumMessages: 4, NumExtensions: 0, NumServices: 0, }, GoTypes: file_proxy_vless_encoding_addons_proto_goTypes, DependencyIndexes: file_proxy_vless_encoding_addons_proto_depIdxs, + EnumInfos: file_proxy_vless_encoding_addons_proto_enumTypes, MessageInfos: file_proxy_vless_encoding_addons_proto_msgTypes, }.Build() File_proxy_vless_encoding_addons_proto = out.File diff --git a/proxy/vless/encoding/addons.proto b/proxy/vless/encoding/addons.proto index 3730e87fd51d..4a7d12f17399 100644 --- a/proxy/vless/encoding/addons.proto +++ b/proxy/vless/encoding/addons.proto @@ -9,4 +9,34 @@ option java_multiple_files = true; message Addons { string Flow = 1; bytes Seed = 2; + SeedMode Mode = 3; + string Duration = 4; // "0-8" means apply to number of packets, "1kb-" means start applying once both side exchange 1kb data, counting two-ways + PaddingConfig Padding = 5; + DelayConfig Delay = 6; + SchedulerConfig SchedulerConfig = 7; +} + +enum SeedMode { + Unknown = 0; + PaddingOnly = 1; + PaddingPlusDelay = 2; + IndependentScheduler = 3; +} + +message PaddingConfig { + uint32 RegularMin = 1; + uint32 RegularMax = 2; + uint32 LongMin = 3; + uint32 LongMax = 4; +} + +message DelayConfig { + bool IsRandom = 1; + uint32 MinMillis = 2; + uint32 MaxMillis = 3; +} + +message SchedulerConfig { + uint32 TimeoutMillis = 1; // original traffic will not be sent right away but when scheduler want to send or pending buffer times out + // Other TBD } From 2c874f5c4b8d0536a08a8db5022201f87ec258f3 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:55:05 -0400 Subject: [PATCH 02/11] Populate Seed (more TBD) and checks --- proxy/vless/account.go | 3 + proxy/vless/account.pb.go | 26 ++++++--- proxy/vless/account.proto | 2 + proxy/vless/encoding/addons.go | 62 ++++++++++++++++++++ proxy/vless/encoding/addons.pb.go | 97 +++++++++++++++---------------- proxy/vless/encoding/addons.proto | 2 +- proxy/vless/inbound/inbound.go | 4 ++ proxy/vless/outbound/outbound.go | 1 + 8 files changed, 139 insertions(+), 58 deletions(-) diff --git a/proxy/vless/account.go b/proxy/vless/account.go index 5349a3bd7be2..98cbe7e7a421 100644 --- a/proxy/vless/account.go +++ b/proxy/vless/account.go @@ -16,6 +16,7 @@ func (a *Account) AsAccount() (protocol.Account, error) { ID: protocol.NewID(id), Flow: a.Flow, // needs parser here? Encryption: a.Encryption, // needs parser here? + Seed: a.Seed, }, nil } @@ -27,6 +28,8 @@ type MemoryAccount struct { Flow string // Encryption of the account. Used for client connections, and only accepts "none" for now. Encryption string + // Seed. Details TBD + Seed string } // Equals implements protocol.Account.Equals(). diff --git a/proxy/vless/account.pb.go b/proxy/vless/account.pb.go index 78854359067f..7a834b2bc83f 100644 --- a/proxy/vless/account.pb.go +++ b/proxy/vless/account.pb.go @@ -31,6 +31,8 @@ type Account struct { Flow string `protobuf:"bytes,2,opt,name=flow,proto3" json:"flow,omitempty"` // Encryption settings. Only applies to client side, and only accepts "none" for now. Encryption string `protobuf:"bytes,3,opt,name=encryption,proto3" json:"encryption,omitempty"` + // Seed settings. Details TBD + Seed string `protobuf:"bytes,4,opt,name=seed,proto3" json:"seed,omitempty"` } func (x *Account) Reset() { @@ -86,23 +88,31 @@ func (x *Account) GetEncryption() string { return "" } +func (x *Account) GetSeed() string { + if x != nil { + return x.Seed + } + return "" +} + var File_proxy_vless_account_proto protoreflect.FileDescriptor var file_proxy_vless_account_proto_rawDesc = []byte{ 0x0a, 0x19, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2f, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x10, 0x78, 0x72, 0x61, - 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x22, 0x4d, 0x0a, + 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x22, 0x61, 0x0a, 0x07, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x6c, 0x6f, 0x77, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x6c, 0x6f, 0x77, 0x12, 0x1e, 0x0a, 0x0a, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x0a, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x52, 0x0a, 0x14, - 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, - 0x6c, 0x65, 0x73, 0x73, 0x50, 0x01, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, - 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0xaa, 0x02, 0x10, - 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x56, 0x6c, 0x65, 0x73, 0x73, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x52, 0x0a, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, + 0x73, 0x65, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x73, 0x65, 0x65, 0x64, + 0x42, 0x52, 0x0a, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, + 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x50, 0x01, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, + 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, + 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, + 0x73, 0xaa, 0x02, 0x10, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x56, + 0x6c, 0x65, 0x73, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/proxy/vless/account.proto b/proxy/vless/account.proto index 51d2cb7deaaf..7b7717442b58 100644 --- a/proxy/vless/account.proto +++ b/proxy/vless/account.proto @@ -13,4 +13,6 @@ message Account { string flow = 2; // Encryption settings. Only applies to client side, and only accepts "none" for now. string encryption = 3; + // Seed settings. Details TBD + string seed = 4; } diff --git a/proxy/vless/encoding/addons.go b/proxy/vless/encoding/addons.go index 374c45d1f528..ce9530bb009d 100644 --- a/proxy/vless/encoding/addons.go +++ b/proxy/vless/encoding/addons.go @@ -1,6 +1,7 @@ package encoding import ( + "bytes" "context" "io" @@ -178,3 +179,64 @@ func (r *LengthPacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) { } return mb, nil } + +func PopulateSeed(seed string, addons *Addons) { + if len(seed) > 0 { + addons.Seed = []byte {1} // only turn on, more TBD + addons.Mode = SeedMode_PaddingPlusDelay + addons.Duration = "0-8" + addons.Padding = &PaddingConfig{ + RegularMin: 0, + RegularMax: 256, + LongMin: 900, + LongMax: 1400, + } + addons.Delay = &DelayConfig{ + IsRandom: true, + MinMillis: 100, + MaxMillis: 500, + } + addons.Scheduler = &SchedulerConfig{ + TimeoutMillis: 600, + } + } +} + +func CheckSeed(requestAddons *Addons, responseAddons *Addons) error { + if !bytes.Equal(requestAddons.Seed, responseAddons.Seed) { + return newError("Seed bytes not match", requestAddons.Seed, responseAddons.Seed) + } + if requestAddons.Mode != responseAddons.Mode { + return newError("Mode not match", requestAddons.Mode, responseAddons.Mode) + } + if requestAddons.Duration != responseAddons.Duration { + return newError("Duration not match", requestAddons.Duration, responseAddons.Duration) + } + if requestAddons.Padding != nil && responseAddons.Padding != nil { + if requestAddons.Padding.RegularMin != responseAddons.Padding.RegularMin || + requestAddons.Padding.RegularMax != responseAddons.Padding.RegularMax || + requestAddons.Padding.LongMin != responseAddons.Padding.LongMin || + requestAddons.Padding.LongMax != responseAddons.Padding.LongMax { + return newError("Padding not match") + } + } else if requestAddons.Padding != nil || responseAddons.Padding != nil { + return newError("Padding of one is nil but the other is not nil") + } + if requestAddons.Delay != nil && responseAddons.Delay != nil { + if requestAddons.Delay.IsRandom != responseAddons.Delay.IsRandom || + requestAddons.Delay.MinMillis != responseAddons.Delay.MinMillis || + requestAddons.Delay.MaxMillis != responseAddons.Delay.MaxMillis { + return newError("Delay not match") + } + } else if requestAddons.Delay != nil || responseAddons.Delay != nil { + return newError("Delay of one is nil but the other is not nil") + } + if requestAddons.Scheduler != nil && responseAddons.Scheduler != nil { + if requestAddons.Scheduler.TimeoutMillis != responseAddons.Scheduler.TimeoutMillis { + return newError("Scheduler not match") + } + } else if requestAddons.Scheduler != nil || responseAddons.Scheduler != nil { + return newError("Scheduler of one is nil but the other is not nil") + } + return nil +} diff --git a/proxy/vless/encoding/addons.pb.go b/proxy/vless/encoding/addons.pb.go index 8436b8ed438f..38086dbfeae7 100644 --- a/proxy/vless/encoding/addons.pb.go +++ b/proxy/vless/encoding/addons.pb.go @@ -77,13 +77,13 @@ type Addons struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Flow string `protobuf:"bytes,1,opt,name=Flow,proto3" json:"Flow,omitempty"` - Seed []byte `protobuf:"bytes,2,opt,name=Seed,proto3" json:"Seed,omitempty"` - Mode SeedMode `protobuf:"varint,3,opt,name=Mode,proto3,enum=xray.proxy.vless.encoding.SeedMode" json:"Mode,omitempty"` - Duration string `protobuf:"bytes,4,opt,name=Duration,proto3" json:"Duration,omitempty"` // "0-8" means apply to number of packets, "1kb-" means start applying once both side exchange 1kb data, counting two-ways - Padding *PaddingConfig `protobuf:"bytes,5,opt,name=Padding,proto3" json:"Padding,omitempty"` - Delay *DelayConfig `protobuf:"bytes,6,opt,name=Delay,proto3" json:"Delay,omitempty"` - SchedulerConfig *SchedulerConfig `protobuf:"bytes,7,opt,name=SchedulerConfig,proto3" json:"SchedulerConfig,omitempty"` + Flow string `protobuf:"bytes,1,opt,name=Flow,proto3" json:"Flow,omitempty"` + Seed []byte `protobuf:"bytes,2,opt,name=Seed,proto3" json:"Seed,omitempty"` + Mode SeedMode `protobuf:"varint,3,opt,name=Mode,proto3,enum=xray.proxy.vless.encoding.SeedMode" json:"Mode,omitempty"` + Duration string `protobuf:"bytes,4,opt,name=Duration,proto3" json:"Duration,omitempty"` // "0-8" means apply to number of packets, "1kb-" means start applying once both side exchange 1kb data, counting two-ways + Padding *PaddingConfig `protobuf:"bytes,5,opt,name=Padding,proto3" json:"Padding,omitempty"` + Delay *DelayConfig `protobuf:"bytes,6,opt,name=Delay,proto3" json:"Delay,omitempty"` + Scheduler *SchedulerConfig `protobuf:"bytes,7,opt,name=Scheduler,proto3" json:"Scheduler,omitempty"` } func (x *Addons) Reset() { @@ -160,9 +160,9 @@ func (x *Addons) GetDelay() *DelayConfig { return nil } -func (x *Addons) GetSchedulerConfig() *SchedulerConfig { +func (x *Addons) GetScheduler() *SchedulerConfig { if x != nil { - return x.SchedulerConfig + return x.Scheduler } return nil } @@ -354,7 +354,7 @@ var file_proxy_vless_encoding_addons_proto_rawDesc = []byte{ 0x0a, 0x21, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2f, 0x61, 0x64, 0x64, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x19, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, - 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x22, 0xdd, + 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x22, 0xd1, 0x02, 0x0a, 0x06, 0x41, 0x64, 0x64, 0x6f, 0x6e, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x46, 0x6c, 0x6f, 0x77, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x46, 0x6c, 0x6f, 0x77, 0x12, 0x12, 0x0a, 0x04, 0x53, 0x65, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x53, 0x65, 0x65, @@ -371,44 +371,43 @@ var file_proxy_vless_encoding_addons_proto_rawDesc = []byte{ 0x6c, 0x61, 0x79, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x43, 0x6f, 0x6e, 0x66, 0x69, - 0x67, 0x52, 0x05, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x12, 0x54, 0x0a, 0x0f, 0x53, 0x63, 0x68, 0x65, - 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x07, 0x20, 0x01, 0x28, - 0x0b, 0x32, 0x2a, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, - 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x53, 0x63, - 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x0f, 0x53, - 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x83, - 0x01, 0x0a, 0x0d, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, - 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x69, 0x6e, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x69, 0x6e, - 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x61, 0x78, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x61, 0x78, - 0x12, 0x18, 0x0a, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x69, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, - 0x0d, 0x52, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x69, 0x6e, 0x12, 0x18, 0x0a, 0x07, 0x4c, 0x6f, - 0x6e, 0x67, 0x4d, 0x61, 0x78, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x4c, 0x6f, 0x6e, - 0x67, 0x4d, 0x61, 0x78, 0x22, 0x65, 0x0a, 0x0b, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x43, 0x6f, 0x6e, - 0x66, 0x69, 0x67, 0x12, 0x1a, 0x0a, 0x08, 0x49, 0x73, 0x52, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x49, 0x73, 0x52, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x12, - 0x1c, 0x0a, 0x09, 0x4d, 0x69, 0x6e, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x0d, 0x52, 0x09, 0x4d, 0x69, 0x6e, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x12, 0x1c, 0x0a, - 0x09, 0x4d, 0x61, 0x78, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, - 0x52, 0x09, 0x4d, 0x61, 0x78, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x22, 0x37, 0x0a, 0x0f, 0x53, - 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x24, - 0x0a, 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, - 0x6c, 0x6c, 0x69, 0x73, 0x2a, 0x58, 0x0a, 0x08, 0x53, 0x65, 0x65, 0x64, 0x4d, 0x6f, 0x64, 0x65, - 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x0f, 0x0a, - 0x0b, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x4f, 0x6e, 0x6c, 0x79, 0x10, 0x01, 0x12, 0x14, - 0x0a, 0x10, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x50, 0x6c, 0x75, 0x73, 0x44, 0x65, 0x6c, - 0x61, 0x79, 0x10, 0x02, 0x12, 0x18, 0x0a, 0x14, 0x49, 0x6e, 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, - 0x65, 0x6e, 0x74, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x10, 0x03, 0x42, 0x6d, - 0x0a, 0x1d, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, - 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x50, - 0x01, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, - 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, - 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, - 0x67, 0xaa, 0x02, 0x19, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x56, - 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x62, 0x06, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x67, 0x52, 0x05, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x12, 0x48, 0x0a, 0x09, 0x53, 0x63, 0x68, 0x65, + 0x64, 0x75, 0x6c, 0x65, 0x72, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x2a, 0x2e, 0x78, 0x72, + 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, + 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, + 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x09, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, + 0x65, 0x72, 0x22, 0x83, 0x01, 0x0a, 0x0d, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x43, 0x6f, + 0x6e, 0x66, 0x69, 0x67, 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, + 0x69, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, + 0x72, 0x4d, 0x69, 0x6e, 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, + 0x61, 0x78, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, + 0x72, 0x4d, 0x61, 0x78, 0x12, 0x18, 0x0a, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x69, 0x6e, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x69, 0x6e, 0x12, 0x18, + 0x0a, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x61, 0x78, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, + 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x61, 0x78, 0x22, 0x65, 0x0a, 0x0b, 0x44, 0x65, 0x6c, 0x61, + 0x79, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x1a, 0x0a, 0x08, 0x49, 0x73, 0x52, 0x61, 0x6e, + 0x64, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x49, 0x73, 0x52, 0x61, 0x6e, + 0x64, 0x6f, 0x6d, 0x12, 0x1c, 0x0a, 0x09, 0x4d, 0x69, 0x6e, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x4d, 0x69, 0x6e, 0x4d, 0x69, 0x6c, 0x6c, 0x69, + 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x4d, 0x61, 0x78, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x4d, 0x61, 0x78, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x22, + 0x37, 0x0a, 0x0f, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, + 0x69, 0x67, 0x12, 0x24, 0x0a, 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, 0x6c, + 0x6c, 0x69, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, + 0x75, 0x74, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x2a, 0x58, 0x0a, 0x08, 0x53, 0x65, 0x65, 0x64, + 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, + 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x4f, 0x6e, 0x6c, 0x79, + 0x10, 0x01, 0x12, 0x14, 0x0a, 0x10, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x50, 0x6c, 0x75, + 0x73, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x10, 0x02, 0x12, 0x18, 0x0a, 0x14, 0x49, 0x6e, 0x64, 0x65, + 0x70, 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x74, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, + 0x10, 0x03, 0x42, 0x6d, 0x0a, 0x1d, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, + 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x65, 0x6e, 0x63, 0x6f, 0x64, + 0x69, 0x6e, 0x67, 0x50, 0x01, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, + 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2f, 0x65, 0x6e, 0x63, + 0x6f, 0x64, 0x69, 0x6e, 0x67, 0xaa, 0x02, 0x19, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, + 0x78, 0x79, 0x2e, 0x56, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, + 0x67, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -436,7 +435,7 @@ var file_proxy_vless_encoding_addons_proto_depIdxs = []int32{ 0, // 0: xray.proxy.vless.encoding.Addons.Mode:type_name -> xray.proxy.vless.encoding.SeedMode 2, // 1: xray.proxy.vless.encoding.Addons.Padding:type_name -> xray.proxy.vless.encoding.PaddingConfig 3, // 2: xray.proxy.vless.encoding.Addons.Delay:type_name -> xray.proxy.vless.encoding.DelayConfig - 4, // 3: xray.proxy.vless.encoding.Addons.SchedulerConfig:type_name -> xray.proxy.vless.encoding.SchedulerConfig + 4, // 3: xray.proxy.vless.encoding.Addons.Scheduler:type_name -> xray.proxy.vless.encoding.SchedulerConfig 4, // [4:4] is the sub-list for method output_type 4, // [4:4] is the sub-list for method input_type 4, // [4:4] is the sub-list for extension type_name diff --git a/proxy/vless/encoding/addons.proto b/proxy/vless/encoding/addons.proto index 4a7d12f17399..4820ef50449c 100644 --- a/proxy/vless/encoding/addons.proto +++ b/proxy/vless/encoding/addons.proto @@ -13,7 +13,7 @@ message Addons { string Duration = 4; // "0-8" means apply to number of packets, "1kb-" means start applying once both side exchange 1kb data, counting two-ways PaddingConfig Padding = 5; DelayConfig Delay = 6; - SchedulerConfig SchedulerConfig = 7; + SchedulerConfig Scheduler = 7; } enum SeedMode { diff --git a/proxy/vless/inbound/inbound.go b/proxy/vless/inbound/inbound.go index f443007dd67d..a0d34e8558ff 100644 --- a/proxy/vless/inbound/inbound.go +++ b/proxy/vless/inbound/inbound.go @@ -443,6 +443,10 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s responseAddons := &encoding.Addons{ // Flow: requestAddons.Flow, } + encoding.PopulateSeed(account.Seed, responseAddons) + if check := encoding.CheckSeed(requestAddons, responseAddons); check != nil { + return newError("Seed configuration mis-match").Base(check).AtWarning() + } var input *bytes.Reader var rawInput *bytes.Buffer diff --git a/proxy/vless/outbound/outbound.go b/proxy/vless/outbound/outbound.go index ed9e07dc56a5..78ee3dd32d87 100644 --- a/proxy/vless/outbound/outbound.go +++ b/proxy/vless/outbound/outbound.go @@ -119,6 +119,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte requestAddons := &encoding.Addons{ Flow: account.Flow, } + encoding.PopulateSeed(account.Seed, requestAddons) var input *bytes.Reader var rawInput *bytes.Buffer From 1a59ab14cd318e4144577c43a91071a2059d6457 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 13 Apr 2024 16:04:58 -0400 Subject: [PATCH 03/11] Add more traffic info for future use --- proxy/proxy.go | 70 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index b507cc39bb2f..8aa1c2e57990 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -92,6 +92,11 @@ type GetOutbound interface { // It is used by XTLS to determine if switch to raw copy mode, It is used by Vision to calculate padding type TrafficState struct { UserUUID []byte + StartTime time.Time + ByteSent int64 + ByteReceived int64 + NumberOfPacketSent int + NumberOfPacketReceived int NumberOfPacketToFilter int EnableXtls bool IsTLS12orAbove bool @@ -115,6 +120,11 @@ type TrafficState struct { func NewTrafficState(userUUID []byte) *TrafficState { return &TrafficState{ UserUUID: userUUID, + StartTime: time.Time{}, + ByteSent: 0, + ByteReceived: 0, + NumberOfPacketSent: 0, + NumberOfPacketReceived: 0, NumberOfPacketToFilter: 8, EnableXtls: false, IsTLS12orAbove: false, @@ -151,6 +161,10 @@ func NewVisionReader(reader buf.Reader, state *TrafficState, context context.Con func (w *VisionReader) ReadMultiBuffer() (buf.MultiBuffer, error) { buffer, err := w.Reader.ReadMultiBuffer() if !buffer.IsEmpty() { + if w.trafficState.StartTime.IsZero() { + w.trafficState.StartTime = time.Now() + } + w.trafficState.ByteReceived += int64(buffer.Len()) if w.trafficState.WithinPaddingBuffers || w.trafficState.NumberOfPacketToFilter > 0 { mb2 := make(buf.MultiBuffer, 0, len(buffer)) for _, b := range buffer { @@ -171,6 +185,7 @@ func (w *VisionReader) ReadMultiBuffer() (buf.MultiBuffer, error) { errors.LogInfo(w.ctx, "XtlsRead unknown command ", w.trafficState.CurrentCommand, buffer.Len()) } } + w.trafficState.NumberOfPacketReceived += len(buffer) if w.trafficState.NumberOfPacketToFilter > 0 { XtlsFilterTls(buffer, w.trafficState, w.ctx) } @@ -199,47 +214,52 @@ func NewVisionWriter(writer buf.Writer, state *TrafficState, context context.Con } func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { + w.trafficState.NumberOfPacketSent += len(mb) if w.trafficState.NumberOfPacketToFilter > 0 { XtlsFilterTls(mb, w.trafficState, w.ctx) } if w.trafficState.IsPadding { if len(mb) == 1 && mb[0] == nil { mb[0] = XtlsPadding(nil, CommandPaddingContinue, &w.writeOnceUserUUID, true, w.ctx) // we do a long padding to hide vless header - return w.Writer.WriteMultiBuffer(mb) - } - mb = ReshapeMultiBuffer(w.ctx, mb) - longPadding := w.trafficState.IsTLS - for i, b := range mb { - if w.trafficState.IsTLS && b.Len() >= 6 && bytes.Equal(TlsApplicationDataStart, b.BytesTo(3)) { - if w.trafficState.EnableXtls { - w.trafficState.WriterSwitchToDirectCopy = true + } else { + mb = ReshapeMultiBuffer(w.ctx, mb) + longPadding := w.trafficState.IsTLS + for i, b := range mb { + if w.trafficState.IsTLS && b.Len() >= 6 && bytes.Equal(TlsApplicationDataStart, b.BytesTo(3)) { + if w.trafficState.EnableXtls { + w.trafficState.WriterSwitchToDirectCopy = true + } + var command byte = CommandPaddingContinue + if i == len(mb) - 1 { + command = CommandPaddingEnd + if w.trafficState.EnableXtls { + command = CommandPaddingDirect + } + } + mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, true, w.ctx) + w.trafficState.IsPadding = false // padding going to end + longPadding = false + continue + } else if !w.trafficState.IsTLS12orAbove && w.trafficState.NumberOfPacketToFilter <= 1 { // For compatibility with earlier vision receiver, we finish padding 1 packet early + w.trafficState.IsPadding = false + mb[i] = XtlsPadding(b, CommandPaddingEnd, &w.writeOnceUserUUID, longPadding, w.ctx) + break } var command byte = CommandPaddingContinue - if i == len(mb)-1 { + if i == len(mb) - 1 && !w.trafficState.IsPadding { command = CommandPaddingEnd if w.trafficState.EnableXtls { command = CommandPaddingDirect } } - mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, true, w.ctx) - w.trafficState.IsPadding = false // padding going to end - longPadding = false - continue - } else if !w.trafficState.IsTLS12orAbove && w.trafficState.NumberOfPacketToFilter <= 1 { // For compatibility with earlier vision receiver, we finish padding 1 packet early - w.trafficState.IsPadding = false - mb[i] = XtlsPadding(b, CommandPaddingEnd, &w.writeOnceUserUUID, longPadding, w.ctx) - break + mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, longPadding, w.ctx) } - var command byte = CommandPaddingContinue - if i == len(mb)-1 && !w.trafficState.IsPadding { - command = CommandPaddingEnd - if w.trafficState.EnableXtls { - command = CommandPaddingDirect - } - } - mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, longPadding, w.ctx) } } + w.trafficState.ByteSent += int64(mb.Len()) + if w.trafficState.StartTime.IsZero() { + w.trafficState.StartTime = time.Now() + } return w.Writer.WriteMultiBuffer(mb) } From a0088a0b95b684a28931283b9e06d73cdf4e93be Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:09:01 -0400 Subject: [PATCH 04/11] Collect stats and possible padding (previously Vision Reader Writer) to all traffic --- proxy/proxy.go | 11 +++++++---- proxy/vless/encoding/addons.go | 34 +++++++++++++++----------------- proxy/vless/inbound/inbound.go | 7 +++---- proxy/vless/outbound/outbound.go | 13 +++--------- 4 files changed, 29 insertions(+), 36 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 8aa1c2e57990..f0030e014d00 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -117,8 +117,8 @@ type TrafficState struct { WriterSwitchToDirectCopy bool } -func NewTrafficState(userUUID []byte) *TrafficState { - return &TrafficState{ +func NewTrafficState(userUUID []byte, flow string) *TrafficState { + var state = TrafficState{ UserUUID: userUUID, StartTime: time.Time{}, ByteSent: 0, @@ -131,15 +131,18 @@ func NewTrafficState(userUUID []byte) *TrafficState { IsTLS: false, Cipher: 0, RemainingServerHello: -1, - WithinPaddingBuffers: true, ReaderSwitchToDirectCopy: false, RemainingCommand: -1, RemainingContent: -1, RemainingPadding: -1, CurrentCommand: 0, - IsPadding: true, WriterSwitchToDirectCopy: false, + }; + if len(flow) > 0 { + state.IsPadding = true; + state.WithinPaddingBuffers = true; } + return &state } // VisionReader is used to read xtls vision protocol diff --git a/proxy/vless/encoding/addons.go b/proxy/vless/encoding/addons.go index ce9530bb009d..526b99959534 100644 --- a/proxy/vless/encoding/addons.go +++ b/proxy/vless/encoding/addons.go @@ -55,23 +55,21 @@ func DecodeHeaderAddons(buffer *buf.Buffer, reader io.Reader) (*Addons, error) { } // EncodeBodyAddons returns a Writer that auto-encrypt content written by caller. -func EncodeBodyAddons(writer io.Writer, request *protocol.RequestHeader, requestAddons *Addons, state *proxy.TrafficState, context context.Context) buf.Writer { +func EncodeBodyAddons(writer buf.Writer, request *protocol.RequestHeader, requestAddons *Addons, state *proxy.TrafficState, context context.Context) buf.Writer { + w := proxy.NewVisionWriter(writer, state, context) if request.Command == protocol.RequestCommandUDP { - return NewMultiLengthPacketWriter(writer.(buf.Writer)) - } - w := buf.NewWriter(writer) - if requestAddons.Flow == vless.XRV { - w = proxy.NewVisionWriter(w, state, context) + return NewMultiLengthPacketWriter(w) } return w } // DecodeBodyAddons returns a Reader from which caller can fetch decrypted body. -func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *Addons) buf.Reader { +func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *Addons, state *proxy.TrafficState, context context.Context) buf.Reader { + r := proxy.NewVisionReader(buf.NewReader(reader), state, context) if request.Command == protocol.RequestCommandUDP { - return NewLengthPacketReader(reader) + return NewLengthPacketReader(&buf.BufferedReader{Reader: r}) } - return buf.NewReader(reader) + return r } func NewMultiLengthPacketWriter(writer buf.Writer) *MultiLengthPacketWriter { @@ -204,39 +202,39 @@ func PopulateSeed(seed string, addons *Addons) { func CheckSeed(requestAddons *Addons, responseAddons *Addons) error { if !bytes.Equal(requestAddons.Seed, responseAddons.Seed) { - return newError("Seed bytes not match", requestAddons.Seed, responseAddons.Seed) + return errors.New("Seed bytes not match", requestAddons.Seed, responseAddons.Seed) } if requestAddons.Mode != responseAddons.Mode { - return newError("Mode not match", requestAddons.Mode, responseAddons.Mode) + return errors.New("Mode not match", requestAddons.Mode, responseAddons.Mode) } if requestAddons.Duration != responseAddons.Duration { - return newError("Duration not match", requestAddons.Duration, responseAddons.Duration) + return errors.New("Duration not match", requestAddons.Duration, responseAddons.Duration) } if requestAddons.Padding != nil && responseAddons.Padding != nil { if requestAddons.Padding.RegularMin != responseAddons.Padding.RegularMin || requestAddons.Padding.RegularMax != responseAddons.Padding.RegularMax || requestAddons.Padding.LongMin != responseAddons.Padding.LongMin || requestAddons.Padding.LongMax != responseAddons.Padding.LongMax { - return newError("Padding not match") + return errors.New("Padding not match") } } else if requestAddons.Padding != nil || responseAddons.Padding != nil { - return newError("Padding of one is nil but the other is not nil") + return errors.New("Padding of one is nil but the other is not nil") } if requestAddons.Delay != nil && responseAddons.Delay != nil { if requestAddons.Delay.IsRandom != responseAddons.Delay.IsRandom || requestAddons.Delay.MinMillis != responseAddons.Delay.MinMillis || requestAddons.Delay.MaxMillis != responseAddons.Delay.MaxMillis { - return newError("Delay not match") + return errors.New("Delay not match") } } else if requestAddons.Delay != nil || responseAddons.Delay != nil { - return newError("Delay of one is nil but the other is not nil") + return errors.New("Delay of one is nil but the other is not nil") } if requestAddons.Scheduler != nil && responseAddons.Scheduler != nil { if requestAddons.Scheduler.TimeoutMillis != responseAddons.Scheduler.TimeoutMillis { - return newError("Scheduler not match") + return errors.New("Scheduler not match") } } else if requestAddons.Scheduler != nil || responseAddons.Scheduler != nil { - return newError("Scheduler of one is nil but the other is not nil") + return errors.New("Scheduler of one is nil but the other is not nil") } return nil } diff --git a/proxy/vless/inbound/inbound.go b/proxy/vless/inbound/inbound.go index a0d34e8558ff..7ea04f964fb7 100644 --- a/proxy/vless/inbound/inbound.go +++ b/proxy/vless/inbound/inbound.go @@ -445,7 +445,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s } encoding.PopulateSeed(account.Seed, responseAddons) if check := encoding.CheckSeed(requestAddons, responseAddons); check != nil { - return newError("Seed configuration mis-match").Base(check).AtWarning() + return errors.New("Seed configuration mis-match").Base(check).AtWarning() } var input *bytes.Reader @@ -516,18 +516,17 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s serverReader := link.Reader // .(*pipe.Reader) serverWriter := link.Writer // .(*pipe.Writer) - trafficState := proxy.NewTrafficState(account.ID.Bytes()) + trafficState := proxy.NewTrafficState(account.ID.Bytes(), account.Flow) postRequest := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) // default: clientReader := reader - clientReader := encoding.DecodeBodyAddons(reader, request, requestAddons) + clientReader := encoding.DecodeBodyAddons(reader, request, requestAddons, trafficState, ctx) var err error if requestAddons.Flow == vless.XRV { ctx1 := session.ContextWithInbound(ctx, nil) // TODO enable splice - clientReader = proxy.NewVisionReader(clientReader, trafficState, ctx1) err = encoding.XtlsRead(clientReader, serverWriter, timer, connection, input, rawInput, trafficState, nil, ctx1) } else { // from clientReader.ReadMultiBuffer to serverWriter.WriteMultiBuffer diff --git a/proxy/vless/outbound/outbound.go b/proxy/vless/outbound/outbound.go index 78ee3dd32d87..11f7d6225dbb 100644 --- a/proxy/vless/outbound/outbound.go +++ b/proxy/vless/outbound/outbound.go @@ -179,7 +179,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte clientReader := link.Reader // .(*pipe.Reader) clientWriter := link.Writer // .(*pipe.Writer) - trafficState := proxy.NewTrafficState(account.ID.Bytes()) + trafficState := proxy.NewTrafficState(account.ID.Bytes(), account.Flow) if request.Command == protocol.RequestCommandUDP && (requestAddons.Flow == vless.XRV || (h.cone && request.Port != 53 && request.Port != 443)) { request.Command = protocol.RequestCommandMux request.Address = net.DomainAddress("v1.mux.cool") @@ -260,16 +260,9 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte } // default: serverReader := buf.NewReader(conn) - serverReader := encoding.DecodeBodyAddons(conn, request, responseAddons) - if requestAddons.Flow == vless.XRV { - serverReader = proxy.NewVisionReader(serverReader, trafficState, ctx) - } + serverReader := encoding.DecodeBodyAddons(conn, request, responseAddons, trafficState, ctx) if request.Command == protocol.RequestCommandMux && request.Port == 666 { - if requestAddons.Flow == vless.XRV { - serverReader = xudp.NewPacketReader(&buf.BufferedReader{Reader: serverReader}) - } else { - serverReader = xudp.NewPacketReader(conn) - } + serverReader = xudp.NewPacketReader(&buf.BufferedReader{Reader: serverReader}) } if requestAddons.Flow == vless.XRV { From 7cc102619da52095ccf84eb0bd0cc073deec5516 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sun, 21 Apr 2024 15:23:47 -0400 Subject: [PATCH 05/11] Add seed padding logic - Seed is decoupled with XTLS Vision, which means Seed can turn on without flow - XTLS Vision now use Seed config to configure its padding only mode --- proxy/addons.pb.go | 512 ++++++++++++++++++++++++ proxy/{vless/encoding => }/addons.proto | 10 +- proxy/proxy.go | 90 ++++- proxy/vless/encoding/addons.go | 40 +- proxy/vless/encoding/encoding.go | 8 +- proxy/vless/encoding/encoding_test.go | 11 +- proxy/vless/inbound/inbound.go | 8 +- proxy/vless/outbound/outbound.go | 2 +- 8 files changed, 631 insertions(+), 50 deletions(-) create mode 100644 proxy/addons.pb.go rename proxy/{vless/encoding => }/addons.proto (72%) diff --git a/proxy/addons.pb.go b/proxy/addons.pb.go new file mode 100644 index 000000000000..e55bba6a11d2 --- /dev/null +++ b/proxy/addons.pb.go @@ -0,0 +1,512 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.33.0 +// protoc v4.23.1 +// source: proxy/addons.proto + +package proxy + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type SeedMode int32 + +const ( + SeedMode_Unknown SeedMode = 0 + SeedMode_PaddingOnly SeedMode = 1 + SeedMode_PaddingPlusDelay SeedMode = 2 + SeedMode_IndependentScheduler SeedMode = 3 +) + +// Enum value maps for SeedMode. +var ( + SeedMode_name = map[int32]string{ + 0: "Unknown", + 1: "PaddingOnly", + 2: "PaddingPlusDelay", + 3: "IndependentScheduler", + } + SeedMode_value = map[string]int32{ + "Unknown": 0, + "PaddingOnly": 1, + "PaddingPlusDelay": 2, + "IndependentScheduler": 3, + } +) + +func (x SeedMode) Enum() *SeedMode { + p := new(SeedMode) + *p = x + return p +} + +func (x SeedMode) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (SeedMode) Descriptor() protoreflect.EnumDescriptor { + return file_proxy_addons_proto_enumTypes[0].Descriptor() +} + +func (SeedMode) Type() protoreflect.EnumType { + return &file_proxy_addons_proto_enumTypes[0] +} + +func (x SeedMode) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use SeedMode.Descriptor instead. +func (SeedMode) EnumDescriptor() ([]byte, []int) { + return file_proxy_addons_proto_rawDescGZIP(), []int{0} +} + +type Addons struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Flow string `protobuf:"bytes,1,opt,name=Flow,proto3" json:"Flow,omitempty"` + Seed []byte `protobuf:"bytes,2,opt,name=Seed,proto3" json:"Seed,omitempty"` + Mode SeedMode `protobuf:"varint,3,opt,name=Mode,proto3,enum=xray.proxy.SeedMode" json:"Mode,omitempty"` + Duration string `protobuf:"bytes,4,opt,name=Duration,proto3" json:"Duration,omitempty"` // "0-8" means apply to number of packets, "1000b-" means start applying once both side exchange 1kb data, counting two-ways + Padding *PaddingConfig `protobuf:"bytes,5,opt,name=Padding,proto3" json:"Padding,omitempty"` + Delay *DelayConfig `protobuf:"bytes,6,opt,name=Delay,proto3" json:"Delay,omitempty"` + Scheduler *SchedulerConfig `protobuf:"bytes,7,opt,name=Scheduler,proto3" json:"Scheduler,omitempty"` +} + +func (x *Addons) Reset() { + *x = Addons{} + if protoimpl.UnsafeEnabled { + mi := &file_proxy_addons_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Addons) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Addons) ProtoMessage() {} + +func (x *Addons) ProtoReflect() protoreflect.Message { + mi := &file_proxy_addons_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Addons.ProtoReflect.Descriptor instead. +func (*Addons) Descriptor() ([]byte, []int) { + return file_proxy_addons_proto_rawDescGZIP(), []int{0} +} + +func (x *Addons) GetFlow() string { + if x != nil { + return x.Flow + } + return "" +} + +func (x *Addons) GetSeed() []byte { + if x != nil { + return x.Seed + } + return nil +} + +func (x *Addons) GetMode() SeedMode { + if x != nil { + return x.Mode + } + return SeedMode_Unknown +} + +func (x *Addons) GetDuration() string { + if x != nil { + return x.Duration + } + return "" +} + +func (x *Addons) GetPadding() *PaddingConfig { + if x != nil { + return x.Padding + } + return nil +} + +func (x *Addons) GetDelay() *DelayConfig { + if x != nil { + return x.Delay + } + return nil +} + +func (x *Addons) GetScheduler() *SchedulerConfig { + if x != nil { + return x.Scheduler + } + return nil +} + +type PaddingConfig struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + RegularMin uint32 `protobuf:"varint,1,opt,name=RegularMin,proto3" json:"RegularMin,omitempty"` + RegularMax uint32 `protobuf:"varint,2,opt,name=RegularMax,proto3" json:"RegularMax,omitempty"` + LongMin uint32 `protobuf:"varint,3,opt,name=LongMin,proto3" json:"LongMin,omitempty"` + LongMax uint32 `protobuf:"varint,4,opt,name=LongMax,proto3" json:"LongMax,omitempty"` +} + +func (x *PaddingConfig) Reset() { + *x = PaddingConfig{} + if protoimpl.UnsafeEnabled { + mi := &file_proxy_addons_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PaddingConfig) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PaddingConfig) ProtoMessage() {} + +func (x *PaddingConfig) ProtoReflect() protoreflect.Message { + mi := &file_proxy_addons_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PaddingConfig.ProtoReflect.Descriptor instead. +func (*PaddingConfig) Descriptor() ([]byte, []int) { + return file_proxy_addons_proto_rawDescGZIP(), []int{1} +} + +func (x *PaddingConfig) GetRegularMin() uint32 { + if x != nil { + return x.RegularMin + } + return 0 +} + +func (x *PaddingConfig) GetRegularMax() uint32 { + if x != nil { + return x.RegularMax + } + return 0 +} + +func (x *PaddingConfig) GetLongMin() uint32 { + if x != nil { + return x.LongMin + } + return 0 +} + +func (x *PaddingConfig) GetLongMax() uint32 { + if x != nil { + return x.LongMax + } + return 0 +} + +type DelayConfig struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + IsRandom bool `protobuf:"varint,1,opt,name=IsRandom,proto3" json:"IsRandom,omitempty"` + MinMillis uint32 `protobuf:"varint,2,opt,name=MinMillis,proto3" json:"MinMillis,omitempty"` + MaxMillis uint32 `protobuf:"varint,3,opt,name=MaxMillis,proto3" json:"MaxMillis,omitempty"` +} + +func (x *DelayConfig) Reset() { + *x = DelayConfig{} + if protoimpl.UnsafeEnabled { + mi := &file_proxy_addons_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DelayConfig) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DelayConfig) ProtoMessage() {} + +func (x *DelayConfig) ProtoReflect() protoreflect.Message { + mi := &file_proxy_addons_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DelayConfig.ProtoReflect.Descriptor instead. +func (*DelayConfig) Descriptor() ([]byte, []int) { + return file_proxy_addons_proto_rawDescGZIP(), []int{2} +} + +func (x *DelayConfig) GetIsRandom() bool { + if x != nil { + return x.IsRandom + } + return false +} + +func (x *DelayConfig) GetMinMillis() uint32 { + if x != nil { + return x.MinMillis + } + return 0 +} + +func (x *DelayConfig) GetMaxMillis() uint32 { + if x != nil { + return x.MaxMillis + } + return 0 +} + +type SchedulerConfig struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + TimeoutMillis uint32 `protobuf:"varint,1,opt,name=TimeoutMillis,proto3" json:"TimeoutMillis,omitempty"` // original traffic will not be sent right away but when scheduler want to send or pending buffer times out +} + +func (x *SchedulerConfig) Reset() { + *x = SchedulerConfig{} + if protoimpl.UnsafeEnabled { + mi := &file_proxy_addons_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SchedulerConfig) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SchedulerConfig) ProtoMessage() {} + +func (x *SchedulerConfig) ProtoReflect() protoreflect.Message { + mi := &file_proxy_addons_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SchedulerConfig.ProtoReflect.Descriptor instead. +func (*SchedulerConfig) Descriptor() ([]byte, []int) { + return file_proxy_addons_proto_rawDescGZIP(), []int{3} +} + +func (x *SchedulerConfig) GetTimeoutMillis() uint32 { + if x != nil { + return x.TimeoutMillis + } + return 0 +} + +var File_proxy_addons_proto protoreflect.FileDescriptor + +var file_proxy_addons_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x61, 0x64, 0x64, 0x6f, 0x6e, 0x73, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, + 0x22, 0x95, 0x02, 0x0a, 0x06, 0x41, 0x64, 0x64, 0x6f, 0x6e, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x46, + 0x6c, 0x6f, 0x77, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x46, 0x6c, 0x6f, 0x77, 0x12, + 0x12, 0x0a, 0x04, 0x53, 0x65, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x53, + 0x65, 0x65, 0x64, 0x12, 0x28, 0x0a, 0x04, 0x4d, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x0e, 0x32, 0x14, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x53, + 0x65, 0x65, 0x64, 0x4d, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x1a, 0x0a, + 0x08, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x08, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x33, 0x0a, 0x07, 0x50, 0x61, 0x64, + 0x64, 0x69, 0x6e, 0x67, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x78, 0x72, 0x61, + 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x43, + 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x07, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x12, 0x2d, + 0x0a, 0x05, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, + 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x44, 0x65, 0x6c, 0x61, 0x79, + 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x05, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x12, 0x39, 0x0a, + 0x09, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x1b, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x53, 0x63, + 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x09, 0x53, + 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x22, 0x83, 0x01, 0x0a, 0x0d, 0x50, 0x61, 0x64, + 0x64, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, + 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x69, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, + 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x69, 0x6e, 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, + 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x61, 0x78, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, + 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x61, 0x78, 0x12, 0x18, 0x0a, 0x07, 0x4c, 0x6f, + 0x6e, 0x67, 0x4d, 0x69, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x4c, 0x6f, 0x6e, + 0x67, 0x4d, 0x69, 0x6e, 0x12, 0x18, 0x0a, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x61, 0x78, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x4c, 0x6f, 0x6e, 0x67, 0x4d, 0x61, 0x78, 0x22, 0x65, + 0x0a, 0x0b, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x1a, 0x0a, + 0x08, 0x49, 0x73, 0x52, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, + 0x08, 0x49, 0x73, 0x52, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x12, 0x1c, 0x0a, 0x09, 0x4d, 0x69, 0x6e, + 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x4d, 0x69, + 0x6e, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x4d, 0x61, 0x78, 0x4d, 0x69, + 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x4d, 0x61, 0x78, 0x4d, + 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x22, 0x37, 0x0a, 0x0f, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, + 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x24, 0x0a, 0x0d, 0x54, 0x69, 0x6d, 0x65, + 0x6f, 0x75, 0x74, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, + 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x2a, 0x58, + 0x0a, 0x08, 0x53, 0x65, 0x65, 0x64, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x6e, + 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x50, 0x61, 0x64, 0x64, 0x69, + 0x6e, 0x67, 0x4f, 0x6e, 0x6c, 0x79, 0x10, 0x01, 0x12, 0x14, 0x0a, 0x10, 0x50, 0x61, 0x64, 0x64, + 0x69, 0x6e, 0x67, 0x50, 0x6c, 0x75, 0x73, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x10, 0x02, 0x12, 0x18, + 0x0a, 0x14, 0x49, 0x6e, 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x74, 0x53, 0x63, 0x68, + 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x10, 0x03, 0x42, 0x40, 0x0a, 0x0e, 0x63, 0x6f, 0x6d, 0x2e, + 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x50, 0x01, 0x5a, 0x1f, 0x67, 0x69, + 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, + 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0xaa, 0x02, 0x0a, + 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_proxy_addons_proto_rawDescOnce sync.Once + file_proxy_addons_proto_rawDescData = file_proxy_addons_proto_rawDesc +) + +func file_proxy_addons_proto_rawDescGZIP() []byte { + file_proxy_addons_proto_rawDescOnce.Do(func() { + file_proxy_addons_proto_rawDescData = protoimpl.X.CompressGZIP(file_proxy_addons_proto_rawDescData) + }) + return file_proxy_addons_proto_rawDescData +} + +var file_proxy_addons_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_proxy_addons_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_proxy_addons_proto_goTypes = []interface{}{ + (SeedMode)(0), // 0: xray.proxy.SeedMode + (*Addons)(nil), // 1: xray.proxy.Addons + (*PaddingConfig)(nil), // 2: xray.proxy.PaddingConfig + (*DelayConfig)(nil), // 3: xray.proxy.DelayConfig + (*SchedulerConfig)(nil), // 4: xray.proxy.SchedulerConfig +} +var file_proxy_addons_proto_depIdxs = []int32{ + 0, // 0: xray.proxy.Addons.Mode:type_name -> xray.proxy.SeedMode + 2, // 1: xray.proxy.Addons.Padding:type_name -> xray.proxy.PaddingConfig + 3, // 2: xray.proxy.Addons.Delay:type_name -> xray.proxy.DelayConfig + 4, // 3: xray.proxy.Addons.Scheduler:type_name -> xray.proxy.SchedulerConfig + 4, // [4:4] is the sub-list for method output_type + 4, // [4:4] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name +} + +func init() { file_proxy_addons_proto_init() } +func file_proxy_addons_proto_init() { + if File_proxy_addons_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proxy_addons_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Addons); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proxy_addons_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PaddingConfig); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proxy_addons_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DelayConfig); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proxy_addons_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SchedulerConfig); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proxy_addons_proto_rawDesc, + NumEnums: 1, + NumMessages: 4, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_proxy_addons_proto_goTypes, + DependencyIndexes: file_proxy_addons_proto_depIdxs, + EnumInfos: file_proxy_addons_proto_enumTypes, + MessageInfos: file_proxy_addons_proto_msgTypes, + }.Build() + File_proxy_addons_proto = out.File + file_proxy_addons_proto_rawDesc = nil + file_proxy_addons_proto_goTypes = nil + file_proxy_addons_proto_depIdxs = nil +} diff --git a/proxy/vless/encoding/addons.proto b/proxy/addons.proto similarity index 72% rename from proxy/vless/encoding/addons.proto rename to proxy/addons.proto index 4820ef50449c..1542299e5982 100644 --- a/proxy/vless/encoding/addons.proto +++ b/proxy/addons.proto @@ -1,16 +1,16 @@ syntax = "proto3"; -package xray.proxy.vless.encoding; -option csharp_namespace = "Xray.Proxy.Vless.Encoding"; -option go_package = "github.com/xtls/xray-core/proxy/vless/encoding"; -option java_package = "com.xray.proxy.vless.encoding"; +package xray.proxy; +option csharp_namespace = "Xray.Proxy"; +option go_package = "github.com/xtls/xray-core/proxy"; +option java_package = "com.xray.proxy"; option java_multiple_files = true; message Addons { string Flow = 1; bytes Seed = 2; SeedMode Mode = 3; - string Duration = 4; // "0-8" means apply to number of packets, "1kb-" means start applying once both side exchange 1kb data, counting two-ways + string Duration = 4; // "0-8" means apply to number of packets, "1000b-" means start applying once both side exchange 1kb data, counting two-ways PaddingConfig Padding = 5; DelayConfig Delay = 6; SchedulerConfig Scheduler = 7; diff --git a/proxy/proxy.go b/proxy/proxy.go index f0030e014d00..cec7e7e74b96 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -13,6 +13,7 @@ import ( "math/big" "runtime" "strconv" + "strings" "time" "github.com/pires/go-proxyproto" @@ -137,9 +138,9 @@ func NewTrafficState(userUUID []byte, flow string) *TrafficState { RemainingPadding: -1, CurrentCommand: 0, WriterSwitchToDirectCopy: false, + IsPadding: true, }; if len(flow) > 0 { - state.IsPadding = true; state.WithinPaddingBuffers = true; } return &state @@ -149,13 +150,15 @@ func NewTrafficState(userUUID []byte, flow string) *TrafficState { // Note Vision probably only make sense as the inner most layer of reader, since it need assess traffic state from origin proxy traffic type VisionReader struct { buf.Reader + addons *Addons trafficState *TrafficState ctx context.Context } -func NewVisionReader(reader buf.Reader, state *TrafficState, context context.Context) *VisionReader { +func NewVisionReader(reader buf.Reader, addon *Addons, state *TrafficState, context context.Context) *VisionReader { return &VisionReader{ Reader: reader, + addons: addon, trafficState: state, ctx: context, } @@ -168,7 +171,7 @@ func (w *VisionReader) ReadMultiBuffer() (buf.MultiBuffer, error) { w.trafficState.StartTime = time.Now() } w.trafficState.ByteReceived += int64(buffer.Len()) - if w.trafficState.WithinPaddingBuffers || w.trafficState.NumberOfPacketToFilter > 0 { + if w.trafficState.WithinPaddingBuffers || !ShouldStopSeed(w.addons, w.trafficState) { mb2 := make(buf.MultiBuffer, 0, len(buffer)) for _, b := range buffer { newbuffer := XtlsUnpadding(b, w.trafficState, w.ctx) @@ -200,16 +203,18 @@ func (w *VisionReader) ReadMultiBuffer() (buf.MultiBuffer, error) { // Note Vision probably only make sense as the inner most layer of writer, since it need assess traffic state from origin proxy traffic type VisionWriter struct { buf.Writer + addons *Addons trafficState *TrafficState ctx context.Context writeOnceUserUUID []byte } -func NewVisionWriter(writer buf.Writer, state *TrafficState, context context.Context) *VisionWriter { +func NewVisionWriter(writer buf.Writer, addon *Addons, state *TrafficState, context context.Context) *VisionWriter { w := make([]byte, len(state.UserUUID)) copy(w, state.UserUUID) return &VisionWriter{ Writer: writer, + addons: addon, trafficState: state, ctx: context, writeOnceUserUUID: w, @@ -221,9 +226,9 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { if w.trafficState.NumberOfPacketToFilter > 0 { XtlsFilterTls(mb, w.trafficState, w.ctx) } - if w.trafficState.IsPadding { + if w.trafficState.IsPadding && ShouldStartSeed(w.addons, w.trafficState){ if len(mb) == 1 && mb[0] == nil { - mb[0] = XtlsPadding(nil, CommandPaddingContinue, &w.writeOnceUserUUID, true, w.ctx) // we do a long padding to hide vless header + mb[0] = XtlsPadding(nil, CommandPaddingContinue, &w.writeOnceUserUUID, true, w.addons, w.ctx) // we do a long padding to hide vless header } else { mb = ReshapeMultiBuffer(w.ctx, mb) longPadding := w.trafficState.IsTLS @@ -234,18 +239,20 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { } var command byte = CommandPaddingContinue if i == len(mb) - 1 { - command = CommandPaddingEnd if w.trafficState.EnableXtls { command = CommandPaddingDirect + w.trafficState.IsPadding = false + } else if ShouldStopSeed(w.addons, w.trafficState) { + command = CommandPaddingEnd + w.trafficState.IsPadding = false } } - mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, true, w.ctx) - w.trafficState.IsPadding = false // padding going to end + mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, true, w.addons, w.ctx) longPadding = false continue - } else if !w.trafficState.IsTLS12orAbove && w.trafficState.NumberOfPacketToFilter <= 1 { // For compatibility with earlier vision receiver, we finish padding 1 packet early + } else if !w.trafficState.IsTLS12orAbove && ShouldStopSeed(w.addons, w.trafficState) { w.trafficState.IsPadding = false - mb[i] = XtlsPadding(b, CommandPaddingEnd, &w.writeOnceUserUUID, longPadding, w.ctx) + mb[i] = XtlsPadding(b, CommandPaddingEnd, &w.writeOnceUserUUID, longPadding, w.addons, w.ctx) break } var command byte = CommandPaddingContinue @@ -255,7 +262,7 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { command = CommandPaddingDirect } } - mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, longPadding, w.ctx) + mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, longPadding, w.addons, w.ctx) } } } @@ -302,24 +309,24 @@ func ReshapeMultiBuffer(ctx context.Context, buffer buf.MultiBuffer) buf.MultiBu } // XtlsPadding add padding to eliminate length signature during tls handshake -func XtlsPadding(b *buf.Buffer, command byte, userUUID *[]byte, longPadding bool, ctx context.Context) *buf.Buffer { +func XtlsPadding(b *buf.Buffer, command byte, userUUID *[]byte, longPadding bool, addons *Addons, ctx context.Context) *buf.Buffer { var contentLen int32 = 0 var paddingLen int32 = 0 if b != nil { contentLen = b.Len() } - if contentLen < 900 && longPadding { - l, err := rand.Int(rand.Reader, big.NewInt(500)) + if contentLen < int32(addons.Padding.LongMin) && longPadding { + l, err := rand.Int(rand.Reader, big.NewInt(int64(addons.Padding.LongMax - addons.Padding.LongMin))) if err != nil { errors.LogDebugInner(ctx, err, "failed to generate padding") } - paddingLen = int32(l.Int64()) + 900 - contentLen + paddingLen = int32(l.Int64()) + int32(addons.Padding.LongMin) - contentLen } else { - l, err := rand.Int(rand.Reader, big.NewInt(256)) + l, err := rand.Int(rand.Reader, big.NewInt(int64(addons.Padding.RegularMax - addons.Padding.RegularMin))) if err != nil { errors.LogDebugInner(ctx, err, "failed to generate padding") } - paddingLen = int32(l.Int64()) + paddingLen = int32(l.Int64()) + int32(addons.Padding.RegularMin) } if paddingLen > buf.Size-21-contentLen { paddingLen = buf.Size - 21 - contentLen @@ -574,3 +581,50 @@ func readV(ctx context.Context, reader buf.Reader, writer buf.Writer, timer sign } return nil } + +func ShouldStartSeed(addons *Addons, trafficState *TrafficState) bool { + if len(addons.Duration) == 0 || len(strings.Split(addons.Duration, "-")) < 2 { + return false + } + start := strings.ToLower(strings.Split(addons.Duration, "-")[0]) + if len(start) == 0 { + return true + } + if strings.Contains(start, "b") { + start = strings.TrimRight(start, "b") + i, err := strconv.Atoi(start) + if err == nil && i <= int(trafficState.ByteSent + trafficState.ByteSent) { + return true + } + } else { + i, err := strconv.Atoi(start) + if err == nil && i <= trafficState.NumberOfPacketSent + trafficState.NumberOfPacketReceived { + return true + } + } + return false +} + +func ShouldStopSeed(addons *Addons, trafficState *TrafficState) bool { + if len(addons.Duration) == 0 || len(strings.Split(addons.Duration, "-")) < 2 { + return true + } + start := strings.ToLower(strings.Split(addons.Duration, "-")[1]) + if len(start) == 0 { // infinite + return false + } + if strings.Contains(start, "b") { + start = strings.TrimRight(start, "b") + i, err := strconv.Atoi(start) + if err == nil && i > int(trafficState.ByteSent + trafficState.ByteSent) { + return false + } + } else { + i, err := strconv.Atoi(start) + if err == nil && i > trafficState.NumberOfPacketSent + trafficState.NumberOfPacketReceived { + return false + } + } + return true +} + diff --git a/proxy/vless/encoding/addons.go b/proxy/vless/encoding/addons.go index 526b99959534..403ffe299341 100644 --- a/proxy/vless/encoding/addons.go +++ b/proxy/vless/encoding/addons.go @@ -13,7 +13,7 @@ import ( "google.golang.org/protobuf/proto" ) -func EncodeHeaderAddons(buffer *buf.Buffer, addons *Addons) error { +func EncodeHeaderAddons(buffer *buf.Buffer, addons *proxy.Addons) error { if addons.Flow == vless.XRV || len(addons.Seed) > 0 { bytes, err := proto.Marshal(addons) if err != nil { @@ -33,8 +33,8 @@ func EncodeHeaderAddons(buffer *buf.Buffer, addons *Addons) error { return nil } -func DecodeHeaderAddons(buffer *buf.Buffer, reader io.Reader) (*Addons, error) { - addons := new(Addons) +func DecodeHeaderAddons(buffer *buf.Buffer, reader io.Reader) (*proxy.Addons, error) { + addons := new(proxy.Addons) buffer.Clear() if _, err := buffer.ReadFullFrom(reader, 1); err != nil { return nil, errors.New("failed to read addons protobuf length").Base(err) @@ -55,8 +55,8 @@ func DecodeHeaderAddons(buffer *buf.Buffer, reader io.Reader) (*Addons, error) { } // EncodeBodyAddons returns a Writer that auto-encrypt content written by caller. -func EncodeBodyAddons(writer buf.Writer, request *protocol.RequestHeader, requestAddons *Addons, state *proxy.TrafficState, context context.Context) buf.Writer { - w := proxy.NewVisionWriter(writer, state, context) +func EncodeBodyAddons(writer buf.Writer, request *protocol.RequestHeader, addons *proxy.Addons, state *proxy.TrafficState, context context.Context) buf.Writer { + w := proxy.NewVisionWriter(writer, addons, state, context) if request.Command == protocol.RequestCommandUDP { return NewMultiLengthPacketWriter(w) } @@ -64,8 +64,8 @@ func EncodeBodyAddons(writer buf.Writer, request *protocol.RequestHeader, reques } // DecodeBodyAddons returns a Reader from which caller can fetch decrypted body. -func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *Addons, state *proxy.TrafficState, context context.Context) buf.Reader { - r := proxy.NewVisionReader(buf.NewReader(reader), state, context) +func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *proxy.Addons, state *proxy.TrafficState, context context.Context) buf.Reader { + r := proxy.NewVisionReader(buf.NewReader(reader), addons, state, context) if request.Command == protocol.RequestCommandUDP { return NewLengthPacketReader(&buf.BufferedReader{Reader: r}) } @@ -178,32 +178,46 @@ func (r *LengthPacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) { return mb, nil } -func PopulateSeed(seed string, addons *Addons) { +func PopulateSeed(seed string, addons *proxy.Addons) { if len(seed) > 0 { addons.Seed = []byte {1} // only turn on, more TBD - addons.Mode = SeedMode_PaddingPlusDelay + addons.Mode = proxy.SeedMode_PaddingPlusDelay addons.Duration = "0-8" - addons.Padding = &PaddingConfig{ + addons.Padding = &proxy.PaddingConfig{ RegularMin: 0, RegularMax: 256, LongMin: 900, LongMax: 1400, } - addons.Delay = &DelayConfig{ + addons.Delay = &proxy.DelayConfig{ IsRandom: true, MinMillis: 100, MaxMillis: 500, } - addons.Scheduler = &SchedulerConfig{ + addons.Scheduler = &proxy.SchedulerConfig{ TimeoutMillis: 600, } + } else if addons.Flow == vless.XRV { + addons.Seed = []byte {1} // only turn on, more TBD + addons.Mode = proxy.SeedMode_PaddingOnly + addons.Duration = "0-8" + addons.Padding = &proxy.PaddingConfig{ + RegularMin: 0, + RegularMax: 256, + LongMin: 900, + LongMax: 1400, + } } } -func CheckSeed(requestAddons *Addons, responseAddons *Addons) error { +func CheckSeed(requestAddons *proxy.Addons, responseAddons *proxy.Addons) error { if !bytes.Equal(requestAddons.Seed, responseAddons.Seed) { return errors.New("Seed bytes not match", requestAddons.Seed, responseAddons.Seed) } + if responseAddons.Flow == vless.XRV && len(responseAddons.Seed) == 0 && requestAddons.Mode == proxy.SeedMode_Unknown { + // old vision server config allow empty seed from clients for backwards compatibility + return nil + } if requestAddons.Mode != responseAddons.Mode { return errors.New("Mode not match", requestAddons.Mode, responseAddons.Mode) } diff --git a/proxy/vless/encoding/encoding.go b/proxy/vless/encoding/encoding.go index 8b067a965321..3e4d8009005f 100644 --- a/proxy/vless/encoding/encoding.go +++ b/proxy/vless/encoding/encoding.go @@ -28,7 +28,7 @@ var addrParser = protocol.NewAddressParser( ) // EncodeRequestHeader writes encoded request header into the given writer. -func EncodeRequestHeader(writer io.Writer, request *protocol.RequestHeader, requestAddons *Addons) error { +func EncodeRequestHeader(writer io.Writer, request *protocol.RequestHeader, requestAddons *proxy.Addons) error { buffer := buf.StackNew() defer buffer.Release() @@ -62,7 +62,7 @@ func EncodeRequestHeader(writer io.Writer, request *protocol.RequestHeader, requ } // DecodeRequestHeader decodes and returns (if successful) a RequestHeader from an input stream. -func DecodeRequestHeader(isfb bool, first *buf.Buffer, reader io.Reader, validator vless.Validator) (*protocol.RequestHeader, *Addons, bool, error) { +func DecodeRequestHeader(isfb bool, first *buf.Buffer, reader io.Reader, validator vless.Validator) (*protocol.RequestHeader, *proxy.Addons, bool, error) { buffer := buf.StackNew() defer buffer.Release() @@ -131,7 +131,7 @@ func DecodeRequestHeader(isfb bool, first *buf.Buffer, reader io.Reader, validat } // EncodeResponseHeader writes encoded response header into the given writer. -func EncodeResponseHeader(writer io.Writer, request *protocol.RequestHeader, responseAddons *Addons) error { +func EncodeResponseHeader(writer io.Writer, request *protocol.RequestHeader, responseAddons *proxy.Addons) error { buffer := buf.StackNew() defer buffer.Release() @@ -151,7 +151,7 @@ func EncodeResponseHeader(writer io.Writer, request *protocol.RequestHeader, res } // DecodeResponseHeader decodes and returns (if successful) a ResponseHeader from an input stream. -func DecodeResponseHeader(reader io.Reader, request *protocol.RequestHeader) (*Addons, error) { +func DecodeResponseHeader(reader io.Reader, request *protocol.RequestHeader) (*proxy.Addons, error) { buffer := buf.StackNew() defer buffer.Release() diff --git a/proxy/vless/encoding/encoding_test.go b/proxy/vless/encoding/encoding_test.go index 9180154a04a8..f832ec8e8261 100644 --- a/proxy/vless/encoding/encoding_test.go +++ b/proxy/vless/encoding/encoding_test.go @@ -9,6 +9,7 @@ import ( "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/uuid" + "github.com/xtls/xray-core/proxy" "github.com/xtls/xray-core/proxy/vless" . "github.com/xtls/xray-core/proxy/vless/encoding" ) @@ -37,7 +38,7 @@ func TestRequestSerialization(t *testing.T) { Address: net.DomainAddress("www.example.com"), Port: net.Port(443), } - expectedAddons := &Addons{} + expectedAddons := &proxy.Addons{} buffer := buf.StackNew() common.Must(EncodeRequestHeader(&buffer, expectedRequest, expectedAddons)) @@ -52,7 +53,7 @@ func TestRequestSerialization(t *testing.T) { t.Error(r) } - addonsComparer := func(x, y *Addons) bool { + addonsComparer := func(x, y *proxy.Addons) bool { return (x.Flow == y.Flow) && (cmp.Equal(x.Seed, y.Seed)) } if r := cmp.Diff(actualAddons, expectedAddons, cmp.Comparer(addonsComparer)); r != "" { @@ -78,7 +79,7 @@ func TestInvalidRequest(t *testing.T) { Address: net.DomainAddress("www.example.com"), Port: net.Port(443), } - expectedAddons := &Addons{} + expectedAddons := &proxy.Addons{} buffer := buf.StackNew() common.Must(EncodeRequestHeader(&buffer, expectedRequest, expectedAddons)) @@ -109,7 +110,7 @@ func TestMuxRequest(t *testing.T) { Command: protocol.RequestCommandMux, Address: net.DomainAddress("v1.mux.cool"), } - expectedAddons := &Addons{} + expectedAddons := &proxy.Addons{} buffer := buf.StackNew() common.Must(EncodeRequestHeader(&buffer, expectedRequest, expectedAddons)) @@ -124,7 +125,7 @@ func TestMuxRequest(t *testing.T) { t.Error(r) } - addonsComparer := func(x, y *Addons) bool { + addonsComparer := func(x, y *proxy.Addons) bool { return (x.Flow == y.Flow) && (cmp.Equal(x.Seed, y.Seed)) } if r := cmp.Diff(actualAddons, expectedAddons, cmp.Comparer(addonsComparer)); r != "" { diff --git a/proxy/vless/inbound/inbound.go b/proxy/vless/inbound/inbound.go index 7ea04f964fb7..508f1f20f41a 100644 --- a/proxy/vless/inbound/inbound.go +++ b/proxy/vless/inbound/inbound.go @@ -200,7 +200,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s } var request *protocol.RequestHeader - var requestAddons *encoding.Addons + var requestAddons *proxy.Addons var err error napfb := h.fallbacks @@ -440,7 +440,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s account := request.User.Account.(*vless.MemoryAccount) - responseAddons := &encoding.Addons{ + responseAddons := &proxy.Addons{ // Flow: requestAddons.Flow, } encoding.PopulateSeed(account.Seed, responseAddons) @@ -521,7 +521,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) // default: clientReader := reader - clientReader := encoding.DecodeBodyAddons(reader, request, requestAddons, trafficState, ctx) + clientReader := encoding.DecodeBodyAddons(reader, request, responseAddons, trafficState, ctx) var err error @@ -549,7 +549,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s } // default: clientWriter := bufferWriter - clientWriter := encoding.EncodeBodyAddons(bufferWriter, request, requestAddons, trafficState, ctx) + clientWriter := encoding.EncodeBodyAddons(bufferWriter, request, responseAddons, trafficState, ctx) multiBuffer, err1 := serverReader.ReadMultiBuffer() if err1 != nil { return err1 // ... diff --git a/proxy/vless/outbound/outbound.go b/proxy/vless/outbound/outbound.go index 11f7d6225dbb..ff188836cc99 100644 --- a/proxy/vless/outbound/outbound.go +++ b/proxy/vless/outbound/outbound.go @@ -116,7 +116,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte account := request.User.Account.(*vless.MemoryAccount) - requestAddons := &encoding.Addons{ + requestAddons := &proxy.Addons{ Flow: account.Flow, } encoding.PopulateSeed(account.Seed, requestAddons) From d8374946162b101ba84f2b6d37e49f259da83c21 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sun, 21 Apr 2024 16:00:33 -0400 Subject: [PATCH 06/11] Fill vless response addon --- proxy/vless/inbound/inbound.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/vless/inbound/inbound.go b/proxy/vless/inbound/inbound.go index 508f1f20f41a..32fd8aad152e 100644 --- a/proxy/vless/inbound/inbound.go +++ b/proxy/vless/inbound/inbound.go @@ -441,7 +441,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s account := request.User.Account.(*vless.MemoryAccount) responseAddons := &proxy.Addons{ - // Flow: requestAddons.Flow, + Flow: account.Flow, } encoding.PopulateSeed(account.Seed, responseAddons) if check := encoding.CheckSeed(requestAddons, responseAddons); check != nil { From c122946808909e981a18248aa5c77fe26155a2a2 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:00:56 -0400 Subject: [PATCH 07/11] Add proxy Scheduler --- proxy/proxy.go | 15 ++++++++-- proxy/scheduler.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 proxy/scheduler.go diff --git a/proxy/proxy.go b/proxy/proxy.go index cec7e7e74b96..f30ca210b57b 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -146,7 +146,7 @@ func NewTrafficState(userUUID []byte, flow string) *TrafficState { return &state } -// VisionReader is used to read xtls vision protocol +// VisionReader is used to read seed protocol // Note Vision probably only make sense as the inner most layer of reader, since it need assess traffic state from origin proxy traffic type VisionReader struct { buf.Reader @@ -199,7 +199,7 @@ func (w *VisionReader) ReadMultiBuffer() (buf.MultiBuffer, error) { return buffer, err } -// VisionWriter is used to write xtls vision protocol +// VisionWriter is used to write seed protocol // Note Vision probably only make sense as the inner most layer of writer, since it need assess traffic state from origin proxy traffic type VisionWriter struct { buf.Writer @@ -207,6 +207,7 @@ type VisionWriter struct { trafficState *TrafficState ctx context.Context writeOnceUserUUID []byte + scheduler *Scheduler } func NewVisionWriter(writer buf.Writer, addon *Addons, state *TrafficState, context context.Context) *VisionWriter { @@ -218,6 +219,7 @@ func NewVisionWriter(writer buf.Writer, addon *Addons, state *TrafficState, cont trafficState: state, ctx: context, writeOnceUserUUID: w, + scheduler: NewScheduler(writer, addon, state, context), } } @@ -270,7 +272,14 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { if w.trafficState.StartTime.IsZero() { w.trafficState.StartTime = time.Now() } - return w.Writer.WriteMultiBuffer(mb) + w.scheduler.Buffer <- mb + if w.addons.Scheduler == nil { + w.scheduler.Trigger <- -1 // send all buffers + } + if len(w.scheduler.Error) > 0 { + return <-w.scheduler.Error + } + return nil } // ReshapeMultiBuffer prepare multi buffer for padding structure (max 21 bytes) diff --git a/proxy/scheduler.go b/proxy/scheduler.go new file mode 100644 index 000000000000..204a04db82ff --- /dev/null +++ b/proxy/scheduler.go @@ -0,0 +1,74 @@ +package proxy + +import ( + "context" + "crypto/rand" + "math/big" + "sync" + "time" + + "github.com/xtls/xray-core/common/buf" + "github.com/xtls/xray-core/common/session" +) + +type Scheduler struct { + Buffer chan buf.MultiBuffer + Trigger chan int + Error chan error + bufferReadLock *sync.Mutex + writer buf.Writer + addons *Addons + trafficState *TrafficState + ctx context.Context +} + +func NewScheduler(w buf.Writer, addon *Addons, state *TrafficState, context context.Context) *Scheduler { + var s = Scheduler{ + Buffer: make(chan buf.MultiBuffer, 100), + Trigger: make(chan int), + Error: make(chan error, 100), + bufferReadLock: new(sync.Mutex), + writer: w, + addons: addon, + trafficState: state, + ctx: context, + } + go s.mainLoop() + if s.addons.Scheduler != nil { + go s.exampleIndependentScheduler() + } + return &s +} + +func(s *Scheduler) mainLoop() { + for trigger := range s.Trigger { + go func() { // each trigger has independent delay, trigger does not block + var d = 0 * time.Millisecond + if s.addons.Delay != nil { + l, err := rand.Int(rand.Reader, big.NewInt(int64(s.addons.Delay.MaxMillis - s.addons.Delay.MinMillis))) + if err != nil { + newError("failed to generate delay", trigger).Base(err).WriteToLog(session.ExportIDToError(s.ctx)) + } + d = time.Duration(uint32(l.Int64()) + s.addons.Delay.MinMillis) + time.Sleep(d * time.Millisecond) + } + + s.bufferReadLock.Lock() // guard against multiple trigger threads + var sending = len(s.Buffer) + if sending > 0 { + newError("Scheduler Trigger for ", sending, " buffer(s) with ", d, " ", trigger).AtDebug().WriteToLog(session.ExportIDToError(s.ctx)) + } + for i := 0; i Date: Sun, 28 Apr 2024 18:37:07 -0400 Subject: [PATCH 08/11] Add VLESS seed tests --- proxy/proxy.go | 2 +- proxy/scheduler.go | 10 +- testing/scenarios/vless_test.go | 228 ++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+), 6 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index f30ca210b57b..b39c70051e76 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -171,7 +171,7 @@ func (w *VisionReader) ReadMultiBuffer() (buf.MultiBuffer, error) { w.trafficState.StartTime = time.Now() } w.trafficState.ByteReceived += int64(buffer.Len()) - if w.trafficState.WithinPaddingBuffers || !ShouldStopSeed(w.addons, w.trafficState) { + if w.trafficState.WithinPaddingBuffers || w.trafficState.NumberOfPacketReceived <= 8 || !ShouldStopSeed(w.addons, w.trafficState) { mb2 := make(buf.MultiBuffer, 0, len(buffer)) for _, b := range buffer { newbuffer := XtlsUnpadding(b, w.trafficState, w.ctx) diff --git a/proxy/scheduler.go b/proxy/scheduler.go index 204a04db82ff..70a491f81272 100644 --- a/proxy/scheduler.go +++ b/proxy/scheduler.go @@ -8,7 +8,7 @@ import ( "time" "github.com/xtls/xray-core/common/buf" - "github.com/xtls/xray-core/common/session" + "github.com/xtls/xray-core/common/errors" ) type Scheduler struct { @@ -47,16 +47,16 @@ func(s *Scheduler) mainLoop() { if s.addons.Delay != nil { l, err := rand.Int(rand.Reader, big.NewInt(int64(s.addons.Delay.MaxMillis - s.addons.Delay.MinMillis))) if err != nil { - newError("failed to generate delay", trigger).Base(err).WriteToLog(session.ExportIDToError(s.ctx)) + errors.LogInfoInner(s.ctx, err, "failed to generate delay", trigger) } - d = time.Duration(uint32(l.Int64()) + s.addons.Delay.MinMillis) - time.Sleep(d * time.Millisecond) + d = time.Duration(uint32(l.Int64()) + s.addons.Delay.MinMillis) * time.Millisecond + time.Sleep(d) } s.bufferReadLock.Lock() // guard against multiple trigger threads var sending = len(s.Buffer) if sending > 0 { - newError("Scheduler Trigger for ", sending, " buffer(s) with ", d, " ", trigger).AtDebug().WriteToLog(session.ExportIDToError(s.ctx)) + errors.LogDebug(s.ctx, "Scheduler Trigger for ", sending, " buffer(s) with ", d, " ", trigger) } for i := 0; i Date: Mon, 1 Jul 2024 21:27:24 -0400 Subject: [PATCH 09/11] Add quick logic for scheduler to send fake packet when no buffer is pending --- proxy/scheduler.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/proxy/scheduler.go b/proxy/scheduler.go index 70a491f81272..dec1f4be7e26 100644 --- a/proxy/scheduler.go +++ b/proxy/scheduler.go @@ -57,9 +57,13 @@ func(s *Scheduler) mainLoop() { var sending = len(s.Buffer) if sending > 0 { errors.LogDebug(s.ctx, "Scheduler Trigger for ", sending, " buffer(s) with ", d, " ", trigger) - } - for i := 0; i 0 { + errors.LogDebug(s.ctx, "Scheduler Trigger for fake buffer with ", d, " ", trigger) + mb := make(buf.MultiBuffer, 1) + s.Error <- s.writer.WriteMultiBuffer(mb) } s.bufferReadLock.Unlock() }() @@ -69,6 +73,6 @@ func(s *Scheduler) mainLoop() { func(s *Scheduler) exampleIndependentScheduler() { for { time.Sleep(500 * time.Millisecond) - s.Trigger <- -1 // send all buffers + s.Trigger <- 1 // send fake buffer if no pending } } From 5d252bf1cfae2e0d25668fa8b19e3f13f77c2911 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Wed, 4 Sep 2024 00:06:51 -0400 Subject: [PATCH 10/11] Experiment: seed ignore buffer and send 1st fake packet instantly --- proxy/proxy.go | 14 ++++----- proxy/scheduler.go | 55 ++++++++++++++++++++++++++-------- proxy/vless/encoding/addons.go | 10 +++---- 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index b39c70051e76..a69148bb33bd 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -206,7 +206,7 @@ type VisionWriter struct { addons *Addons trafficState *TrafficState ctx context.Context - writeOnceUserUUID []byte + writeOnceUserUUID *[]byte scheduler *Scheduler } @@ -218,8 +218,8 @@ func NewVisionWriter(writer buf.Writer, addon *Addons, state *TrafficState, cont addons: addon, trafficState: state, ctx: context, - writeOnceUserUUID: w, - scheduler: NewScheduler(writer, addon, state, context), + writeOnceUserUUID: &w, + scheduler: NewScheduler(writer, addon, state, &w, context), } } @@ -230,7 +230,7 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { } if w.trafficState.IsPadding && ShouldStartSeed(w.addons, w.trafficState){ if len(mb) == 1 && mb[0] == nil { - mb[0] = XtlsPadding(nil, CommandPaddingContinue, &w.writeOnceUserUUID, true, w.addons, w.ctx) // we do a long padding to hide vless header + mb[0] = XtlsPadding(nil, CommandPaddingContinue, w.writeOnceUserUUID, true, w.addons, w.ctx) // we do a long padding to hide vless header } else { mb = ReshapeMultiBuffer(w.ctx, mb) longPadding := w.trafficState.IsTLS @@ -249,12 +249,12 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { w.trafficState.IsPadding = false } } - mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, true, w.addons, w.ctx) + mb[i] = XtlsPadding(b, command, w.writeOnceUserUUID, true, w.addons, w.ctx) longPadding = false continue } else if !w.trafficState.IsTLS12orAbove && ShouldStopSeed(w.addons, w.trafficState) { w.trafficState.IsPadding = false - mb[i] = XtlsPadding(b, CommandPaddingEnd, &w.writeOnceUserUUID, longPadding, w.addons, w.ctx) + mb[i] = XtlsPadding(b, CommandPaddingEnd, w.writeOnceUserUUID, longPadding, w.addons, w.ctx) break } var command byte = CommandPaddingContinue @@ -264,7 +264,7 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { command = CommandPaddingDirect } } - mb[i] = XtlsPadding(b, command, &w.writeOnceUserUUID, longPadding, w.addons, w.ctx) + mb[i] = XtlsPadding(b, command, w.writeOnceUserUUID, longPadding, w.addons, w.ctx) } } } diff --git a/proxy/scheduler.go b/proxy/scheduler.go index dec1f4be7e26..24c59888d500 100644 --- a/proxy/scheduler.go +++ b/proxy/scheduler.go @@ -12,25 +12,29 @@ import ( ) type Scheduler struct { - Buffer chan buf.MultiBuffer - Trigger chan int - Error chan error - bufferReadLock *sync.Mutex - writer buf.Writer - addons *Addons - trafficState *TrafficState - ctx context.Context + Buffer chan buf.MultiBuffer + Trigger chan int + Error chan error + closed chan int + bufferReadLock *sync.Mutex + writer buf.Writer + addons *Addons + trafficState *TrafficState + writeOnceUserUUID *[]byte + ctx context.Context } -func NewScheduler(w buf.Writer, addon *Addons, state *TrafficState, context context.Context) *Scheduler { +func NewScheduler(w buf.Writer, addon *Addons, state *TrafficState, userUUID *[]byte, context context.Context) *Scheduler { var s = Scheduler{ Buffer: make(chan buf.MultiBuffer, 100), Trigger: make(chan int), Error: make(chan error, 100), + closed: make(chan int), bufferReadLock: new(sync.Mutex), writer: w, addons: addon, trafficState: state, + writeOnceUserUUID: userUUID, ctx: context, } go s.mainLoop() @@ -42,6 +46,9 @@ func NewScheduler(w buf.Writer, addon *Addons, state *TrafficState, context cont func(s *Scheduler) mainLoop() { for trigger := range s.Trigger { + if len(s.closed) > 0 { + return + } go func() { // each trigger has independent delay, trigger does not block var d = 0 * time.Millisecond if s.addons.Delay != nil { @@ -58,12 +65,31 @@ func(s *Scheduler) mainLoop() { if sending > 0 { errors.LogDebug(s.ctx, "Scheduler Trigger for ", sending, " buffer(s) with ", d, " ", trigger) for i := 0; i 0 { + } else if trigger > 0 && s.trafficState.IsPadding && ShouldStartSeed(s.addons, s.trafficState) && !ShouldStopSeed(s.addons, s.trafficState) { errors.LogDebug(s.ctx, "Scheduler Trigger for fake buffer with ", d, " ", trigger) + s.trafficState.NumberOfPacketSent += 1 mb := make(buf.MultiBuffer, 1) - s.Error <- s.writer.WriteMultiBuffer(mb) + mb[0] = XtlsPadding(nil, CommandPaddingContinue, s.writeOnceUserUUID, true, s.addons, s.ctx) + s.trafficState.ByteSent += int64(mb.Len()) + if s.trafficState.StartTime.IsZero() { + s.trafficState.StartTime = time.Now() + } + err := s.writer.WriteMultiBuffer(mb) + if err != nil { + s.Error <- err + s.closed <- 1 + return + } + if buffered, ok := s.writer.(*buf.BufferedWriter); ok { + buffered.SetBuffered(false) + } } s.bufferReadLock.Unlock() }() @@ -72,7 +98,10 @@ func(s *Scheduler) mainLoop() { func(s *Scheduler) exampleIndependentScheduler() { for { - time.Sleep(500 * time.Millisecond) + if len(s.closed) > 0 { + return + } s.Trigger <- 1 // send fake buffer if no pending + time.Sleep(500 * time.Millisecond) } } diff --git a/proxy/vless/encoding/addons.go b/proxy/vless/encoding/addons.go index 403ffe299341..feee6e87ffcc 100644 --- a/proxy/vless/encoding/addons.go +++ b/proxy/vless/encoding/addons.go @@ -189,11 +189,11 @@ func PopulateSeed(seed string, addons *proxy.Addons) { LongMin: 900, LongMax: 1400, } - addons.Delay = &proxy.DelayConfig{ - IsRandom: true, - MinMillis: 100, - MaxMillis: 500, - } + // addons.Delay = &proxy.DelayConfig{ + // IsRandom: true, + // MinMillis: 100, + // MaxMillis: 500, + // } addons.Scheduler = &proxy.SchedulerConfig{ TimeoutMillis: 600, } From f840cd16fe9bf5dfaa255da479d3d3f5415e7305 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 21 Sep 2024 22:02:08 -0400 Subject: [PATCH 11/11] Experiment: pingpong seed --- common/buf/copy.go | 29 ++-- proxy/addons.pb.go | 49 +++--- proxy/addons.proto | 1 + proxy/freedom/freedom.go | 2 +- proxy/proxy.go | 33 ++-- proxy/scheduler.go | 25 +++- proxy/vless/encoding/addons.go | 16 +- proxy/vless/encoding/encoding.go | 7 +- proxy/vless/inbound/inbound.go | 17 ++- proxy/vless/outbound/outbound.go | 24 ++- testing/scenarios/vless_test.go | 250 ++++--------------------------- 11 files changed, 155 insertions(+), 298 deletions(-) diff --git a/common/buf/copy.go b/common/buf/copy.go index 4cc3be881d88..95f09e8f58d1 100644 --- a/common/buf/copy.go +++ b/common/buf/copy.go @@ -11,8 +11,8 @@ import ( type dataHandler func(MultiBuffer) -type copyHandler struct { - onData []dataHandler +type CopyHandler struct { + OnData []dataHandler } // SizeCounter is for counting bytes copied by Copy(). @@ -21,12 +21,12 @@ type SizeCounter struct { } // CopyOption is an option for copying data. -type CopyOption func(*copyHandler) +type CopyOption func(*CopyHandler) // UpdateActivity is a CopyOption to update activity on each data copy operation. func UpdateActivity(timer signal.ActivityUpdater) CopyOption { - return func(handler *copyHandler) { - handler.onData = append(handler.onData, func(MultiBuffer) { + return func(handler *CopyHandler) { + handler.OnData = append(handler.OnData, func(MultiBuffer) { timer.Update() }) } @@ -34,8 +34,8 @@ func UpdateActivity(timer signal.ActivityUpdater) CopyOption { // CountSize is a CopyOption that sums the total size of data copied into the given SizeCounter. func CountSize(sc *SizeCounter) CopyOption { - return func(handler *copyHandler) { - handler.onData = append(handler.onData, func(b MultiBuffer) { + return func(handler *CopyHandler) { + handler.OnData = append(handler.OnData, func(b MultiBuffer) { sc.Size += int64(b.Len()) }) } @@ -43,8 +43,8 @@ func CountSize(sc *SizeCounter) CopyOption { // AddToStatCounter a CopyOption add to stat counter func AddToStatCounter(sc stats.Counter) CopyOption { - return func(handler *copyHandler) { - handler.onData = append(handler.onData, func(b MultiBuffer) { + return func(handler *CopyHandler) { + handler.OnData = append(handler.OnData, func(b MultiBuffer) { if sc != nil { sc.Add(int64(b.Len())) } @@ -88,18 +88,17 @@ func IsWriteError(err error) bool { return ok } -func copyInternal(reader Reader, writer Writer, handler *copyHandler) error { +func copyInternal(reader Reader, writer Writer, handler *CopyHandler) error { for { buffer, err := reader.ReadMultiBuffer() if !buffer.IsEmpty() { - for _, handler := range handler.onData { - handler(buffer) - } - if werr := writer.WriteMultiBuffer(buffer); werr != nil { return writeError{werr} } } + for _, handler := range handler.OnData { + handler(buffer) + } if err != nil { return readError{err} @@ -109,7 +108,7 @@ func copyInternal(reader Reader, writer Writer, handler *copyHandler) error { // Copy dumps all payload from reader to writer or stops when an error occurs. It returns nil when EOF. func Copy(reader Reader, writer Writer, options ...CopyOption) error { - var handler copyHandler + var handler CopyHandler for _, option := range options { option(&handler) } diff --git a/proxy/addons.pb.go b/proxy/addons.pb.go index e55bba6a11d2..02f804ffce23 100644 --- a/proxy/addons.pb.go +++ b/proxy/addons.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.33.0 -// protoc v4.23.1 +// protoc-gen-go v1.34.2 +// protoc v5.27.0 // source: proxy/addons.proto package proxy @@ -307,6 +307,7 @@ type SchedulerConfig struct { unknownFields protoimpl.UnknownFields TimeoutMillis uint32 `protobuf:"varint,1,opt,name=TimeoutMillis,proto3" json:"TimeoutMillis,omitempty"` // original traffic will not be sent right away but when scheduler want to send or pending buffer times out + PingPong bool `protobuf:"varint,2,opt,name=PingPong,proto3" json:"PingPong,omitempty"` // Other TBD } func (x *SchedulerConfig) Reset() { @@ -348,6 +349,13 @@ func (x *SchedulerConfig) GetTimeoutMillis() uint32 { return 0 } +func (x *SchedulerConfig) GetPingPong() bool { + if x != nil { + return x.PingPong + } + return false +} + var File_proxy_addons_proto protoreflect.FileDescriptor var file_proxy_addons_proto_rawDesc = []byte{ @@ -385,21 +393,22 @@ var file_proxy_addons_proto_rawDesc = []byte{ 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x4d, 0x69, 0x6e, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x4d, 0x61, 0x78, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x4d, 0x61, 0x78, 0x4d, - 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x22, 0x37, 0x0a, 0x0f, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, + 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x22, 0x53, 0x0a, 0x0f, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x24, 0x0a, 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, - 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x2a, 0x58, - 0x0a, 0x08, 0x53, 0x65, 0x65, 0x64, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x6e, - 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x50, 0x61, 0x64, 0x64, 0x69, - 0x6e, 0x67, 0x4f, 0x6e, 0x6c, 0x79, 0x10, 0x01, 0x12, 0x14, 0x0a, 0x10, 0x50, 0x61, 0x64, 0x64, - 0x69, 0x6e, 0x67, 0x50, 0x6c, 0x75, 0x73, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x10, 0x02, 0x12, 0x18, - 0x0a, 0x14, 0x49, 0x6e, 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x74, 0x53, 0x63, 0x68, - 0x65, 0x64, 0x75, 0x6c, 0x65, 0x72, 0x10, 0x03, 0x42, 0x40, 0x0a, 0x0e, 0x63, 0x6f, 0x6d, 0x2e, - 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x50, 0x01, 0x5a, 0x1f, 0x67, 0x69, - 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, - 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0xaa, 0x02, 0x0a, - 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, + 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x12, 0x1a, + 0x0a, 0x08, 0x50, 0x69, 0x6e, 0x67, 0x50, 0x6f, 0x6e, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, + 0x52, 0x08, 0x50, 0x69, 0x6e, 0x67, 0x50, 0x6f, 0x6e, 0x67, 0x2a, 0x58, 0x0a, 0x08, 0x53, 0x65, + 0x65, 0x64, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, + 0x6e, 0x10, 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x4f, 0x6e, + 0x6c, 0x79, 0x10, 0x01, 0x12, 0x14, 0x0a, 0x10, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x50, + 0x6c, 0x75, 0x73, 0x44, 0x65, 0x6c, 0x61, 0x79, 0x10, 0x02, 0x12, 0x18, 0x0a, 0x14, 0x49, 0x6e, + 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x74, 0x53, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, + 0x65, 0x72, 0x10, 0x03, 0x42, 0x40, 0x0a, 0x0e, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, + 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x50, 0x01, 0x5a, 0x1f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, + 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0xaa, 0x02, 0x0a, 0x58, 0x72, 0x61, 0x79, + 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -416,7 +425,7 @@ func file_proxy_addons_proto_rawDescGZIP() []byte { var file_proxy_addons_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_proxy_addons_proto_msgTypes = make([]protoimpl.MessageInfo, 4) -var file_proxy_addons_proto_goTypes = []interface{}{ +var file_proxy_addons_proto_goTypes = []any{ (SeedMode)(0), // 0: xray.proxy.SeedMode (*Addons)(nil), // 1: xray.proxy.Addons (*PaddingConfig)(nil), // 2: xray.proxy.PaddingConfig @@ -441,7 +450,7 @@ func file_proxy_addons_proto_init() { return } if !protoimpl.UnsafeEnabled { - file_proxy_addons_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + file_proxy_addons_proto_msgTypes[0].Exporter = func(v any, i int) any { switch v := v.(*Addons); i { case 0: return &v.state @@ -453,7 +462,7 @@ func file_proxy_addons_proto_init() { return nil } } - file_proxy_addons_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + file_proxy_addons_proto_msgTypes[1].Exporter = func(v any, i int) any { switch v := v.(*PaddingConfig); i { case 0: return &v.state @@ -465,7 +474,7 @@ func file_proxy_addons_proto_init() { return nil } } - file_proxy_addons_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + file_proxy_addons_proto_msgTypes[2].Exporter = func(v any, i int) any { switch v := v.(*DelayConfig); i { case 0: return &v.state @@ -477,7 +486,7 @@ func file_proxy_addons_proto_init() { return nil } } - file_proxy_addons_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + file_proxy_addons_proto_msgTypes[3].Exporter = func(v any, i int) any { switch v := v.(*SchedulerConfig); i { case 0: return &v.state diff --git a/proxy/addons.proto b/proxy/addons.proto index 1542299e5982..6f7d8b57dfc7 100644 --- a/proxy/addons.proto +++ b/proxy/addons.proto @@ -38,5 +38,6 @@ message DelayConfig { message SchedulerConfig { uint32 TimeoutMillis = 1; // original traffic will not be sent right away but when scheduler want to send or pending buffer times out + bool PingPong = 2; // Other TBD } diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index f2d68eea6a2a..0bc4d377df00 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -231,7 +231,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte inTimer = inbound.Timer } if !isTLSConn(conn) { // it would be tls conn in special use case of MITM, we need to let link handle traffic - return proxy.CopyRawConnIfExist(ctx, conn, writeConn, link.Writer, timer, inTimer) + return proxy.CopyRawConnIfExist(ctx, conn, writeConn, link.Writer, timer, inTimer, nil) } } var reader buf.Reader diff --git a/proxy/proxy.go b/proxy/proxy.go index a69148bb33bd..f8b5c7633968 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -207,7 +207,7 @@ type VisionWriter struct { trafficState *TrafficState ctx context.Context writeOnceUserUUID *[]byte - scheduler *Scheduler + Scheduler *Scheduler } func NewVisionWriter(writer buf.Writer, addon *Addons, state *TrafficState, context context.Context) *VisionWriter { @@ -219,7 +219,7 @@ func NewVisionWriter(writer buf.Writer, addon *Addons, state *TrafficState, cont trafficState: state, ctx: context, writeOnceUserUUID: &w, - scheduler: NewScheduler(writer, addon, state, &w, context), + Scheduler: NewScheduler(writer, addon, state, &w, context), } } @@ -272,12 +272,24 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { if w.trafficState.StartTime.IsZero() { w.trafficState.StartTime = time.Now() } - w.scheduler.Buffer <- mb - if w.addons.Scheduler == nil { - w.scheduler.Trigger <- -1 // send all buffers + w.Scheduler.Buffer <- mb + w.Scheduler.Trigger <- -1 // send all buffers if no independent scheduler + if w.addons.Scheduler != nil { + w.Scheduler.TimeoutLock.Lock() + w.Scheduler.TimeoutCounter++ + w.Scheduler.TimeoutLock.Unlock() + go func() { + time.Sleep(time.Duration(w.addons.Scheduler.TimeoutMillis) * time.Millisecond) + w.Scheduler.TimeoutLock.Lock() + w.Scheduler.TimeoutCounter-- + if w.Scheduler.TimeoutCounter == 0 { + w.Scheduler.Trigger <- 0 // send when the latest buffer timeout + } + w.Scheduler.TimeoutLock.Unlock() + }() } - if len(w.scheduler.Error) > 0 { - return <-w.scheduler.Error + if len(w.Scheduler.Error) > 0 { + return <-w.Scheduler.Error } return nil } @@ -509,7 +521,7 @@ func UnwrapRawConn(conn net.Conn) (net.Conn, stats.Counter, stats.Counter) { // CopyRawConnIfExist use the most efficient copy method. // - If caller don't want to turn on splice, do not pass in both reader conn and writer conn // - writer are from *transport.Link -func CopyRawConnIfExist(ctx context.Context, readerConn net.Conn, writerConn net.Conn, writer buf.Writer, timer *signal.ActivityTimer, inTimer *signal.ActivityTimer) error { +func CopyRawConnIfExist(ctx context.Context, readerConn net.Conn, writerConn net.Conn, writer buf.Writer, timer *signal.ActivityTimer, inTimer *signal.ActivityTimer, scheduler *Scheduler) error { readerConn, readCounter, _ := UnwrapRawConn(readerConn) writerConn, _, writeCounter := UnwrapRawConn(writerConn) reader := buf.NewReader(readerConn) @@ -572,10 +584,13 @@ func CopyRawConnIfExist(ctx context.Context, readerConn net.Conn, writerConn net if readCounter != nil { readCounter.Add(int64(buffer.Len())) } - timer.Update() if werr := writer.WriteMultiBuffer(buffer); werr != nil { return werr } + timer.Update() + } + if scheduler != nil { + scheduler.Trigger <- 2 } if err != nil { return err diff --git a/proxy/scheduler.go b/proxy/scheduler.go index 24c59888d500..6bc7577212f3 100644 --- a/proxy/scheduler.go +++ b/proxy/scheduler.go @@ -15,6 +15,8 @@ type Scheduler struct { Buffer chan buf.MultiBuffer Trigger chan int Error chan error + TimeoutCounter int + TimeoutLock *sync.Mutex closed chan int bufferReadLock *sync.Mutex writer buf.Writer @@ -24,11 +26,21 @@ type Scheduler struct { ctx context.Context } +func TriggerScheduler(scheduler *Scheduler) buf.CopyOption { + return func(handler *buf.CopyHandler) { + handler.OnData = append(handler.OnData, func(buf.MultiBuffer) { + scheduler.Trigger <- 2 // send fake buffer if no pending + }) + } +} + func NewScheduler(w buf.Writer, addon *Addons, state *TrafficState, userUUID *[]byte, context context.Context) *Scheduler { var s = Scheduler{ Buffer: make(chan buf.MultiBuffer, 100), Trigger: make(chan int), Error: make(chan error, 100), + TimeoutCounter: 0, + TimeoutLock: new(sync.Mutex), closed: make(chan int), bufferReadLock: new(sync.Mutex), writer: w, @@ -37,11 +49,14 @@ func NewScheduler(w buf.Writer, addon *Addons, state *TrafficState, userUUID *[] writeOnceUserUUID: userUUID, ctx: context, } + return &s +} + +func(s *Scheduler) Start() { go s.mainLoop() - if s.addons.Scheduler != nil { + if s.addons.Scheduler != nil && !s.addons.Scheduler.PingPong { go s.exampleIndependentScheduler() } - return &s } func(s *Scheduler) mainLoop() { @@ -49,6 +64,12 @@ func(s *Scheduler) mainLoop() { if len(s.closed) > 0 { return } + if trigger == -1 && s.addons.Scheduler != nil { + continue + } + if trigger == 2 && (s.addons.Scheduler == nil || !s.addons.Scheduler.PingPong) { + continue + } go func() { // each trigger has independent delay, trigger does not block var d = 0 * time.Millisecond if s.addons.Delay != nil { diff --git a/proxy/vless/encoding/addons.go b/proxy/vless/encoding/addons.go index feee6e87ffcc..fd9cb240acbd 100644 --- a/proxy/vless/encoding/addons.go +++ b/proxy/vless/encoding/addons.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "io" + "strings" "github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/errors" @@ -54,15 +55,6 @@ func DecodeHeaderAddons(buffer *buf.Buffer, reader io.Reader) (*proxy.Addons, er return addons, nil } -// EncodeBodyAddons returns a Writer that auto-encrypt content written by caller. -func EncodeBodyAddons(writer buf.Writer, request *protocol.RequestHeader, addons *proxy.Addons, state *proxy.TrafficState, context context.Context) buf.Writer { - w := proxy.NewVisionWriter(writer, addons, state, context) - if request.Command == protocol.RequestCommandUDP { - return NewMultiLengthPacketWriter(w) - } - return w -} - // DecodeBodyAddons returns a Reader from which caller can fetch decrypted body. func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *proxy.Addons, state *proxy.TrafficState, context context.Context) buf.Reader { r := proxy.NewVisionReader(buf.NewReader(reader), addons, state, context) @@ -181,7 +173,7 @@ func (r *LengthPacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) { func PopulateSeed(seed string, addons *proxy.Addons) { if len(seed) > 0 { addons.Seed = []byte {1} // only turn on, more TBD - addons.Mode = proxy.SeedMode_PaddingPlusDelay + addons.Mode = proxy.SeedMode_IndependentScheduler addons.Duration = "0-8" addons.Padding = &proxy.PaddingConfig{ RegularMin: 0, @@ -196,6 +188,7 @@ func PopulateSeed(seed string, addons *proxy.Addons) { // } addons.Scheduler = &proxy.SchedulerConfig{ TimeoutMillis: 600, + PingPong: strings.Contains(seed, "pingpong"), } } else if addons.Flow == vless.XRV { addons.Seed = []byte {1} // only turn on, more TBD @@ -244,7 +237,8 @@ func CheckSeed(requestAddons *proxy.Addons, responseAddons *proxy.Addons) error return errors.New("Delay of one is nil but the other is not nil") } if requestAddons.Scheduler != nil && responseAddons.Scheduler != nil { - if requestAddons.Scheduler.TimeoutMillis != responseAddons.Scheduler.TimeoutMillis { + if requestAddons.Scheduler.TimeoutMillis != responseAddons.Scheduler.TimeoutMillis || + requestAddons.Scheduler.PingPong != responseAddons.Scheduler.PingPong { return errors.New("Scheduler not match") } } else if requestAddons.Scheduler != nil || responseAddons.Scheduler != nil { diff --git a/proxy/vless/encoding/encoding.go b/proxy/vless/encoding/encoding.go index 3e4d8009005f..f95ba5602e66 100644 --- a/proxy/vless/encoding/encoding.go +++ b/proxy/vless/encoding/encoding.go @@ -172,7 +172,7 @@ func DecodeResponseHeader(reader io.Reader, request *protocol.RequestHeader) (*p } // XtlsRead filter and read xtls protocol -func XtlsRead(reader buf.Reader, writer buf.Writer, timer *signal.ActivityTimer, conn net.Conn, input *bytes.Reader, rawInput *bytes.Buffer, trafficState *proxy.TrafficState, ob *session.Outbound, ctx context.Context) error { +func XtlsRead(reader buf.Reader, writer buf.Writer, timer *signal.ActivityTimer, scheduler *proxy.Scheduler, conn net.Conn, input *bytes.Reader, rawInput *bytes.Buffer, trafficState *proxy.TrafficState, ob *session.Outbound, ctx context.Context) error { err := func() error { for { if trafficState.ReaderSwitchToDirectCopy { @@ -188,11 +188,10 @@ func XtlsRead(reader buf.Reader, writer buf.Writer, timer *signal.ActivityTimer, ob.CanSpliceCopy = 1 } } - return proxy.CopyRawConnIfExist(ctx, conn, writerConn, writer, timer, inTimer) + return proxy.CopyRawConnIfExist(ctx, conn, writerConn, writer, timer, inTimer, scheduler) } buffer, err := reader.ReadMultiBuffer() if !buffer.IsEmpty() { - timer.Update() if trafficState.ReaderSwitchToDirectCopy { // XTLS Vision processes struct TLS Conn's input and rawInput if inputBuffer, err := buf.ReadFrom(input); err == nil { @@ -209,7 +208,9 @@ func XtlsRead(reader buf.Reader, writer buf.Writer, timer *signal.ActivityTimer, if werr := writer.WriteMultiBuffer(buffer); werr != nil { return werr } + timer.Update() } + scheduler.Trigger <- 2 if err != nil { return err } diff --git a/proxy/vless/inbound/inbound.go b/proxy/vless/inbound/inbound.go index 32fd8aad152e..b23cc66ae58d 100644 --- a/proxy/vless/inbound/inbound.go +++ b/proxy/vless/inbound/inbound.go @@ -517,6 +517,12 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s serverReader := link.Reader // .(*pipe.Reader) serverWriter := link.Writer // .(*pipe.Writer) trafficState := proxy.NewTrafficState(account.ID.Bytes(), account.Flow) + bufferWriter := buf.NewBufferedWriter(buf.NewWriter(connection)) + var clientWriter buf.Writer + v := proxy.NewVisionWriter(bufferWriter, requestAddons, trafficState, ctx) + scheduler := v.Scheduler + clientWriter = v + postRequest := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) @@ -527,10 +533,10 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s if requestAddons.Flow == vless.XRV { ctx1 := session.ContextWithInbound(ctx, nil) // TODO enable splice - err = encoding.XtlsRead(clientReader, serverWriter, timer, connection, input, rawInput, trafficState, nil, ctx1) + err = encoding.XtlsRead(clientReader, serverWriter, timer, scheduler, connection, input, rawInput, trafficState, nil, ctx1) } else { // from clientReader.ReadMultiBuffer to serverWriter.WriteMultiBuffer - err = buf.Copy(clientReader, serverWriter, buf.UpdateActivity(timer)) + err = buf.Copy(clientReader, serverWriter, buf.UpdateActivity(timer), proxy.TriggerScheduler(scheduler)) } if err != nil { @@ -543,13 +549,14 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s getResponse := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly) - bufferWriter := buf.NewBufferedWriter(buf.NewWriter(connection)) if err := encoding.EncodeResponseHeader(bufferWriter, request, responseAddons); err != nil { return errors.New("failed to encode response header").Base(err).AtWarning() } - // default: clientWriter := bufferWriter - clientWriter := encoding.EncodeBodyAddons(bufferWriter, request, responseAddons, trafficState, ctx) + scheduler.Start() + if request.Command == protocol.RequestCommandUDP { + clientWriter = encoding.NewMultiLengthPacketWriter(clientWriter) + } multiBuffer, err1 := serverReader.ReadMultiBuffer() if err1 != nil { return err1 // ... diff --git a/proxy/vless/outbound/outbound.go b/proxy/vless/outbound/outbound.go index ff188836cc99..11706eb4176e 100644 --- a/proxy/vless/outbound/outbound.go +++ b/proxy/vless/outbound/outbound.go @@ -185,18 +185,23 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte request.Address = net.DomainAddress("v1.mux.cool") request.Port = net.Port(666) } + bufferWriter := buf.NewBufferedWriter(buf.NewWriter(conn)) + var serverWriter buf.Writer + v := proxy.NewVisionWriter(bufferWriter, requestAddons, trafficState, ctx) + scheduler := v.Scheduler + serverWriter = v postRequest := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) - - bufferWriter := buf.NewBufferedWriter(buf.NewWriter(conn)) + if err := encoding.EncodeRequestHeader(bufferWriter, request, requestAddons); err != nil { return errors.New("failed to encode request header").Base(err).AtWarning() } - // default: serverWriter := bufferWriter - serverWriter := encoding.EncodeBodyAddons(bufferWriter, request, requestAddons, trafficState, ctx) - if request.Command == protocol.RequestCommandMux && request.Port == 666 { + scheduler.Start() + if request.Command == protocol.RequestCommandUDP { + serverWriter = encoding.NewMultiLengthPacketWriter(serverWriter) + } else if request.Command == protocol.RequestCommandMux && request.Port == 666 { serverWriter = xudp.NewPacketWriter(serverWriter, target, xudp.GetGlobalID(ctx)) } timeoutReader, ok := clientReader.(buf.TimeoutReader) @@ -222,6 +227,11 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte if err := bufferWriter.SetBuffered(false); err != nil { return errors.New("failed to write A request payload").Base(err).AtWarning() } + if requestAddons.Scheduler != nil && requestAddons.Scheduler.PingPong { + go func() { + scheduler.Trigger <- 2 // client kickstart the pingpong! + }() + } var err error if requestAddons.Flow == vless.XRV { @@ -266,10 +276,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte } if requestAddons.Flow == vless.XRV { - err = encoding.XtlsRead(serverReader, clientWriter, timer, conn, input, rawInput, trafficState, ob, ctx) + err = encoding.XtlsRead(serverReader, clientWriter, timer, scheduler, conn, input, rawInput, trafficState, ob, ctx) } else { // from serverReader.ReadMultiBuffer to clientWriter.WriteMultiBuffer - err = buf.Copy(serverReader, clientWriter, buf.UpdateActivity(timer)) + err = buf.Copy(serverReader, clientWriter, buf.UpdateActivity(timer), proxy.TriggerScheduler(scheduler)) } if err != nil { diff --git a/testing/scenarios/vless_test.go b/testing/scenarios/vless_test.go index d9cb35e4c46c..8dac863ba706 100644 --- a/testing/scenarios/vless_test.go +++ b/testing/scenarios/vless_test.go @@ -30,102 +30,18 @@ import ( ) func TestVless(t *testing.T) { - tcpServer := tcp.Server{ - MsgProcessor: xor, - } - dest, err := tcpServer.Start() - common.Must(err) - defer tcpServer.Close() - - userID := protocol.NewID(uuid.New()) - serverPort := tcp.PickPort() - serverConfig := &core.Config{ - App: []*serial.TypedMessage{ - serial.ToTypedMessage(&log.Config{ - ErrorLogLevel: clog.Severity_Debug, - ErrorLogType: log.LogType_Console, - }), - }, - Inbound: []*core.InboundHandlerConfig{ - { - ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ - PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, - Listen: net.NewIPOrDomain(net.LocalHostIP), - }), - ProxySettings: serial.ToTypedMessage(&inbound.Config{ - Clients: []*protocol.User{ - { - Account: serial.ToTypedMessage(&vless.Account{ - Id: userID.String(), - }), - }, - }, - }), - }, - }, - Outbound: []*core.OutboundHandlerConfig{ - { - ProxySettings: serial.ToTypedMessage(&freedom.Config{}), - }, - }, - } - - clientPort := tcp.PickPort() - clientConfig := &core.Config{ - App: []*serial.TypedMessage{ - serial.ToTypedMessage(&log.Config{ - ErrorLogLevel: clog.Severity_Debug, - ErrorLogType: log.LogType_Console, - }), - }, - Inbound: []*core.InboundHandlerConfig{ - { - ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ - PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, - Listen: net.NewIPOrDomain(net.LocalHostIP), - }), - ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ - Address: net.NewIPOrDomain(dest.Address), - Port: uint32(dest.Port), - Networks: []net.Network{net.Network_TCP}, - }), - }, - }, - Outbound: []*core.OutboundHandlerConfig{ - { - ProxySettings: serial.ToTypedMessage(&outbound.Config{ - Vnext: []*protocol.ServerEndpoint{ - { - Address: net.NewIPOrDomain(net.LocalHostIP), - Port: uint32(serverPort), - User: []*protocol.User{ - { - Account: serial.ToTypedMessage(&vless.Account{ - Id: userID.String(), - }), - }, - }, - }, - }, - }), - }, - }, - } + testVlessSeed(t, "") +} - servers, err := InitializeServerConfigs(serverConfig, clientConfig) - common.Must(err) - defer CloseAllServers(servers) +func TestVlessSeedWithFixedTrigger(t *testing.T) { + testVlessSeed(t, "1") +} - var errg errgroup.Group - for i := 0; i < 10; i++ { - errg.Go(testTCPConn(clientPort, 1024*1024, time.Second*30)) - } - if err := errg.Wait(); err != nil { - t.Error(err) - } +func TestVlessSeedWithPingPong(t *testing.T) { + testVlessSeed(t, "pingpong") } -func TestVlessSeedWithIndependentScheduler(t *testing.T) { +func testVlessSeed(t *testing.T, seed string) { tcpServer := tcp.Server{ MsgProcessor: xor, } @@ -153,7 +69,7 @@ func TestVlessSeedWithIndependentScheduler(t *testing.T) { { Account: serial.ToTypedMessage(&vless.Account{ Id: userID.String(), - Seed: "1", + Seed: seed, }), }, }, @@ -184,9 +100,7 @@ func TestVlessSeedWithIndependentScheduler(t *testing.T) { ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), - NetworkList: &net.NetworkList{ - Network: []net.Network{net.Network_TCP}, - }, + Networks: []net.Network{net.Network_TCP}, }), }, }, @@ -201,7 +115,7 @@ func TestVlessSeedWithIndependentScheduler(t *testing.T) { { Account: serial.ToTypedMessage(&vless.Account{ Id: userID.String(), - Seed: "1", + Seed: seed, }), }, }, @@ -348,130 +262,18 @@ func TestVlessTls(t *testing.T) { } func TestVlessXtlsVision(t *testing.T) { - tcpServer := tcp.Server{ - MsgProcessor: xor, - } - dest, err := tcpServer.Start() - common.Must(err) - defer tcpServer.Close() - - userID := protocol.NewID(uuid.New()) - serverPort := tcp.PickPort() - serverConfig := &core.Config{ - App: []*serial.TypedMessage{ - serial.ToTypedMessage(&log.Config{ - ErrorLogLevel: clog.Severity_Debug, - ErrorLogType: log.LogType_Console, - }), - }, - Inbound: []*core.InboundHandlerConfig{ - { - ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ - PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, - Listen: net.NewIPOrDomain(net.LocalHostIP), - StreamSettings: &internet.StreamConfig{ - ProtocolName: "tcp", - SecurityType: serial.GetMessageType(&tls.Config{}), - SecuritySettings: []*serial.TypedMessage{ - serial.ToTypedMessage(&tls.Config{ - Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil))}, - }), - }, - }, - }), - ProxySettings: serial.ToTypedMessage(&inbound.Config{ - Clients: []*protocol.User{ - { - Account: serial.ToTypedMessage(&vless.Account{ - Id: userID.String(), - Flow: vless.XRV, - }), - }, - }, - }), - }, - }, - Outbound: []*core.OutboundHandlerConfig{ - { - ProxySettings: serial.ToTypedMessage(&freedom.Config{}), - }, - }, - } - - clientPort := tcp.PickPort() - clientConfig := &core.Config{ - App: []*serial.TypedMessage{ - serial.ToTypedMessage(&log.Config{ - ErrorLogLevel: clog.Severity_Debug, - ErrorLogType: log.LogType_Console, - }), - }, - Inbound: []*core.InboundHandlerConfig{ - { - ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ - PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, - Listen: net.NewIPOrDomain(net.LocalHostIP), - }), - ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ - Address: net.NewIPOrDomain(dest.Address), - Port: uint32(dest.Port), - Networks: []net.Network{net.Network_TCP}, - }), - }, - }, - Outbound: []*core.OutboundHandlerConfig{ - { - ProxySettings: serial.ToTypedMessage(&outbound.Config{ - Vnext: []*protocol.ServerEndpoint{ - { - Address: net.NewIPOrDomain(net.LocalHostIP), - Port: uint32(serverPort), - User: []*protocol.User{ - { - Account: serial.ToTypedMessage(&vless.Account{ - Id: userID.String(), - Flow: vless.XRV, - }), - }, - }, - }, - }, - }), - SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ - StreamSettings: &internet.StreamConfig{ - ProtocolName: "tcp", - TransportSettings: []*internet.TransportConfig{ - { - ProtocolName: "tcp", - Settings: serial.ToTypedMessage(&transtcp.Config{}), - }, - }, - SecurityType: serial.GetMessageType(&tls.Config{}), - SecuritySettings: []*serial.TypedMessage{ - serial.ToTypedMessage(&tls.Config{ - AllowInsecure: true, - }), - }, - }, - }), - }, - }, - } + testVlessXtlsVisionWithSeed(t, "") +} - servers, err := InitializeServerConfigs(serverConfig, clientConfig) - common.Must(err) - defer CloseAllServers(servers) +func TestVlessXtlsVisionWithFixedTrigger(t *testing.T) { + testVlessXtlsVisionWithSeed(t, "1") +} - var errg errgroup.Group - for i := 0; i < 10; i++ { - errg.Go(testTCPConn(clientPort, 1024*1024, time.Second*30)) - } - if err := errg.Wait(); err != nil { - t.Error(err) - } +func TestVlessXtlsVisionWithPingPong(t *testing.T) { + testVlessXtlsVisionWithSeed(t, "pingpong") } -func TestVlessXtlsVisionWithSeed(t *testing.T) { +func testVlessXtlsVisionWithSeed(t *testing.T, seed string) { tcpServer := tcp.Server{ MsgProcessor: xor, } @@ -494,7 +296,7 @@ func TestVlessXtlsVisionWithSeed(t *testing.T) { PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, Listen: net.NewIPOrDomain(net.LocalHostIP), StreamSettings: &internet.StreamConfig{ - Protocol: internet.TransportProtocol_TCP, + ProtocolName: "tcp", SecurityType: serial.GetMessageType(&tls.Config{}), SecuritySettings: []*serial.TypedMessage{ serial.ToTypedMessage(&tls.Config{ @@ -509,7 +311,7 @@ func TestVlessXtlsVisionWithSeed(t *testing.T) { Account: serial.ToTypedMessage(&vless.Account{ Id: userID.String(), Flow: vless.XRV, - Seed: "1", + Seed: seed, }), }, }, @@ -540,9 +342,7 @@ func TestVlessXtlsVisionWithSeed(t *testing.T) { ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), - NetworkList: &net.NetworkList{ - Network: []net.Network{net.Network_TCP}, - }, + Networks: []net.Network{net.Network_TCP}, }), }, }, @@ -558,7 +358,7 @@ func TestVlessXtlsVisionWithSeed(t *testing.T) { Account: serial.ToTypedMessage(&vless.Account{ Id: userID.String(), Flow: vless.XRV, - Seed: "1", + Seed: seed, }), }, }, @@ -567,10 +367,10 @@ func TestVlessXtlsVisionWithSeed(t *testing.T) { }), SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ StreamSettings: &internet.StreamConfig{ - Protocol: internet.TransportProtocol_TCP, + ProtocolName: "tcp", TransportSettings: []*internet.TransportConfig{ { - Protocol: internet.TransportProtocol_TCP, + ProtocolName: "tcp", Settings: serial.ToTypedMessage(&transtcp.Config{}), }, },