From 758d0d82d4b17800fe56c926493482e07607c926 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 4 May 2023 11:44:51 +0800 Subject: [PATCH 01/13] Tun mode force start sing-box core --- v2rayN/v2rayN/Handler/CoreConfigHandler.cs | 7 +- v2rayN/v2rayN/Handler/CoreHandler.cs | 91 ++++++++++------------ 2 files changed, 47 insertions(+), 51 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs index 7e3aca47..c0dc712e 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs @@ -19,15 +19,16 @@ namespace v2rayN.Handler msg = ResUI.CheckServerSettings; return -1; } + var config = LazyConfig.Instance.GetConfig(); msg = ResUI.InitialConfiguration; if (node.configType == EConfigType.Custom) { return GenerateClientCustomConfig(node, fileName, out msg); } - else if (LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box) + else if (config.tunModeItem.enableTun || LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box) { - var configGenSingbox = new CoreConfigSingbox(LazyConfig.Instance.GetConfig()); + var configGenSingbox = new CoreConfigSingbox(config); if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0) { return -1; @@ -43,7 +44,7 @@ namespace v2rayN.Handler } else { - var coreConfigV2ray = new CoreConfigV2ray(LazyConfig.Instance.GetConfig()); + var coreConfigV2ray = new CoreConfigV2ray(config); if (coreConfigV2ray.GenerateClientConfigContent(node, out V2rayConfig? v2rayConfig, out msg) != 0) { return -1; diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index 24cf5255..d10e129a 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -12,7 +12,6 @@ namespace v2rayN.Handler internal class CoreHandler { private Config _config; - private CoreInfo? _coreInfo; private Process? _process; private Process? _processPre; private Action _updateFunc; @@ -35,11 +34,6 @@ namespace v2rayN.Handler return; } - if (SetCore(node) != 0) - { - ShowMsg(false, ResUI.CheckServerSettings); - return; - } string fileName = Utils.GetConfigPath(Global.coreConfigFileName); if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0) { @@ -74,30 +68,13 @@ namespace v2rayN.Handler { try { + bool hasProc = false; if (_process != null) { KillProcess(_process); _process.Dispose(); _process = null; - } - else - { - if (_coreInfo == null || _coreInfo.coreExes == null) - { - return; - } - foreach (string vName in _coreInfo.coreExes) - { - Process[] existing = Process.GetProcessesByName(vName); - foreach (Process p in existing) - { - string? path = p.MainModule?.FileName; - if (path == $"{Utils.GetBinPath(vName, _coreInfo.coreType)}.exe") - { - KillProcess(p); - } - } - } + hasProc =true; } if (_processPre != null) @@ -105,6 +82,31 @@ namespace v2rayN.Handler KillProcess(_processPre); _processPre.Dispose(); _processPre = null; + hasProc = true; + } + + if (!hasProc) + { + var coreInfos = LazyConfig.Instance.GetCoreInfos(); + foreach (var it in coreInfos) + { + if (it.coreType == ECoreType.v2rayN) + { + continue; + } + foreach (string vName in it.coreExes) + { + Process[] existing = Process.GetProcessesByName(vName); + foreach (Process p in existing) + { + string? path = p.MainModule?.FileName; + if (path == $"{Utils.GetBinPath(vName, it.coreType)}.exe") + { + KillProcess(p); + } + } + } + } } } catch (Exception ex) @@ -152,7 +154,18 @@ namespace v2rayN.Handler { ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString())); - var proc = RunProcess(node, _coreInfo, "", ShowMsg); + ECoreType coreType; + if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun) + { + coreType = ECoreType.sing_box; + } + else + { + coreType = LazyConfig.Instance.GetCoreType(node, node.configType); + } + var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType); + + var proc = RunProcess(node, coreInfo, "", ShowMsg); if (proc is null) { return; @@ -162,21 +175,20 @@ namespace v2rayN.Handler //start a socks service if (_process != null && !_process.HasExited) { - if ((node.configType == EConfigType.Custom && node.preSocksPort > 0) - || (node.configType != EConfigType.Custom && _coreInfo.coreType != ECoreType.sing_box && _config.tunModeItem.enableTun)) + if ((node.configType == EConfigType.Custom && node.preSocksPort > 0)) { var itemSocks = new ProfileItem() { coreType = ECoreType.sing_box, configType = EConfigType.Socks, address = Global.Loopback, - port = node.preSocksPort > 0 ? node.preSocksPort : LazyConfig.Instance.GetLocalPort(Global.InboundSocks) + port = node.preSocksPort }; string fileName2 = Utils.GetConfigPath(Global.corePreConfigFileName); if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0) { - var coreInfo = LazyConfig.Instance.GetCoreInfo(ECoreType.sing_box); - var proc2 = RunProcess(node, coreInfo, $" -c {Global.corePreConfigFileName}", ShowMsg); + var coreInfo2 = LazyConfig.Instance.GetCoreInfo(ECoreType.sing_box); + var proc2 = RunProcess(node, coreInfo2, $" -c {Global.corePreConfigFileName}", ShowMsg); if (proc2 is not null) { _processPre = proc2; @@ -257,23 +269,6 @@ namespace v2rayN.Handler _updateFunc(updateToTrayTooltip, msg); } - private int SetCore(ProfileItem node) - { - if (node == null) - { - return -1; - } - var coreType = LazyConfig.Instance.GetCoreType(node, node.configType); - - _coreInfo = LazyConfig.Instance.GetCoreInfo(coreType); - - if (_coreInfo == null) - { - return -1; - } - return 0; - } - #region Process private Process? RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, Action update) From 7e4f66d533f07ab8c3aa31891f724901939f9af6 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 4 May 2023 16:30:35 +0800 Subject: [PATCH 02/13] Add Multiplex protocol settings for sing-box --- v2rayN/v2rayN/App.xaml | 2 +- v2rayN/v2rayN/Global.cs | 1 + v2rayN/v2rayN/Handler/ConfigHandler.cs | 12 ++++ v2rayN/v2rayN/Handler/CoreConfigSingbox.cs | 9 +-- v2rayN/v2rayN/Handler/CoreHandler.cs | 2 +- v2rayN/v2rayN/Handler/MainFormHandler.cs | 4 +- v2rayN/v2rayN/Mode/Config.cs | 1 + v2rayN/v2rayN/Mode/ConfigItems.cs | 11 +++ v2rayN/v2rayN/Mode/SingboxConfig.cs | 1 + v2rayN/v2rayN/Resx/ResUI.Designer.cs | 9 +++ v2rayN/v2rayN/Resx/ResUI.resx | 3 + v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx | 3 + .../ViewModels/OptionSettingViewModel.cs | 3 + v2rayN/v2rayN/Views/DNSSettingWindow.xaml | 1 - v2rayN/v2rayN/Views/OptionSettingWindow.xaml | 71 +++++-------------- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 10 +-- .../Views/RoutingRuleSettingWindow.xaml.cs | 2 +- v2rayN/v2rayN/Views/SubEditWindow.xaml | 2 - 18 files changed, 76 insertions(+), 71 deletions(-) diff --git a/v2rayN/v2rayN/App.xaml b/v2rayN/v2rayN/App.xaml index a5bafb0d..ad8f5d4a 100644 --- a/v2rayN/v2rayN/App.xaml +++ b/v2rayN/v2rayN/App.xaml @@ -1,9 +1,9 @@  diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index c689e6d2..c23542c8 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -158,6 +158,7 @@ public static readonly List TunMtus = new() { "9000", "1500" }; public static readonly List TunStacks = new() { "gvisor", "system" }; public static readonly List PresetMsgFilters = new() { "proxy", "direct", "block", "" }; + public static readonly List SingboxMuxs = new() { "h2mux", "smux", "yamux", "" }; #endregion const diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index 1f7e6598..b5eaf3d0 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -191,6 +191,18 @@ namespace v2rayN.Handler config.guiItem.statisticsFreshRate = 1; } + if (config.mux4Sbox == null) + { + config.mux4Sbox = new() + { + protocol = Global.SingboxMuxs[0], + max_connections = 4, + min_streams = 4, + max_streams = 0, + padding = true + }; + } + LazyConfig.Instance.SetConfig(config); return 0; } diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 0916cd7b..2b7bb575 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -288,10 +288,11 @@ namespace v2rayN.Handler var mux = new Multiplex4Sbox() { enabled = true, - protocol = "smux", - max_connections = 4, - min_streams = 4, - max_streams = 0, + protocol = _config.mux4Sbox.protocol, + max_connections = _config.mux4Sbox.max_connections, + min_streams = _config.mux4Sbox.min_streams, + max_streams = _config.mux4Sbox.max_streams, + padding = _config.mux4Sbox.padding }; outbound.multiplex = mux; } diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index d10e129a..3f9f496a 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -74,7 +74,7 @@ namespace v2rayN.Handler KillProcess(_process); _process.Dispose(); _process = null; - hasProc =true; + hasProc = true; } if (_processPre != null) diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index bae28515..4390a6a9 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -208,8 +208,8 @@ namespace v2rayN.Handler { updateHandle.UpdateGeoFileAll(config, (bool success, string msg) => { - update(false, msg); - }); + update(false, msg); + }); autoUpdateGeoTime = dtNow; } } diff --git a/v2rayN/v2rayN/Mode/Config.cs b/v2rayN/v2rayN/Mode/Config.cs index 978508e8..f6639b93 100644 --- a/v2rayN/v2rayN/Mode/Config.cs +++ b/v2rayN/v2rayN/Mode/Config.cs @@ -27,6 +27,7 @@ public UIItem uiItem { get; set; } public ConstItem constItem { get; set; } public SpeedTestItem speedTestItem { get; set; } + public Mux4Sbox mux4Sbox { get; set; } public List inbound { get; set; } public List globalHotkeys { get; set; } public List coreTypeItem { get; set; } diff --git a/v2rayN/v2rayN/Mode/ConfigItems.cs b/v2rayN/v2rayN/Mode/ConfigItems.cs index 4ab9d3ce..59df1309 100644 --- a/v2rayN/v2rayN/Mode/ConfigItems.cs +++ b/v2rayN/v2rayN/Mode/ConfigItems.cs @@ -191,6 +191,7 @@ namespace v2rayN.Mode /// 域名解析策略 /// public string domainStrategy { get; set; } + public string domainStrategy4Singbox { get; set; } public string domainMatcher { get; set; } @@ -205,4 +206,14 @@ namespace v2rayN.Mode public int Width { get; set; } public int Index { get; set; } } + + [Serializable] + public class Mux4Sbox + { + public string protocol { get; set; } + public int max_connections { get; set; } + public int min_streams { get; set; } + public int max_streams { get; set; } + public bool padding { get; set; } + } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Mode/SingboxConfig.cs b/v2rayN/v2rayN/Mode/SingboxConfig.cs index fc8a09df..64e06798 100644 --- a/v2rayN/v2rayN/Mode/SingboxConfig.cs +++ b/v2rayN/v2rayN/Mode/SingboxConfig.cs @@ -127,6 +127,7 @@ public int max_connections { get; set; } public int min_streams { get; set; } public int max_streams { get; set; } + public bool padding { get; set; } } public class Utls4Sbox diff --git a/v2rayN/v2rayN/Resx/ResUI.Designer.cs b/v2rayN/v2rayN/Resx/ResUI.Designer.cs index c09993d2..a40b512b 100644 --- a/v2rayN/v2rayN/Resx/ResUI.Designer.cs +++ b/v2rayN/v2rayN/Resx/ResUI.Designer.cs @@ -2572,6 +2572,15 @@ namespace v2rayN.Resx { } } + /// + /// 查找类似 Singbox Mux Protocol 的本地化字符串。 + /// + public static string TbSettingsMux4SboxProtocol { + get { + return ResourceManager.GetString("TbSettingsMux4SboxProtocol", resourceCulture); + } + } + /// /// 查找类似 Turn on Mux Multiplexing 的本地化字符串。 /// diff --git a/v2rayN/v2rayN/Resx/ResUI.resx b/v2rayN/v2rayN/Resx/ResUI.resx index d4c2f302..42d25a66 100644 --- a/v2rayN/v2rayN/Resx/ResUI.resx +++ b/v2rayN/v2rayN/Resx/ResUI.resx @@ -1129,4 +1129,7 @@ Sing-box domain strategy + + Singbox Mux Protocol + \ No newline at end of file diff --git a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx index da34e169..778d3cae 100644 --- a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx @@ -1126,4 +1126,7 @@ Sing-box域名解析策略 + + Singbox Mux 多路复用协议 + \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs index 3f74de06..2607a847 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs @@ -31,6 +31,7 @@ namespace v2rayN.ViewModels [Reactive] public bool defAllowInsecure { get; set; } [Reactive] public string defFingerprint { get; set; } [Reactive] public string defUserAgent { get; set; } + [Reactive] public string mux4SboxProtocol { get; set; } #endregion Core @@ -131,6 +132,7 @@ namespace v2rayN.ViewModels defAllowInsecure = _config.coreBasicItem.defAllowInsecure; defFingerprint = _config.coreBasicItem.defFingerprint; defUserAgent = _config.coreBasicItem.defUserAgent; + mux4SboxProtocol = _config.mux4Sbox.protocol; #endregion Core @@ -298,6 +300,7 @@ namespace v2rayN.ViewModels _config.coreBasicItem.defAllowInsecure = defAllowInsecure; _config.coreBasicItem.defFingerprint = defFingerprint; _config.coreBasicItem.defUserAgent = defUserAgent; + _config.mux4Sbox.protocol = mux4SboxProtocol; //Kcp //_config.kcpItem.mtu = Kcpmtu; diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml index c19509ef..5f4caf4b 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml @@ -126,7 +126,6 @@ VerticalScrollBarVisibility="Auto" /> - \ No newline at end of file diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml index becc1e84..3b568f8b 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml @@ -67,6 +67,7 @@ + @@ -291,6 +292,21 @@ Margin="{StaticResource SettingItemMargin}" Style="{StaticResource ToolbarTextBlock}" Text="{x:Static resx:ResUI.TbSettingsDefUserAgentTips}" /> + + + @@ -790,34 +806,6 @@ - - - - - - - - -