From 7955ff541531dca25c6e81a8453d4ffc04f71218 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 25 Aug 2022 15:55:01 +0800 Subject: [PATCH] add function to mixin, support append and prepend --- clashN/clashN/Handler/CoreConfigHandler.cs | 58 +++++++++++++++++++++- clashN/clashN/Sample/SampleMixin.yaml | 10 ++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/clashN/clashN/Handler/CoreConfigHandler.cs b/clashN/clashN/Handler/CoreConfigHandler.cs index 52faf23..83a95a7 100644 --- a/clashN/clashN/Handler/CoreConfigHandler.cs +++ b/clashN/clashN/Handler/CoreConfigHandler.cs @@ -123,7 +123,14 @@ namespace clashN.Handler } //Mixin - MixinContent(fileContent, config, node); + try + { + MixinContent(fileContent, config, node); + } + catch (Exception ex) + { + Utils.SaveLog("GenerateClientCustomConfig-Mixin", ex); + } File.WriteAllText(fileName, Utils.ToYaml(fileContent)); //check again @@ -174,7 +181,14 @@ namespace clashN.Handler continue; } - ModifyContent(fileContent, item.Key, item.Value); + if (item.Key.StartsWith("prepend-") || item.Key.StartsWith("append-")) + { + ModifyContentMerge(fileContent, item.Key, item.Value); + } + else + { + ModifyContent(fileContent, item.Key, item.Value); + } } return; } @@ -190,5 +204,45 @@ namespace clashN.Handler fileContent.Add(key, value); } } + private static void ModifyContentMerge(Dictionary fileContent, string key, object value) + { + bool blPrepend = false; + if (key.StartsWith("prepend-")) + { + blPrepend = true; + key = key.Replace("prepend-", ""); + } + else if (key.StartsWith("append-")) + { + blPrepend = false; + key = key.Replace("append-", ""); + } + else + { + return; + } + if (!fileContent.ContainsKey(key)) + { + return; + } + var lstOri = (List)fileContent[key]; + var lstValue = (List)value; + + if (blPrepend) + { + lstValue.Reverse(); + foreach (var item in lstValue) + { + lstOri.Insert(0, item); + } + } + else + { + foreach (var item in lstValue) + { + lstOri.Add(item); + } + } + } } } diff --git a/clashN/clashN/Sample/SampleMixin.yaml b/clashN/clashN/Sample/SampleMixin.yaml index a5df953..5619902 100644 --- a/clashN/clashN/Sample/SampleMixin.yaml +++ b/clashN/clashN/Sample/SampleMixin.yaml @@ -3,6 +3,16 @@ # # 注意下面缩进,请用支持yaml显示的编辑器打开 # +# 使用clash配置文件关键字则覆盖原配置 +# +# append-rules 数组合并至原配置rules数组后 +# prepend-rules 数组合并至原配置rules数组前 +# append-proxies 数组合并至原配置proxies数组后 +# prepend-proxies 数组合并至原配置proxies数组前 +# append-proxy-groups 数组合并至原配置proxy-groups数组后 +# prepend-proxy-groups 数组合并至原配置proxy-groups数组前 +# + dns: enable: true enhanced-mode: fake-ip