From cefddb0aa44151d85a2e4ec32c8134cac7dd79fa Mon Sep 17 00:00:00 2001 From: mkmark Date: Thu, 18 Jul 2024 15:51:27 +0800 Subject: [PATCH] feat: packet_encoding for v4 config --- infra/conf/v4/shadowsocks.go | 23 ++++++++++++++++------- infra/conf/v4/socks.go | 22 ++++++++++++++++------ infra/conf/v4/socks_test.go | 9 ++++++--- infra/conf/v4/trojan.go | 15 ++++++++++++--- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/infra/conf/v4/shadowsocks.go b/infra/conf/v4/shadowsocks.go index ecfc506e5..e58458f61 100644 --- a/infra/conf/v4/shadowsocks.go +++ b/infra/conf/v4/shadowsocks.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" @@ -10,13 +11,14 @@ import ( ) type ShadowsocksServerConfig struct { - Cipher string `json:"method"` - Password string `json:"password"` - UDP bool `json:"udp"` - Level byte `json:"level"` - Email string `json:"email"` - NetworkList *cfgcommon.NetworkList `json:"network"` - IVCheck bool `json:"ivCheck"` + Cipher string `json:"method"` + Password string `json:"password"` + UDP bool `json:"udp"` + Level byte `json:"level"` + Email string `json:"email"` + NetworkList *cfgcommon.NetworkList `json:"network"` + IVCheck bool `json:"ivCheck"` + PacketEncoding string `json:"packetEncoding"` } func (v *ShadowsocksServerConfig) Build() (proto.Message, error) { @@ -42,6 +44,13 @@ func (v *ShadowsocksServerConfig) Build() (proto.Message, error) { Account: serial.ToTypedMessage(account), } + switch v.PacketEncoding { + case "Packet": + config.PacketEncoding = packetaddr.PacketAddrType_Packet + case "", "None": + config.PacketEncoding = packetaddr.PacketAddrType_None + } + return config, nil } diff --git a/infra/conf/v4/socks.go b/infra/conf/v4/socks.go index 2a8754195..367eb3613 100644 --- a/infra/conf/v4/socks.go +++ b/infra/conf/v4/socks.go @@ -6,6 +6,7 @@ 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" @@ -30,12 +31,13 @@ const ( ) type SocksServerConfig struct { - AuthMethod string `json:"auth"` - Accounts []*SocksAccount `json:"accounts"` - UDP bool `json:"udp"` - Host *cfgcommon.Address `json:"ip"` - Timeout uint32 `json:"timeout"` - UserLevel uint32 `json:"userLevel"` + AuthMethod string `json:"auth"` + Accounts []*SocksAccount `json:"accounts"` + UDP bool `json:"udp"` + Host *cfgcommon.Address `json:"ip"` + Timeout uint32 `json:"timeout"` + UserLevel uint32 `json:"userLevel"` + PacketEncoding string `json:"packetEncoding"` } func (v *SocksServerConfig) Build() (proto.Message, error) { @@ -64,6 +66,14 @@ func (v *SocksServerConfig) Build() (proto.Message, error) { config.Timeout = v.Timeout config.UserLevel = v.UserLevel + + switch v.PacketEncoding { + case "Packet": + config.PacketEncoding = packetaddr.PacketAddrType_Packet + case "", "None": + config.PacketEncoding = packetaddr.PacketAddrType_None + } + return config, nil } diff --git a/infra/conf/v4/socks_test.go b/infra/conf/v4/socks_test.go index 1d4a881a7..3f6fc4296 100644 --- a/infra/conf/v4/socks_test.go +++ b/infra/conf/v4/socks_test.go @@ -4,6 +4,7 @@ import ( "testing" "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/serial" "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon" @@ -30,7 +31,8 @@ func TestSocksInboundConfig(t *testing.T) { "udp": false, "ip": "127.0.0.1", "timeout": 5, - "userLevel": 1 + "userLevel": 1, + "packetEncoding": "Packet" }`, Parser: testassist.LoadJSON(creator), Output: &socks.ServerConfig{ @@ -44,8 +46,9 @@ func TestSocksInboundConfig(t *testing.T) { Ip: []byte{127, 0, 0, 1}, }, }, - Timeout: 5, - UserLevel: 1, + Timeout: 5, + UserLevel: 1, + PacketEncoding: packetaddr.PacketAddrType_Packet, }, }, }) diff --git a/infra/conf/v4/trojan.go b/infra/conf/v4/trojan.go index 56551da24..ac339e803 100644 --- a/infra/conf/v4/trojan.go +++ b/infra/conf/v4/trojan.go @@ -11,6 +11,7 @@ import ( "github.com/golang/protobuf/proto" "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/serial" "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon" @@ -91,9 +92,10 @@ type TrojanUserConfig struct { // TrojanServerConfig is Inbound configuration type TrojanServerConfig struct { - Clients []*TrojanUserConfig `json:"clients"` - Fallback json.RawMessage `json:"fallback"` - Fallbacks []*TrojanInboundFallback `json:"fallbacks"` + Clients []*TrojanUserConfig `json:"clients"` + Fallback json.RawMessage `json:"fallback"` + Fallbacks []*TrojanInboundFallback `json:"fallbacks"` + PacketEncoding string `json:"packetEncoding"` } // Build implements Buildable @@ -167,5 +169,12 @@ func (c *TrojanServerConfig) Build() (proto.Message, error) { } } + switch c.PacketEncoding { + case "Packet": + config.PacketEncoding = packetaddr.PacketAddrType_Packet + case "", "None": + config.PacketEncoding = packetaddr.PacketAddrType_None + } + return config, nil }