From e9b0372174c5be22bd6469e713f93512bfbdeb8d Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 29 Apr 2023 19:38:00 +0800 Subject: [PATCH] Enable pre tun mode for other cores --- v2rayN/v2rayN/Handler/CoreHandler.cs | 48 +++++++++++-------- v2rayN/v2rayN/Handler/SpeedtestHandler.cs | 6 +-- .../v2rayN/ViewModels/MainWindowViewModel.cs | 4 +- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index 9875ff85..24cf5255 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -11,29 +11,31 @@ namespace v2rayN.Handler /// internal class CoreHandler { + private Config _config; private CoreInfo? _coreInfo; private Process? _process; private Process? _processPre; private Action _updateFunc; - public CoreHandler(Action update) + public CoreHandler(Config config, Action update) { + _config = config; _updateFunc = update; Environment.SetEnvironmentVariable("v2ray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("xray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process); } - public void LoadCore(Config config) + public void LoadCore() { - var node = ConfigHandler.GetDefaultServer(ref config); + var node = ConfigHandler.GetDefaultServer(ref _config); if (node == null) { ShowMsg(false, ResUI.CheckServerSettings); return; } - if (SetCore(config, node) != 0) + if (SetCore(node) != 0) { ShowMsg(false, ResUI.CheckServerSettings); return; @@ -52,10 +54,10 @@ namespace v2rayN.Handler } } - public int LoadCoreConfigString(Config config, List _selecteds) + public int LoadCoreConfigString(List _selecteds) { int pid = -1; - string configStr = CoreConfigHandler.GenerateClientSpeedtestConfigString(config, _selecteds, out string msg); + string configStr = CoreConfigHandler.GenerateClientSpeedtestConfigString(_config, _selecteds, out string msg); if (configStr == "") { ShowMsg(false, msg); @@ -158,23 +160,27 @@ namespace v2rayN.Handler _process = proc; //start a socks service - if (_process != null && !_process.HasExited && node.configType == EConfigType.Custom && node.preSocksPort > 0) + if (_process != null && !_process.HasExited) { - var itemSocks = new ProfileItem() + if ((node.configType == EConfigType.Custom && node.preSocksPort > 0) + || (node.configType != EConfigType.Custom && _coreInfo.coreType != ECoreType.sing_box && _config.tunModeItem.enableTun)) { - coreType = ECoreType.sing_box, - configType = EConfigType.Socks, - address = Global.Loopback, - 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); - if (proc2 is not null) + var itemSocks = new ProfileItem() { - _processPre = proc2; + coreType = ECoreType.sing_box, + configType = EConfigType.Socks, + address = Global.Loopback, + port = node.preSocksPort > 0 ? node.preSocksPort : LazyConfig.Instance.GetLocalPort(Global.InboundSocks) + }; + 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); + if (proc2 is not null) + { + _processPre = proc2; + } } } } @@ -251,7 +257,7 @@ namespace v2rayN.Handler _updateFunc(updateToTrayTooltip, msg); } - private int SetCore(Config config, ProfileItem node) + private int SetCore(ProfileItem node) { if (node == null) { diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index ebf4b2c4..f177ae7a 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -150,7 +150,7 @@ namespace v2rayN.Handler { string msg = string.Empty; - pid = _coreHandler.LoadCoreConfigString(_config, _selecteds); + pid = _coreHandler.LoadCoreConfigString(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore); @@ -212,7 +212,7 @@ namespace v2rayN.Handler // _selecteds = _selecteds.OrderBy(t => t.delay).ToList(); //} - pid = _coreHandler.LoadCoreConfigString(_config, _selecteds); + pid = _coreHandler.LoadCoreConfigString(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore); @@ -269,7 +269,7 @@ namespace v2rayN.Handler private async Task RunSpeedTestMulti() { int pid = -1; - pid = _coreHandler.LoadCoreConfigString(_config, _selecteds); + pid = _coreHandler.LoadCoreConfigString(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore); diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index 16c517d6..3c4b3f8d 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -557,7 +557,7 @@ namespace v2rayN.ViewModels { ConfigHandler.InitBuiltinRouting(ref _config); ConfigHandler.InitBuiltinDNS(_config); - _coreHandler = new CoreHandler(UpdateHandler); + _coreHandler = new CoreHandler(_config, UpdateHandler); if (_config.guiItem.enableStatistics) { @@ -1481,7 +1481,7 @@ namespace v2rayN.ViewModels await Task.Run(() => { - _coreHandler.LoadCore(_config); + _coreHandler.LoadCore(); //ConfigHandler.SaveConfig(ref _config, false);