From 7ceaf5c071ded85f8bb70c7dd154ba4d84ba681d Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 6 Sep 2024 08:57:42 +0800 Subject: [PATCH] Add preprocess of yaml file https://github.com/2dust/v2rayN/issues/5646 --- v2rayN/ServiceLib/Common/YamlUtils.cs | 29 +++++++++++++++++-- .../Handler/CoreConfig/CoreConfigClash.cs | 6 ++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/v2rayN/ServiceLib/Common/YamlUtils.cs b/v2rayN/ServiceLib/Common/YamlUtils.cs index 7fd4c67e..f87fd63c 100644 --- a/v2rayN/ServiceLib/Common/YamlUtils.cs +++ b/v2rayN/ServiceLib/Common/YamlUtils.cs @@ -1,4 +1,5 @@ -using YamlDotNet.Serialization; +using YamlDotNet.Core; +using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; namespace ServiceLib.Common @@ -35,13 +36,17 @@ namespace ServiceLib.Common /// /// /// - public static string ToYaml(Object obj) + public static string ToYaml(Object? obj) { + string result = string.Empty; + if (obj == null) + { + return result; + } var serializer = new SerializerBuilder() .WithNamingConvention(HyphenatedNamingConvention.Instance) .Build(); - string result = string.Empty; try { result = serializer.Serialize(obj); @@ -53,6 +58,24 @@ namespace ServiceLib.Common return result; } + public static string? PreprocessYaml(string str) + { + var deserializer = new DeserializerBuilder() + .WithNamingConvention(PascalCaseNamingConvention.Instance) + .Build(); + try + { + var mergingParser = new MergingParser(new Parser(new StringReader(str))); + var obj = new DeserializerBuilder().Build().Deserialize(mergingParser); + return ToYaml(obj); + } + catch (Exception ex) + { + Logging.SaveLog("PreprocessYaml", ex); + return null; + } + } + #endregion YAML } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigClash.cs b/v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigClash.cs index 878e7d7c..9e84f8ef 100644 --- a/v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigClash.cs +++ b/v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigClash.cs @@ -64,6 +64,12 @@ var txtFile = File.ReadAllText(addressFileName); txtFile = txtFile.Replace(tagYamlStr1, tagYamlStr2); + //YAML anchors + if (txtFile.Contains("<<:") && txtFile.Contains("*") && txtFile.Contains("&")) + { + txtFile = YamlUtils.PreprocessYaml(txtFile); + } + var fileContent = YamlUtils.FromYaml>(txtFile); if (fileContent == null) {