From e461fce2cbd349c986d23d778b4febd3f2df7d14 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 26 Apr 2023 09:36:30 +0800 Subject: [PATCH] add sing-box tun bypassMode --- v2rayN/v2rayN/Handler/CoreConfigSingbox.cs | 37 +++++++++++++-- v2rayN/v2rayN/Handler/TunHandler.cs | 52 ++++++++++++---------- 2 files changed, 62 insertions(+), 27 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 5f83bc55..1c96da3b 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -1,5 +1,4 @@ -using System.ComponentModel; -using v2rayN.Base; +using v2rayN.Base; using v2rayN.Mode; using v2rayN.Resx; @@ -382,7 +381,7 @@ namespace v2rayN.Handler { outbound = "direct", process_name = lstDirectExe - }); + }); } if (_config.routingBasicItem.enableRoutingAdvanced) @@ -412,6 +411,38 @@ namespace v2rayN.Handler } } } + + if (_config.tunModeItem.enableTun) + { + if (_config.tunModeItem.bypassMode) + { + //direct ips + if (_config.tunModeItem.directIP != null && _config.tunModeItem.directIP.Count > 0) + { + singboxConfig.route.rules.Add(new() { outbound = "direct", ip_cidr = _config.tunModeItem.directIP }); + } + //direct process + if (_config.tunModeItem.directProcess != null && _config.tunModeItem.directProcess.Count > 0) + { + singboxConfig.route.rules.Add(new() { outbound = "direct", process_name = _config.tunModeItem.directProcess }); + } + } + else + { + //proxy ips + if (_config.tunModeItem.proxyIP != null && _config.tunModeItem.proxyIP.Count > 0) + { + singboxConfig.route.rules.Add(new() { outbound = "proxy", ip_cidr = _config.tunModeItem.proxyIP }); + } + //proxy process + if (_config.tunModeItem.proxyProcess != null && _config.tunModeItem.proxyProcess.Count > 0) + { + singboxConfig.route.rules.Add(new() { outbound = "proxy", process_name = _config.tunModeItem.proxyProcess }); + } + + singboxConfig.route.rules.Add(new() { outbound = "direct", inbound = new() { "tun-in" } }); + } + } } catch (Exception ex) { diff --git a/v2rayN/v2rayN/Handler/TunHandler.cs b/v2rayN/v2rayN/Handler/TunHandler.cs index c20f22a3..95184f72 100644 --- a/v2rayN/v2rayN/Handler/TunHandler.cs +++ b/v2rayN/v2rayN/Handler/TunHandler.cs @@ -135,30 +135,7 @@ namespace v2rayN.Base configStr = configStr.Replace("$dns_object$", dnsObject); //exe - List lstDnsExe = new(); - List lstDirectExe = new(); - var coreInfos = LazyConfig.Instance.GetCoreInfos(); - foreach (var it in coreInfos) - { - if (it.coreType == ECoreType.v2rayN) - { - continue; - } - foreach (var it2 in it.coreExes) - { - if (!lstDnsExe.Contains(it2) && it.coreType != ECoreType.sing_box) - { - //lstDnsExe.Add(it2); - lstDnsExe.Add($"{it2}.exe"); - } - - if (!lstDirectExe.Contains(it2)) - { - //lstDirectExe.Add(it2); - lstDirectExe.Add($"{it2}.exe"); - } - } - } + routingDirectExe(out List lstDnsExe, out List lstDirectExe); string strDns = string.Join("\",\"", lstDnsExe.ToArray()); configStr = configStr.Replace("$dnsProcessName$", $"\"{strDns}\""); @@ -209,6 +186,33 @@ namespace v2rayN.Base return true; } + + private void routingDirectExe(out List lstDnsExe, out List lstDirectExe) + { + lstDnsExe = new(); + lstDirectExe = new(); + var coreInfos = LazyConfig.Instance.GetCoreInfos(); + foreach (var it in coreInfos) + { + if (it.coreType == ECoreType.v2rayN) + { + continue; + } + foreach (var it2 in it.coreExes) + { + if (!lstDnsExe.Contains(it2) && it.coreType != ECoreType.sing_box) + { + lstDnsExe.Add($"{it2}.exe"); + } + + if (!lstDirectExe.Contains(it2)) + { + lstDirectExe.Add($"{it2}.exe"); + } + } + } + } + private void CoreStop() { try