Skip to content

Commit 6204223

Browse files
committed
Improved tun mode
Enabling tun mode. Only one core will be run when using sing-box core; When using a non-sing-box, the sing-box will be used to start an additional front Socks service to provide a tun entry, which will then run two cores
1 parent b77cc3c commit 6204223

File tree

13 files changed

+105
-52
lines changed

13 files changed

+105
-52
lines changed

v2rayN/ProtosLib/ProtosLib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Google.Protobuf" Version="3.27.0" />
12+
<PackageReference Include="Google.Protobuf" Version="3.27.1" />
1313
<PackageReference Include="Grpc.Net.Client" Version="2.63.0" />
1414
<PackageReference Include="Grpc.Tools" Version="2.64.0">
1515
<PrivateAssets>all</PrivateAssets>

v2rayN/v2rayN/Enums/EInboundProtocol.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public enum EInboundProtocol
88
http2,
99
pac,
1010
api,
11+
api2,
1112
speedtest = 21
1213
}
1314
}

v2rayN/v2rayN/Global.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ internal class Global
2323
public const string SpeedPingTestUrl = @"https://www.google.com/generate_204";
2424
public const string JuicityCoreUrl = "https://github.com/juicity/juicity/releases";
2525
public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/";
26-
public const string SingboxRulesetUrl = @"https://raw.githubusercontent.com/SagerNet/sing-{0}/rule-set/{1}.srs";
26+
public const string SingboxRulesetUrlGeosite = @"https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-{0}.srs";
27+
public const string SingboxRulesetUrlGeoip = @"https://raw.githubusercontent.com/Loyalsoldier/geoip/release/srs/{0}.srs";
2728

2829
public const string PromotionUrl = @"aHR0cHM6Ly85LjIzNDQ1Ni54eXovYWJjLmh0bWw=";
2930
public const string ConfigFileName = "guiNConfig.json";

v2rayN/v2rayN/Handler/ConfigHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ public static RoutingItem GetDefaultRouting(Config config)
16151615

16161616
public static int InitBuiltinRouting(Config config, bool blImportAdvancedRules = false)
16171617
{
1618-
var ver = "V2-";
1618+
var ver = "V3-";
16191619
var items = LazyConfig.Instance.RoutingItems();
16201620
if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(ver)).ToList().Count <= 0)
16211621
{

v2rayN/v2rayN/Handler/CoreConfig/CoreConfigHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static int GenerateClientConfig(ProfileItem node, string? fileName, out s
2727
{
2828
return GenerateClientCustomConfig(node, fileName, out msg);
2929
}
30-
else if (config.tunModeItem.enableTun || LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
30+
else if (LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
3131
{
3232
var configGenSingbox = new CoreConfigSingbox(config);
3333
if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0)

v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ private int GenInbounds(SingboxConfig singboxConfig)
120120
var listen = "::";
121121
singboxConfig.inbounds = [];
122122

123-
if (!_config.tunModeItem.enableTun || _config.tunModeItem.enableTun && _config.tunModeItem.enableExInbound)
123+
if (!_config.tunModeItem.enableTun
124+
|| (_config.tunModeItem.enableTun && _config.tunModeItem.enableExInbound && _config.runningCoreType == ECoreType.sing_box))
124125
{
125126
var inbound = new Inbound4Sbox()
126127
{
@@ -191,7 +192,7 @@ private int GenInbounds(SingboxConfig singboxConfig)
191192
tunInbound.strict_route = _config.tunModeItem.strictRoute;
192193
tunInbound.stack = _config.tunModeItem.stack;
193194
tunInbound.sniff = _config.inbound[0].sniffingEnabled;
194-
tunInbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled;
195+
//tunInbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled;
195196
if (_config.tunModeItem.enableIPv6Address == false)
196197
{
197198
tunInbound.inet6_address = null;
@@ -844,12 +845,14 @@ private int GenDnsDomains(SingboxConfig singboxConfig)
844845
.ToList();
845846
if (lstDomain != null && lstDomain.Count > 0)
846847
{
848+
//var strategy = dns4Sbox.servers.Where(t => !Utils.IsNullOrEmpty(t.strategy)).Select(t => t.strategy).FirstOrDefault();
847849
var tag = "local_local";
848850
dns4Sbox.servers.Add(new()
849851
{
850852
tag = tag,
851853
address = "223.5.5.5",
852854
detour = Global.DirectTag,
855+
//strategy = strategy
853856
});
854857
dns4Sbox.rules.Add(new()
855858
{
@@ -868,7 +871,7 @@ private int GenExperimental(SingboxConfig singboxConfig)
868871
singboxConfig.experimental ??= new Experimental4Sbox();
869872
singboxConfig.experimental.clash_api = new Clash_Api4Sbox()
870873
{
871-
external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort}",
874+
external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}",
872875
};
873876
}
874877

@@ -950,12 +953,14 @@ private int ConvertGeo2Ruleset(SingboxConfig singboxConfig)
950953
}
951954
else
952955
{
956+
var geo = item.Split('-').FirstOrDefault() ?? geosite;
957+
var value = item.Split('-').LastOrDefault();
953958
singboxConfig.route.rule_set.Add(new()
954959
{
955960
type = "remote",
956961
format = "binary",
957962
tag = item,
958-
url = string.Format(Global.SingboxRulesetUrl, item.StartsWith(geosite) ? geosite : geoip, item),
963+
url = string.Format(geo.Equals(geosite) ? Global.SingboxRulesetUrlGeosite : Global.SingboxRulesetUrlGeoip, value),
959964
download_detour = Global.ProxyTag
960965
});
961966
}

v2rayN/v2rayN/Handler/CoreHandler.cs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@ public void LoadCore()
5757
CoreStart(node);
5858

5959
//In tun mode, do a delay check and restart the core
60-
if (_config.tunModeItem.enableTun)
61-
{
62-
Observable.Range(1, 1)
63-
.Delay(TimeSpan.FromSeconds(15))
64-
.Subscribe(x =>
65-
{
66-
{
67-
if (_process == null || _process.HasExited)
68-
{
69-
CoreStart(node);
70-
ShowMsg(false, "Tun mode restart the core once");
71-
Logging.SaveLog("Tun mode restart the core once");
72-
}
73-
}
74-
});
75-
}
60+
//if (_config.tunModeItem.enableTun)
61+
//{
62+
// Observable.Range(1, 1)
63+
// .Delay(TimeSpan.FromSeconds(15))
64+
// .Subscribe(x =>
65+
// {
66+
// {
67+
// if (_process == null || _process.HasExited)
68+
// {
69+
// CoreStart(node);
70+
// ShowMsg(false, "Tun mode restart the core once");
71+
// Logging.SaveLog("Tun mode restart the core once");
72+
// }
73+
// }
74+
// });
75+
//}
7676
}
7777
}
7878

@@ -186,15 +186,16 @@ private void CoreStart(ProfileItem node)
186186
ShowMsg(false, $"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}");
187187
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
188188

189-
ECoreType coreType;
190-
if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun)
191-
{
192-
coreType = ECoreType.sing_box;
193-
}
194-
else
195-
{
196-
coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
197-
}
189+
//ECoreType coreType;
190+
//if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun)
191+
//{
192+
// coreType = ECoreType.sing_box;
193+
//}
194+
//else
195+
//{
196+
// coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
197+
//}
198+
var coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
198199
_config.runningCoreType = coreType;
199200
var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType);
200201

@@ -206,20 +207,35 @@ private void CoreStart(ProfileItem node)
206207
}
207208
_process = proc;
208209

209-
//start a socks service
210+
//start a pre service
210211
if (_process != null && !_process.HasExited)
211212
{
212-
if ((node.configType == EConfigType.Custom && node.preSocksPort > 0))
213+
ProfileItem? itemSocks = null;
214+
var preCoreType = ECoreType.sing_box;
215+
if (node.configType != EConfigType.Custom && coreType != ECoreType.sing_box && _config.tunModeItem.enableTun)
213216
{
214-
var preCoreType = _config.tunModeItem.enableTun ? ECoreType.sing_box : ECoreType.Xray;
215-
var itemSocks = new ProfileItem()
217+
itemSocks = new ProfileItem()
218+
{
219+
coreType = preCoreType,
220+
configType = EConfigType.Socks,
221+
address = Global.Loopback,
222+
port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks)
223+
};
224+
}
225+
else if ((node.configType == EConfigType.Custom && node.preSocksPort > 0))
226+
{
227+
preCoreType = _config.tunModeItem.enableTun ? ECoreType.sing_box : ECoreType.Xray;
228+
itemSocks = new ProfileItem()
216229
{
217230
coreType = preCoreType,
218231
configType = EConfigType.Socks,
219232
address = Global.Loopback,
220233
port = node.preSocksPort
221234
};
222235
_config.runningCoreType = preCoreType;
236+
}
237+
if (itemSocks != null)
238+
{
223239
string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
224240
if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0)
225241
{

v2rayN/v2rayN/Handler/LazyConfig.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@ public sealed class LazyConfig
1313
public static LazyConfig Instance => _instance.Value;
1414

1515
private int? _statePort;
16+
private int? _statePort2;
1617

1718
public int StatePort
1819
{
1920
get
2021
{
21-
if (_statePort is null)
22-
{
23-
_statePort = Utils.GetFreePort(GetLocalPort(EInboundProtocol.api));
24-
}
25-
22+
_statePort ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api));
2623
return _statePort.Value;
2724
}
2825
}
2926

27+
public int StatePort2
28+
{
29+
get
30+
{
31+
_statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2));
32+
return _statePort2.Value;
33+
}
34+
}
35+
3036
private Job _processJob = new();
3137

3238
public LazyConfig()

v2rayN/v2rayN/Handler/StatisticsSingbox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private async void Init()
2828

2929
try
3030
{
31-
url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort}/traffic";
31+
url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort2}/traffic";
3232

3333
if (webSocket == null)
3434
{

v2rayN/v2rayN/Models/SingboxConfig.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class Dns4Sbox
2828
public bool? disable_expire { get; set; }
2929
public bool? independent_cache { get; set; }
3030
public bool? reverse_mapping { get; set; }
31+
public string? client_subnet { get; set; }
3132
public Fakeip4Sbox? fakeip { get; set; }
3233
}
3334

@@ -44,12 +45,13 @@ public class Rule4Sbox
4445
public string? outbound { get; set; }
4546
public string? server { get; set; }
4647
public bool? disable_cache { get; set; }
47-
public List<string>? inbound { get; set; }
48-
public List<string>? protocol { get; set; }
4948
public string? type { get; set; }
5049
public string? mode { get; set; }
51-
public List<string>? network { get; set; }
5250
public bool? ip_is_private { get; set; }
51+
public string? client_subnet { get; set; }
52+
public List<string>? inbound { get; set; }
53+
public List<string>? protocol { get; set; }
54+
public List<string>? network { get; set; }
5355
public List<int>? port { get; set; }
5456
public List<string>? port_range { get; set; }
5557
public List<string>? geosite { get; set; }
@@ -62,6 +64,7 @@ public class Rule4Sbox
6264
public List<string>? source_ip_cidr { get; set; }
6365
public List<string>? process_name { get; set; }
6466
public List<string>? rule_set { get; set; }
67+
public List<Rule4Sbox>? rules { get; set; }
6568
}
6669

6770
[Serializable]
@@ -184,11 +187,13 @@ public class HyObfs4Sbox
184187

185188
public class Server4Sbox
186189
{
187-
public string tag { get; set; }
188-
public string address { get; set; }
189-
public string address_resolver { get; set; }
190-
public string strategy { get; set; }
190+
public string? tag { get; set; }
191+
public string? address { get; set; }
192+
public string? address_resolver { get; set; }
193+
public string? address_strategy { get; set; }
194+
public string? strategy { get; set; }
191195
public string? detour { get; set; }
196+
public string? client_subnet { get; set; }
192197
}
193198

194199
public class Experimental4Sbox

v2rayN/v2rayN/Sample/custom_routing_black

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,22 @@
1616

1717
"domain": [
1818
"geosite:geolocation-!cn",
19-
"geosite:greatfire"
19+
"geosite:greatfire"
20+
]
21+
},
22+
{
23+
"outboundTag": "proxy",
24+
"ip": [
25+
"1.0.0.1",
26+
"1.1.1.1",
27+
"8.8.8.8",
28+
"8.8.4.4",
29+
"geoip:facebook",
30+
"geoip:fastly",
31+
"geoip:google",
32+
"geoip:netflix",
33+
"geoip:telegram",
34+
"geoip:twitter"
2035
]
2136
},
2237
{

v2rayN/v2rayN/Sample/custom_routing_white

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
{
2323
"outboundTag": "direct",
2424
"ip": [
25+
"223.5.5.5/32",
26+
"119.29.29.29/32",
27+
"180.76.76.76/32",
28+
"114.114.114.114/32",
2529
"geoip:private",
2630
"geoip:cn"
2731
]

v2rayN/v2rayN/v2rayN.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<ItemGroup>
1818
<PackageReference Include="Downloader" Version="3.0.6" />
19-
<PackageReference Include="MaterialDesignThemes" Version="5.0.0" />
19+
<PackageReference Include="MaterialDesignThemes" Version="5.1.0" />
2020
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.0.131" />
2121
<PackageReference Include="QRCoder.Xaml" Version="1.5.1" />
2222
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />

0 commit comments

Comments
 (0)