From e03cf8f5183116f514284cdc17fb1d4e00f03bb2 Mon Sep 17 00:00:00 2001 From: snltty <1069410172@qq.com> Date: Sun, 25 May 2025 16:48:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E5=AD=98=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=97=B6=E5=88=A0=E9=99=A4=E4=B8=80=E4=BA=9B=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E5=86=85=E5=AE=B9=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=94=A4=E9=86=92=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/linker.messenger.store.file/FileConfig.cs | 109 +++++++++--------- .../RunningConfig.cs | 1 + .../relay/Config.cs | 2 + .../signIn/Config.cs | 4 + src/linker.messenger.tuntap/TuntapDecenter.cs | 4 +- version.txt | 2 +- 6 files changed, 66 insertions(+), 56 deletions(-) diff --git a/src/linker.messenger.store.file/FileConfig.cs b/src/linker.messenger.store.file/FileConfig.cs index 3b965869..38d032ca 100644 --- a/src/linker.messenger.store.file/FileConfig.cs +++ b/src/linker.messenger.store.file/FileConfig.cs @@ -7,7 +7,7 @@ using System.Text.Json.Serialization; using linker.libs.timer; using System.Text.Json; using System.Text.Json.Nodes; -using linker.messenger.tunnel.stun.enums; +using System.Collections.Generic; namespace linker.messenger.store.file { @@ -21,6 +21,7 @@ namespace linker.messenger.store.file private string configPath = "./configs/"; private Dictionary fsDic = new Dictionary(); + private List saveJsonIgnorePaths = new List(); public ConfigInfo Data { get; private set; } = new ConfigInfo(); @@ -41,45 +42,39 @@ namespace linker.messenger.store.file private void Init() { - /* - Type saveAttr = typeof(SaveJsonIgnore); - List> saveJsonIgnores = FindPathsWithSaveJsonIgnore(Data); - List> FindPathsWithSaveJsonIgnore(object obj) - { - var paths = new List>(); - FindPathsRecursive(obj, new List(), paths); - return paths; - } - void FindPathsRecursive(object obj, List currentPath, List> resultPaths) - { - if (obj == null) return; - - Type type = obj.GetType(); - foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) - { - currentPath.Add(property.Name); - currentPath = currentPath.ToArray().ToList(); - - if (Attribute.IsDefined(property, saveAttr)) - { - resultPaths.Add(currentPath); - } - else if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && !property.PropertyType.IsArray && !typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType)) - { - try - { - object value = property.GetValue(obj); - FindPathsRecursive(value, currentPath, resultPaths); - } - catch (Exception) - { - } - } - } - } - Console.WriteLine(saveJsonIgnores.ToJson()); - */ + Type saveAttr = typeof(SaveJsonIgnore); + saveJsonIgnorePaths = FindPathsWithSaveJsonIgnore(Data); + List FindPathsWithSaveJsonIgnore(object obj) + { + var paths = new List(); + FindPathsRecursive(obj, string.Empty, paths); + return paths; + } + void FindPathsRecursive(object obj, string currentPath, List resultPaths) + { + if (obj == null) return; + Type type = obj.GetType(); + foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) + { + string propertyPath = string.IsNullOrEmpty(currentPath) ? property.Name : $"{currentPath}.{property.Name}"; + if (Attribute.IsDefined(property, saveAttr)) + { + resultPaths.Add([.. propertyPath.Split('.')]); + } + else if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && !property.PropertyType.IsArray && !typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType)) + { + try + { + object value = property.GetValue(obj); + FindPathsRecursive(value, propertyPath, resultPaths); + } + catch (Exception) + { + } + } + } + } if (Directory.Exists(Path.Combine(Helper.currentDirectory, configPath)) == false) { Directory.CreateDirectory(Path.Combine(Helper.currentDirectory, configPath)); @@ -156,8 +151,28 @@ namespace linker.messenger.store.file LoggerHelper.Instance.Error($"{item.Value.Property.Name} save not found"); continue; } - string text = item.Value.PropertyMethod.Serialize(item.Value.Property.GetValue(Data)); + JsonNode jsonNode = JsonNode.Parse(item.Value.Property.GetValue(Data).ToJson()); + try + { + foreach (string[] path in saveJsonIgnorePaths.Where(c => c.Length > 0 && c[0] == item.Value.Property.Name)) + { + if (path.Length == 0) continue; + + JsonNode root = jsonNode; + for (int i = 1; i < path.Length - 1; i++) + { + if (root.AsObject().TryGetPropertyValue(path[i], out root) == false || root == null) break; + if (root == null) break; + } + root?.AsObject().Remove(path[^1]); + } + } + catch (Exception) + { + } + + string text = item.Value.PropertyMethod.Serialize(jsonNode); if (json != null && json.RootElement.TryGetProperty(item.Value.Property.Name, out JsonElement import)) { text = item.Value.PropertyMethod.Deserialize(text).ToJson(); @@ -183,6 +198,7 @@ namespace linker.messenger.store.file } finally { + GC.Collect(); slim.Release(); } @@ -297,18 +313,6 @@ namespace linker.messenger.store.file public string Serialize(object obj) { - /* - JsonNode jsonNode = JsonNode.Parse(obj.ToJson()); - JsonObject jsonObj = jsonNode.AsObject(); - - jsonObj.Remove("Accesss"); - jsonObj.Remove("Server"); - jsonObj.Remove("Group"); - if (jsonObj.TryGetPropertyValue("Relay", out JsonNode relay)) - { - relay.AsObject().Remove("Server"); - } - */ #if DEBUG return obj.ToJsonFormat(); #else @@ -364,7 +368,6 @@ namespace linker.messenger.store.file [AttributeUsage(AttributeTargets.Property)] public sealed class SaveJsonIgnore : Attribute { - } } diff --git a/src/linker.messenger.store.file/RunningConfig.cs b/src/linker.messenger.store.file/RunningConfig.cs index d10b7c30..a62d0fe8 100644 --- a/src/linker.messenger.store.file/RunningConfig.cs +++ b/src/linker.messenger.store.file/RunningConfig.cs @@ -95,6 +95,7 @@ namespace linker.messenger.store.file } finally { + GC.Collect(); slim.Release(); } } diff --git a/src/linker.messenger.store.file/relay/Config.cs b/src/linker.messenger.store.file/relay/Config.cs index f0752936..c16e5065 100644 --- a/src/linker.messenger.store.file/relay/Config.cs +++ b/src/linker.messenger.store.file/relay/Config.cs @@ -28,6 +28,8 @@ namespace linker.messenger.store.file /// 中继服务器列表 /// public RelayServerInfo[] Servers { get; set; } = new RelayServerInfo[] { new RelayServerInfo { } }; + + [SaveJsonIgnore] public RelayServerInfo Server => Servers[0]; } diff --git a/src/linker.messenger.store.file/signIn/Config.cs b/src/linker.messenger.store.file/signIn/Config.cs index 336ecce6..165c6bbc 100644 --- a/src/linker.messenger.store.file/signIn/Config.cs +++ b/src/linker.messenger.store.file/signIn/Config.cs @@ -18,6 +18,8 @@ namespace linker.messenger.store.file new SignInClientServerInfo{ Name="Linker", Host="linker.snltty.com:1802" } #endif }; + + [SaveJsonIgnore] public SignInClientServerInfo Server => Servers[0]; public SignInClientServerInfo[] Servers { @@ -43,6 +45,8 @@ namespace linker.messenger.store.file } private SignInClientGroupInfo[] groups = new[] { new SignInClientGroupInfo { } }; + + [SaveJsonIgnore] public SignInClientGroupInfo Group => Groups[0]; public SignInClientGroupInfo[] Groups { diff --git a/src/linker.messenger.tuntap/TuntapDecenter.cs b/src/linker.messenger.tuntap/TuntapDecenter.cs index ecc036d1..d83405f8 100644 --- a/src/linker.messenger.tuntap/TuntapDecenter.cs +++ b/src/linker.messenger.tuntap/TuntapDecenter.cs @@ -136,7 +136,7 @@ namespace linker.messenger.tuntap { tuntapProxy.SetIP(item.MachineId, NetworkHelper.ToValue(item.IP)); } - foreach (var item in Infos.Values.Where(c => c.Available==false || c.Exists || c.IP.Equals(IPAddress.Any))) + foreach (var item in Infos.Values.Where(c => c.Available == false || c.Exists || c.IP.Equals(IPAddress.Any))) { tuntapProxy.RemoveIP(item.MachineId); } @@ -156,7 +156,7 @@ namespace linker.messenger.tuntap foreach (var item in infos.Where(c => c.Available == true).OrderBy(c => c.IP, new IPAddressComparer()).OrderByDescending(c => c.Status)) { - item.Exists = hashSet.Contains(NetworkHelper.ToValue(item.IP)); + item.Exists = item.IP.Equals(IPAddress.Any) == false && hashSet.Contains(NetworkHelper.ToValue(item.IP)); hashSet.Add(NetworkHelper.ToValue(item.IP)); } diff --git a/version.txt b/version.txt index f64477be..fd4635b7 100644 --- a/version.txt +++ b/version.txt @@ -1,4 +1,4 @@ v1.8.1 -2025-05-25 15:50:58 +2025-05-25 16:47:59 1. 一些累计更新 2. 优化修复一些已知BUG \ No newline at end of file