From f357245f939a46ef8ed83f34ebed09fc7f06ad5b Mon Sep 17 00:00:00 2001 From: mmmray <142015632+mmmray@users.noreply.github.com> Date: Fri, 6 Sep 2024 06:31:26 +0800 Subject: [PATCH] Config: Remove global `transport` (#3751) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/XTLS/Xray-core/pull/3751#issuecomment-2329564039 --------- Co-authored-by: 风扇滑翔翼 --- infra/conf/transport.go | 106 -------------------------- infra/conf/transport_test.go | 144 ----------------------------------- infra/conf/xray.go | 42 ++-------- infra/conf/xray_test.go | 53 ------------- 4 files changed, 8 insertions(+), 337 deletions(-) delete mode 100644 infra/conf/transport.go diff --git a/infra/conf/transport.go b/infra/conf/transport.go deleted file mode 100644 index 7957ee7d..00000000 --- a/infra/conf/transport.go +++ /dev/null @@ -1,106 +0,0 @@ -package conf - -import ( - "github.com/xtls/xray-core/common/errors" - "github.com/xtls/xray-core/common/serial" - "github.com/xtls/xray-core/transport/global" - "github.com/xtls/xray-core/transport/internet" -) - -type TransportConfig struct { - TCPConfig *TCPConfig `json:"tcpSettings"` - KCPConfig *KCPConfig `json:"kcpSettings"` - WSConfig *WebSocketConfig `json:"wsSettings"` - HTTPConfig *HTTPConfig `json:"httpSettings"` - GRPCConfig *GRPCConfig `json:"grpcSettings"` - GUNConfig *GRPCConfig `json:"gunSettings"` - HTTPUPGRADEConfig *HttpUpgradeConfig `json:"httpupgradeSettings"` - SplitHTTPConfig *SplitHTTPConfig `json:"splithttpSettings"` -} - -// Build implements Buildable. -func (c *TransportConfig) Build() (*global.Config, error) { - config := new(global.Config) - - if c.TCPConfig != nil { - ts, err := c.TCPConfig.Build() - if err != nil { - return nil, errors.New("failed to build TCP config").Base(err).AtError() - } - config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{ - ProtocolName: "tcp", - Settings: serial.ToTypedMessage(ts), - }) - } - - if c.KCPConfig != nil { - ts, err := c.KCPConfig.Build() - if err != nil { - return nil, errors.New("failed to build mKCP config").Base(err).AtError() - } - config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{ - ProtocolName: "mkcp", - Settings: serial.ToTypedMessage(ts), - }) - } - - if c.WSConfig != nil { - ts, err := c.WSConfig.Build() - if err != nil { - return nil, errors.New("failed to build WebSocket config").Base(err) - } - config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{ - ProtocolName: "websocket", - Settings: serial.ToTypedMessage(ts), - }) - } - - if c.HTTPConfig != nil { - ts, err := c.HTTPConfig.Build() - if err != nil { - return nil, errors.New("Failed to build HTTP config.").Base(err) - } - config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{ - ProtocolName: "http", - Settings: serial.ToTypedMessage(ts), - }) - } - - if c.GRPCConfig == nil { - c.GRPCConfig = c.GUNConfig - } - if c.GRPCConfig != nil { - gs, err := c.GRPCConfig.Build() - if err != nil { - return nil, errors.New("Failed to build gRPC config.").Base(err) - } - config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{ - ProtocolName: "grpc", - Settings: serial.ToTypedMessage(gs), - }) - } - - if c.HTTPUPGRADEConfig != nil { - hs, err := c.HTTPUPGRADEConfig.Build() - if err != nil { - return nil, errors.New("failed to build HttpUpgrade config").Base(err) - } - config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{ - ProtocolName: "httpupgrade", - Settings: serial.ToTypedMessage(hs), - }) - } - - if c.SplitHTTPConfig != nil { - shs, err := c.SplitHTTPConfig.Build() - if err != nil { - return nil, errors.New("failed to build SplitHTTP config").Base(err) - } - config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{ - ProtocolName: "splithttp", - Settings: serial.ToTypedMessage(shs), - }) - } - - return config, nil -} diff --git a/infra/conf/transport_test.go b/infra/conf/transport_test.go index 86f707bd..e7470082 100644 --- a/infra/conf/transport_test.go +++ b/infra/conf/transport_test.go @@ -4,16 +4,8 @@ import ( "encoding/json" "testing" - "github.com/xtls/xray-core/common/serial" . "github.com/xtls/xray-core/infra/conf" - "github.com/xtls/xray-core/transport/global" "github.com/xtls/xray-core/transport/internet" - "github.com/xtls/xray-core/transport/internet/grpc" - "github.com/xtls/xray-core/transport/internet/headers/http" - "github.com/xtls/xray-core/transport/internet/headers/noop" - "github.com/xtls/xray-core/transport/internet/kcp" - "github.com/xtls/xray-core/transport/internet/tcp" - "github.com/xtls/xray-core/transport/internet/websocket" "google.golang.org/protobuf/proto" ) @@ -157,139 +149,3 @@ func TestSocketConfig(t *testing.T) { t.Fatalf("unexpected parsed TFO value, which should be -1") } } - -func TestTransportConfig(t *testing.T) { - createParser := func() func(string) (proto.Message, error) { - return func(s string) (proto.Message, error) { - config := new(TransportConfig) - if err := json.Unmarshal([]byte(s), config); err != nil { - return nil, err - } - return config.Build() - } - } - - runMultiTestCase(t, []TestCase{ - { - Input: `{ - "tcpSettings": { - "header": { - "type": "http", - "request": { - "version": "1.1", - "method": "GET", - "path": "/b", - "headers": { - "a": "b", - "c": "d" - } - }, - "response": { - "version": "1.0", - "status": "404", - "reason": "Not Found" - } - } - }, - "kcpSettings": { - "mtu": 1200, - "header": { - "type": "none" - } - }, - "wsSettings": { - "path": "/t" - }, - "grpcSettings": { - "serviceName": "name", - "multiMode": true - } - }`, - Parser: createParser(), - Output: &global.Config{ - TransportSettings: []*internet.TransportConfig{ - { - ProtocolName: "tcp", - Settings: serial.ToTypedMessage(&tcp.Config{ - HeaderSettings: serial.ToTypedMessage(&http.Config{ - Request: &http.RequestConfig{ - Version: &http.Version{Value: "1.1"}, - Method: &http.Method{Value: "GET"}, - Uri: []string{"/b"}, - Header: []*http.Header{ - {Name: "a", Value: []string{"b"}}, - {Name: "c", Value: []string{"d"}}, - }, - }, - Response: &http.ResponseConfig{ - Version: &http.Version{Value: "1.0"}, - Status: &http.Status{Code: "404", Reason: "Not Found"}, - Header: []*http.Header{ - { - Name: "Content-Type", - Value: []string{"application/octet-stream", "video/mpeg"}, - }, - { - Name: "Transfer-Encoding", - Value: []string{"chunked"}, - }, - { - Name: "Connection", - Value: []string{"keep-alive"}, - }, - { - Name: "Pragma", - Value: []string{"no-cache"}, - }, - { - Name: "Cache-Control", - Value: []string{"private", "no-cache"}, - }, - }, - }, - }), - }), - }, - { - ProtocolName: "mkcp", - Settings: serial.ToTypedMessage(&kcp.Config{ - Mtu: &kcp.MTU{Value: 1200}, - HeaderConfig: serial.ToTypedMessage(&noop.Config{}), - }), - }, - { - ProtocolName: "websocket", - Settings: serial.ToTypedMessage(&websocket.Config{ - Path: "/t", - }), - }, - { - ProtocolName: "grpc", - Settings: serial.ToTypedMessage(&grpc.Config{ - ServiceName: "name", - MultiMode: true, - }), - }, - }, - }, - }, - { - Input: `{ - "gunSettings": { - "serviceName": "name" - } - }`, - Parser: createParser(), - Output: &global.Config{ - TransportSettings: []*internet.TransportConfig{ - { - ProtocolName: "grpc", - Settings: serial.ToTypedMessage(&grpc.Config{ - ServiceName: "name", - }), - }, - }, - }, - }, - }) -} diff --git a/infra/conf/xray.go b/infra/conf/xray.go index 9488589c..e0f838c8 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -405,12 +405,15 @@ type Config struct { // and should not be used. OutboundDetours []OutboundDetourConfig `json:"outboundDetour"` + // Deprecated: Global transport config is no longer used + // left for returning error + Transport map[string]json.RawMessage `json:"transport"` + LogConfig *LogConfig `json:"log"` RouterConfig *RouterConfig `json:"routing"` DNSConfig *DNSConfig `json:"dns"` InboundConfigs []InboundDetourConfig `json:"inbounds"` OutboundConfigs []OutboundDetourConfig `json:"outbounds"` - Transport *TransportConfig `json:"transport"` Policy *PolicyConfig `json:"policy"` API *APIConfig `json:"api"` Metrics *MetricsConfig `json:"metrics"` @@ -540,27 +543,6 @@ func (c *Config) Override(o *Config, fn string) { } } -func applyTransportConfig(s *StreamConfig, t *TransportConfig) { - if s.TCPSettings == nil { - s.TCPSettings = t.TCPConfig - } - if s.KCPSettings == nil { - s.KCPSettings = t.KCPConfig - } - if s.WSSettings == nil { - s.WSSettings = t.WSConfig - } - if s.HTTPSettings == nil { - s.HTTPSettings = t.HTTPConfig - } - if s.HTTPUPGRADESettings == nil { - s.HTTPUPGRADESettings = t.HTTPUPGRADEConfig - } - if s.SplitHTTPSettings == nil { - s.SplitHTTPSettings = t.SplitHTTPConfig - } -} - // Build implements Buildable. func (c *Config) Build() (*core.Config, error) { if err := PostProcessConfigureFile(c); err != nil { @@ -685,13 +667,11 @@ func (c *Config) Build() (*core.Config, error) { }}} } + if len(c.Transport) > 0 { + return nil, errors.New("Global transport config is deprecated") + } + for _, rawInboundConfig := range inbounds { - if c.Transport != nil { - if rawInboundConfig.StreamSetting == nil { - rawInboundConfig.StreamSetting = &StreamConfig{} - } - applyTransportConfig(rawInboundConfig.StreamSetting, c.Transport) - } ic, err := rawInboundConfig.Build() if err != nil { return nil, err @@ -714,12 +694,6 @@ func (c *Config) Build() (*core.Config, error) { } for _, rawOutboundConfig := range outbounds { - if c.Transport != nil { - if rawOutboundConfig.StreamSetting == nil { - rawOutboundConfig.StreamSetting = &StreamConfig{} - } - applyTransportConfig(rawOutboundConfig.StreamSetting, c.Transport) - } oc, err := rawOutboundConfig.Build() if err != nil { return nil, err diff --git a/infra/conf/xray_test.go b/infra/conf/xray_test.go index 03e27142..0e0b28f4 100644 --- a/infra/conf/xray_test.go +++ b/infra/conf/xray_test.go @@ -23,7 +23,6 @@ import ( "github.com/xtls/xray-core/proxy/vmess" "github.com/xtls/xray-core/proxy/vmess/inbound" "github.com/xtls/xray-core/transport/internet" - "github.com/xtls/xray-core/transport/internet/http" "github.com/xtls/xray-core/transport/internet/tls" "github.com/xtls/xray-core/transport/internet/websocket" "google.golang.org/protobuf/proto" @@ -128,11 +127,6 @@ func TestXrayConfig(t *testing.T) { } ] } - }, - "transport": { - "httpSettings": { - "path": "/test" - } } }`, Parser: createParser(), @@ -172,17 +166,6 @@ func TestXrayConfig(t *testing.T) { Outbound: []*core.OutboundHandlerConfig{ { SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ - StreamSettings: &internet.StreamConfig{ - ProtocolName: "tcp", - TransportSettings: []*internet.TransportConfig{ - { - ProtocolName: "http", - Settings: serial.ToTypedMessage(&http.Config{ - Path: "/test", - }), - }, - }, - }, }), ProxySettings: serial.ToTypedMessage(&freedom.Config{ DomainStrategy: freedom.Config_AS_IS, @@ -192,33 +175,11 @@ func TestXrayConfig(t *testing.T) { { Tag: "blocked", SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ - StreamSettings: &internet.StreamConfig{ - ProtocolName: "tcp", - TransportSettings: []*internet.TransportConfig{ - { - ProtocolName: "http", - Settings: serial.ToTypedMessage(&http.Config{ - Path: "/test", - }), - }, - }, - }, }), ProxySettings: serial.ToTypedMessage(&blackhole.Config{}), }, { SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ - StreamSettings: &internet.StreamConfig{ - ProtocolName: "tcp", - TransportSettings: []*internet.TransportConfig{ - { - ProtocolName: "http", - Settings: serial.ToTypedMessage(&http.Config{ - Path: "/test", - }), - }, - }, - }, }), ProxySettings: serial.ToTypedMessage(&dns_proxy.Config{ Server: &net.Endpoint{}, @@ -242,12 +203,6 @@ func TestXrayConfig(t *testing.T) { }, }), }, - { - ProtocolName: "http", - Settings: serial.ToTypedMessage(&http.Config{ - Path: "/test", - }), - }, }, SecurityType: "xray.transport.internet.tls.Config", SecuritySettings: []*serial.TypedMessage{ @@ -295,12 +250,6 @@ func TestXrayConfig(t *testing.T) { }, }), }, - { - ProtocolName: "http", - Settings: serial.ToTypedMessage(&http.Config{ - Path: "/test", - }), - }, }, SecurityType: "xray.transport.internet.tls.Config", SecuritySettings: []*serial.TypedMessage{ @@ -387,7 +336,6 @@ func TestConfig_Override(t *testing.T) { LogConfig: &LogConfig{}, RouterConfig: &RouterConfig{}, DNSConfig: &DNSConfig{}, - Transport: &TransportConfig{}, Policy: &PolicyConfig{}, API: &APIConfig{}, Stats: &StatsConfig{}, @@ -398,7 +346,6 @@ func TestConfig_Override(t *testing.T) { LogConfig: &LogConfig{}, RouterConfig: &RouterConfig{}, DNSConfig: &DNSConfig{}, - Transport: &TransportConfig{}, Policy: &PolicyConfig{}, API: &APIConfig{}, Stats: &StatsConfig{},