From 5bbef8db1221eda3e9813db8c081249a73d47336 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Wed, 2 Jun 2021 14:01:15 +0800 Subject: [PATCH] Refactor clean temp file Refactor Mode.GetRules() yield --- Netch/Constants.cs | 2 ++ Netch/Controllers/TUNController.cs | 5 ++--- Netch/Forms/MainForm.cs | 4 +--- Netch/Models/Mode.cs | 8 +++----- Netch/Models/NetRoute.cs | 6 +++--- Netch/Servers/Trojan/TrojanController.cs | 2 +- Netch/Servers/V2ray/V2rayController.cs | 2 +- 7 files changed, 13 insertions(+), 16 deletions(-) diff --git a/Netch/Constants.cs b/Netch/Constants.cs index fbff7201..614417a4 100644 --- a/Netch/Constants.cs +++ b/Netch/Constants.cs @@ -2,6 +2,8 @@ { public static class Constants { + public const string TempConfig = "data\\last.json"; + public const string TempRouteFile = "data\\route.txt"; public const string EOF = "\r\n"; public const string OutputTemplate = @"[{Timestamp:yyyy-MM-dd HH:mm:ss}][{Level}] {Message:lj}{NewLine}{Exception}"; public const string LogFile = "logging\\application.log"; diff --git a/Netch/Controllers/TUNController.cs b/Netch/Controllers/TUNController.cs index bc95f6d0..bcaa176a 100644 --- a/Netch/Controllers/TUNController.cs +++ b/Netch/Controllers/TUNController.cs @@ -36,8 +36,7 @@ namespace Netch.Controllers var server = MainController.Server!; _serverAddresses = DnsUtils.Lookup(server.Hostname)!; // server address have been cached when MainController.Start - IPAddress address; - (_outbound, address) = NetRoute.GetBestRouteTemplate(); + _outbound = NetRoute.GetBestRouteTemplate(out var address); CheckDriver(); Dial(NameList.TYPE_ADAPMTU, "1500"); @@ -202,7 +201,7 @@ namespace Netch.Controllers } catch (Exception e) { - Log.Error(e,"复制 wintun.dll 异常"); + Log.Error(e, "复制 wintun.dll 异常"); throw new MessageException($"Failed to copy wintun.dll to system directory: {e.Message}"); } } diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 316838bb..8d592aee 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1225,11 +1225,9 @@ namespace Netch.Forms Hide(); if (saveConfiguration) - { Configuration.Save(); - } - foreach (var file in new[] { "data\\last.json", "data\\privoxy.conf" }) + foreach (var file in new[] { Constants.TempConfig, Constants.TempRouteFile }) if (File.Exists(file)) File.Delete(file); diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index eac280e9..9f2e9764 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -48,7 +48,6 @@ namespace Netch.Models public IEnumerable GetRules() { - var result = new List(); foreach (var s in Content) { if (string.IsNullOrWhiteSpace(s)) @@ -76,15 +75,14 @@ namespace Netch.Models if (mode.Content.Any(rule => rule.StartsWith(include))) throw new Exception("Cannot reference mode that reference other mode"); - result.AddRange(mode.GetRules()); + foreach (var rule in mode.GetRules()) + yield return rule; } else { - result.Add(s); + yield return s; } } - - return result; } private static (string, ModeType) ReadHead(string fileName) diff --git a/Netch/Models/NetRoute.cs b/Netch/Models/NetRoute.cs index 4c87551e..01b5073d 100644 --- a/Netch/Models/NetRoute.cs +++ b/Netch/Models/NetRoute.cs @@ -16,14 +16,14 @@ namespace Netch.Models }; } - public static (NetRoute, IPAddress address) GetBestRouteTemplate() + public static NetRoute GetBestRouteTemplate(out IPAddress address) { if (IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route) != 0) throw new MessageException("GetBestRoute 搜索失败"); - var address = new IPAddress(route.dwForwardNextHop.S_addr); + address = new IPAddress(route.dwForwardNextHop.S_addr); var gateway = new IPAddress(route.dwForwardNextHop.S_un_b); - return (TemplateBuilder(gateway.ToString(), (int)route.dwForwardIfIndex), address); + return TemplateBuilder(gateway.ToString(), (int)route.dwForwardIfIndex); } public int InterfaceIndex; diff --git a/Netch/Servers/Trojan/TrojanController.cs b/Netch/Servers/Trojan/TrojanController.cs index dcf254c5..fe3f61e6 100644 --- a/Netch/Servers/Trojan/TrojanController.cs +++ b/Netch/Servers/Trojan/TrojanController.cs @@ -40,7 +40,7 @@ namespace Netch.Servers.Trojan if (!string.IsNullOrWhiteSpace(server.Host)) trojanConfig.ssl.sni = server.Host; - File.WriteAllBytes("data\\last.json", JsonSerializer.SerializeToUtf8Bytes(trojanConfig, Global.NewDefaultJsonSerializerOptions)); + File.WriteAllBytes(Constants.TempConfig, JsonSerializer.SerializeToUtf8Bytes(trojanConfig, Global.NewDefaultJsonSerializerOptions)); StartInstanceAuto("-c ..\\data\\last.json"); } diff --git a/Netch/Servers/V2ray/V2rayController.cs b/Netch/Servers/V2ray/V2rayController.cs index 4ccc4797..134d0aad 100644 --- a/Netch/Servers/V2ray/V2rayController.cs +++ b/Netch/Servers/V2ray/V2rayController.cs @@ -23,7 +23,7 @@ namespace Netch.Servers.V2ray public virtual void Start(in Server s, in Mode mode) { - File.WriteAllText("data\\last.json", V2rayConfigUtils.GenerateClientConfig(s, mode)); + File.WriteAllText(Constants.TempConfig, V2rayConfigUtils.GenerateClientConfig(s, mode)); StartInstanceAuto("-config ..\\data\\last.json"); }