add function to mixin, support append and prepend

This commit is contained in:
2dust
2022-08-25 15:55:01 +08:00
parent 181ed3bc6c
commit 7955ff5415
2 changed files with 66 additions and 2 deletions

View File

@@ -123,7 +123,14 @@ namespace clashN.Handler
} }
//Mixin //Mixin
MixinContent(fileContent, config, node); try
{
MixinContent(fileContent, config, node);
}
catch (Exception ex)
{
Utils.SaveLog("GenerateClientCustomConfig-Mixin", ex);
}
File.WriteAllText(fileName, Utils.ToYaml(fileContent)); File.WriteAllText(fileName, Utils.ToYaml(fileContent));
//check again //check again
@@ -174,7 +181,14 @@ namespace clashN.Handler
continue; 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; return;
} }
@@ -190,5 +204,45 @@ namespace clashN.Handler
fileContent.Add(key, value); fileContent.Add(key, value);
} }
} }
private static void ModifyContentMerge(Dictionary<string, object> 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<object>)fileContent[key];
var lstValue = (List<object>)value;
if (blPrepend)
{
lstValue.Reverse();
foreach (var item in lstValue)
{
lstOri.Insert(0, item);
}
}
else
{
foreach (var item in lstValue)
{
lstOri.Add(item);
}
}
}
} }
} }

View File

@@ -3,6 +3,16 @@
# #
# 注意下面缩进请用支持yaml显示的编辑器打开 # 注意下面缩进请用支持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: dns:
enable: true enable: true
enhanced-mode: fake-ip enhanced-mode: fake-ip