From 256a8166cf5e7933f663c2bc8b0eb673d3a17936 Mon Sep 17 00:00:00 2001 From: dyhkwong <50692134+dyhkwong@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:39:22 +0800 Subject: [PATCH] Add packetEncoding for Hysteria 2 --- infra/conf/v4/hysteria2.go | 11 +++- infra/conf/v4/transport_internet.go | 11 ++-- proxy/hysteria2/client.go | 47 +++++++++++++++++ proxy/hysteria2/config.pb.go | 78 ++++++++++++++++++----------- proxy/hysteria2/config.proto | 5 +- proxy/hysteria2/protocol.go | 23 +++++++++ proxy/hysteria2/server.go | 15 ++++-- 7 files changed, 150 insertions(+), 40 deletions(-) diff --git a/infra/conf/v4/hysteria2.go b/infra/conf/v4/hysteria2.go index 6d2b301987d..690513be7b8 100644 --- a/infra/conf/v4/hysteria2.go +++ b/infra/conf/v4/hysteria2.go @@ -3,6 +3,7 @@ package v4 import ( "github.com/golang/protobuf/proto" + "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" "github.com/v2fly/v2ray-core/v5/common/protocol" "github.com/v2fly/v2ray-core/v5/common/serial" "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon" @@ -60,10 +61,18 @@ func (c *Hysteria2ClientConfig) Build() (proto.Message, error) { } // Hysteria2ServerConfig is Inbound configuration -type Hysteria2ServerConfig struct{} +type Hysteria2ServerConfig struct { + PacketEncoding string `json:"packetEncoding"` +} // Build implements Buildable func (c *Hysteria2ServerConfig) Build() (proto.Message, error) { config := new(hysteria2.ServerConfig) + switch c.PacketEncoding { + case "Packet": + config.PacketEncoding = packetaddr.PacketAddrType_Packet + case "", "None": + config.PacketEncoding = packetaddr.PacketAddrType_None + } return config, nil } diff --git a/infra/conf/v4/transport_internet.go b/infra/conf/v4/transport_internet.go index 9584205deef..61412d95842 100644 --- a/infra/conf/v4/transport_internet.go +++ b/infra/conf/v4/transport_internet.go @@ -145,9 +145,10 @@ type Hy2ConfigCongestion struct { } type Hy2Config struct { - Password string `json:"password"` - Congestion Hy2ConfigCongestion `json:"congestion"` - UseUdpExtension bool `json:"use_udp_extension"` + Password string `json:"password"` + Congestion Hy2ConfigCongestion `json:"congestion"` + UseUdpExtension bool `json:"use_udp_extension"` + IgnoreClientBandwidth bool `json:"ignore_client_bandwidth"` } // Build implements Buildable. @@ -158,7 +159,9 @@ func (c *Hy2Config) Build() (proto.Message, error) { DownMbps: c.Congestion.DownMbps, UpMbps: c.Congestion.UpMbps, }, - UseUdpExtension: c.UseUdpExtension}, nil + UseUdpExtension: c.UseUdpExtension, + IgnoreClientBandwidth: c.IgnoreClientBandwidth, + }, nil } type WebSocketConfig struct { diff --git a/proxy/hysteria2/client.go b/proxy/hysteria2/client.go index 5615c6a9e18..b4bfa3a1b21 100644 --- a/proxy/hysteria2/client.go +++ b/proxy/hysteria2/client.go @@ -9,6 +9,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" @@ -19,6 +20,7 @@ import ( "github.com/v2fly/v2ray-core/v5/transport" "github.com/v2fly/v2ray-core/v5/transport/internet" hyTransport "github.com/v2fly/v2ray-core/v5/transport/internet/hysteria2" + "github.com/v2fly/v2ray-core/v5/transport/internet/udp" ) // Client is an inbound handler @@ -98,6 +100,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 + n, 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} + packetWriter := &PacketWriter{Writer: connWriter, Target: dest, HyConn: hyConn} + + // write some request payload to buffer + if _, err := packetWriter.WriteTo(buffer[:n], 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, HyConn: hyConn} + packetConnectionReader := &PacketConnectionReader{reader: packetReader} + + return udp.CopyPacketConn(packetConn, packetConnectionReader, 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) diff --git a/proxy/hysteria2/config.pb.go b/proxy/hysteria2/config.pb.go index 491af3896f3..1d9d291fa05 100644 --- a/proxy/hysteria2/config.pb.go +++ b/proxy/hysteria2/config.pb.go @@ -1,6 +1,7 @@ package hysteria2 import ( + packetaddr "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" protocol "github.com/v2fly/v2ray-core/v5/common/protocol" _ "github.com/v2fly/v2ray-core/v5/common/protoext" protoreflect "google.golang.org/protobuf/reflect/protoreflect" @@ -105,6 +106,8 @@ type ServerConfig struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields + + PacketEncoding packetaddr.PacketAddrType `protobuf:"varint,1,opt,name=packet_encoding,json=packetEncoding,proto3,enum=v2ray.core.net.packetaddr.PacketAddrType" json:"packet_encoding,omitempty"` } func (x *ServerConfig) Reset() { @@ -139,37 +142,50 @@ func (*ServerConfig) Descriptor() ([]byte, []int) { return file_proxy_hysteria2_config_proto_rawDescGZIP(), []int{2} } +func (x *ServerConfig) GetPacketEncoding() packetaddr.PacketAddrType { + if x != nil { + return x.PacketEncoding + } + return packetaddr.PacketAddrType(0) +} + var File_proxy_hysteria2_config_proto protoreflect.FileDescriptor var file_proxy_hysteria2_config_proto_rawDesc = []byte{ 0x0a, 0x1c, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x1a, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, - 0x2e, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 0x1a, 0x1a, 0x63, 0x6f, 0x6d, 0x6d, - 0x6f, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 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, 0x1a, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 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, 0x22, 0x09, 0x0a, 0x07, 0x41, - 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x6d, 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, 0x3a, 0x19, 0x82, 0xb5, 0x18, 0x15, - 0x0a, 0x08, 0x6f, 0x75, 0x74, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x12, 0x09, 0x68, 0x79, 0x73, 0x74, - 0x65, 0x72, 0x69, 0x61, 0x32, 0x22, 0x28, 0x0a, 0x0c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, - 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x3a, 0x18, 0x82, 0xb5, 0x18, 0x14, 0x0a, 0x07, 0x69, 0x6e, 0x62, - 0x6f, 0x75, 0x6e, 0x64, 0x12, 0x09, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 0x42, - 0x6f, 0x0a, 0x1e, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, - 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, - 0x32, 0x50, 0x01, 0x5a, 0x2e, 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, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, - 0x69, 0x61, 0x32, 0xaa, 0x02, 0x1a, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65, - 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x48, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x2e, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 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, 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, 0x1a, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 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, 0x22, 0x09, 0x0a, 0x07, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x6d, + 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, 0x3a, 0x19, 0x82, 0xb5, 0x18, 0x15, 0x0a, 0x08, 0x6f, 0x75, 0x74, 0x62, 0x6f, 0x75, + 0x6e, 0x64, 0x12, 0x09, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 0x22, 0x7c, 0x0a, + 0x0c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x52, 0x0a, + 0x0f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x5f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, + 0x18, 0x01, 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, 0x18, 0x82, 0xb5, 0x18, 0x14, 0x0a, 0x07, 0x69, 0x6e, 0x62, 0x6f, 0x75, 0x6e, 0x64, + 0x12, 0x09, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 0x42, 0x6f, 0x0a, 0x1e, 0x63, + 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x78, 0x79, 0x2e, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 0x50, 0x01, 0x5a, + 0x2e, 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, 0x68, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 0xaa, + 0x02, 0x1a, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65, 0x2e, 0x50, 0x72, 0x6f, + 0x78, 0x79, 0x2e, 0x48, 0x79, 0x73, 0x74, 0x65, 0x72, 0x69, 0x61, 0x32, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -190,14 +206,16 @@ var file_proxy_hysteria2_config_proto_goTypes = []any{ (*ClientConfig)(nil), // 1: v2ray.core.proxy.hysteria2.ClientConfig (*ServerConfig)(nil), // 2: v2ray.core.proxy.hysteria2.ServerConfig (*protocol.ServerEndpoint)(nil), // 3: v2ray.core.common.protocol.ServerEndpoint + (packetaddr.PacketAddrType)(0), // 4: v2ray.core.net.packetaddr.PacketAddrType } var file_proxy_hysteria2_config_proto_depIdxs = []int32{ 3, // 0: v2ray.core.proxy.hysteria2.ClientConfig.server:type_name -> v2ray.core.common.protocol.ServerEndpoint - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name + 4, // 1: v2ray.core.proxy.hysteria2.ServerConfig.packet_encoding:type_name -> v2ray.core.net.packetaddr.PacketAddrType + 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_hysteria2_config_proto_init() } diff --git a/proxy/hysteria2/config.proto b/proxy/hysteria2/config.proto index bde30ff2885..ccda75b5e80 100644 --- a/proxy/hysteria2/config.proto +++ b/proxy/hysteria2/config.proto @@ -6,11 +6,10 @@ option go_package = "github.com/v2fly/v2ray-core/v5/proxy/hysteria2"; option java_package = "com.v2ray.core.proxy.hysteria2"; option java_multiple_files = true; -import "common/protocol/user.proto"; +import "common/net/packetaddr/config.proto"; import "common/protocol/server_spec.proto"; import "common/protoext/extensions.proto"; - message Account { } @@ -24,4 +23,6 @@ message ClientConfig { message ServerConfig { option (v2ray.core.common.protoext.message_opt).type = "inbound"; option (v2ray.core.common.protoext.message_opt).short_name = "hysteria2"; + + v2ray.core.net.packetaddr.PacketAddrType packet_encoding = 1; } diff --git a/proxy/hysteria2/protocol.go b/proxy/hysteria2/protocol.go index 74e1586328a..d78737a992d 100644 --- a/proxy/hysteria2/protocol.go +++ b/proxy/hysteria2/protocol.go @@ -185,3 +185,26 @@ func (r *PacketReader) ReadMultiBufferWithMetadata() (*PacketPayload, error) { b := buf.FromBytes(data) return &PacketPayload{Target: *dest, Buffer: buf.MultiBuffer{b}}, nil } + +type PacketConnectionReader struct { + reader *PacketReader + payload *PacketPayload +} + +func (r *PacketConnectionReader) ReadFrom(p []byte) (n int, addr net.Addr, err error) { + if r.payload == nil || r.payload.Buffer.IsEmpty() { + r.payload, err = r.reader.ReadMultiBufferWithMetadata() + if err != nil { + return + } + } + + addr = &net.UDPAddr{ + IP: r.payload.Target.Address.IP(), + Port: int(r.payload.Target.Port), + } + + r.payload.Buffer, n = buf.SplitFirstBytes(r.payload.Buffer, p) + + return +} diff --git a/proxy/hysteria2/server.go b/proxy/hysteria2/server.go index ce283c4d68d..cbefadc37dc 100644 --- a/proxy/hysteria2/server.go +++ b/proxy/hysteria2/server.go @@ -13,6 +13,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" + "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" udp_proto "github.com/v2fly/v2ray-core/v5/common/protocol/udp" "github.com/v2fly/v2ray-core/v5/common/session" "github.com/v2fly/v2ray-core/v5/common/signal" @@ -32,14 +33,16 @@ func init() { // Server is an inbound connection handler that handles messages in protocol. type Server struct { - policyManager policy.Manager + policyManager policy.Manager + packetEncoding packetaddr.PacketAddrType } // NewServer creates a new inbound handler. func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { v := core.MustFromContext(ctx) server := &Server{ - policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager), + policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager), + packetEncoding: config.PacketEncoding, } return server, nil } @@ -167,8 +170,14 @@ func (s *Server) handleConnection(ctx context.Context, sessionPolicy policy.Sess return nil } -func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReader, clientWriter *PacketWriter, dispatcher routing.Dispatcher) error { // {{{ +func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReader, clientWriter *PacketWriter, dispatcher routing.Dispatcher) error { udpDispatcherConstructor := udp.NewSplitDispatcher + switch s.packetEncoding { + case packetaddr.PacketAddrType_None: + 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 {