Add tun and mixin functions to clash

pull/5377/head
2dust 2024-06-28 20:29:44 +08:00
parent ccb8cd5c04
commit 22d4f435de
6 changed files with 124 additions and 59 deletions

View File

@ -31,6 +31,7 @@ namespace v2rayN
public const string CoreConfigFileName = "config.json"; public const string CoreConfigFileName = "config.json";
public const string CorePreConfigFileName = "configPre.json"; public const string CorePreConfigFileName = "configPre.json";
public const string CoreSpeedtestConfigFileName = "configSpeedtest.json"; public const string CoreSpeedtestConfigFileName = "configSpeedtest.json";
public const string ClashMixinConfigFileName = "Mixin.yaml";
public const string V2raySampleClient = "v2rayN.Sample.SampleClientConfig"; public const string V2raySampleClient = "v2rayN.Sample.SampleClientConfig";
public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig"; public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig";
public const string V2raySampleHttpRequestFileName = "v2rayN.Sample.SampleHttpRequest"; 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 TunSingboxRulesFileName = "v2rayN.Sample.tun_singbox_rules";
public const string DNSV2rayNormalFileName = "v2rayN.Sample.dns_v2ray_normal"; public const string DNSV2rayNormalFileName = "v2rayN.Sample.dns_v2ray_normal";
public const string DNSSingboxNormalFileName = "v2rayN.Sample.dns_singbox_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 DefaultSecurity = "auto";
public const string DefaultNetwork = "tcp"; public const string DefaultNetwork = "tcp";

View File

@ -82,7 +82,8 @@ namespace v2rayN.Handler.CoreConfig
//socks-port //socks-port
fileContent["socks-port"] = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks); fileContent["socks-port"] = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks);
//log-level //log-level
fileContent["log-level"] = _config.coreBasicItem.loglevel; fileContent["log-level"] = GetLogLevel(_config.coreBasicItem.loglevel);
//external-controller //external-controller
fileContent["external-controller"] = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}"; fileContent["external-controller"] = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}";
//allow-lan //allow-lan
@ -97,7 +98,7 @@ namespace v2rayN.Handler.CoreConfig
} }
//ipv6 //ipv6
//fileContent["ipv6"] = _config.EnableIpv6; fileContent["ipv6"] = _config.clashUIItem.enableIPv6;
//mode //mode
if (!fileContent.ContainsKey("mode")) if (!fileContent.ContainsKey("mode"))
@ -112,27 +113,27 @@ namespace v2rayN.Handler.CoreConfig
} }
} }
////enable tun mode //enable tun mode
//if (config.EnableTun) if (_config.tunModeItem.enableTun)
//{ {
// string tun = Utils.GetEmbedText(Global.SampleTun); string tun = Utils.GetEmbedText(Global.ClashTunYaml);
// if (!string.IsNullOrEmpty(tun)) if (!string.IsNullOrEmpty(tun))
// { {
// var tunContent = Utils.FromYaml<Dictionary<string, object>>(tun); var tunContent = YamlUtils.FromYaml<Dictionary<string, object>>(tun);
// if (tunContent != null) if (tunContent != null)
// fileContent["tun"] = tunContent["tun"]; fileContent["tun"] = tunContent["tun"];
// } }
//} }
//Mixin //Mixin
//try try
//{ {
// MixinContent(fileContent, config, node); MixinContent(fileContent, node);
//} }
//catch (Exception ex) catch (Exception ex)
//{ {
// Logging.SaveLog("GenerateClientConfigClash-Mixin", ex); Logging.SaveLog("GenerateClientConfigClash-Mixin", ex);
//} }
var txtFileNew = YamlUtils.ToYaml(fileContent).Replace(tagYamlStr2, tagYamlStr3); var txtFileNew = YamlUtils.ToYaml(fileContent).Replace(tagYamlStr2, tagYamlStr3);
File.WriteAllText(fileName, txtFileNew); File.WriteAllText(fileName, txtFileNew);
@ -156,49 +157,48 @@ namespace v2rayN.Handler.CoreConfig
return 0; return 0;
} }
//private static void MixinContent(Dictionary<string, object> fileContent, Config config, ProfileItem node) private void MixinContent(Dictionary<string, object> fileContent, ProfileItem node)
//{ {
// if (!config.EnableMixinContent) //if (!_config.clashUIItem.enableMixinContent)
// { //{
// return; // return;
// } //}
// var path = Utils.GetConfigPath(Global.mixinConfigFileName); var path = Utils.GetConfigPath(Global.ClashMixinConfigFileName);
// if (!File.Exists(path)) if (!File.Exists(path))
// { {
// return; return;
// } }
// var txtFile = File.ReadAllText(Utils.GetConfigPath(Global.mixinConfigFileName)); var txtFile = File.ReadAllText(Utils.GetConfigPath(Global.ClashMixinConfigFileName));
// //txtFile = txtFile.Replace("!<str>", "");
// var mixinContent = YamlUtils.FromYaml<Dictionary<string, object>>(txtFile); var mixinContent = YamlUtils.FromYaml<Dictionary<string, object>>(txtFile);
// if (mixinContent == null) if (mixinContent == null)
// { {
// return; return;
// } }
// foreach (var item in mixinContent) foreach (var item in mixinContent)
// { {
// if (!config.EnableTun && item.Key == "tun") if (!_config.tunModeItem.enableTun && item.Key == "tun")
// { {
// continue; continue;
// } }
// if (item.Key.StartsWith("prepend-") if (item.Key.StartsWith("prepend-")
// || item.Key.StartsWith("append-") || item.Key.StartsWith("append-")
// || item.Key.StartsWith("removed-")) || item.Key.StartsWith("removed-"))
// { {
// ModifyContentMerge(fileContent, item.Key, item.Value); ModifyContentMerge(fileContent, item.Key, item.Value);
// } }
// else else
// { {
// fileContent[item.Key] = item.Value; fileContent[item.Key] = item.Value;
// } }
// } }
// return; return;
//} }
private static void ModifyContentMerge(Dictionary<string, object> fileContent, string key, object value) private void ModifyContentMerge(Dictionary<string, object> fileContent, string key, object value)
{ {
bool blPrepend = false; bool blPrepend = false;
bool blRemoved = 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;
}
}
} }
} }

View File

@ -217,6 +217,8 @@ namespace v2rayN.Models
{ {
public ERuleMode ruleMode { get; set; } public ERuleMode ruleMode { get; set; }
public bool showInTaskbar { get; set; } public bool showInTaskbar { get; set; }
public bool enableIPv6 { get; set; }
public bool enableMixinContent { get; set; }
public int proxiesSorting { get; set; } public int proxiesSorting { get; set; }
public bool proxiesAutoRefresh { get; set; } public bool proxiesAutoRefresh { get; set; }
public int proxiesAutoDelayTestInterval { get; set; } = 10; public int proxiesAutoDelayTestInterval { get; set; } = 10;

View File

@ -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

View File

@ -0,0 +1,7 @@
tun:
enable: true
stack: gvisor
dns-hijack:
- 0.0.0.0:53
auto-route: true
auto-detect-interface: true

View File

@ -31,6 +31,8 @@
<ItemGroup> <ItemGroup>
<AdditionalFiles Include="app.manifest" /> <AdditionalFiles Include="app.manifest" />
<EmbeddedResource Include="Sample\clash_mixin_yaml" />
<EmbeddedResource Include="Sample\clash_tun_yaml" />
<EmbeddedResource Include="Sample\SingboxSampleOutbound" /> <EmbeddedResource Include="Sample\SingboxSampleOutbound" />
<EmbeddedResource Include="Sample\SingboxSampleClientConfig"> <EmbeddedResource Include="Sample\SingboxSampleClientConfig">
<CopyToOutputDirectory>Never</CopyToOutputDirectory> <CopyToOutputDirectory>Never</CopyToOutputDirectory>