From 271fdbc2a8797645c2914eb4ed49ab830cd66a95 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Thu, 31 Jul 2025 10:46:01 +0800 Subject: [PATCH] Refactor --- .../ServiceLib/Handler/CoreConfigHandler.cs | 228 ++++++++++-------- v2rayN/ServiceLib/Handler/CoreHandler.cs | 65 +---- v2rayN/ServiceLib/Models/CoreLaunchContext.cs | 53 ++++ .../CoreConfig/CoreConfigServiceBase.cs | 83 +++++++ .../CoreConfig/CoreConfigSingboxService.cs | 24 +- .../CoreConfig/CoreConfigV2rayService.cs | 37 ++- .../Minimal/CoreConfigBrookService.cs | 14 +- .../{ => Minimal}/CoreConfigClashService.cs | 18 +- .../Minimal/CoreConfigHy2Service.cs | 22 +- .../Minimal/CoreConfigJuicityService.cs | 14 +- .../Minimal/CoreConfigNaiveService.cs | 14 +- .../Minimal/CoreConfigShadowquicService.cs | 14 +- .../Minimal/CoreConfigTuicService.cs | 14 +- .../ViewModels/ProfilesViewModel.cs | 6 +- 14 files changed, 337 insertions(+), 269 deletions(-) create mode 100644 v2rayN/ServiceLib/Models/CoreLaunchContext.cs create mode 100644 v2rayN/ServiceLib/Services/CoreConfig/CoreConfigServiceBase.cs rename v2rayN/ServiceLib/Services/CoreConfig/{ => Minimal}/CoreConfigClashService.cs (93%) diff --git a/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs b/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs index 41005efe..780e6f2b 100644 --- a/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs @@ -9,33 +9,26 @@ public class CoreConfigHandler { private static readonly string _tag = "CoreConfigHandler"; - public static async Task GenerateClientConfig(ProfileItem node, string? fileName) + public static async Task GenerateClientConfig(CoreLaunchContext context, string? fileName) { - var config = AppHandler.Instance.Config; var result = new RetResult(); - if (node.ConfigType == EConfigType.Custom) + if (context.ConfigType == EConfigType.Custom) { - result = node.CoreType switch - { - ECoreType.mihomo => await new CoreConfigClashService(config).GenerateClientCustomConfig(node, fileName), - ECoreType.sing_box => await new CoreConfigSingboxService(config).GenerateClientCustomConfig(node, fileName), - _ => await GenerateClientCustomConfig(node, fileName) - }; - } - else if (AppHandler.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box || (Global.SingboxSupportConfigType.Contains(node.ConfigType) && !Global.XraySupportConfigType.Contains(node.ConfigType))) - { - result = await new CoreConfigSingboxService(config).GenerateClientConfigContent(node); - } - else if (Global.XraySupportConfigType.Contains(node.ConfigType)) - { - result = await new CoreConfigV2rayService(config).GenerateClientConfigContent(node); + result = await GetCoreConfigServiceForCustom(context.CoreType).GenerateClientCustomConfig(context.Node, fileName); } else { - result.Msg = ResUI.OperationFailed; - result.Success = false; - return result; + try + { + result = await GetCoreConfigServiceForClientConfig(context.CoreType).GenerateClientConfigContent(context.Node); + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + result.Msg = ResUI.FailedGenDefaultConfiguration; + return result; + } } if (result.Success != true) { @@ -49,96 +42,44 @@ public class CoreConfigHandler return result; } - public static async Task GeneratePureEndpointConfig(ProfileItem node, string? fileName) + public static async Task GeneratePassthroughConfig(CoreLaunchContext context, string? fileName) { - var config = AppHandler.Instance.Config; var result = new RetResult(); - var coreType = AppHandler.Instance.GetSplitCoreType(node, node.ConfigType); - - result = coreType switch - { - ECoreType.sing_box => await new CoreConfigSingboxService(config).GeneratePureEndpointConfig(node), - ECoreType.Xray => await new CoreConfigV2rayService(config).GeneratePureEndpointConfig(node), - ECoreType.hysteria2 => await new CoreConfigHy2Service(config).GeneratePureEndpointConfig(node), - ECoreType.naiveproxy => await new CoreConfigNaiveService(config).GeneratePureEndpointConfig(node), - ECoreType.tuic => await new CoreConfigTuicService(config).GeneratePureEndpointConfig(node), - ECoreType.juicity => await new CoreConfigJuicityService(config).GeneratePureEndpointConfig(node), - ECoreType.brook => await new CoreConfigBrookService(config).GeneratePureEndpointConfig(node), - ECoreType.shadowquic => await new CoreConfigShadowquicService(config).GeneratePureEndpointConfig(node), - _ => throw new NotImplementedException(), - }; - - if (result.Success != true) - { - return result; - } - if (fileName.IsNotEmpty() && result.Data != null) - { - await File.WriteAllTextAsync(fileName, result.Data.ToString()); - } - return result; - } - - private static async Task GenerateClientCustomConfig(ProfileItem node, string? fileName) - { - var ret = new RetResult(); try { - if (node == null || fileName is null) - { - ret.Msg = ResUI.CheckServerSettings; - return ret; - } - - if (File.Exists(fileName)) - { - File.SetAttributes(fileName, FileAttributes.Normal); //If the file has a read-only attribute, direct deletion will fail - File.Delete(fileName); - } - - string addressFileName = node.Address; - if (!File.Exists(addressFileName)) - { - addressFileName = Utils.GetConfigPath(addressFileName); - } - if (!File.Exists(addressFileName)) - { - ret.Msg = ResUI.FailedGenDefaultConfiguration; - return ret; - } - File.Copy(addressFileName, fileName); - File.SetAttributes(fileName, FileAttributes.Normal); //Copy will keep the attributes of addressFileName, so we need to add write permissions to fileName just in case of addressFileName is a read-only file. - - //check again - if (!File.Exists(fileName)) - { - ret.Msg = ResUI.FailedGenDefaultConfiguration; - return ret; - } - - ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); - ret.Success = true; - return await Task.FromResult(ret); + result = await GetCoreConfigServiceForPassthrough(context.CoreType).GeneratePassthroughConfig(context.Node); } catch (Exception ex) { Logging.SaveLog(_tag, ex); - ret.Msg = ResUI.FailedGenDefaultConfiguration; - return ret; + result.Msg = ResUI.FailedGenDefaultConfiguration; + return result; } + + if (result.Success != true) + { + return result; + } + if (fileName.IsNotEmpty() && result.Data != null) + { + await File.WriteAllTextAsync(fileName, result.Data.ToString()); + } + return result; } public static async Task GenerateClientSpeedtestConfig(Config config, string fileName, List selecteds, ECoreType coreType) { var result = new RetResult(); - if (coreType == ECoreType.sing_box) + try { - result = await new CoreConfigSingboxService(config).GenerateClientSpeedtestConfig(selecteds); + result = await GetCoreConfigServiceForMultipleSpeedtest(coreType).GenerateClientSpeedtestConfig(selecteds); } - else if (coreType == ECoreType.Xray) + catch (Exception ex) { - result = await new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(selecteds); + Logging.SaveLog(_tag, ex); + result.Msg = ResUI.FailedGenDefaultConfiguration; + return result; } if (result.Success != true) { @@ -148,21 +89,24 @@ public class CoreConfigHandler return result; } - public static async Task GenerateClientSpeedtestConfig(Config config, ProfileItem node, ServerTestItem testItem, string fileName) + public static async Task GenerateClientSpeedtestConfig(Config config, CoreLaunchContext context, ServerTestItem testItem, string fileName) { var result = new RetResult(); var initPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.speedtest); var port = Utils.GetFreePort(initPort + testItem.QueueNum); testItem.Port = port; - if (AppHandler.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box) + try { - result = await new CoreConfigSingboxService(config).GenerateClientSpeedtestConfig(node, port); + result = await GetCoreConfigServiceForSpeedtest(context.CoreType).GenerateClientSpeedtestConfig(context.Node, port); } - else if (Global.XraySupportConfigType.Contains(node.ConfigType)) + catch (Exception ex) { - result = await new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(node, port); + Logging.SaveLog(_tag, ex); + result.Msg = ResUI.FailedGenDefaultConfiguration; + return result; } + if (result.Success != true) { return result; @@ -191,4 +135,94 @@ public class CoreConfigHandler await File.WriteAllTextAsync(fileName, result.Data.ToString()); return result; } + + private static CoreConfigServiceMinimalBase GetCoreConfigServiceForPassthrough(ECoreType coreType) + { + switch (coreType) + { + case ECoreType.sing_box: + return new CoreConfigSingboxService(AppHandler.Instance.Config); + case ECoreType.Xray: + return new CoreConfigV2rayService(AppHandler.Instance.Config); + case ECoreType.hysteria2: + return new CoreConfigHy2Service(AppHandler.Instance.Config); + case ECoreType.naiveproxy: + return new CoreConfigNaiveService(AppHandler.Instance.Config); + case ECoreType.tuic: + return new CoreConfigTuicService(AppHandler.Instance.Config); + case ECoreType.juicity: + return new CoreConfigJuicityService(AppHandler.Instance.Config); + case ECoreType.brook: + return new CoreConfigBrookService(AppHandler.Instance.Config); + case ECoreType.shadowquic: + return new CoreConfigShadowquicService(AppHandler.Instance.Config); + default: + throw new NotImplementedException($"Core type {coreType} is not implemented for passthrough configuration."); + } + } + + private static CoreConfigServiceMinimalBase GetCoreConfigServiceForSpeedtest(ECoreType coreType) + { + switch (coreType) + { + case ECoreType.sing_box: + return new CoreConfigSingboxService(AppHandler.Instance.Config); + case ECoreType.Xray: + return new CoreConfigV2rayService(AppHandler.Instance.Config); + case ECoreType.hysteria2: + return new CoreConfigHy2Service(AppHandler.Instance.Config); + case ECoreType.naiveproxy: + return new CoreConfigNaiveService(AppHandler.Instance.Config); + case ECoreType.tuic: + return new CoreConfigTuicService(AppHandler.Instance.Config); + case ECoreType.juicity: + return new CoreConfigJuicityService(AppHandler.Instance.Config); + case ECoreType.brook: + return new CoreConfigBrookService(AppHandler.Instance.Config); + case ECoreType.shadowquic: + return new CoreConfigShadowquicService(AppHandler.Instance.Config); + default: + throw new NotImplementedException($"Core type {coreType} is not implemented for passthrough configuration."); + } + } + + private static CoreConfigServiceBase GetCoreConfigServiceForMultipleSpeedtest(ECoreType coreType) + { + switch (coreType) + { + case ECoreType.sing_box: + return new CoreConfigSingboxService(AppHandler.Instance.Config); + case ECoreType.Xray: + return new CoreConfigV2rayService(AppHandler.Instance.Config); + default: + throw new NotImplementedException($"Core type {coreType} is not implemented for passthrough configuration."); + } + } + + private static CoreConfigServiceMinimalBase GetCoreConfigServiceForCustom(ECoreType coreType) + { + switch (coreType) + { + case ECoreType.mihomo: + return new CoreConfigClashService(AppHandler.Instance.Config); + case ECoreType.sing_box: + return new CoreConfigSingboxService(AppHandler.Instance.Config); + default: + // CoreConfigServiceMinimalBase + return new CoreConfigV2rayService(AppHandler.Instance.Config); + } + } + + private static CoreConfigServiceBase GetCoreConfigServiceForClientConfig(ECoreType coreType) + { + switch (coreType) + { + case ECoreType.sing_box: + return new CoreConfigSingboxService(AppHandler.Instance.Config); + case ECoreType.Xray: + return new CoreConfigV2rayService(AppHandler.Instance.Config); + default: + throw new NotImplementedException($"Core type {coreType} is not implemented for client configuration."); + } + } } diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index 3ab29804..01838cc1 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -130,10 +130,12 @@ public class CoreHandler return -1; } - var coreType = AppHandler.Instance.GetCoreType(node, node.ConfigType); + var context = new CoreLaunchContext(node, _config); + context.AdjustForConfigType(); + var coreType = context.CoreType; var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false)); var configPath = Utils.GetBinConfigPath(fileName, coreType); - var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, node, testItem, configPath); + var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, context, testItem, configPath); if (result.Success != true) { return -1; @@ -179,63 +181,13 @@ public class CoreHandler #region Private - /// - /// Core launch context that encapsulates all parameters required for launching - /// - private class CoreLaunchContext - { - public ProfileItem Node { get; set; } - public bool SplitCore { get; set; } - public ECoreType CoreType { get; set; } - public ECoreType? PreCoreType { get; set; } - public ECoreType PureEndpointCore { get; set; } - public ECoreType SplitRouteCore { get; set; } - public bool EnableTun { get; set; } - public int PreSocksPort { get; set; } - - public CoreLaunchContext(ProfileItem node, Config config) - { - Node = node; - SplitCore = config.SplitCoreItem.EnableSplitCore; - CoreType = AppHandler.Instance.GetCoreType(node, node.ConfigType); - PureEndpointCore = AppHandler.Instance.GetSplitCoreType(node, node.ConfigType); - SplitRouteCore = config.SplitCoreItem.RouteCoreType; - EnableTun = config.TunModeItem.EnableTun; - PreSocksPort = 0; - PreCoreType = null; - } - - /// - /// Adjust context parameters based on configuration type - /// - public void AdjustForConfigType() - { - (SplitCore, CoreType, PreCoreType) = AppHandler.Instance.GetCoreAndPreType(Node); - if (Node.ConfigType == EConfigType.Custom) - { - if (Node.PreSocksPort > 0) - { - PreSocksPort = Node.PreSocksPort.Value; - } - else - { - EnableTun = false; - } - } - else if (PreCoreType != null) - { - PreSocksPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.split); - } - } - } - private async Task CoreStart(CoreLaunchContext context) { var coreType = context.SplitCore ? context.PureEndpointCore : context.CoreType; var fileName = Utils.GetBinConfigPath(Global.CoreConfigFileName, coreType); var result = context.SplitCore - ? await CoreConfigHandler.GeneratePureEndpointConfig(context.Node, fileName) - : await CoreConfigHandler.GenerateClientConfig(context.Node, fileName); + ? await CoreConfigHandler.GeneratePassthroughConfig(context, fileName) + : await CoreConfigHandler.GenerateClientConfig(context, fileName); if (result.Success != true) { @@ -287,8 +239,9 @@ public class CoreHandler Sni = context.EnableTun && Utils.IsDomain(context.Node.Address) ? context.Node.Address : string.Empty, //Tun2SocksAddress Port = context.PreSocksPort }; - - var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName); + var itemSocksLaunch = new CoreLaunchContext(itemSocks, _config); + + var result = await CoreConfigHandler.GenerateClientConfig(itemSocksLaunch, fileName); if (!result.Success) { UpdateFunc(true, result.Msg); diff --git a/v2rayN/ServiceLib/Models/CoreLaunchContext.cs b/v2rayN/ServiceLib/Models/CoreLaunchContext.cs new file mode 100644 index 00000000..cf327dac --- /dev/null +++ b/v2rayN/ServiceLib/Models/CoreLaunchContext.cs @@ -0,0 +1,53 @@ +namespace ServiceLib.Models; + +/// +/// Core launch context that encapsulates all parameters required for launching +/// +public class CoreLaunchContext +{ + public ProfileItem Node { get; set; } + public bool SplitCore { get; set; } + public ECoreType CoreType { get; set; } + public ECoreType? PreCoreType { get; set; } + public ECoreType PureEndpointCore { get; set; } + public ECoreType SplitRouteCore { get; set; } + public bool EnableTun { get; set; } + public int PreSocksPort { get; set; } + public EConfigType ConfigType { get; set; } + + public CoreLaunchContext(ProfileItem node, Config config) + { + Node = node; + SplitCore = config.SplitCoreItem.EnableSplitCore; + CoreType = AppHandler.Instance.GetCoreType(node, node.ConfigType); + PureEndpointCore = AppHandler.Instance.GetSplitCoreType(node, node.ConfigType); + SplitRouteCore = config.SplitCoreItem.RouteCoreType; + EnableTun = config.TunModeItem.EnableTun; + PreSocksPort = 0; + PreCoreType = null; + ConfigType = node.ConfigType; + } + + /// + /// Adjust context parameters based on configuration type + /// + public void AdjustForConfigType() + { + (SplitCore, CoreType, PreCoreType) = AppHandler.Instance.GetCoreAndPreType(Node); + if (Node.ConfigType == EConfigType.Custom) + { + if (Node.PreSocksPort > 0) + { + PreSocksPort = Node.PreSocksPort.Value; + } + else + { + EnableTun = false; + } + } + else if (PreCoreType != null) + { + PreSocksPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.split); + } + } +} diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigServiceBase.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigServiceBase.cs new file mode 100644 index 00000000..9015fbe6 --- /dev/null +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigServiceBase.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using YamlDotNet.Core.Tokens; + +namespace ServiceLib.Services.CoreConfig; + +public abstract class CoreConfigServiceMinimalBase(Config config) +{ + public virtual string _tag => GetType().Name; + protected Config _config = config; + + public virtual Task GeneratePassthroughConfig(ProfileItem node) + { + return GeneratePassthroughConfig(node, AppHandler.Instance.GetLocalPort(EInboundProtocol.split)); + } + public virtual Task GenerateClientSpeedtestConfig(ProfileItem node, int port) + { + return GeneratePassthroughConfig(node, port); + } + protected abstract Task GeneratePassthroughConfig(ProfileItem node, int port); + public virtual async Task GenerateClientCustomConfig(ProfileItem node, string? fileName) + { + var ret = new RetResult(); + try + { + if (node == null || fileName is null) + { + ret.Msg = ResUI.CheckServerSettings; + return ret; + } + + if (File.Exists(fileName)) + { + File.SetAttributes(fileName, FileAttributes.Normal); //If the file has a read-only attribute, direct deletion will fail + File.Delete(fileName); + } + + string addressFileName = node.Address; + if (!File.Exists(addressFileName)) + { + addressFileName = Utils.GetConfigPath(addressFileName); + } + if (!File.Exists(addressFileName)) + { + ret.Msg = ResUI.FailedGenDefaultConfiguration; + return ret; + } + File.Copy(addressFileName, fileName); + File.SetAttributes(fileName, FileAttributes.Normal); //Copy will keep the attributes of addressFileName, so we need to add write permissions to fileName just in case of addressFileName is a read-only file. + + //check again + if (!File.Exists(fileName)) + { + ret.Msg = ResUI.FailedGenDefaultConfiguration; + return ret; + } + + ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); + ret.Success = true; + return await Task.FromResult(ret); + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + ret.Msg = ResUI.FailedGenDefaultConfiguration; + return ret; + } + } +} + +public abstract class CoreConfigServiceBase(Config config) : CoreConfigServiceMinimalBase(config) +{ + public abstract Task GenerateClientConfigContent(ProfileItem node); + public abstract Task GenerateClientMultipleLoadConfig(List selecteds, EMultipleLoad multipleLoad); + public virtual Task GenerateClientMultipleLoadConfig(List selecteds) + { + return GenerateClientMultipleLoadConfig(selecteds, EMultipleLoad.LeastPing); + } + public abstract Task GenerateClientSpeedtestConfig(List selecteds); +} diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 5ff9c35d..51678637 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -9,19 +9,11 @@ using ServiceLib.Models; namespace ServiceLib.Services.CoreConfig; -public class CoreConfigSingboxService +public class CoreConfigSingboxService(Config config) : CoreConfigServiceBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigSingboxService"; - - public CoreConfigSingboxService(Config config) - { - _config = config; - } - #region public gen function - public async Task GenerateClientConfigContent(ProfileItem node) + public override async Task GenerateClientConfigContent(ProfileItem node) { var ret = new RetResult(); try @@ -94,7 +86,7 @@ public class CoreConfigSingboxService } } - public async Task GenerateClientSpeedtestConfig(List selecteds) + public override async Task GenerateClientSpeedtestConfig(List selecteds) { var ret = new RetResult(); try @@ -271,7 +263,7 @@ public class CoreConfigSingboxService } } - public async Task GenerateClientSpeedtestConfig(ProfileItem node, int port) + public override async Task GenerateClientSpeedtestConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -342,7 +334,7 @@ public class CoreConfigSingboxService } } - public async Task GenerateClientMultipleLoadConfig(List selecteds) + public override async Task GenerateClientMultipleLoadConfig(List selecteds, EMultipleLoad multipleLoad) { var ret = new RetResult(); try @@ -434,7 +426,7 @@ public class CoreConfigSingboxService } } - public async Task GenerateClientCustomConfig(ProfileItem node, string? fileName) + public override async Task GenerateClientCustomConfig(ProfileItem node, string? fileName) { var ret = new RetResult(); if (node == null || fileName is null) @@ -515,7 +507,7 @@ public class CoreConfigSingboxService } } - public async Task GeneratePureEndpointConfig(ProfileItem node) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -555,7 +547,7 @@ public class CoreConfigSingboxService type = EInboundProtocol.mixed.ToString(), tag = EInboundProtocol.socks.ToString(), listen = Global.Loopback, - listen_port = AppHandler.Instance.GetLocalPort(EInboundProtocol.split) + listen_port = port }; singboxConfig.inbounds = new() { inbound }; diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs index 0920cb95..7f219032 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs @@ -5,19 +5,11 @@ using System.Text.Json.Nodes; namespace ServiceLib.Services.CoreConfig; -public class CoreConfigV2rayService +public class CoreConfigV2rayService(Config config) : CoreConfigServiceBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigV2rayService"; - - public CoreConfigV2rayService(Config config) - { - _config = config; - } - #region public gen function - public async Task GenerateClientConfigContent(ProfileItem node) + public override async Task GenerateClientConfigContent(ProfileItem node) { var ret = new RetResult(); try @@ -78,7 +70,7 @@ public class CoreConfigV2rayService } } - public async Task GenerateClientMultipleLoadConfig(List selecteds, EMultipleLoad multipleLoad) + public override async Task GenerateClientMultipleLoadConfig(List selecteds, EMultipleLoad multipleLoad) { var ret = new RetResult(); @@ -203,7 +195,7 @@ public class CoreConfigV2rayService } } - public async Task GenerateClientSpeedtestConfig(List selecteds) + public override async Task GenerateClientSpeedtestConfig(List selecteds) { var ret = new RetResult(); try @@ -355,7 +347,7 @@ public class CoreConfigV2rayService } } - public async Task GenerateClientSpeedtestConfig(ProfileItem node, int port) + public override async Task GenerateClientSpeedtestConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -413,7 +405,7 @@ public class CoreConfigV2rayService } } - public async Task GeneratePureEndpointConfig(ProfileItem node) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -449,13 +441,18 @@ public class CoreConfigV2rayService await GenLog(v2rayConfig); - var inbound = GetInbound(_config.Inbound.First(), EInboundProtocol.split, true); - inbound.sniffing = new Sniffing4Ray + v2rayConfig.inbounds = new() { new() { - enabled = false - }; - - v2rayConfig.inbounds = new() { inbound }; + tag = EInboundProtocol.socks.ToString(), + listen = Global.Loopback, + port = port, + protocol = EInboundProtocol.mixed.ToString(), + settings = new Inboundsettings4Ray() + { + udp = true, + auth = "noauth" + }, + } }; await GenOutbound(node, v2rayConfig.outbounds.First()); diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigBrookService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigBrookService.cs index 6ef1353e..b5f34dbe 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigBrookService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigBrookService.cs @@ -1,15 +1,7 @@ namespace ServiceLib.Services.CoreConfig.Minimal; -public class CoreConfigBrookService +public class CoreConfigBrookService(Config config) : CoreConfigServiceMinimalBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigBrookService"; - - public CoreConfigBrookService(Config config) - { - _config = config; - } - - public async Task GeneratePureEndpointConfig(ProfileItem node) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -30,7 +22,7 @@ public class CoreConfigBrookService var processArgs = "client"; // inbound - processArgs += " --socks5 " + Global.Loopback + ":" + AppHandler.Instance.GetLocalPort(EInboundProtocol.split).ToString(); + processArgs += " --socks5 " + Global.Loopback + ":" + port.ToString(); // outbound processArgs += " --server " + node.Address + ":" + node.Port; diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigClashService.cs similarity index 93% rename from v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs rename to v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigClashService.cs index 50c6df67..313dabf0 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigClashService.cs @@ -1,19 +1,21 @@ -namespace ServiceLib.Services.CoreConfig; +namespace ServiceLib.Services.CoreConfig.Minimal; /// /// Core configuration file processing class /// -public class CoreConfigClashService +public class CoreConfigClashService(Config config) : CoreConfigServiceMinimalBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigClashService"; - - public CoreConfigClashService(Config config) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { - _config = config; + var ret = new RetResult + { + Success = false, + Msg = ResUI.OperationFailed + }; + return await Task.FromResult(ret); } - public async Task GenerateClientCustomConfig(ProfileItem node, string? fileName) + public override async Task GenerateClientCustomConfig(ProfileItem node, string? fileName) { var ret = new RetResult(); if (node == null || fileName is null) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigHy2Service.cs b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigHy2Service.cs index 819a3e55..fe269a5b 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigHy2Service.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigHy2Service.cs @@ -1,17 +1,9 @@ using System.Text.Json.Nodes; namespace ServiceLib.Services.CoreConfig.Minimal; -public class CoreConfigHy2Service +public class CoreConfigHy2Service(Config config) : CoreConfigServiceMinimalBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigHy2Service"; - - public CoreConfigHy2Service(Config config) - { - _config = config; - } - - public async Task GeneratePureEndpointConfig(ProfileItem node) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -34,14 +26,14 @@ public class CoreConfigHy2Service // inbound configJsonNode["socks5"] = new JsonObject { - ["listen"] = Global.Loopback + ":" + AppHandler.Instance.GetLocalPort(EInboundProtocol.split).ToString() + ["listen"] = Global.Loopback + ":" + port.ToString() }; // outbound - var port = string.Empty; + var outboundPort = string.Empty; if (node.Ports.IsNotEmpty()) { - port = node.Ports.Replace(':', '-'); + outboundPort = node.Ports.Replace(':', '-'); if (_config.HysteriaItem.HopInterval > 0) { configJsonNode["transport"] = new JsonObject @@ -55,9 +47,9 @@ public class CoreConfigHy2Service } else { - port = node.Port.ToString(); + outboundPort = node.Port.ToString(); } - configJsonNode["server"] = node.Address + ":" + port; + configJsonNode["server"] = node.Address + ":" + outboundPort; configJsonNode["auth"] = node.Id; if (node.Sni.IsNotEmpty()) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigJuicityService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigJuicityService.cs index 66fccbbd..0d73147d 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigJuicityService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigJuicityService.cs @@ -1,17 +1,9 @@ using System.Text.Json.Nodes; namespace ServiceLib.Services.CoreConfig.Minimal; -public class CoreConfigJuicityService +public class CoreConfigJuicityService(Config config) : CoreConfigServiceMinimalBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigJuicityService"; - - public CoreConfigJuicityService(Config config) - { - _config = config; - } - - public async Task GeneratePureEndpointConfig(ProfileItem node) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -45,7 +37,7 @@ public class CoreConfigJuicityService configJsonNode["log_level"] = logLevel; // inbound - configJsonNode["listen"] = ":" + AppHandler.Instance.GetLocalPort(EInboundProtocol.split).ToString(); + configJsonNode["listen"] = ":" + port.ToString(); // outbound configJsonNode["server"] = node.Address + ":" + node.Port; diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigNaiveService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigNaiveService.cs index 0e1230cc..b86e053a 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigNaiveService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigNaiveService.cs @@ -1,17 +1,9 @@ using System.Text.Json.Nodes; namespace ServiceLib.Services.CoreConfig.Minimal; -public class CoreConfigNaiveService +public class CoreConfigNaiveService(Config config) : CoreConfigServiceMinimalBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigNaiveService"; - - public CoreConfigNaiveService(Config config) - { - _config = config; - } - - public async Task GeneratePureEndpointConfig(ProfileItem node) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -32,7 +24,7 @@ public class CoreConfigNaiveService var configJsonNode = new JsonObject(); // inbound - configJsonNode["listen"] = Global.SocksProtocol + Global.Loopback + ":" + AppHandler.Instance.GetLocalPort(EInboundProtocol.split).ToString(); + configJsonNode["listen"] = Global.SocksProtocol + Global.Loopback + ":" + port.ToString(); // outbound configJsonNode["proxy"] = (node.HeaderType == "quic" ? "quic://" : Global.HttpsProtocol) + node.Id + "@" + node.Address + ":" + node.Port; diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigShadowquicService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigShadowquicService.cs index c16c99f3..86ec9c53 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigShadowquicService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigShadowquicService.cs @@ -1,17 +1,9 @@ using System.Text.Json.Nodes; namespace ServiceLib.Services.CoreConfig.Minimal; -public class CoreConfigShadowquicService +public class CoreConfigShadowquicService(Config config) : CoreConfigServiceMinimalBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigShadowquicService"; - - public CoreConfigShadowquicService(Config config) - { - _config = config; - } - - public async Task GeneratePureEndpointConfig(ProfileItem node) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -48,7 +40,7 @@ public class CoreConfigShadowquicService var inboundNode = new Dictionary { ["type"] = "socks5", - ["listen"] = Global.Loopback + ":" + AppHandler.Instance.GetLocalPort(EInboundProtocol.split).ToString() + ["listen"] = Global.Loopback + ":" + port.ToString() }; configYamlNode["inbound"] = inboundNode; diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigTuicService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigTuicService.cs index 55dfecb1..0edc612c 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigTuicService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigTuicService.cs @@ -1,17 +1,9 @@ using System.Text.Json.Nodes; namespace ServiceLib.Services.CoreConfig.Minimal; -public class CoreConfigTuicService +public class CoreConfigTuicService(Config config) : CoreConfigServiceMinimalBase(config) { - private Config _config; - private static readonly string _tag = "CoreConfigTuicService"; - - public CoreConfigTuicService(Config config) - { - _config = config; - } - - public async Task GeneratePureEndpointConfig(ProfileItem node) + protected override async Task GeneratePassthroughConfig(ProfileItem node, int port) { var ret = new RetResult(); try @@ -47,7 +39,7 @@ public class CoreConfigTuicService // inbound configJsonNode["local"] = new JsonObject { - ["server"] = Global.Loopback + ":" + AppHandler.Instance.GetLocalPort(EInboundProtocol.split).ToString() + ["server"] = Global.Loopback + ":" + port.ToString() }; // outbound diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index f312b890..3c25b073 100644 --- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -770,7 +770,8 @@ public class ProfilesViewModel : MyReactiveObject } if (blClipboard) { - var result = await CoreConfigHandler.GenerateClientConfig(item, null); + var coreLaunchContext = new CoreLaunchContext(item, _config); + var result = await CoreConfigHandler.GenerateClientConfig(coreLaunchContext, null); if (result.Success != true) { NoticeHandler.Instance.Enqueue(result.Msg); @@ -793,7 +794,8 @@ public class ProfilesViewModel : MyReactiveObject { return; } - var result = await CoreConfigHandler.GenerateClientConfig(item, fileName); + var coreLaunchContext = new CoreLaunchContext(item, _config); + var result = await CoreConfigHandler.GenerateClientConfig(coreLaunchContext, fileName); if (result.Success != true) { NoticeHandler.Instance.Enqueue(result.Msg);