From 22d4f435de378a57c03ef9a2d86bc84d908e0b26 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 28 Jun 2024 20:29:44 +0800 Subject: [PATCH] Add tun and mixin functions to clash --- v2rayN/v2rayN/Global.cs | 3 + .../Handler/CoreConfig/CoreConfigClash.cs | 130 ++++++++++-------- v2rayN/v2rayN/Models/ConfigItems.cs | 2 + v2rayN/v2rayN/Sample/clash_mixin_yaml | 39 ++++++ v2rayN/v2rayN/Sample/clash_tun_yaml | 7 + v2rayN/v2rayN/v2rayN.csproj | 2 + 6 files changed, 124 insertions(+), 59 deletions(-) create mode 100644 v2rayN/v2rayN/Sample/clash_mixin_yaml create mode 100644 v2rayN/v2rayN/Sample/clash_tun_yaml diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 17d39a5a..7dda772e 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -31,6 +31,7 @@ namespace v2rayN public const string CoreConfigFileName = "config.json"; public const string CorePreConfigFileName = "configPre.json"; public const string CoreSpeedtestConfigFileName = "configSpeedtest.json"; + public const string ClashMixinConfigFileName = "Mixin.yaml"; public const string V2raySampleClient = "v2rayN.Sample.SampleClientConfig"; public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig"; public const string V2raySampleHttpRequestFileName = "v2rayN.Sample.SampleHttpRequest"; @@ -44,6 +45,8 @@ namespace v2rayN public const string TunSingboxRulesFileName = "v2rayN.Sample.tun_singbox_rules"; public const string DNSV2rayNormalFileName = "v2rayN.Sample.dns_v2ray_normal"; public const string DNSSingboxNormalFileName = "v2rayN.Sample.dns_singbox_normal"; + public const string ClashMixinYaml = "v2rayN.Sample.clash_mixin_yaml"; + public const string ClashTunYaml = "v2rayN.Sample.clash_tun_yaml"; public const string DefaultSecurity = "auto"; public const string DefaultNetwork = "tcp"; diff --git a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigClash.cs b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigClash.cs index 900c3fd5..80e48adb 100644 --- a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigClash.cs +++ b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigClash.cs @@ -82,7 +82,8 @@ namespace v2rayN.Handler.CoreConfig //socks-port fileContent["socks-port"] = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks); //log-level - fileContent["log-level"] = _config.coreBasicItem.loglevel; + fileContent["log-level"] = GetLogLevel(_config.coreBasicItem.loglevel); + //external-controller fileContent["external-controller"] = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}"; //allow-lan @@ -97,7 +98,7 @@ namespace v2rayN.Handler.CoreConfig } //ipv6 - //fileContent["ipv6"] = _config.EnableIpv6; + fileContent["ipv6"] = _config.clashUIItem.enableIPv6; //mode if (!fileContent.ContainsKey("mode")) @@ -112,27 +113,27 @@ namespace v2rayN.Handler.CoreConfig } } - ////enable tun mode - //if (config.EnableTun) - //{ - // string tun = Utils.GetEmbedText(Global.SampleTun); - // if (!string.IsNullOrEmpty(tun)) - // { - // var tunContent = Utils.FromYaml>(tun); - // if (tunContent != null) - // fileContent["tun"] = tunContent["tun"]; - // } - //} + //enable tun mode + if (_config.tunModeItem.enableTun) + { + string tun = Utils.GetEmbedText(Global.ClashTunYaml); + if (!string.IsNullOrEmpty(tun)) + { + var tunContent = YamlUtils.FromYaml>(tun); + if (tunContent != null) + fileContent["tun"] = tunContent["tun"]; + } + } //Mixin - //try - //{ - // MixinContent(fileContent, config, node); - //} - //catch (Exception ex) - //{ - // Logging.SaveLog("GenerateClientConfigClash-Mixin", ex); - //} + try + { + MixinContent(fileContent, node); + } + catch (Exception ex) + { + Logging.SaveLog("GenerateClientConfigClash-Mixin", ex); + } var txtFileNew = YamlUtils.ToYaml(fileContent).Replace(tagYamlStr2, tagYamlStr3); File.WriteAllText(fileName, txtFileNew); @@ -156,49 +157,48 @@ namespace v2rayN.Handler.CoreConfig return 0; } - //private static void MixinContent(Dictionary fileContent, Config config, ProfileItem node) - //{ - // if (!config.EnableMixinContent) - // { - // return; - // } + private void MixinContent(Dictionary fileContent, ProfileItem node) + { + //if (!_config.clashUIItem.enableMixinContent) + //{ + // return; + //} - // var path = Utils.GetConfigPath(Global.mixinConfigFileName); - // if (!File.Exists(path)) - // { - // return; - // } + var path = Utils.GetConfigPath(Global.ClashMixinConfigFileName); + if (!File.Exists(path)) + { + return; + } - // var txtFile = File.ReadAllText(Utils.GetConfigPath(Global.mixinConfigFileName)); - // //txtFile = txtFile.Replace("!", ""); + var txtFile = File.ReadAllText(Utils.GetConfigPath(Global.ClashMixinConfigFileName)); - // var mixinContent = YamlUtils.FromYaml>(txtFile); - // if (mixinContent == null) - // { - // return; - // } - // foreach (var item in mixinContent) - // { - // if (!config.EnableTun && item.Key == "tun") - // { - // continue; - // } + var mixinContent = YamlUtils.FromYaml>(txtFile); + if (mixinContent == null) + { + return; + } + foreach (var item in mixinContent) + { + if (!_config.tunModeItem.enableTun && item.Key == "tun") + { + continue; + } - // if (item.Key.StartsWith("prepend-") - // || item.Key.StartsWith("append-") - // || item.Key.StartsWith("removed-")) - // { - // ModifyContentMerge(fileContent, item.Key, item.Value); - // } - // else - // { - // fileContent[item.Key] = item.Value; - // } - // } - // return; - //} + if (item.Key.StartsWith("prepend-") + || item.Key.StartsWith("append-") + || item.Key.StartsWith("removed-")) + { + ModifyContentMerge(fileContent, item.Key, item.Value); + } + else + { + fileContent[item.Key] = item.Value; + } + } + return; + } - private static void ModifyContentMerge(Dictionary fileContent, string key, object value) + private void ModifyContentMerge(Dictionary fileContent, string key, object value) { bool blPrepend = false; bool blRemoved = false; @@ -255,5 +255,17 @@ namespace v2rayN.Handler.CoreConfig } } } + + private string GetLogLevel(string level) + { + if (level == "none") + { + return "silent"; + } + else + { + return level; + } + } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Models/ConfigItems.cs b/v2rayN/v2rayN/Models/ConfigItems.cs index e88806c3..5efbd7dc 100644 --- a/v2rayN/v2rayN/Models/ConfigItems.cs +++ b/v2rayN/v2rayN/Models/ConfigItems.cs @@ -217,6 +217,8 @@ namespace v2rayN.Models { public ERuleMode ruleMode { get; set; } public bool showInTaskbar { get; set; } + public bool enableIPv6 { get; set; } + public bool enableMixinContent { get; set; } public int proxiesSorting { get; set; } public bool proxiesAutoRefresh { get; set; } public int proxiesAutoDelayTestInterval { get; set; } = 10; diff --git a/v2rayN/v2rayN/Sample/clash_mixin_yaml b/v2rayN/v2rayN/Sample/clash_mixin_yaml new file mode 100644 index 00000000..940144ee --- /dev/null +++ b/v2rayN/v2rayN/Sample/clash_mixin_yaml @@ -0,0 +1,39 @@ +# +# 配置文件内容不会被修改,混合行为只会发生在内存中 +# +# 注意下面缩进,请用支持yaml显示的编辑器打开 +# +# 使用clash配置文件关键字则覆盖原配置 +# +# removed-rules 循环匹配rules数组每行,符合则移除当前行 (此规则请放最前面) +# +# append-rules 数组合并至原配置rules数组后 +# prepend-rules 数组合并至原配置rules数组前 +# append-proxies 数组合并至原配置proxies数组后 +# prepend-proxies 数组合并至原配置proxies数组前 +# append-proxy-groups 数组合并至原配置proxy-groups数组后 +# prepend-proxy-groups 数组合并至原配置proxy-groups数组前 +# append-rule-providers 数组合并至原配置rule-providers数组后 +# prepend-rule-providers 数组合并至原配置rule-providers数组前 +# + +dns: + enable: true + enhanced-mode: fake-ip + nameserver: + - 114.114.114.114 + - 223.5.5.5 + - 8.8.8.8 + fallback: [] + fake-ip-filter: + - +.stun.*.* + - +.stun.*.*.* + - +.stun.*.*.*.* + - +.stun.*.*.*.*.* + - "*.n.n.srv.nintendo.net" + - +.stun.playstation.net + - xbox.*.*.microsoft.com + - "*.*.xboxlive.com" + - "*.msftncsi.com" + - "*.msftconnecttest.com" + - WORKGROUP \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/clash_tun_yaml b/v2rayN/v2rayN/Sample/clash_tun_yaml new file mode 100644 index 00000000..d8427f33 --- /dev/null +++ b/v2rayN/v2rayN/Sample/clash_tun_yaml @@ -0,0 +1,7 @@ +tun: + enable: true + stack: gvisor + dns-hijack: + - 0.0.0.0:53 + auto-route: true + auto-detect-interface: true diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index f749356e..4d86368f 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -31,6 +31,8 @@ + + Never