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 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";

View File

@ -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<Dictionary<string, object>>(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<Dictionary<string, object>>(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<string, object> fileContent, Config config, ProfileItem node)
//{
// if (!config.EnableMixinContent)
// {
// return;
// }
private void MixinContent(Dictionary<string, object> 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("!<str>", "");
var txtFile = File.ReadAllText(Utils.GetConfigPath(Global.ClashMixinConfigFileName));
// var mixinContent = YamlUtils.FromYaml<Dictionary<string, object>>(txtFile);
// if (mixinContent == null)
// {
// return;
// }
// foreach (var item in mixinContent)
// {
// if (!config.EnableTun && item.Key == "tun")
// {
// continue;
// }
var mixinContent = YamlUtils.FromYaml<Dictionary<string, object>>(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<string, object> fileContent, string key, object value)
private void ModifyContentMerge(Dictionary<string, object> 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;
}
}
}
}

View File

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

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>
<AdditionalFiles Include="app.manifest" />
<EmbeddedResource Include="Sample\clash_mixin_yaml" />
<EmbeddedResource Include="Sample\clash_tun_yaml" />
<EmbeddedResource Include="Sample\SingboxSampleOutbound" />
<EmbeddedResource Include="Sample\SingboxSampleClientConfig">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>