From 620422350f312ea7a7c2897b4806316aeea0192a Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 24 Jun 2024 15:19:42 +0800 Subject: [PATCH] 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 --- v2rayN/ProtosLib/ProtosLib.csproj | 2 +- v2rayN/v2rayN/Enums/EInboundProtocol.cs | 1 + v2rayN/v2rayN/Global.cs | 3 +- v2rayN/v2rayN/Handler/ConfigHandler.cs | 2 +- .../Handler/CoreConfig/CoreConfigHandler.cs | 2 +- .../Handler/CoreConfig/CoreConfigSingbox.cs | 13 +++- v2rayN/v2rayN/Handler/CoreHandler.cs | 74 +++++++++++-------- v2rayN/v2rayN/Handler/LazyConfig.cs | 16 ++-- v2rayN/v2rayN/Handler/StatisticsSingbox.cs | 2 +- v2rayN/v2rayN/Models/SingboxConfig.cs | 19 +++-- v2rayN/v2rayN/Sample/custom_routing_black | 17 ++++- v2rayN/v2rayN/Sample/custom_routing_white | 4 + v2rayN/v2rayN/v2rayN.csproj | 2 +- 13 files changed, 105 insertions(+), 52 deletions(-) diff --git a/v2rayN/ProtosLib/ProtosLib.csproj b/v2rayN/ProtosLib/ProtosLib.csproj index 2c84666b..81cc41f9 100644 --- a/v2rayN/ProtosLib/ProtosLib.csproj +++ b/v2rayN/ProtosLib/ProtosLib.csproj @@ -9,7 +9,7 @@ - + all diff --git a/v2rayN/v2rayN/Enums/EInboundProtocol.cs b/v2rayN/v2rayN/Enums/EInboundProtocol.cs index 5bc955a8..07cd1369 100644 --- a/v2rayN/v2rayN/Enums/EInboundProtocol.cs +++ b/v2rayN/v2rayN/Enums/EInboundProtocol.cs @@ -8,6 +8,7 @@ http2, pac, api, + api2, speedtest = 21 } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 27ad33e0..726c9c18 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -23,7 +23,8 @@ namespace v2rayN public const string SpeedPingTestUrl = @"https://www.google.com/generate_204"; public const string JuicityCoreUrl = "https://github.com/juicity/juicity/releases"; public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/"; - public const string SingboxRulesetUrl = @"https://raw.githubusercontent.com/SagerNet/sing-{0}/rule-set/{1}.srs"; + public const string SingboxRulesetUrlGeosite = @"https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-{0}.srs"; + public const string SingboxRulesetUrlGeoip = @"https://raw.githubusercontent.com/Loyalsoldier/geoip/release/srs/{0}.srs"; public const string PromotionUrl = @"aHR0cHM6Ly85LjIzNDQ1Ni54eXovYWJjLmh0bWw="; public const string ConfigFileName = "guiNConfig.json"; diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index c2a28571..e80325c4 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -1615,7 +1615,7 @@ namespace v2rayN.Handler public static int InitBuiltinRouting(Config config, bool blImportAdvancedRules = false) { - var ver = "V2-"; + var ver = "V3-"; var items = LazyConfig.Instance.RoutingItems(); if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(ver)).ToList().Count <= 0) { diff --git a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigHandler.cs b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigHandler.cs index 3f73f542..cd83394c 100644 --- a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigHandler.cs @@ -27,7 +27,7 @@ namespace v2rayN.Handler.CoreConfig { return GenerateClientCustomConfig(node, fileName, out msg); } - else if (config.tunModeItem.enableTun || LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box) + else if (LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box) { var configGenSingbox = new CoreConfigSingbox(config); if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0) diff --git a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs index 89f468c4..04c4aee4 100644 --- a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs @@ -120,7 +120,8 @@ namespace v2rayN.Handler.CoreConfig var listen = "::"; singboxConfig.inbounds = []; - if (!_config.tunModeItem.enableTun || _config.tunModeItem.enableTun && _config.tunModeItem.enableExInbound) + if (!_config.tunModeItem.enableTun + || (_config.tunModeItem.enableTun && _config.tunModeItem.enableExInbound && _config.runningCoreType == ECoreType.sing_box)) { var inbound = new Inbound4Sbox() { @@ -191,7 +192,7 @@ namespace v2rayN.Handler.CoreConfig tunInbound.strict_route = _config.tunModeItem.strictRoute; tunInbound.stack = _config.tunModeItem.stack; tunInbound.sniff = _config.inbound[0].sniffingEnabled; - tunInbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled; + //tunInbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled; if (_config.tunModeItem.enableIPv6Address == false) { tunInbound.inet6_address = null; @@ -844,12 +845,14 @@ namespace v2rayN.Handler.CoreConfig .ToList(); if (lstDomain != null && lstDomain.Count > 0) { + //var strategy = dns4Sbox.servers.Where(t => !Utils.IsNullOrEmpty(t.strategy)).Select(t => t.strategy).FirstOrDefault(); var tag = "local_local"; dns4Sbox.servers.Add(new() { tag = tag, address = "223.5.5.5", detour = Global.DirectTag, + //strategy = strategy }); dns4Sbox.rules.Add(new() { @@ -868,7 +871,7 @@ namespace v2rayN.Handler.CoreConfig singboxConfig.experimental ??= new Experimental4Sbox(); singboxConfig.experimental.clash_api = new Clash_Api4Sbox() { - external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort}", + external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}", }; } @@ -950,12 +953,14 @@ namespace v2rayN.Handler.CoreConfig } else { + var geo = item.Split('-').FirstOrDefault() ?? geosite; + var value = item.Split('-').LastOrDefault(); singboxConfig.route.rule_set.Add(new() { type = "remote", format = "binary", tag = item, - url = string.Format(Global.SingboxRulesetUrl, item.StartsWith(geosite) ? geosite : geoip, item), + url = string.Format(geo.Equals(geosite) ? Global.SingboxRulesetUrlGeosite : Global.SingboxRulesetUrlGeoip, value), download_detour = Global.ProxyTag }); } diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index dbe70658..c24f6740 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -57,22 +57,22 @@ namespace v2rayN.Handler CoreStart(node); //In tun mode, do a delay check and restart the core - if (_config.tunModeItem.enableTun) - { - Observable.Range(1, 1) - .Delay(TimeSpan.FromSeconds(15)) - .Subscribe(x => - { - { - if (_process == null || _process.HasExited) - { - CoreStart(node); - ShowMsg(false, "Tun mode restart the core once"); - Logging.SaveLog("Tun mode restart the core once"); - } - } - }); - } + //if (_config.tunModeItem.enableTun) + //{ + // Observable.Range(1, 1) + // .Delay(TimeSpan.FromSeconds(15)) + // .Subscribe(x => + // { + // { + // if (_process == null || _process.HasExited) + // { + // CoreStart(node); + // ShowMsg(false, "Tun mode restart the core once"); + // Logging.SaveLog("Tun mode restart the core once"); + // } + // } + // }); + //} } } @@ -186,15 +186,16 @@ namespace v2rayN.Handler ShowMsg(false, $"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}"); ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); - ECoreType coreType; - if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun) - { - coreType = ECoreType.sing_box; - } - else - { - coreType = LazyConfig.Instance.GetCoreType(node, node.configType); - } + //ECoreType coreType; + //if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun) + //{ + // coreType = ECoreType.sing_box; + //} + //else + //{ + // coreType = LazyConfig.Instance.GetCoreType(node, node.configType); + //} + var coreType = LazyConfig.Instance.GetCoreType(node, node.configType); _config.runningCoreType = coreType; var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType); @@ -206,13 +207,25 @@ namespace v2rayN.Handler } _process = proc; - //start a socks service + //start a pre service if (_process != null && !_process.HasExited) { - if ((node.configType == EConfigType.Custom && node.preSocksPort > 0)) + ProfileItem? itemSocks = null; + var preCoreType = ECoreType.sing_box; + if (node.configType != EConfigType.Custom && coreType != ECoreType.sing_box && _config.tunModeItem.enableTun) { - var preCoreType = _config.tunModeItem.enableTun ? ECoreType.sing_box : ECoreType.Xray; - var itemSocks = new ProfileItem() + itemSocks = new ProfileItem() + { + coreType = preCoreType, + configType = EConfigType.Socks, + address = Global.Loopback, + port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks) + }; + } + else if ((node.configType == EConfigType.Custom && node.preSocksPort > 0)) + { + preCoreType = _config.tunModeItem.enableTun ? ECoreType.sing_box : ECoreType.Xray; + itemSocks = new ProfileItem() { coreType = preCoreType, configType = EConfigType.Socks, @@ -220,6 +233,9 @@ namespace v2rayN.Handler port = node.preSocksPort }; _config.runningCoreType = preCoreType; + } + if (itemSocks != null) + { string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName); if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0) { diff --git a/v2rayN/v2rayN/Handler/LazyConfig.cs b/v2rayN/v2rayN/Handler/LazyConfig.cs index b1061b6d..d2c58faf 100644 --- a/v2rayN/v2rayN/Handler/LazyConfig.cs +++ b/v2rayN/v2rayN/Handler/LazyConfig.cs @@ -13,20 +13,26 @@ namespace v2rayN.Handler public static LazyConfig Instance => _instance.Value; private int? _statePort; + private int? _statePort2; public int StatePort { get { - if (_statePort is null) - { - _statePort = Utils.GetFreePort(GetLocalPort(EInboundProtocol.api)); - } - + _statePort ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api)); return _statePort.Value; } } + public int StatePort2 + { + get + { + _statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2)); + return _statePort2.Value; + } + } + private Job _processJob = new(); public LazyConfig() diff --git a/v2rayN/v2rayN/Handler/StatisticsSingbox.cs b/v2rayN/v2rayN/Handler/StatisticsSingbox.cs index bc9ac0e9..d7a8b97e 100644 --- a/v2rayN/v2rayN/Handler/StatisticsSingbox.cs +++ b/v2rayN/v2rayN/Handler/StatisticsSingbox.cs @@ -28,7 +28,7 @@ namespace v2rayN.Handler try { - url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort}/traffic"; + url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort2}/traffic"; if (webSocket == null) { diff --git a/v2rayN/v2rayN/Models/SingboxConfig.cs b/v2rayN/v2rayN/Models/SingboxConfig.cs index 330401a3..ced5adbd 100644 --- a/v2rayN/v2rayN/Models/SingboxConfig.cs +++ b/v2rayN/v2rayN/Models/SingboxConfig.cs @@ -28,6 +28,7 @@ public bool? disable_expire { get; set; } public bool? independent_cache { get; set; } public bool? reverse_mapping { get; set; } + public string? client_subnet { get; set; } public Fakeip4Sbox? fakeip { get; set; } } @@ -44,12 +45,13 @@ public string? outbound { get; set; } public string? server { get; set; } public bool? disable_cache { get; set; } - public List? inbound { get; set; } - public List? protocol { get; set; } public string? type { get; set; } public string? mode { get; set; } - public List? network { get; set; } public bool? ip_is_private { get; set; } + public string? client_subnet { get; set; } + public List? inbound { get; set; } + public List? protocol { get; set; } + public List? network { get; set; } public List? port { get; set; } public List? port_range { get; set; } public List? geosite { get; set; } @@ -62,6 +64,7 @@ public List? source_ip_cidr { get; set; } public List? process_name { get; set; } public List? rule_set { get; set; } + public List? rules { get; set; } } [Serializable] @@ -184,11 +187,13 @@ public class Server4Sbox { - public string tag { get; set; } - public string address { get; set; } - public string address_resolver { get; set; } - public string strategy { get; set; } + public string? tag { get; set; } + public string? address { get; set; } + public string? address_resolver { get; set; } + public string? address_strategy { get; set; } + public string? strategy { get; set; } public string? detour { get; set; } + public string? client_subnet { get; set; } } public class Experimental4Sbox diff --git a/v2rayN/v2rayN/Sample/custom_routing_black b/v2rayN/v2rayN/Sample/custom_routing_black index 37913f70..f56cfa2c 100644 --- a/v2rayN/v2rayN/Sample/custom_routing_black +++ b/v2rayN/v2rayN/Sample/custom_routing_black @@ -16,7 +16,22 @@ "domain": [ "geosite:geolocation-!cn", - "geosite:greatfire" + "geosite:greatfire" + ] + }, + { + "outboundTag": "proxy", + "ip": [ + "1.0.0.1", + "1.1.1.1", + "8.8.8.8", + "8.8.4.4", + "geoip:facebook", + "geoip:fastly", + "geoip:google", + "geoip:netflix", + "geoip:telegram", + "geoip:twitter" ] }, { diff --git a/v2rayN/v2rayN/Sample/custom_routing_white b/v2rayN/v2rayN/Sample/custom_routing_white index 37ed49da..7d34872f 100644 --- a/v2rayN/v2rayN/Sample/custom_routing_white +++ b/v2rayN/v2rayN/Sample/custom_routing_white @@ -22,6 +22,10 @@ { "outboundTag": "direct", "ip": [ + "223.5.5.5/32", + "119.29.29.29/32", + "180.76.76.76/32", + "114.114.114.114/32", "geoip:private", "geoip:cn" ] diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 629e562e..9b1ef128 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -16,7 +16,7 @@ - +