Skip to content

Commit

Permalink
Add preprocess of yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Sep 6, 2024
1 parent 7901a59 commit 7ceaf5c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
29 changes: 26 additions & 3 deletions v2rayN/ServiceLib/Common/YamlUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using YamlDotNet.Serialization;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace ServiceLib.Common
Expand Down Expand Up @@ -35,13 +36,17 @@ public static T FromYaml<T>(string str)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
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);
Expand All @@ -53,6 +58,24 @@ public static string ToYaml(Object obj)
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
}
}
6 changes: 6 additions & 0 deletions v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigClash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public int GenerateClientCustomConfig(ProfileItem node, string? fileName, out st
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<Dictionary<string, object>>(txtFile);
if (fileContent == null)
{
Expand Down

0 comments on commit 7ceaf5c

Please sign in to comment.