From 5f0642a671e4c611c4d86604f7d6f3d488452d93 Mon Sep 17 00:00:00 2001 From: Kobe Arthur Scofield <26771058+KobeArthurScofield@users.noreply.github.com> Date: Sun, 15 Sep 2024 12:55:54 +0800 Subject: [PATCH] Errors: Add PrintRemovedFeatureError & PrintDeprecatedFeatureWarning (#3804) --- app/dns/dns.go | 3 +-- app/dns/hosts.go | 3 +-- common/errors/feature_errors.go | 25 +++++++++++++++++++++++++ features/feature.go | 15 --------------- infra/conf/transport_internet.go | 2 +- infra/conf/trojan.go | 4 ++-- infra/conf/xray.go | 2 +- proxy/socks/server.go | 3 +-- 8 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 common/errors/feature_errors.go diff --git a/app/dns/dns.go b/app/dns/dns.go index 4abc8b33..f4802275 100644 --- a/app/dns/dns.go +++ b/app/dns/dns.go @@ -15,7 +15,6 @@ import ( "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/strmatcher" - "github.com/xtls/xray-core/features" "github.com/xtls/xray-core/features/dns" ) @@ -96,7 +95,7 @@ func New(ctx context.Context, config *Config) (*DNS, error) { geoipContainer := router.GeoIPMatcherContainer{} for _, endpoint := range config.NameServers { - features.PrintDeprecatedFeatureWarning("simple DNS server") + errors.PrintDeprecatedFeatureWarning("simple DNS server", "") client, err := NewSimpleClient(ctx, endpoint, clientIP) if err != nil { return nil, errors.New("failed to create client").Base(err) diff --git a/app/dns/hosts.go b/app/dns/hosts.go index a60d2514..4dd01041 100644 --- a/app/dns/hosts.go +++ b/app/dns/hosts.go @@ -7,7 +7,6 @@ import ( "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/strmatcher" - "github.com/xtls/xray-core/features" "github.com/xtls/xray-core/features/dns" ) @@ -26,7 +25,7 @@ func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDoma } if legacy != nil { - features.PrintDeprecatedFeatureWarning("simple host mapping") + errors.PrintDeprecatedFeatureWarning("simple host mapping", "") for domain, ip := range legacy { matcher, err := strmatcher.Full.New(domain) diff --git a/common/errors/feature_errors.go b/common/errors/feature_errors.go new file mode 100644 index 00000000..ea9b4c03 --- /dev/null +++ b/common/errors/feature_errors.go @@ -0,0 +1,25 @@ +package errors + +import ( + "context" +) + +// PrintDeprecatedFeatureWarning prints a warning for deprecated and going to be removed feature. +// Do not remove this function even there is no reference to it. +func PrintDeprecatedFeatureWarning(feature string, migrateFeature string) { + if len(migrateFeature) > 0 { + LogWarning(context.Background(), "This feature " + feature + " is deprecated and being migrated to " + migrateFeature + ". Please update your config(s) according to release note and documentation before removal.") + } else { + LogWarning(context.Background(), "This feature " + feature + " is deprecated. Please update your config(s) according to release note and documentation before removal.") + } +} + +// PrintRemovedFeatureError prints an error message for removed feature then return an error. And after long enough time the message can also be removed, uses as an indicator. +// Do not remove this function even there is no reference to it. +func PrintRemovedFeatureError(feature string, migrateFeature string) (error) { + if len(migrateFeature) > 0 { + return New("The feature " + feature + " has been removed and migrated to " + migrateFeature + ". Please update your config(s) according to release note and documentation.") + } else { + return New("The feature " + feature + " has been removed. Please update your config(s) according to release note and documentation.") + } +} diff --git a/features/feature.go b/features/feature.go index 4ed71600..026b8fa5 100644 --- a/features/feature.go +++ b/features/feature.go @@ -1,27 +1,12 @@ package features import ( - "context" - "github.com/xtls/xray-core/common" - "github.com/xtls/xray-core/common/errors" ) -//go:generate go run github.com/xtls/xray-core/common/errors/errorgen - // Feature is the interface for Xray features. All features must implement this interface. // All existing features have an implementation in app directory. These features can be replaced by third-party ones. type Feature interface { common.HasType common.Runnable } - -// PrintDeprecatedFeatureWarning prints a warning for deprecated feature. -func PrintDeprecatedFeatureWarning(feature string) { - errors.LogWarning(context.Background(), "You are using a deprecated feature: " + feature + ". Please update your config file(s) with latest configuration format, or update your client software.") -} - -// PrintRemovedFeatureError prints an error message for removed feature. And after long enough time the message can also be removed, use as an indicator. -func PrintRemovedFeatureError(feature string) { - errors.New("The feature " + feature + " is removed. Please update your config file(s) according to release notes and documentations.") -} diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index 8cc057a8..b61914ae 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -813,7 +813,7 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) { config.SecuritySettings = append(config.SecuritySettings, tm) config.SecurityType = tm.Type case "xtls": - return nil, errors.New(`Please use VLESS flow "xtls-rprx-vision" with TLS or REALITY.`) + return nil, errors.PrintRemovedFeatureError(`Legacy XTLS`, `xtls-rprx-vision with TLS or REALITY`) default: return nil, errors.New(`Unknown security "` + c.Security + `".`) } diff --git a/infra/conf/trojan.go b/infra/conf/trojan.go index 13a1df73..03ac6f46 100644 --- a/infra/conf/trojan.go +++ b/infra/conf/trojan.go @@ -52,7 +52,7 @@ func (c *TrojanClientConfig) Build() (proto.Message, error) { return nil, errors.New("Trojan password is not specified.") } if rec.Flow != "" { - return nil, errors.New(`Trojan doesn't support "flow" anymore.`) + return nil, errors.PrintRemovedFeatureError(`Flow for Trojan`, ``) } config.Server[idx] = &protocol.ServerEndpoint{ @@ -106,7 +106,7 @@ func (c *TrojanServerConfig) Build() (proto.Message, error) { for idx, rawUser := range c.Clients { if rawUser.Flow != "" { - return nil, errors.New(`Trojan doesn't support "flow" anymore.`) + return nil, errors.PrintRemovedFeatureError(`Flow for Trojan`, ``) } config.Users[idx] = &protocol.User{ diff --git a/infra/conf/xray.go b/infra/conf/xray.go index 07f70273..0b5c60e6 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -593,7 +593,7 @@ func (c *Config) Build() (*core.Config, error) { } if len(c.Transport) > 0 { - return nil, errors.New("Global transport config is deprecated") + return nil, errors.PrintRemovedFeatureError("Global transport config", "streamSettings in inbounds and outbounds") } for _, rawInboundConfig := range inbounds { diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 571fabb2..2b7bca7b 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -16,7 +16,6 @@ import ( "github.com/xtls/xray-core/common/signal" "github.com/xtls/xray-core/common/task" "github.com/xtls/xray-core/core" - "github.com/xtls/xray-core/features" "github.com/xtls/xray-core/features/policy" "github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/proxy/http" @@ -56,7 +55,7 @@ func (s *Server) policy() policy.Session { config := s.config p := s.policyManager.ForLevel(config.UserLevel) if config.Timeout > 0 { - features.PrintDeprecatedFeatureWarning("Socks timeout") + errors.PrintDeprecatedFeatureWarning("Socks timeout", "Policy level") } if config.Timeout > 0 && config.UserLevel == 0 { p.Timeouts.ConnectionIdle = time.Duration(config.Timeout) * time.Second