From f2ae2777fe67bfa8f8cb087c9d3cc95cc7237056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Sat, 20 Aug 2022 10:39:33 +0800 Subject: [PATCH 1/7] Feat: add PacketAddr support to Trojan server --- proxy/trojan/config.pb.go | 98 +++++++++++++++++----------- proxy/trojan/config.proto | 2 + proxy/trojan/server.go | 30 ++++++--- proxy/trojan/simplified/config.go | 1 + proxy/trojan/simplified/config.pb.go | 92 ++++++++++++++++---------- proxy/trojan/simplified/config.proto | 2 + 6 files changed, 145 insertions(+), 80 deletions(-) diff --git a/proxy/trojan/config.pb.go b/proxy/trojan/config.pb.go index 6a4cfb0d0f8..e7c0916ce25 100644 --- a/proxy/trojan/config.pb.go +++ b/proxy/trojan/config.pb.go @@ -1,6 +1,13 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v3.21.11 +// source: proxy/trojan/config.proto + package trojan import ( + packetaddr "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" protocol "github.com/v2fly/v2ray-core/v5/common/protocol" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" @@ -193,8 +200,9 @@ type ServerConfig struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Users []*protocol.User `protobuf:"bytes,1,rep,name=users,proto3" json:"users,omitempty"` - Fallbacks []*Fallback `protobuf:"bytes,3,rep,name=fallbacks,proto3" json:"fallbacks,omitempty"` + Users []*protocol.User `protobuf:"bytes,1,rep,name=users,proto3" json:"users,omitempty"` + Fallbacks []*Fallback `protobuf:"bytes,3,rep,name=fallbacks,proto3" json:"fallbacks,omitempty"` + PacketEncoding packetaddr.PacketAddrType `protobuf:"varint,4,opt,name=packet_encoding,json=packetEncoding,proto3,enum=v2ray.core.net.packetaddr.PacketAddrType" json:"packet_encoding,omitempty"` } func (x *ServerConfig) Reset() { @@ -243,6 +251,13 @@ func (x *ServerConfig) GetFallbacks() []*Fallback { return nil } +func (x *ServerConfig) GetPacketEncoding() packetaddr.PacketAddrType { + if x != nil { + return x.PacketEncoding + } + return packetaddr.PacketAddrType(0) +} + var File_proxy_trojan_config_proto protoreflect.FileDescriptor var file_proxy_trojan_config_proto_rawDesc = []byte{ @@ -253,37 +268,44 @@ var file_proxy_trojan_config_proto_rawDesc = []byte{ 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x21, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x73, 0x70, 0x65, 0x63, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x22, 0x25, 0x0a, 0x07, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1a, - 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x6e, 0x0a, 0x08, 0x46, 0x61, - 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x6c, 0x70, 0x6e, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x6c, 0x70, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, - 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x12, - 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x79, - 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x65, 0x73, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x04, 0x64, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x78, 0x76, 0x65, 0x72, 0x18, 0x05, - 0x20, 0x01, 0x28, 0x04, 0x52, 0x04, 0x78, 0x76, 0x65, 0x72, 0x22, 0x52, 0x0a, 0x0c, 0x43, 0x6c, - 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x42, 0x0a, 0x06, 0x73, 0x65, - 0x72, 0x76, 0x65, 0x72, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2a, 0x2e, 0x76, 0x32, 0x72, - 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x45, 0x6e, - 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x52, 0x06, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x22, 0x87, - 0x01, 0x0a, 0x0c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, - 0x36, 0x0a, 0x05, 0x75, 0x73, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x20, + 0x6f, 0x74, 0x6f, 0x1a, 0x22, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x6e, 0x65, 0x74, 0x2f, + 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, + 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x25, 0x0a, 0x07, 0x41, 0x63, 0x63, 0x6f, 0x75, + 0x6e, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x6e, + 0x0a, 0x08, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x6c, + 0x70, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x6c, 0x70, 0x6e, 0x12, 0x12, + 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, + 0x74, 0x68, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x65, 0x73, 0x74, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x78, 0x76, + 0x65, 0x72, 0x18, 0x05, 0x20, 0x01, 0x28, 0x04, 0x52, 0x04, 0x78, 0x76, 0x65, 0x72, 0x22, 0x52, + 0x0a, 0x0c, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x42, + 0x0a, 0x06, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2a, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, - 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2e, 0x55, 0x73, 0x65, 0x72, - 0x52, 0x05, 0x75, 0x73, 0x65, 0x72, 0x73, 0x12, 0x3f, 0x0a, 0x09, 0x66, 0x61, 0x6c, 0x6c, 0x62, - 0x61, 0x63, 0x6b, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x21, 0x2e, 0x76, 0x32, 0x72, - 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x74, 0x72, - 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x52, 0x09, 0x66, - 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x73, 0x42, 0x66, 0x0a, 0x1b, 0x63, 0x6f, 0x6d, 0x2e, - 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, - 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x50, 0x01, 0x5a, 0x2b, 0x67, 0x69, 0x74, 0x68, 0x75, - 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76, 0x32, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32, 0x72, 0x61, - 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x35, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, - 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0xaa, 0x02, 0x17, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, - 0x6f, 0x72, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x54, 0x72, 0x6f, 0x6a, 0x61, 0x6e, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2e, 0x53, 0x65, 0x72, 0x76, + 0x65, 0x72, 0x45, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x52, 0x06, 0x73, 0x65, 0x72, 0x76, + 0x65, 0x72, 0x22, 0xdb, 0x01, 0x0a, 0x0c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x6f, 0x6e, + 0x66, 0x69, 0x67, 0x12, 0x36, 0x0a, 0x05, 0x75, 0x73, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x20, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, + 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2e, + 0x55, 0x73, 0x65, 0x72, 0x52, 0x05, 0x75, 0x73, 0x65, 0x72, 0x73, 0x12, 0x3f, 0x0a, 0x09, 0x66, + 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x21, + 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x78, + 0x79, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, + 0x6b, 0x52, 0x09, 0x66, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x73, 0x12, 0x52, 0x0a, 0x0f, + 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x5f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x29, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, + 0x72, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x2e, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x61, 0x64, 0x64, + 0x72, 0x2e, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x41, 0x64, 0x64, 0x72, 0x54, 0x79, 0x70, 0x65, + 0x52, 0x0e, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, + 0x42, 0x66, 0x0a, 0x1b, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, + 0x72, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x50, + 0x01, 0x5a, 0x2b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76, 0x32, + 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, + 0x35, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0xaa, 0x02, + 0x17, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x78, + 0x79, 0x2e, 0x54, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -306,16 +328,18 @@ var file_proxy_trojan_config_proto_goTypes = []interface{}{ (*ServerConfig)(nil), // 3: v2ray.core.proxy.trojan.ServerConfig (*protocol.ServerEndpoint)(nil), // 4: v2ray.core.common.protocol.ServerEndpoint (*protocol.User)(nil), // 5: v2ray.core.common.protocol.User + (packetaddr.PacketAddrType)(0), // 6: v2ray.core.net.packetaddr.PacketAddrType } var file_proxy_trojan_config_proto_depIdxs = []int32{ 4, // 0: v2ray.core.proxy.trojan.ClientConfig.server:type_name -> v2ray.core.common.protocol.ServerEndpoint 5, // 1: v2ray.core.proxy.trojan.ServerConfig.users:type_name -> v2ray.core.common.protocol.User 1, // 2: v2ray.core.proxy.trojan.ServerConfig.fallbacks:type_name -> v2ray.core.proxy.trojan.Fallback - 3, // [3:3] is the sub-list for method output_type - 3, // [3:3] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 3, // [3:3] is the sub-list for extension extendee - 0, // [0:3] is the sub-list for field type_name + 6, // 3: v2ray.core.proxy.trojan.ServerConfig.packet_encoding:type_name -> v2ray.core.net.packetaddr.PacketAddrType + 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_trojan_config_proto_init() } diff --git a/proxy/trojan/config.proto b/proxy/trojan/config.proto index 9e09945cf99..01104f1d3c9 100644 --- a/proxy/trojan/config.proto +++ b/proxy/trojan/config.proto @@ -8,6 +8,7 @@ option java_multiple_files = true; import "common/protocol/user.proto"; import "common/protocol/server_spec.proto"; +import "common/net/packetaddr/config.proto"; message Account { string password = 1; @@ -28,4 +29,5 @@ message ClientConfig { message ServerConfig { repeated v2ray.core.common.protocol.User users = 1; repeated Fallback fallbacks = 3; + v2ray.core.net.packetaddr.PacketAddrType packet_encoding = 4; } diff --git a/proxy/trojan/server.go b/proxy/trojan/server.go index e628e6968fc..e8739a35faf 100644 --- a/proxy/trojan/server.go +++ b/proxy/trojan/server.go @@ -12,6 +12,7 @@ import ( "github.com/v2fly/v2ray-core/v5/common/errors" "github.com/v2fly/v2ray-core/v5/common/log" "github.com/v2fly/v2ray-core/v5/common/net" + packetaddr "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" "github.com/v2fly/v2ray-core/v5/common/protocol" udp_proto "github.com/v2fly/v2ray-core/v5/common/protocol/udp" "github.com/v2fly/v2ray-core/v5/common/retry" @@ -33,9 +34,10 @@ func init() { // Server is an inbound connection handler that handles messages in trojan protocol. type Server struct { - policyManager policy.Manager - validator *Validator - fallbacks map[string]map[string]*Fallback // or nil + policyManager policy.Manager + validator *Validator + fallbacks map[string]map[string]*Fallback // or nil + packetEncoding packetaddr.PacketAddrType } // NewServer creates a new trojan inbound handler. @@ -54,8 +56,9 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { v := core.MustFromContext(ctx) server := &Server{ - policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager), - validator: validator, + policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager), + validator: validator, + packetEncoding: config.PacketEncoding, } if config.Fallbacks != nil { @@ -204,7 +207,16 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn internet } func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReader, clientWriter *PacketWriter, dispatcher routing.Dispatcher) error { - udpServer := udp.NewSplitDispatcher(dispatcher, func(ctx context.Context, packet *udp_proto.Packet) { + udpDispatcherConstructor := udp.NewSplitDispatcher + switch s.packetEncoding { + case packetaddr.PacketAddrType_None: + break + case packetaddr.PacketAddrType_Packet: + packetAddrDispatcherFactory := udp.NewPacketAddrDispatcherCreator(ctx) + udpDispatcherConstructor = packetAddrDispatcherFactory.NewPacketAddrDispatcher + } + + udpServer := udpDispatcherConstructor(dispatcher, func(ctx context.Context, packet *udp_proto.Packet) { if err := clientWriter.WriteMultiBufferWithMetadata(buf.MultiBuffer{packet.Payload}, packet.Source); err != nil { newError("failed to write response").Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx)) } @@ -225,8 +237,8 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade } return nil } - - ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{ + currentPacketCtx := ctx + currentPacketCtx = log.ContextWithAccessMessage(currentPacketCtx, &log.AccessMessage{ From: inbound.Source, To: p.Target, Status: log.AccessAccepted, @@ -236,7 +248,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade newError("tunnelling request to ", p.Target).WriteToLog(session.ExportIDToError(ctx)) for _, b := range p.Buffer { - udpServer.Dispatch(ctx, p.Target, b) + udpServer.Dispatch(currentPacketCtx, p.Target, b) } } } diff --git a/proxy/trojan/simplified/config.go b/proxy/trojan/simplified/config.go index b66170471d2..f0fd289a216 100644 --- a/proxy/trojan/simplified/config.go +++ b/proxy/trojan/simplified/config.go @@ -22,6 +22,7 @@ func init() { } return }(), + PacketEncoding: simplifiedServer.PacketEncoding, } return common.CreateObject(ctx, fullServer) })) diff --git a/proxy/trojan/simplified/config.pb.go b/proxy/trojan/simplified/config.pb.go index 404b59d7e3d..1e3e2f88429 100644 --- a/proxy/trojan/simplified/config.pb.go +++ b/proxy/trojan/simplified/config.pb.go @@ -1,7 +1,14 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v3.21.11 +// source: proxy/trojan/simplified/config.proto + package simplified import ( net "github.com/v2fly/v2ray-core/v5/common/net" + packetaddr "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" _ "github.com/v2fly/v2ray-core/v5/common/protoext" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" @@ -21,7 +28,8 @@ type ServerConfig struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Users []string `protobuf:"bytes,1,rep,name=users,proto3" json:"users,omitempty"` + Users []string `protobuf:"bytes,1,rep,name=users,proto3" json:"users,omitempty"` + PacketEncoding packetaddr.PacketAddrType `protobuf:"varint,2,opt,name=packet_encoding,json=packetEncoding,proto3,enum=v2ray.core.net.packetaddr.PacketAddrType" json:"packet_encoding,omitempty"` } func (x *ServerConfig) Reset() { @@ -63,6 +71,13 @@ func (x *ServerConfig) GetUsers() []string { return nil } +func (x *ServerConfig) GetPacketEncoding() packetaddr.PacketAddrType { + if x != nil { + return x.PacketEncoding + } + return packetaddr.PacketAddrType(0) +} + type ClientConfig struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -137,30 +152,37 @@ var file_proxy_trojan_simplified_config_proto_rawDesc = []byte{ 0x6f, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x65, 0x78, 0x74, 0x2f, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x18, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x6e, 0x65, 0x74, 0x2f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x3f, 0x0a, 0x0c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, - 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x14, 0x0a, 0x05, 0x75, 0x73, 0x65, 0x72, 0x73, 0x18, - 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x05, 0x75, 0x73, 0x65, 0x72, 0x73, 0x3a, 0x19, 0x82, 0xb5, - 0x18, 0x09, 0x0a, 0x07, 0x69, 0x6e, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x82, 0xb5, 0x18, 0x08, 0x12, - 0x06, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x22, 0x97, 0x01, 0x0a, 0x0c, 0x43, 0x6c, 0x69, 0x65, - 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x3b, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, - 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x21, 0x2e, 0x76, 0x32, 0x72, 0x61, - 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x6e, 0x65, - 0x74, 0x2e, 0x49, 0x50, 0x4f, 0x72, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x52, 0x07, 0x61, 0x64, - 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x0d, 0x52, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, - 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, - 0x73, 0x77, 0x6f, 0x72, 0x64, 0x3a, 0x1a, 0x82, 0xb5, 0x18, 0x0a, 0x0a, 0x08, 0x6f, 0x75, 0x74, - 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x82, 0xb5, 0x18, 0x08, 0x12, 0x06, 0x74, 0x72, 0x6f, 0x6a, 0x61, - 0x6e, 0x42, 0x87, 0x01, 0x0a, 0x26, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, - 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, - 0x6e, 0x2e, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x69, 0x66, 0x69, 0x65, 0x64, 0x50, 0x01, 0x5a, 0x36, - 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76, 0x32, 0x66, 0x6c, 0x79, - 0x2f, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x35, 0x2f, 0x70, - 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2f, 0x73, 0x69, 0x6d, 0x70, - 0x6c, 0x69, 0x66, 0x69, 0x65, 0x64, 0xaa, 0x02, 0x22, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, - 0x6f, 0x72, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x54, 0x72, 0x6f, 0x6a, 0x61, 0x6e, - 0x2e, 0x53, 0x69, 0x6d, 0x70, 0x6c, 0x69, 0x66, 0x69, 0x65, 0x64, 0x62, 0x06, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x33, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x22, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x6e, + 0x65, 0x74, 0x2f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x2f, 0x63, 0x6f, + 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x93, 0x01, 0x0a, 0x0c, 0x53, + 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x14, 0x0a, 0x05, 0x75, + 0x73, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x05, 0x75, 0x73, 0x65, 0x72, + 0x73, 0x12, 0x52, 0x0a, 0x0f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x5f, 0x65, 0x6e, 0x63, 0x6f, + 0x64, 0x69, 0x6e, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x29, 0x2e, 0x76, 0x32, 0x72, + 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x2e, 0x70, 0x61, 0x63, 0x6b, + 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x2e, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x41, 0x64, 0x64, + 0x72, 0x54, 0x79, 0x70, 0x65, 0x52, 0x0e, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x45, 0x6e, 0x63, + 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x19, 0x82, 0xb5, 0x18, 0x09, 0x0a, 0x07, 0x69, 0x6e, 0x62, + 0x6f, 0x75, 0x6e, 0x64, 0x82, 0xb5, 0x18, 0x08, 0x12, 0x06, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, + 0x22, 0x97, 0x01, 0x0a, 0x0c, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x69, + 0x67, 0x12, 0x3b, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x21, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, + 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x6e, 0x65, 0x74, 0x2e, 0x49, 0x50, 0x4f, 0x72, 0x44, + 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x12, + 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x70, 0x6f, + 0x72, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x3a, 0x1a, + 0x82, 0xb5, 0x18, 0x0a, 0x0a, 0x08, 0x6f, 0x75, 0x74, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x82, 0xb5, + 0x18, 0x08, 0x12, 0x06, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x42, 0x87, 0x01, 0x0a, 0x26, 0x63, + 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x78, 0x79, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x73, 0x69, 0x6d, 0x70, 0x6c, + 0x69, 0x66, 0x69, 0x65, 0x64, 0x50, 0x01, 0x5a, 0x36, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x76, 0x32, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2d, + 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x35, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x74, 0x72, + 0x6f, 0x6a, 0x61, 0x6e, 0x2f, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x69, 0x66, 0x69, 0x65, 0x64, 0xaa, + 0x02, 0x22, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65, 0x2e, 0x50, 0x72, 0x6f, + 0x78, 0x79, 0x2e, 0x54, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x53, 0x69, 0x6d, 0x70, 0x6c, 0x69, + 0x66, 0x69, 0x65, 0x64, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -177,17 +199,19 @@ func file_proxy_trojan_simplified_config_proto_rawDescGZIP() []byte { var file_proxy_trojan_simplified_config_proto_msgTypes = make([]protoimpl.MessageInfo, 2) var file_proxy_trojan_simplified_config_proto_goTypes = []interface{}{ - (*ServerConfig)(nil), // 0: v2ray.core.proxy.trojan.simplified.ServerConfig - (*ClientConfig)(nil), // 1: v2ray.core.proxy.trojan.simplified.ClientConfig - (*net.IPOrDomain)(nil), // 2: v2ray.core.common.net.IPOrDomain + (*ServerConfig)(nil), // 0: v2ray.core.proxy.trojan.simplified.ServerConfig + (*ClientConfig)(nil), // 1: v2ray.core.proxy.trojan.simplified.ClientConfig + (packetaddr.PacketAddrType)(0), // 2: v2ray.core.net.packetaddr.PacketAddrType + (*net.IPOrDomain)(nil), // 3: v2ray.core.common.net.IPOrDomain } var file_proxy_trojan_simplified_config_proto_depIdxs = []int32{ - 2, // 0: v2ray.core.proxy.trojan.simplified.ClientConfig.address:type_name -> v2ray.core.common.net.IPOrDomain - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name + 2, // 0: v2ray.core.proxy.trojan.simplified.ServerConfig.packet_encoding:type_name -> v2ray.core.net.packetaddr.PacketAddrType + 3, // 1: v2ray.core.proxy.trojan.simplified.ClientConfig.address:type_name -> v2ray.core.common.net.IPOrDomain + 2, // [2:2] is the sub-list for method output_type + 2, // [2:2] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name } func init() { file_proxy_trojan_simplified_config_proto_init() } diff --git a/proxy/trojan/simplified/config.proto b/proxy/trojan/simplified/config.proto index cf95fb9ec3d..5925be02ca4 100644 --- a/proxy/trojan/simplified/config.proto +++ b/proxy/trojan/simplified/config.proto @@ -8,12 +8,14 @@ option java_multiple_files = true; import "common/protoext/extensions.proto"; import "common/net/address.proto"; +import "common/net/packetaddr/config.proto"; message ServerConfig{ option (v2ray.core.common.protoext.message_opt).type = "inbound"; option (v2ray.core.common.protoext.message_opt).short_name = "trojan"; repeated string users = 1; + v2ray.core.net.packetaddr.PacketAddrType packet_encoding = 2; } message ClientConfig { From 01a268656809e3678e195c8c1b933c8718db35e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Sat, 20 Aug 2022 13:04:16 +0800 Subject: [PATCH 2/7] Feat: add PacketAddr support to Trojan client --- proxy/trojan/client.go | 49 +++++++++++++++++++++++++++++++++++++++- proxy/trojan/protocol.go | 33 +++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/proxy/trojan/client.go b/proxy/trojan/client.go index dfb1680c23e..afd9aa6d02f 100644 --- a/proxy/trojan/client.go +++ b/proxy/trojan/client.go @@ -7,6 +7,7 @@ import ( "github.com/v2fly/v2ray-core/v5/common" "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/net" + "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" "github.com/v2fly/v2ray-core/v5/common/protocol" "github.com/v2fly/v2ray-core/v5/common/retry" "github.com/v2fly/v2ray-core/v5/common/session" @@ -16,6 +17,7 @@ import ( "github.com/v2fly/v2ray-core/v5/proxy" "github.com/v2fly/v2ray-core/v5/transport" "github.com/v2fly/v2ray-core/v5/transport/internet" + "github.com/v2fly/v2ray-core/v5/transport/internet/udp" ) // Client is an inbound handler for trojan protocol @@ -85,6 +87,51 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter ctx, cancel := context.WithCancel(ctx) timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) + if packetConn, err := packetaddr.ToPacketAddrConn(link, destination); err == nil { + postRequest := func() error { + defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) + + var buffer [2048]byte + _, addr, err := packetConn.ReadFrom(buffer[:]) + if err != nil { + return newError("failed to read a packet").Base(err) + } + dest := net.DestinationFromAddr(addr) + + bufferWriter := buf.NewBufferedWriter(buf.NewWriter(conn)) + connWriter := &ConnWriter{Writer: bufferWriter, Target: dest, Account: account} + packetWriter := &PacketWriter{Writer: connWriter, Target: dest} + + // write some request payload to buffer + if _, err := packetWriter.WriteTo(buffer[:], addr); err != nil { + return newError("failed to write a request payload").Base(err) + } + + // Flush; bufferWriter.WriteMultiBuffer now is bufferWriter.writer.WriteMultiBuffer + if err = bufferWriter.SetBuffered(false); err != nil { + return newError("failed to flush payload").Base(err).AtWarning() + } + + return udp.CopyPacketConn(packetWriter, packetConn, udp.UpdateActivity(timer)) + } + + getResponse := func() error { + defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly) + + packetReader := &PacketReader{Reader: conn} + splitReader := &PacketSplitReader{Reader: packetReader} + + return udp.CopyPacketConn(packetConn, splitReader, udp.UpdateActivity(timer)) + } + + responseDoneAndCloseWriter := task.OnSuccess(getResponse, task.Close(link.Writer)) + if err := task.Run(ctx, postRequest, responseDoneAndCloseWriter); err != nil { + return newError("connection ends").Base(err) + } + + return nil + } + postRequest := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) @@ -100,7 +147,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter // write some request payload to buffer if err = buf.CopyOnceTimeout(link.Reader, bodyWriter, proxy.FirstPayloadTimeout); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout { - return newError("failed to write A request payload").Base(err).AtWarning() + return newError("failed to write a request payload").Base(err).AtWarning() } // Flush; bufferWriter.WriteMultiBuffer now is bufferWriter.writer.WriteMultiBuffer diff --git a/proxy/trojan/protocol.go b/proxy/trojan/protocol.go index 03a17edf86f..6b5f29f0ef1 100644 --- a/proxy/trojan/protocol.go +++ b/proxy/trojan/protocol.go @@ -3,6 +3,7 @@ package trojan import ( "encoding/binary" "io" + gonet "net" "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/net" @@ -128,6 +129,12 @@ func (w *PacketWriter) WriteMultiBufferWithMetadata(mb buf.MultiBuffer, dest net return nil } +func (w *PacketWriter) WriteTo(payload []byte, addr gonet.Addr) (int, error) { + dest := net.DestinationFromAddr(addr) + + return w.writePacket(payload, dest) +} + func (w *PacketWriter) writePacket(payload []byte, dest net.Destination) (int, error) { // nolint: unparam buffer := buf.StackNew() defer buffer.Release() @@ -279,3 +286,29 @@ func (r *PacketReader) ReadMultiBufferWithMetadata() (*PacketPayload, error) { return &PacketPayload{Target: dest, Buffer: mb}, nil } + +type PacketSplitReader struct { + Reader *PacketReader + Payload *PacketPayload +} + +func (r *PacketSplitReader) ReadFrom(p []byte) (int, gonet.Addr, error) { + var err error + + if r.Payload == nil || r.Payload.Buffer.IsEmpty() { + r.Payload, err = r.Reader.ReadMultiBufferWithMetadata() + if err != nil { + return 0, nil, err + } + } + + addr := &gonet.UDPAddr{ + IP: r.Payload.Target.Address.IP(), + Port: int(r.Payload.Target.Port), + } + + mb, nBytes := buf.SplitBytes(r.Payload.Buffer, p) + r.Payload.Buffer = mb + + return nBytes, addr, nil +} From 147641e981383614e16c549910f976927ee69f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Sat, 20 Aug 2022 13:11:12 +0800 Subject: [PATCH 3/7] Style: refine code style --- proxy/trojan/protocol.go | 13 +++++-------- proxy/trojan/server.go | 1 - 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/proxy/trojan/protocol.go b/proxy/trojan/protocol.go index 6b5f29f0ef1..0dddd298435 100644 --- a/proxy/trojan/protocol.go +++ b/proxy/trojan/protocol.go @@ -292,23 +292,20 @@ type PacketSplitReader struct { Payload *PacketPayload } -func (r *PacketSplitReader) ReadFrom(p []byte) (int, gonet.Addr, error) { - var err error - +func (r *PacketSplitReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { if r.Payload == nil || r.Payload.Buffer.IsEmpty() { r.Payload, err = r.Reader.ReadMultiBufferWithMetadata() if err != nil { - return 0, nil, err + return } } - addr := &gonet.UDPAddr{ + addr = &gonet.UDPAddr{ IP: r.Payload.Target.Address.IP(), Port: int(r.Payload.Target.Port), } - mb, nBytes := buf.SplitBytes(r.Payload.Buffer, p) - r.Payload.Buffer = mb + r.Payload.Buffer, n = buf.SplitBytes(r.Payload.Buffer, p) - return nBytes, addr, nil + return } diff --git a/proxy/trojan/server.go b/proxy/trojan/server.go index e8739a35faf..10ab41e6dc4 100644 --- a/proxy/trojan/server.go +++ b/proxy/trojan/server.go @@ -210,7 +210,6 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade udpDispatcherConstructor := udp.NewSplitDispatcher switch s.packetEncoding { case packetaddr.PacketAddrType_None: - break case packetaddr.PacketAddrType_Packet: packetAddrDispatcherFactory := udp.NewPacketAddrDispatcherCreator(ctx) udpDispatcherConstructor = packetAddrDispatcherFactory.NewPacketAddrDispatcher From c10e16cb5e22790c84b80e66223fc209776f930d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Sat, 20 Aug 2022 17:15:20 +0800 Subject: [PATCH 4/7] Style: format imports --- proxy/trojan/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/trojan/server.go b/proxy/trojan/server.go index 10ab41e6dc4..ff0177cad2c 100644 --- a/proxy/trojan/server.go +++ b/proxy/trojan/server.go @@ -12,7 +12,7 @@ import ( "github.com/v2fly/v2ray-core/v5/common/errors" "github.com/v2fly/v2ray-core/v5/common/log" "github.com/v2fly/v2ray-core/v5/common/net" - packetaddr "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" + "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" "github.com/v2fly/v2ray-core/v5/common/protocol" udp_proto "github.com/v2fly/v2ray-core/v5/common/protocol/udp" "github.com/v2fly/v2ray-core/v5/common/retry" From 1bd1de5f526c854e1ecb1088a16b1cee68a44383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Wed, 21 Dec 2022 17:15:42 +0800 Subject: [PATCH 5/7] fix: read logic of PacketConnectionReader --- proxy/trojan/client.go | 6 +++++- proxy/trojan/protocol.go | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/proxy/trojan/client.go b/proxy/trojan/client.go index afd9aa6d02f..72243018179 100644 --- a/proxy/trojan/client.go +++ b/proxy/trojan/client.go @@ -2,6 +2,7 @@ package trojan import ( "context" + sync "sync" core "github.com/v2fly/v2ray-core/v5" "github.com/v2fly/v2ray-core/v5/common" @@ -119,7 +120,10 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly) packetReader := &PacketReader{Reader: conn} - splitReader := &PacketSplitReader{Reader: packetReader} + splitReader := &PacketConnectionReader{ + readerAccess: &sync.Mutex{}, + reader: packetReader, + } return udp.CopyPacketConn(packetConn, splitReader, udp.UpdateActivity(timer)) } diff --git a/proxy/trojan/protocol.go b/proxy/trojan/protocol.go index 0dddd298435..654f77db8da 100644 --- a/proxy/trojan/protocol.go +++ b/proxy/trojan/protocol.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "io" gonet "net" + sync "sync" "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/net" @@ -287,25 +288,29 @@ func (r *PacketReader) ReadMultiBufferWithMetadata() (*PacketPayload, error) { return &PacketPayload{Target: dest, Buffer: mb}, nil } -type PacketSplitReader struct { - Reader *PacketReader - Payload *PacketPayload +type PacketConnectionReader struct { + readerAccess *sync.Mutex + reader *PacketReader + payload *PacketPayload } -func (r *PacketSplitReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { - if r.Payload == nil || r.Payload.Buffer.IsEmpty() { - r.Payload, err = r.Reader.ReadMultiBufferWithMetadata() +func (r *PacketConnectionReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { + r.readerAccess.Lock() + defer r.readerAccess.Unlock() + + if r.payload == nil || r.payload.Buffer.IsEmpty() { + r.payload, err = r.reader.ReadMultiBufferWithMetadata() if err != nil { return } } addr = &gonet.UDPAddr{ - IP: r.Payload.Target.Address.IP(), - Port: int(r.Payload.Target.Port), + IP: r.payload.Target.Address.IP(), + Port: int(r.payload.Target.Port), } - r.Payload.Buffer, n = buf.SplitBytes(r.Payload.Buffer, p) + r.payload.Buffer, n = buf.SplitFirstBytes(r.payload.Buffer, p) return } From ca185b33108a42d4180b089f093a56714927c62e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Wed, 21 Dec 2022 17:20:22 +0800 Subject: [PATCH 6/7] chore: format code --- proxy/trojan/client.go | 2 +- proxy/trojan/protocol.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/proxy/trojan/client.go b/proxy/trojan/client.go index 72243018179..b4b3bf6df16 100644 --- a/proxy/trojan/client.go +++ b/proxy/trojan/client.go @@ -2,7 +2,7 @@ package trojan import ( "context" - sync "sync" + "sync" core "github.com/v2fly/v2ray-core/v5" "github.com/v2fly/v2ray-core/v5/common" diff --git a/proxy/trojan/protocol.go b/proxy/trojan/protocol.go index 654f77db8da..9d4b74d924e 100644 --- a/proxy/trojan/protocol.go +++ b/proxy/trojan/protocol.go @@ -4,7 +4,7 @@ import ( "encoding/binary" "io" gonet "net" - sync "sync" + "sync" "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/net" From 67c19f7054e987e876c7cd46d8190d101a547b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Wed, 21 Dec 2022 17:44:04 +0800 Subject: [PATCH 7/7] feat: refine PacketConnectionReader --- proxy/trojan/client.go | 8 ++------ proxy/trojan/protocol.go | 9 ++------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/proxy/trojan/client.go b/proxy/trojan/client.go index b4b3bf6df16..4b5d788d898 100644 --- a/proxy/trojan/client.go +++ b/proxy/trojan/client.go @@ -2,7 +2,6 @@ package trojan import ( "context" - "sync" core "github.com/v2fly/v2ray-core/v5" "github.com/v2fly/v2ray-core/v5/common" @@ -120,12 +119,9 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly) packetReader := &PacketReader{Reader: conn} - splitReader := &PacketConnectionReader{ - readerAccess: &sync.Mutex{}, - reader: packetReader, - } + packetConnectionReader := &PacketConnectionReader{reader: packetReader} - return udp.CopyPacketConn(packetConn, splitReader, udp.UpdateActivity(timer)) + return udp.CopyPacketConn(packetConn, packetConnectionReader, udp.UpdateActivity(timer)) } responseDoneAndCloseWriter := task.OnSuccess(getResponse, task.Close(link.Writer)) diff --git a/proxy/trojan/protocol.go b/proxy/trojan/protocol.go index 9d4b74d924e..21e6a5a84cd 100644 --- a/proxy/trojan/protocol.go +++ b/proxy/trojan/protocol.go @@ -4,7 +4,6 @@ import ( "encoding/binary" "io" gonet "net" - "sync" "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/net" @@ -289,15 +288,11 @@ func (r *PacketReader) ReadMultiBufferWithMetadata() (*PacketPayload, error) { } type PacketConnectionReader struct { - readerAccess *sync.Mutex - reader *PacketReader - payload *PacketPayload + reader *PacketReader + payload *PacketPayload } func (r *PacketConnectionReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { - r.readerAccess.Lock() - defer r.readerAccess.Unlock() - if r.payload == nil || r.payload.Buffer.IsEmpty() { r.payload, err = r.reader.ReadMultiBufferWithMetadata() if err != nil {