From 5780f0df90f5ea7724c00ed6e13127dc1be3d816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=90=B2=93=F0=90=B3=9B=F0=90=B3=AA=F0=90=B3=82?= =?UTF-8?q?=F0=90=B3=90=20=F0=90=B2=80=F0=90=B3=A2=F0=90=B3=A6=F0=90=B3=AB?= =?UTF-8?q?=F0=90=B3=A2=20=F0=90=B2=A5=F0=90=B3=94=F0=90=B3=9B=F0=90=B3=AA?= =?UTF-8?q?=F0=90=B3=8C=F0=90=B3=91=F0=90=B3=96=F0=90=B3=87?= <26771058+KobeArthurScofield@users.noreply.github.com> Date: Sat, 28 Nov 2026 00:50:08 +0800 Subject: [PATCH] Config: Remove legacy nested format in every proxy outbound (#7505) https://github.com/XTLS/Xray-core/pull/7364#issuecomment-7142063028 --- common/reflect/marshal_test.go | 16 +-- infra/conf/http.go | 87 ++++-------- infra/conf/shadowsocks.go | 70 +++------- infra/conf/socks.go | 85 ++++-------- infra/conf/socks_test.go | 29 ---- infra/conf/transport_test.go | 4 +- infra/conf/trojan.go | 81 +++-------- infra/conf/vless.go | 237 +++++++++++++-------------------- infra/conf/vless_test.go | 35 ----- infra/conf/vmess.go | 101 +++++--------- infra/conf/vmess_test.go | 36 ----- infra/conf/xray.go | 2 +- 12 files changed, 224 insertions(+), 559 deletions(-) diff --git a/common/reflect/marshal_test.go b/common/reflect/marshal_test.go index a343b8ff4..fd313d097 100644 --- a/common/reflect/marshal_test.go +++ b/common/reflect/marshal_test.go @@ -191,18 +191,10 @@ func getConfig() string { { "protocol": "vless", "settings": { - "vnext": [ - { - "address": "1.2.3.4", - "port": 1234, - "users": [ - { - "id": "4784f9b8-a879-4fec-9718-ebddefa47750", - "encryption": "none" - } - ] - } - ] + "address": "1.2.3.4", + "port": 1234, + "id": "4784f9b8-a879-4fec-9718-ebddefa47750", + "encryption": "none" }, "tag": "XHTTP_IN", "streamSettings": { diff --git a/infra/conf/http.go b/infra/conf/http.go index 3e1482cb5..4e29f6088 100644 --- a/infra/conf/http.go +++ b/infra/conf/http.go @@ -1,9 +1,6 @@ package conf import ( - "encoding/json" - - "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/serial" "github.com/xtls/xray-core/proxy/http" @@ -49,73 +46,37 @@ func (c *HTTPServerConfig) Build() (proto.Message, error) { return config, nil } -type HTTPRemoteConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Users []json.RawMessage `json:"users"` -} - type HTTPClientConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level uint32 `json:"level"` - Email string `json:"email"` - Username string `json:"user"` - Password string `json:"pass"` - Servers []*HTTPRemoteConfig `json:"servers"` - Headers map[string]string `json:"headers"` + Address *Address `json:"address"` + Port uint16 `json:"port"` + Level uint32 `json:"level"` + Email string `json:"email"` + Username string `json:"user"` + Password string `json:"pass"` + Headers map[string]string `json:"headers"` } func (v *HTTPClientConfig) Build() (proto.Message, error) { config := new(http.ClientConfig) - if v.Address != nil { - v.Servers = []*HTTPRemoteConfig{ - { - Address: v.Address, - Port: v.Port, - }, - } - if len(v.Username) > 0 { - v.Servers[0].Users = []json.RawMessage{{}} - } + + server := &protocol.ServerEndpoint{ + Address: v.Address.Build(), + Port: uint32(v.Port), } - if len(v.Servers) != 1 { - return nil, errors.New(`HTTP settings: "servers" should have one and only one member. Multiple endpoints in "servers" should use multiple HTTP outbounds and routing balancer instead`) - } - for _, serverConfig := range v.Servers { - if len(serverConfig.Users) > 1 { - return nil, errors.New(`HTTP servers: "users" should have one member at most. Multiple members in "users" should use multiple HTTP outbounds and routing balancer instead`) - } - server := &protocol.ServerEndpoint{ - Address: serverConfig.Address.Build(), - Port: uint32(serverConfig.Port), - } - for _, rawUser := range serverConfig.Users { - user := new(protocol.User) - if v.Address != nil { - user.Level = v.Level - user.Email = v.Email - } else { - if err := json.Unmarshal(rawUser, user); err != nil { - return nil, errors.New("failed to parse HTTP user").Base(err) - } - } - account := new(HTTPAccount) - if v.Address != nil { - account.Username = v.Username - account.Password = v.Password - } else { - if err := json.Unmarshal(rawUser, account); err != nil { - return nil, errors.New("failed to parse HTTP account").Base(err) - } - } - user.Account = serial.ToTypedMessage(account.Build()) - server.User = user - break - } - config.Server = server - break + if len(v.Username) > 0 { + account := &HTTPAccount{ + Username: v.Username, + Password: v.Password, + } + user := &protocol.User{ + Level: v.Level, + Email: v.Email, + Account: serial.ToTypedMessage(account.Build()), + } + server.User = user } + + config.Server = server config.Header = make([]*http.Header, 0, 32) for key, value := range v.Headers { config.Header = append(config.Header, &http.Header{ diff --git a/infra/conf/shadowsocks.go b/infra/conf/shadowsocks.go index 13862dd83..cd07cd6d7 100644 --- a/infra/conf/shadowsocks.go +++ b/infra/conf/shadowsocks.go @@ -177,7 +177,7 @@ func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) { return config, nil } -type ShadowsocksServerTarget struct { +type ShadowsocksClientConfig struct { Address *Address `json:"address"` Port uint16 `json:"port"` Level byte `json:"level"` @@ -186,69 +186,43 @@ type ShadowsocksServerTarget struct { Password string `json:"password"` } -type ShadowsocksClientConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level byte `json:"level"` - Email string `json:"email"` - Cipher string `json:"method"` - Password string `json:"password"` - Servers []*ShadowsocksServerTarget `json:"servers"` -} - func (v *ShadowsocksClientConfig) Build() (proto.Message, error) { errors.PrintNonRemovalDeprecatedFeatureWarning("Shadowsocks (with no Forward Secrecy, etc.)", "VLESS Encryption") - if v.Address != nil { - v.Servers = []*ShadowsocksServerTarget{ - { - Address: v.Address, - Port: v.Port, - Level: v.Level, - Email: v.Email, - Cipher: v.Cipher, - Password: v.Password, - }, - } - } - if len(v.Servers) != 1 { - return nil, errors.New(`Shadowsocks settings: "servers" should have one and only one member. Multiple endpoints in "servers" should use multiple Shadowsocks outbounds and routing balancer instead`) - } - - server := v.Servers[0] - if server.Address == nil { + if v.Address == nil { return nil, errors.New("Shadowsocks server address is not set.") } - if server.Port == 0 { + if v.Port == 0 { return nil, errors.New("Invalid Shadowsocks port.") } - if server.Password == "" { + if v.Password == "" { return nil, errors.New("Shadowsocks password is not specified.") } - if _, err := shadowsocks_2022.GetCipherMethod(server.Cipher); err == nil { - config := new(shadowsocks_2022.ClientConfig) - config.Address = server.Address.Build() - config.Port = uint32(server.Port) - config.Method = server.Cipher - config.Key = server.Password + if _, err := shadowsocks_2022.GetCipherMethod(v.Cipher); err == nil { + config := &shadowsocks_2022.ClientConfig{ + Address: v.Address.Build(), + Port: uint32(v.Port), + Method: v.Cipher, + Key: v.Password, + } return config, nil } + config := new(shadowsocks.ClientConfig) - account := &shadowsocks.Account{ - Password: server.Password, - } - account.CipherType = cipherFromString(server.Cipher) - if account.CipherType == shadowsocks.CipherType_UNKNOWN { - return nil, errors.New("unknown cipher method: ", server.Cipher) + if cipherFromString(v.Cipher) == shadowsocks.CipherType_UNKNOWN { + return nil, errors.New("unknown cipher method: ", v.Cipher) } ss := &protocol.ServerEndpoint{ - Address: server.Address.Build(), - Port: uint32(server.Port), + Address: v.Address.Build(), + Port: uint32(v.Port), User: &protocol.User{ - Level: uint32(server.Level), - Email: server.Email, - Account: serial.ToTypedMessage(account), + Level: uint32(v.Level), + Email: v.Email, + Account: serial.ToTypedMessage(&shadowsocks.Account{ + Password: v.Password, + CipherType: cipherFromString(v.Cipher), + }), }, } config.Server = ss diff --git a/infra/conf/socks.go b/infra/conf/socks.go index 33e290b7b..026498cb0 100644 --- a/infra/conf/socks.go +++ b/infra/conf/socks.go @@ -1,9 +1,6 @@ package conf import ( - "encoding/json" - - "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/serial" "github.com/xtls/xray-core/proxy/socks" @@ -67,71 +64,35 @@ func (v *SocksServerConfig) Build() (proto.Message, error) { return config, nil } -type SocksRemoteConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Users []json.RawMessage `json:"users"` -} - type SocksClientConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level uint32 `json:"level"` - Email string `json:"email"` - Username string `json:"user"` - Password string `json:"pass"` - Servers []*SocksRemoteConfig `json:"servers"` + Address *Address `json:"address"` + Port uint16 `json:"port"` + Level uint32 `json:"level"` + Email string `json:"email"` + Username string `json:"user"` + Password string `json:"pass"` } func (v *SocksClientConfig) Build() (proto.Message, error) { config := new(socks.ClientConfig) - if v.Address != nil { - v.Servers = []*SocksRemoteConfig{ - { - Address: v.Address, - Port: v.Port, - }, - } - if len(v.Username) > 0 { - v.Servers[0].Users = []json.RawMessage{{}} - } + + server := &protocol.ServerEndpoint{ + Address: v.Address.Build(), + Port: uint32(v.Port), } - if len(v.Servers) != 1 { - return nil, errors.New(`SOCKS settings: "servers" should have one and only one member. Multiple endpoints in "servers" should use multiple SOCKS outbounds and routing balancer instead`) - } - for _, serverConfig := range v.Servers { - if len(serverConfig.Users) > 1 { - return nil, errors.New(`SOCKS servers: "users" should have one member at most. Multiple members in "users" should use multiple SOCKS outbounds and routing balancer instead`) - } - server := &protocol.ServerEndpoint{ - Address: serverConfig.Address.Build(), - Port: uint32(serverConfig.Port), - } - for _, rawUser := range serverConfig.Users { - user := new(protocol.User) - if v.Address != nil { - user.Level = v.Level - user.Email = v.Email - } else { - if err := json.Unmarshal(rawUser, user); err != nil { - return nil, errors.New("failed to parse Socks user").Base(err) - } - } - account := new(SocksAccount) - if v.Address != nil { - account.Username = v.Username - account.Password = v.Password - } else { - if err := json.Unmarshal(rawUser, account); err != nil { - return nil, errors.New("failed to parse socks account").Base(err) - } - } - user.Account = serial.ToTypedMessage(account.Build()) - server.User = user - break - } - config.Server = server - break + if len(v.Username) > 0 { + account := &SocksAccount{ + Username: v.Username, + Password: v.Password, + } + user := &protocol.User{ + Level: v.Level, + Email: v.Email, + Account: serial.ToTypedMessage(account.Build()), + } + server.User = user } + + config.Server = server return config, nil } diff --git a/infra/conf/socks_test.go b/infra/conf/socks_test.go index d94802ecc..b837fe09d 100644 --- a/infra/conf/socks_test.go +++ b/infra/conf/socks_test.go @@ -53,35 +53,6 @@ func TestSocksOutboundConfig(t *testing.T) { } runMultiTestCase(t, []TestCase{ - { - Input: `{ - "servers": [{ - "address": "127.0.0.1", - "port": 1234, - "users": [ - {"user": "test user", "pass": "test pass", "email": "test@email.com"} - ] - }] - }`, - Parser: loadJSON(creator), - Output: &socks.ClientConfig{ - Server: &protocol.ServerEndpoint{ - Address: &net.IPOrDomain{ - Address: &net.IPOrDomain_Ip{ - Ip: []byte{127, 0, 0, 1}, - }, - }, - Port: 1234, - User: &protocol.User{ - Email: "test@email.com", - Account: serial.ToTypedMessage(&socks.Account{ - Username: "test user", - Password: "test pass", - }), - }, - }, - }, - }, { Input: `{ "address": "127.0.0.1", diff --git a/infra/conf/transport_test.go b/infra/conf/transport_test.go index 6fac69483..b496b1e04 100644 --- a/infra/conf/transport_test.go +++ b/infra/conf/transport_test.go @@ -532,7 +532,7 @@ func TestXDriveOutboundConfig(t *testing.T) { `{"protocol": "vless", "settings": {` + xdriveServer + `, ` + xdriveEnc + `, "multiplex": {"concurrency": 8}}, ` + xdriveStream + `}`, `{"protocol": "vless", "settings": {` + xdriveServer + `, ` + xdriveEnc + `, "multiplex": {"concurrency": 8, "xudpConcurrency": 8, "xudpProxyUDP443": "allow"}}, ` + xdriveStream + `}`, `{"protocol": "vless", "settings": {` + xdriveServer + `, ` + xdriveEnc + `, "reverse": {"tag": "r"}}, ` + xdriveStream + `}`, - `{"protocol": "vless", "settings": {"vnext": [{"address": "127.0.0.1", "port": 443, "users": [{` + xdriveID + `, ` + xdriveEnc + `}]}], "multiplex": {"concurrency": 8}}, ` + xdriveStream + `}`, + `{"protocol": "vless", "settings": {"address": "127.0.0.1", "port": 443, ` + xdriveID + `, ` + xdriveEnc + `, "multiplex": {"concurrency": 8}}, ` + xdriveStream + `}`, } { if err := build(input); err != nil { t.Errorf("%s: %v", input, err) @@ -543,7 +543,7 @@ func TestXDriveOutboundConfig(t *testing.T) { {`{"protocol": "vless", "settings": {"address": "example.com", "port": 443, ` + xdriveID + `, "encryption": "none", "multiplex": {"concurrency": 8}}, ` + xdriveStream + `}`, `"encryption"`}, {`{"protocol": "vless", "settings": {` + xdriveServer + `, ` + xdriveEnc + `, "multiplex": {"concurrency": 8, "xudpProxyUDP443": "skip"}}, ` + xdriveStream + `}`, `"xudpProxyUDP443":"skip"`}, {`{"protocol": "vless", "settings": {` + xdriveServer + `, ` + xdriveEnc + `, "flow": "xtls-rprx-vision", "multiplex": {"concurrency": 8}}, ` + xdriveStream + `}`, `"flow"`}, - {`{"protocol": "vless", "settings": {"vnext": [{"address": "127.0.0.1", "port": 443, "users": [{` + xdriveID + `, ` + xdriveEnc + `, "flow": "xtls-rprx-vision"}]}], "multiplex": {"concurrency": 8}}, ` + xdriveStream + `}`, `"flow"`}, + {`{"protocol": "vless", "settings": {"address": "127.0.0.1", "port": 443, ` + xdriveID + `, ` + xdriveEnc + `, "flow": "xtls-rprx-vision", "multiplex": {"concurrency": 8}}, ` + xdriveStream + `}`, `"flow"`}, {`{"protocol": "vless", "settings": {` + xdriveServer + `, ` + xdriveEnc + `, "testpre": 1, "multiplex": {"concurrency": 8}}, ` + xdriveStream + `}`, `"testpre"`}, {`{"protocol": "vmess", "settings": {` + xdriveServer + `}, ` + xdriveStream + `, "mux": {"enabled": true}}`, "VLESS"}, {`{"protocol": "trojan", "settings": {"address": "example.com", "port": 443, "password": "p"}, ` + xdriveStream + `, "mux": {"enabled": true}}`, "VLESS"}, diff --git a/infra/conf/trojan.go b/infra/conf/trojan.go index 084813720..45ba0ef3c 100644 --- a/infra/conf/trojan.go +++ b/infra/conf/trojan.go @@ -17,76 +17,41 @@ import ( "google.golang.org/protobuf/proto" ) -// TrojanServerTarget is configuration of a single trojan server -type TrojanServerTarget struct { +// TrojanClientConfig is configuration of trojan servers +type TrojanClientConfig struct { Address *Address `json:"address"` Port uint16 `json:"port"` Level byte `json:"level"` Email string `json:"email"` Password string `json:"password"` - Flow string `json:"flow"` -} - -// TrojanClientConfig is configuration of trojan servers -type TrojanClientConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level byte `json:"level"` - Email string `json:"email"` - Password string `json:"password"` - Flow string `json:"flow"` - Servers []*TrojanServerTarget `json:"servers"` } // Build implements Buildable func (c *TrojanClientConfig) Build() (proto.Message, error) { errors.PrintNonRemovalDeprecatedFeatureWarning("Trojan (with no Flow, etc.)", "VLESS with Flow & Seed") - if c.Address != nil { - c.Servers = []*TrojanServerTarget{ - { - Address: c.Address, - Port: c.Port, - Level: c.Level, - Email: c.Email, - Password: c.Password, - Flow: c.Flow, - }, - } - } - if len(c.Servers) != 1 { - return nil, errors.New(`Trojan settings: "servers" should have one and only one member. Multiple endpoints in "servers" should use multiple Trojan outbounds and routing balancer instead`) - } - config := &trojan.ClientConfig{} - for _, rec := range c.Servers { - if rec.Address == nil { - return nil, errors.New("Trojan server address is not set.") - } - if rec.Port == 0 { - return nil, errors.New("Invalid Trojan port.") - } - if rec.Password == "" { - return nil, errors.New("Trojan password is not specified.") - } - if rec.Flow != "" { - return nil, errors.PrintRemovedFeatureError(`Flow for Trojan`, ``) - } + if c.Address == nil { + return nil, errors.New("Trojan server address is not set.") + } + if c.Port == 0 { + return nil, errors.New("Invalid Trojan port.") + } + if c.Password == "" { + return nil, errors.New("Trojan password is not specified.") + } - config.Server = &protocol.ServerEndpoint{ - Address: rec.Address.Build(), - Port: uint32(rec.Port), - User: &protocol.User{ - Level: uint32(rec.Level), - Email: rec.Email, - Account: serial.ToTypedMessage(&trojan.Account{ - Password: rec.Password, - }), - }, - } - - break + config.Server = &protocol.ServerEndpoint{ + Address: c.Address.Build(), + Port: uint32(c.Port), + User: &protocol.User{ + Level: uint32(c.Level), + Email: c.Email, + Account: serial.ToTypedMessage(&trojan.Account{ + Password: c.Password, + }), + }, } return config, nil @@ -107,7 +72,6 @@ type TrojanUserConfig struct { Password string `json:"password"` Level byte `json:"level"` Email string `json:"email"` - Flow string `json:"flow"` } // TrojanServerConfig is Inbound configuration @@ -131,9 +95,6 @@ func (c *TrojanServerConfig) Build() (proto.Message, error) { processClient := func(idx int) error { rawUser := c.Users[idx] - if rawUser.Flow != "" { - return errors.PrintRemovedFeatureError(`Flow for Trojan`, ``) - } config.Users[idx] = &protocol.User{ Level: uint32(rawUser.Level), diff --git a/infra/conf/vless.go b/infra/conf/vless.go index dcc355593..724f81de4 100644 --- a/infra/conf/vless.go +++ b/infra/conf/vless.go @@ -256,26 +256,19 @@ func (c *MultiplexConfig) Build() (*vless.Multiplex, error) { }) } -type VLessOutboundVnext struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Users []json.RawMessage `json:"users"` -} - type VLessOutboundConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level uint32 `json:"level"` - Email string `json:"email"` - Id string `json:"id"` - Flow string `json:"flow"` - Seed string `json:"seed"` - Encryption string `json:"encryption"` - Multiplex *MultiplexConfig `json:"multiplex"` - Reverse *VLessReverseConfig `json:"reverse"` - Testpre uint32 `json:"testpre"` - Testseed []uint32 `json:"testseed"` - Vnext []*VLessOutboundVnext `json:"vnext"` + Address *Address `json:"address"` + Port uint16 `json:"port"` + Level uint32 `json:"level"` + Email string `json:"email"` + Id string `json:"id"` + Flow string `json:"flow"` + Seed string `json:"seed"` + Encryption string `json:"encryption"` + Multiplex *MultiplexConfig `json:"multiplex"` + Reverse *VLessReverseConfig `json:"reverse"` + Testpre uint32 `json:"testpre"` + Testseed []uint32 `json:"testseed"` } // Build implements Buildable @@ -286,130 +279,92 @@ func (c *VLessOutboundConfig) Build() (proto.Message, error) { } config := &outbound.Config{Multiplex: multiplex} - if c.Address != nil { - c.Vnext = []*VLessOutboundVnext{ - { - Address: c.Address, - Port: c.Port, - Users: []json.RawMessage{{}}, - }, - } + if c.Address == nil { + return nil, errors.New(`VLESS: "address" is not set`) } - if len(c.Vnext) != 1 { - return nil, errors.New(`VLESS settings: "vnext" should have one and only one member. Multiple endpoints in "vnext" should use multiple VLESS outbounds and routing balancer instead`) - } - for _, rec := range c.Vnext { - if rec.Address == nil { - return nil, errors.New(`VLESS vnext: "address" is not set`) - } - if len(rec.Users) != 1 { - return nil, errors.New(`VLESS vnext: "users" should have one and only one member. Multiple members in "users" should use multiple VLESS outbounds and routing balancer instead`) - } - spec := &protocol.ServerEndpoint{ - Address: rec.Address.Build(), - Port: uint32(rec.Port), - } - for _, rawUser := range rec.Users { - user := new(protocol.User) - if c.Address != nil { - user.Level = c.Level - user.Email = c.Email - } else { - if err := json.Unmarshal(rawUser, user); err != nil { - return nil, errors.New(`VLESS users: invalid user`).Base(err) - } - } - account := new(vless.Account) - if c.Address != nil { - account.Id = c.Id - account.Flow = c.Flow - //account.Seed = c.Seed - account.Encryption = c.Encryption - if c.Reverse != nil { - rvs, err := c.Reverse.Build() - if err != nil { - return nil, err - } - account.Reverse = rvs - } - account.Testpre = c.Testpre - account.Testseed = c.Testseed - } else { - if err := json.Unmarshal(rawUser, account); err != nil { - return nil, errors.New(`VLESS users: invalid user`).Base(err) - } - // validateOutboundTransportSecurity needs to see these - c.Encryption = account.Encryption - c.Address = rec.Address - if account.Reverse != nil { // may not be reached: error json unmarshal - return nil, errors.New(`VLESS users: please use simplified outbound's config style to use "reverse"`) - } - } - - u, err := uuid.ParseString(account.Id) - if err != nil { - return nil, err - } - account.Id = u.String() - - switch account.Flow { - case "": - case vless.XRV, vless.XRV + "-udp443": - default: - return nil, errors.New(`VLESS users: "flow" doesn't support "` + account.Flow + `" in this version`) - } - - if !func() bool { - s := strings.Split(account.Encryption, ".") - if len(s) < 4 || s[0] != "mlkem768x25519plus" { - return false - } - switch s[1] { - case "native": - case "xorpub": - account.XorMode = 1 - case "random": - account.XorMode = 2 - default: - return false - } - switch s[2] { - case "1rtt": - case "0rtt": - account.Seconds = 1 - default: - return false - } - padding := 0 - for _, r := range s[3:] { - if len(r) < 20 { - padding += len(r) + 1 - continue - } - if b, _ := base64.RawURLEncoding.DecodeString(r); len(b) != 32 && len(b) != 1184 { - return false - } - } - account.Encryption = account.Encryption[27+len(s[2]):] - if padding > 0 { - account.Padding = account.Encryption[:padding-1] - account.Encryption = account.Encryption[padding:] - } - return true - }() && account.Encryption != "none" { - if account.Encryption == "" { - return nil, errors.New(`VLESS users: please add/set "encryption":"none" for every user`) - } - return nil, errors.New(`VLESS users: unsupported "encryption": ` + account.Encryption) - } - - user.Account = serial.ToTypedMessage(account) - spec.User = user - break - } - config.Vnext = spec - break + uid, err := uuid.ParseString(c.Id) + if err != nil { + return nil, err } + switch c.Flow { + case "": + case vless.XRV, vless.XRV + "-udp443": + default: + return nil, errors.New(`VLESS users: "flow" doesn't support "` + c.Flow + `" in this version`) + } + + spec := &protocol.ServerEndpoint{ + Address: c.Address.Build(), + Port: uint32(c.Port), + } + user := &protocol.User{ + Level: c.Level, + Email: c.Email, + } + account := &vless.Account{ + Id: uid.String(), + Flow: c.Flow, + // Seed: c.Seed, + Encryption: c.Encryption, + Testpre: c.Testpre, + Testseed: c.Testseed, + } + if c.Reverse != nil { + rvs, err := c.Reverse.Build() + if err != nil { + return nil, err + } + account.Reverse = rvs + } + + if !func() bool { + s := strings.Split(account.Encryption, ".") + if len(s) < 4 || s[0] != "mlkem768x25519plus" { + return false + } + switch s[1] { + case "native": + case "xorpub": + account.XorMode = 1 + case "random": + account.XorMode = 2 + default: + return false + } + switch s[2] { + case "1rtt": + case "0rtt": + account.Seconds = 1 + default: + return false + } + padding := 0 + for _, r := range s[3:] { + if len(r) < 20 { + padding += len(r) + 1 + continue + } + if b, _ := base64.RawURLEncoding.DecodeString(r); len(b) != 32 && len(b) != 1184 { + return false + } + } + account.Encryption = account.Encryption[27+len(s[2]):] + if padding > 0 { + account.Padding = account.Encryption[:padding-1] + account.Encryption = account.Encryption[padding:] + } + return true + }() && account.Encryption != "none" { + if account.Encryption == "" { + return nil, errors.New(`VLESS users: please add/set "encryption":"none" for every user`) + } + return nil, errors.New(`VLESS users: unsupported "encryption": ` + account.Encryption) + } + + user.Account = serial.ToTypedMessage(account) + spec.User = user + + config.Vnext = spec return config, nil } diff --git a/infra/conf/vless_test.go b/infra/conf/vless_test.go index 8c85fdeb5..cfb97b342 100644 --- a/infra/conf/vless_test.go +++ b/infra/conf/vless_test.go @@ -18,41 +18,6 @@ func TestVLessOutbound(t *testing.T) { } runMultiTestCase(t, []TestCase{ - { - Input: `{ - "vnext": [{ - "address": "example.com", - "port": 443, - "users": [ - { - "id": "27848739-7e62-4138-9fd3-098a63964b6b", - "flow": "xtls-rprx-vision-udp443", - "encryption": "none", - "level": 0 - } - ] - }] - }`, - Parser: loadJSON(creator), - Output: &outbound.Config{ - Vnext: &protocol.ServerEndpoint{ - Address: &net.IPOrDomain{ - Address: &net.IPOrDomain_Domain{ - Domain: "example.com", - }, - }, - Port: 443, - User: &protocol.User{ - Account: serial.ToTypedMessage(&vless.Account{ - Id: "27848739-7e62-4138-9fd3-098a63964b6b", - Flow: "xtls-rprx-vision-udp443", - Encryption: "none", - }), - Level: 0, - }, - }, - }, - }, { Input: `{ "address": "example.com", diff --git a/infra/conf/vmess.go b/infra/conf/vmess.go index 84938a67e..7c69dcc8c 100644 --- a/infra/conf/vmess.go +++ b/infra/conf/vmess.go @@ -102,22 +102,15 @@ func (c *VMessInboundConfig) Build() (proto.Message, error) { return config, nil } -type VMessOutboundTarget struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Users []json.RawMessage `json:"users"` -} - type VMessOutboundConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level uint32 `json:"level"` - Email string `json:"email"` - ID string `json:"id"` - Security string `json:"security"` - Experiments string `json:"experiments"` - Receivers []*VMessOutboundTarget `json:"vnext"` - Multiplex *MultiplexConfig `json:"multiplex"` + Address *Address `json:"address"` + Port uint16 `json:"port"` + Level uint32 `json:"level"` + Email string `json:"email"` + ID string `json:"id"` + Security string `json:"security"` + Experiments string `json:"experiments"` + Multiplex *MultiplexConfig `json:"multiplex"` } // Build implements Buildable @@ -129,62 +122,30 @@ func (c *VMessOutboundConfig) Build() (proto.Message, error) { return nil, err } config := &outbound.Config{Multiplex: multiplex} - if c.Address != nil { - c.Receivers = []*VMessOutboundTarget{ - { - Address: c.Address, - Port: c.Port, - Users: []json.RawMessage{{}}, - }, - } - } - if len(c.Receivers) != 1 { - return nil, errors.New(`VMess settings: "vnext" should have one and only one member. Multiple endpoints in "vnext" should use multiple VMess outbounds and routing balancer instead`) - } - for _, rec := range c.Receivers { - if len(rec.Users) != 1 { - return nil, errors.New(`VMess vnext: "users" should have one and only one member. Multiple members in "users" should use multiple VMess outbounds and routing balancer instead`) - } - if rec.Address == nil { - return nil, errors.New(`VMess vnext: "address" is not set`) - } - spec := &protocol.ServerEndpoint{ - Address: rec.Address.Build(), - Port: uint32(rec.Port), - } - for _, rawUser := range rec.Users { - user := new(protocol.User) - if c.Address != nil { - user.Level = c.Level - user.Email = c.Email - } else { - if err := json.Unmarshal(rawUser, user); err != nil { - return nil, errors.New("invalid VMess user").Base(err) - } - } - account := new(VMessAccount) - if c.Address != nil { - account.ID = c.ID - account.Security = c.Security - account.Experiments = c.Experiments - } else { - if err := json.Unmarshal(rawUser, account); err != nil { - return nil, errors.New("invalid VMess user").Base(err) - } - } - u, err := uuid.ParseString(account.ID) - if err != nil { - return nil, err - } - account.ID = u.String() - - user.Account = serial.ToTypedMessage(account.Build()) - spec.User = user - break - } - config.Receiver = spec - break + if c.Address == nil { + return nil, errors.New(`VMess: "address" is not set`) } + uid, err := uuid.ParseString(c.ID) + if err != nil { + return nil, err + } + + account := &VMessAccount{ + ID: uid.String(), + Security: c.Security, + Experiments: c.Experiments, + } + spec := &protocol.ServerEndpoint{ + Address: c.Address.Build(), + Port: uint32(c.Port), + User: &protocol.User{ + Level: c.Level, + Email: c.Email, + Account: serial.ToTypedMessage(account.Build()), + }, + } + + config.Receiver = spec return config, nil } diff --git a/infra/conf/vmess_test.go b/infra/conf/vmess_test.go index b94db83e1..6452354c1 100644 --- a/infra/conf/vmess_test.go +++ b/infra/conf/vmess_test.go @@ -18,42 +18,6 @@ func TestVMessOutbound(t *testing.T) { } runMultiTestCase(t, []TestCase{ - { - Input: `{ - "vnext": [{ - "address": "127.0.0.1", - "port": 80, - "users": [ - { - "id": "e641f5ad-9397-41e3-bf1a-e8740dfed019", - "email": "love@example.com", - "level": 255 - } - ] - }] - }`, - Parser: loadJSON(creator), - Output: &outbound.Config{ - Receiver: &protocol.ServerEndpoint{ - Address: &net.IPOrDomain{ - Address: &net.IPOrDomain_Ip{ - Ip: []byte{127, 0, 0, 1}, - }, - }, - Port: 80, - User: &protocol.User{ - Email: "love@example.com", - Level: 255, - Account: serial.ToTypedMessage(&vmess.Account{ - Id: "e641f5ad-9397-41e3-bf1a-e8740dfed019", - SecuritySettings: &protocol.SecurityConfig{ - Type: protocol.SecurityType_AUTO, - }, - }), - }, - }, - }, - }, { Input: `{ "address": "127.0.0.1", diff --git a/infra/conf/xray.go b/infra/conf/xray.go index fe295a493..01c2ddf23 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -238,7 +238,7 @@ func validateOutboundTransportSecurity(rawConfig interface{}, senderSettings *pr } if tjCfg, ok := rawConfig.(*TrojanClientConfig); ok { - if requiresTransportSecurity(tjCfg.Servers[0].Address) { + if requiresTransportSecurity(tjCfg.Address) { return errors.New("trojan without TLS is prohibited unless the server address is a private IP or domain") } }