Skip to content

Commit 22d4f43

Browse files
committed
Add tun and mixin functions to clash
1 parent ccb8cd5 commit 22d4f43

File tree

6 files changed

+124
-59
lines changed

6 files changed

+124
-59
lines changed

v2rayN/v2rayN/Global.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal class Global
3131
public const string CoreConfigFileName = "config.json";
3232
public const string CorePreConfigFileName = "configPre.json";
3333
public const string CoreSpeedtestConfigFileName = "configSpeedtest.json";
34+
public const string ClashMixinConfigFileName = "Mixin.yaml";
3435
public const string V2raySampleClient = "v2rayN.Sample.SampleClientConfig";
3536
public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig";
3637
public const string V2raySampleHttpRequestFileName = "v2rayN.Sample.SampleHttpRequest";
@@ -44,6 +45,8 @@ internal class Global
4445
public const string TunSingboxRulesFileName = "v2rayN.Sample.tun_singbox_rules";
4546
public const string DNSV2rayNormalFileName = "v2rayN.Sample.dns_v2ray_normal";
4647
public const string DNSSingboxNormalFileName = "v2rayN.Sample.dns_singbox_normal";
48+
public const string ClashMixinYaml = "v2rayN.Sample.clash_mixin_yaml";
49+
public const string ClashTunYaml = "v2rayN.Sample.clash_tun_yaml";
4750

4851
public const string DefaultSecurity = "auto";
4952
public const string DefaultNetwork = "tcp";

v2rayN/v2rayN/Handler/CoreConfig/CoreConfigClash.cs

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public int GenerateClientConfig(ProfileItem node, string? fileName, out string m
8282
//socks-port
8383
fileContent["socks-port"] = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks);
8484
//log-level
85-
fileContent["log-level"] = _config.coreBasicItem.loglevel;
85+
fileContent["log-level"] = GetLogLevel(_config.coreBasicItem.loglevel);
86+
8687
//external-controller
8788
fileContent["external-controller"] = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}";
8889
//allow-lan
@@ -97,7 +98,7 @@ public int GenerateClientConfig(ProfileItem node, string? fileName, out string m
9798
}
9899

99100
//ipv6
100-
//fileContent["ipv6"] = _config.EnableIpv6;
101+
fileContent["ipv6"] = _config.clashUIItem.enableIPv6;
101102

102103
//mode
103104
if (!fileContent.ContainsKey("mode"))
@@ -112,27 +113,27 @@ public int GenerateClientConfig(ProfileItem node, string? fileName, out string m
112113
}
113114
}
114115

115-
////enable tun mode
116-
//if (config.EnableTun)
117-
//{
118-
// string tun = Utils.GetEmbedText(Global.SampleTun);
119-
// if (!string.IsNullOrEmpty(tun))
120-
// {
121-
// var tunContent = Utils.FromYaml<Dictionary<string, object>>(tun);
122-
// if (tunContent != null)
123-
// fileContent["tun"] = tunContent["tun"];
124-
// }
125-
//}
116+
//enable tun mode
117+
if (_config.tunModeItem.enableTun)
118+
{
119+
string tun = Utils.GetEmbedText(Global.ClashTunYaml);
120+
if (!string.IsNullOrEmpty(tun))
121+
{
122+
var tunContent = YamlUtils.FromYaml<Dictionary<string, object>>(tun);
123+
if (tunContent != null)
124+
fileContent["tun"] = tunContent["tun"];
125+
}
126+
}
126127

127128
//Mixin
128-
//try
129-
//{
130-
// MixinContent(fileContent, config, node);
131-
//}
132-
//catch (Exception ex)
133-
//{
134-
// Logging.SaveLog("GenerateClientConfigClash-Mixin", ex);
135-
//}
129+
try
130+
{
131+
MixinContent(fileContent, node);
132+
}
133+
catch (Exception ex)
134+
{
135+
Logging.SaveLog("GenerateClientConfigClash-Mixin", ex);
136+
}
136137

137138
var txtFileNew = YamlUtils.ToYaml(fileContent).Replace(tagYamlStr2, tagYamlStr3);
138139
File.WriteAllText(fileName, txtFileNew);
@@ -156,49 +157,48 @@ public int GenerateClientConfig(ProfileItem node, string? fileName, out string m
156157
return 0;
157158
}
158159

159-
//private static void MixinContent(Dictionary<string, object> fileContent, Config config, ProfileItem node)
160-
//{
161-
// if (!config.EnableMixinContent)
162-
// {
163-
// return;
164-
// }
160+
private void MixinContent(Dictionary<string, object> fileContent, ProfileItem node)
161+
{
162+
//if (!_config.clashUIItem.enableMixinContent)
163+
//{
164+
// return;
165+
//}
165166

166-
// var path = Utils.GetConfigPath(Global.mixinConfigFileName);
167-
// if (!File.Exists(path))
168-
// {
169-
// return;
170-
// }
167+
var path = Utils.GetConfigPath(Global.ClashMixinConfigFileName);
168+
if (!File.Exists(path))
169+
{
170+
return;
171+
}
171172

172-
// var txtFile = File.ReadAllText(Utils.GetConfigPath(Global.mixinConfigFileName));
173-
// //txtFile = txtFile.Replace("!<str>", "");
173+
var txtFile = File.ReadAllText(Utils.GetConfigPath(Global.ClashMixinConfigFileName));
174174

175-
// var mixinContent = YamlUtils.FromYaml<Dictionary<string, object>>(txtFile);
176-
// if (mixinContent == null)
177-
// {
178-
// return;
179-
// }
180-
// foreach (var item in mixinContent)
181-
// {
182-
// if (!config.EnableTun && item.Key == "tun")
183-
// {
184-
// continue;
185-
// }
175+
var mixinContent = YamlUtils.FromYaml<Dictionary<string, object>>(txtFile);
176+
if (mixinContent == null)
177+
{
178+
return;
179+
}
180+
foreach (var item in mixinContent)
181+
{
182+
if (!_config.tunModeItem.enableTun && item.Key == "tun")
183+
{
184+
continue;
185+
}
186186

187-
// if (item.Key.StartsWith("prepend-")
188-
// || item.Key.StartsWith("append-")
189-
// || item.Key.StartsWith("removed-"))
190-
// {
191-
// ModifyContentMerge(fileContent, item.Key, item.Value);
192-
// }
193-
// else
194-
// {
195-
// fileContent[item.Key] = item.Value;
196-
// }
197-
// }
198-
// return;
199-
//}
187+
if (item.Key.StartsWith("prepend-")
188+
|| item.Key.StartsWith("append-")
189+
|| item.Key.StartsWith("removed-"))
190+
{
191+
ModifyContentMerge(fileContent, item.Key, item.Value);
192+
}
193+
else
194+
{
195+
fileContent[item.Key] = item.Value;
196+
}
197+
}
198+
return;
199+
}
200200

201-
private static void ModifyContentMerge(Dictionary<string, object> fileContent, string key, object value)
201+
private void ModifyContentMerge(Dictionary<string, object> fileContent, string key, object value)
202202
{
203203
bool blPrepend = false;
204204
bool blRemoved = false;
@@ -255,5 +255,17 @@ private static void ModifyContentMerge(Dictionary<string, object> fileContent, s
255255
}
256256
}
257257
}
258+
259+
private string GetLogLevel(string level)
260+
{
261+
if (level == "none")
262+
{
263+
return "silent";
264+
}
265+
else
266+
{
267+
return level;
268+
}
269+
}
258270
}
259271
}

v2rayN/v2rayN/Models/ConfigItems.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ public class ClashUIItem
217217
{
218218
public ERuleMode ruleMode { get; set; }
219219
public bool showInTaskbar { get; set; }
220+
public bool enableIPv6 { get; set; }
221+
public bool enableMixinContent { get; set; }
220222
public int proxiesSorting { get; set; }
221223
public bool proxiesAutoRefresh { get; set; }
222224
public int proxiesAutoDelayTestInterval { get; set; } = 10;

v2rayN/v2rayN/Sample/clash_mixin_yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# 配置文件内容不会被修改,混合行为只会发生在内存中
3+
#
4+
# 注意下面缩进,请用支持yaml显示的编辑器打开
5+
#
6+
# 使用clash配置文件关键字则覆盖原配置
7+
#
8+
# removed-rules 循环匹配rules数组每行,符合则移除当前行 (此规则请放最前面)
9+
#
10+
# append-rules 数组合并至原配置rules数组后
11+
# prepend-rules 数组合并至原配置rules数组前
12+
# append-proxies 数组合并至原配置proxies数组后
13+
# prepend-proxies 数组合并至原配置proxies数组前
14+
# append-proxy-groups 数组合并至原配置proxy-groups数组后
15+
# prepend-proxy-groups 数组合并至原配置proxy-groups数组前
16+
# append-rule-providers 数组合并至原配置rule-providers数组后
17+
# prepend-rule-providers 数组合并至原配置rule-providers数组前
18+
#
19+
20+
dns:
21+
enable: true
22+
enhanced-mode: fake-ip
23+
nameserver:
24+
- 114.114.114.114
25+
- 223.5.5.5
26+
- 8.8.8.8
27+
fallback: []
28+
fake-ip-filter:
29+
- +.stun.*.*
30+
- +.stun.*.*.*
31+
- +.stun.*.*.*.*
32+
- +.stun.*.*.*.*.*
33+
- "*.n.n.srv.nintendo.net"
34+
- +.stun.playstation.net
35+
- xbox.*.*.microsoft.com
36+
- "*.*.xboxlive.com"
37+
- "*.msftncsi.com"
38+
- "*.msftconnecttest.com"
39+
- WORKGROUP

v2rayN/v2rayN/Sample/clash_tun_yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tun:
2+
enable: true
3+
stack: gvisor
4+
dns-hijack:
5+
- 0.0.0.0:53
6+
auto-route: true
7+
auto-detect-interface: true

v2rayN/v2rayN/v2rayN.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
<ItemGroup>
3333
<AdditionalFiles Include="app.manifest" />
34+
<EmbeddedResource Include="Sample\clash_mixin_yaml" />
35+
<EmbeddedResource Include="Sample\clash_tun_yaml" />
3436
<EmbeddedResource Include="Sample\SingboxSampleOutbound" />
3537
<EmbeddedResource Include="Sample\SingboxSampleClientConfig">
3638
<CopyToOutputDirectory>Never</CopyToOutputDirectory>

0 commit comments

Comments
 (0)