diff --git a/v2rayN/ServiceLib/Common/EmbedUtils.cs b/v2rayN/ServiceLib/Common/EmbedUtils.cs new file mode 100644 index 00000000..b647d154 --- /dev/null +++ b/v2rayN/ServiceLib/Common/EmbedUtils.cs @@ -0,0 +1,61 @@ +using System.Collections.Concurrent; +using System.Reflection; + +namespace ServiceLib.Common; + +public static class EmbedUtils +{ + private static readonly string _tag = "EmbedUtils"; + private static readonly ConcurrentDictionary _dicEmbedCache = new(); + + /// + /// Get embedded text resources + /// + /// + /// + public static string GetEmbedText(string res) + { + if (_dicEmbedCache.TryGetValue(res, out var value)) + { + return value; + } + var result = string.Empty; + + try + { + var assembly = Assembly.GetExecutingAssembly(); + using var stream = assembly.GetManifestResourceStream(res); + ArgumentNullException.ThrowIfNull(stream); + using StreamReader reader = new(stream); + result = reader.ReadToEnd(); + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + } + + _dicEmbedCache.TryAdd(res, result); + return result; + } + + /// + /// Get local storage resources + /// + /// + public static string? LoadResource(string? res) + { + try + { + if (File.Exists(res)) + { + return File.ReadAllText(res); + } + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + } + + return null; + } +} diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index b318f89d..f4b60f3c 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -17,56 +17,6 @@ namespace ServiceLib.Common { private static readonly string _tag = "Utils"; - #region 资源操作 - - /// - /// 获取嵌入文本资源 - /// - /// - /// - public static string GetEmbedText(string res) - { - var result = string.Empty; - - try - { - var assembly = Assembly.GetExecutingAssembly(); - using var stream = assembly.GetManifestResourceStream(res); - ArgumentNullException.ThrowIfNull(stream); - using StreamReader reader = new(stream); - result = reader.ReadToEnd(); - } - catch (Exception ex) - { - Logging.SaveLog(_tag, ex); - } - - return result; - } - - /// - /// 取得存储资源 - /// - /// - public static string? LoadResource(string? res) - { - try - { - if (File.Exists(res)) - { - return File.ReadAllText(res); - } - } - catch (Exception ex) - { - Logging.SaveLog(_tag, ex); - } - - return null; - } - - #endregion 资源操作 - #region 转换函数 /// diff --git a/v2rayN/ServiceLib/Handler/AutoStartupHandler.cs b/v2rayN/ServiceLib/Handler/AutoStartupHandler.cs index 2e3846b2..79ade84b 100644 --- a/v2rayN/ServiceLib/Handler/AutoStartupHandler.cs +++ b/v2rayN/ServiceLib/Handler/AutoStartupHandler.cs @@ -142,7 +142,7 @@ namespace ServiceLib.Handler { try { - var linuxConfig = Utils.GetEmbedText(Global.LinuxAutostartConfig); + var linuxConfig = EmbedUtils.GetEmbedText(Global.LinuxAutostartConfig); if (linuxConfig.IsNotEmpty()) { linuxConfig = linuxConfig.Replace("$ExecPath$", Utils.GetExePath()); diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index f5732982..5932d54f 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -21,7 +21,7 @@ namespace ServiceLib.Handler public static Config? LoadConfig() { Config? config = null; - var result = Utils.LoadResource(Utils.GetConfigPath(_configRes)); + var result = EmbedUtils.LoadResource(Utils.GetConfigPath(_configRes)); if (Utils.IsNotEmpty(result)) { config = JsonUtils.Deserialize(result); @@ -1735,7 +1735,7 @@ namespace ServiceLib.Handler Url = string.Empty, Sort = maxSort + 1, }; - await AddBatchRoutingRules(item2, Utils.GetEmbedText(Global.CustomRoutingFileName + "white")); + await AddBatchRoutingRules(item2, EmbedUtils.GetEmbedText(Global.CustomRoutingFileName + "white")); //Blacklist var item3 = new RoutingItem() @@ -1744,7 +1744,7 @@ namespace ServiceLib.Handler Url = string.Empty, Sort = maxSort + 2, }; - await AddBatchRoutingRules(item3, Utils.GetEmbedText(Global.CustomRoutingFileName + "black")); + await AddBatchRoutingRules(item3, EmbedUtils.GetEmbedText(Global.CustomRoutingFileName + "black")); //Global var item1 = new RoutingItem() @@ -1753,7 +1753,7 @@ namespace ServiceLib.Handler Url = string.Empty, Sort = maxSort + 3, }; - await AddBatchRoutingRules(item1, Utils.GetEmbedText(Global.CustomRoutingFileName + "global")); + await AddBatchRoutingRules(item1, EmbedUtils.GetEmbedText(Global.CustomRoutingFileName + "global")); if (!blImportAdvancedRules) { diff --git a/v2rayN/ServiceLib/Handler/PacHandler.cs b/v2rayN/ServiceLib/Handler/PacHandler.cs index 7eaa59b9..b6f81ca6 100644 --- a/v2rayN/ServiceLib/Handler/PacHandler.cs +++ b/v2rayN/ServiceLib/Handler/PacHandler.cs @@ -35,7 +35,7 @@ namespace ServiceLib.Handler var path = Path.Combine(_configPath, "pac.txt"); if (!File.Exists(path)) { - var pac = Utils.GetEmbedText(Global.PacFileName); + var pac = EmbedUtils.GetEmbedText(Global.PacFileName); await File.AppendAllTextAsync(path, pac); } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs index 2a732e2b..8867dce3 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs @@ -116,7 +116,7 @@ namespace ServiceLib.Services.CoreConfig //enable tun mode if (_config.TunModeItem.EnableTun) { - string tun = Utils.GetEmbedText(Global.ClashTunYaml); + string tun = EmbedUtils.GetEmbedText(Global.ClashTunYaml); if (Utils.IsNotEmpty(tun)) { var tunContent = YamlUtils.FromYaml>(tun); diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 046989dc..3b044f57 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -35,7 +35,7 @@ namespace ServiceLib.Services.CoreConfig ret.Msg = ResUI.InitialConfiguration; - string result = Utils.GetEmbedText(Global.SingboxSampleClient); + string result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient); if (Utils.IsNullOrEmpty(result)) { ret.Msg = ResUI.FailedGetDefaultConfiguration; @@ -91,8 +91,8 @@ namespace ServiceLib.Services.CoreConfig ret.Msg = ResUI.InitialConfiguration; - var result = Utils.GetEmbedText(Global.SingboxSampleClient); - var txtOutbound = Utils.GetEmbedText(Global.SingboxSampleOutbound); + var result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient); + var txtOutbound = EmbedUtils.GetEmbedText(Global.SingboxSampleOutbound); if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty()) { ret.Msg = ResUI.FailedGetDefaultConfiguration; @@ -255,8 +255,8 @@ namespace ServiceLib.Services.CoreConfig ret.Msg = ResUI.InitialConfiguration; - string result = Utils.GetEmbedText(Global.SingboxSampleClient); - string txtOutbound = Utils.GetEmbedText(Global.SingboxSampleOutbound); + string result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient); + string txtOutbound = EmbedUtils.GetEmbedText(Global.SingboxSampleOutbound); if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty()) { ret.Msg = ResUI.FailedGetDefaultConfiguration; @@ -546,7 +546,7 @@ namespace ServiceLib.Services.CoreConfig _config.TunModeItem.Stack = Global.TunStacks.First(); } - var tunInbound = JsonUtils.Deserialize(Utils.GetEmbedText(Global.TunSingboxInboundFileName)) ?? new Inbound4Sbox { }; + var tunInbound = JsonUtils.Deserialize(EmbedUtils.GetEmbedText(Global.TunSingboxInboundFileName)) ?? new Inbound4Sbox { }; tunInbound.interface_name = Utils.IsOSX() ? $"utun{new Random().Next(99)}" : "singbox_tun"; tunInbound.mtu = _config.TunModeItem.Mtu; tunInbound.strict_route = _config.TunModeItem.StrictRoute; @@ -867,7 +867,7 @@ namespace ServiceLib.Services.CoreConfig //current proxy var outbound = singboxConfig.outbounds.First(); - var txtOutbound = Utils.GetEmbedText(Global.SingboxSampleOutbound); + var txtOutbound = EmbedUtils.GetEmbedText(Global.SingboxSampleOutbound); //Previous proxy var prevNode = await AppHandler.Instance.GetProfileItemViaRemarks(subItem.PrevProfile); @@ -934,7 +934,7 @@ namespace ServiceLib.Services.CoreConfig { singboxConfig.route.auto_detect_interface = true; - var tunRules = JsonUtils.Deserialize>(Utils.GetEmbedText(Global.TunSingboxRulesFileName)); + var tunRules = JsonUtils.Deserialize>(EmbedUtils.GetEmbedText(Global.TunSingboxRulesFileName)); if (tunRules != null) { singboxConfig.route.rules.AddRange(tunRules); @@ -1171,11 +1171,11 @@ namespace ServiceLib.Services.CoreConfig var strDNS = string.Empty; if (_config.TunModeItem.EnableTun) { - strDNS = Utils.IsNullOrEmpty(item?.TunDNS) ? Utils.GetEmbedText(Global.TunSingboxDNSFileName) : item?.TunDNS; + strDNS = Utils.IsNullOrEmpty(item?.TunDNS) ? EmbedUtils.GetEmbedText(Global.TunSingboxDNSFileName) : item?.TunDNS; } else { - strDNS = Utils.IsNullOrEmpty(item?.NormalDNS) ? Utils.GetEmbedText(Global.DNSSingboxNormalFileName) : item?.NormalDNS; + strDNS = Utils.IsNullOrEmpty(item?.NormalDNS) ? EmbedUtils.GetEmbedText(Global.DNSSingboxNormalFileName) : item?.NormalDNS; } var dns4Sbox = JsonUtils.Deserialize(strDNS); @@ -1326,7 +1326,7 @@ namespace ServiceLib.Services.CoreConfig var routing = await ConfigHandler.GetDefaultRouting(_config); if (Utils.IsNotEmpty(routing.CustomRulesetPath4Singbox)) { - var result = Utils.LoadResource(routing.CustomRulesetPath4Singbox); + var result = EmbedUtils.LoadResource(routing.CustomRulesetPath4Singbox); if (Utils.IsNotEmpty(result)) { customRulesets = (JsonUtils.Deserialize>(result) ?? []) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs index 998002cf..c79cc56c 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs @@ -1,4 +1,4 @@ -using System.Net; +using System.Net; using System.Net.NetworkInformation; using System.Text.Json.Nodes; @@ -36,7 +36,7 @@ namespace ServiceLib.Services.CoreConfig ret.Msg = ResUI.InitialConfiguration; - var result = Utils.GetEmbedText(Global.V2raySampleClient); + var result = EmbedUtils.GetEmbedText(Global.V2raySampleClient); if (Utils.IsNullOrEmpty(result)) { ret.Msg = ResUI.FailedGetDefaultConfiguration; @@ -91,8 +91,8 @@ namespace ServiceLib.Services.CoreConfig ret.Msg = ResUI.InitialConfiguration; - string result = Utils.GetEmbedText(Global.V2raySampleClient); - string txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound); + string result = EmbedUtils.GetEmbedText(Global.V2raySampleClient); + string txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound); if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty()) { ret.Msg = ResUI.FailedGetDefaultConfiguration; @@ -214,8 +214,8 @@ namespace ServiceLib.Services.CoreConfig ret.Msg = ResUI.InitialConfiguration; - var result = Utils.GetEmbedText(Global.V2raySampleClient); - var txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound); + var result = EmbedUtils.GetEmbedText(Global.V2raySampleClient); + var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound); if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty()) { ret.Msg = ResUI.FailedGetDefaultConfiguration; @@ -428,7 +428,7 @@ namespace ServiceLib.Services.CoreConfig private Inbounds4Ray GetInbound(InItem inItem, EInboundProtocol protocol, bool bSocks) { - string result = Utils.GetEmbedText(Global.V2raySampleInbound); + string result = EmbedUtils.GetEmbedText(Global.V2raySampleInbound); if (Utils.IsNullOrEmpty(result)) { return new(); @@ -992,7 +992,7 @@ namespace ServiceLib.Services.CoreConfig }; //request Host - string request = Utils.GetEmbedText(Global.V2raySampleHttpRequestFileName); + string request = EmbedUtils.GetEmbedText(Global.V2raySampleHttpRequestFileName); string[] arrHost = host.Split(','); string host2 = string.Join(",".AppendQuotes(), arrHost); request = request.Replace("$requestHost$", $"{host2.AppendQuotes()}"); @@ -1028,7 +1028,7 @@ namespace ServiceLib.Services.CoreConfig var domainStrategy4Freedom = item?.DomainStrategy4Freedom; if (Utils.IsNullOrEmpty(normalDNS)) { - normalDNS = Utils.GetEmbedText(Global.DNSV2rayNormalFileName); + normalDNS = EmbedUtils.GetEmbedText(Global.DNSV2rayNormalFileName); } //Outbound Freedom domainStrategy @@ -1196,7 +1196,7 @@ namespace ServiceLib.Services.CoreConfig //current proxy var outbound = v2rayConfig.outbounds.First(); - var txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound); + var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound); //Previous proxy var prevNode = await AppHandler.Instance.GetProfileItemViaRemarks(subItem.PrevProfile); @@ -1247,4 +1247,4 @@ namespace ServiceLib.Services.CoreConfig #endregion private gen function } -} \ No newline at end of file +} diff --git a/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs index bf8252f8..9dbdba49 100644 --- a/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs @@ -31,14 +31,14 @@ namespace ServiceLib.ViewModels ImportDefConfig4V2rayCmd = ReactiveCommand.CreateFromTask(async () => { - normalDNS = Utils.GetEmbedText(Global.DNSV2rayNormalFileName); + normalDNS = EmbedUtils.GetEmbedText(Global.DNSV2rayNormalFileName); await Task.CompletedTask; }); ImportDefConfig4SingboxCmd = ReactiveCommand.CreateFromTask(async () => { - normalDNS2 = Utils.GetEmbedText(Global.DNSSingboxNormalFileName); - tunDNS2 = Utils.GetEmbedText(Global.TunSingboxDNSFileName); + normalDNS2 = EmbedUtils.GetEmbedText(Global.DNSSingboxNormalFileName); + tunDNS2 = EmbedUtils.GetEmbedText(Global.TunSingboxDNSFileName); await Task.CompletedTask; }); diff --git a/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs index 02aa8c57..76cd301c 100644 --- a/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs @@ -257,7 +257,7 @@ namespace ServiceLib.ViewModels return; } - var result = Utils.LoadResource(fileName); + var result = EmbedUtils.LoadResource(fileName); if (Utils.IsNullOrEmpty(result)) { return;