From 5986853801bbab6d4e4cefec468ceb47f42f87a8 Mon Sep 17 00:00:00 2001 From: lazycat75246 <153997642+lazycat75246@users.noreply.github.com> Date: Sun, 17 Dec 2023 17:26:21 +0800 Subject: [PATCH] add autoswitch toggle button --- v2rayN/v2rayN/Handler/ConfigHandler.cs | 7 +++ v2rayN/v2rayN/Handler/ServerAutoSwitch.cs | 7 ++- v2rayN/v2rayN/Mode/Config.cs | 1 + v2rayN/v2rayN/Mode/ConfigItems.cs | 9 ++++ v2rayN/v2rayN/Resx/ResUI.Designer.cs | 9 ++++ v2rayN/v2rayN/Resx/ResUI.resx | 3 ++ v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx | 3 ++ .../v2rayN/ViewModels/MainWindowViewModel.cs | 50 +++++++++++++++++-- v2rayN/v2rayN/Views/MainWindow.xaml | 8 ++- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 9 +++- 10 files changed, 100 insertions(+), 6 deletions(-) diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index 46f72d07..258d7896 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -133,6 +133,13 @@ namespace v2rayN.Handler mtu = 9000, }; } + if (config.autoSwitchItem == null) + { + config.autoSwitchItem = new AutoSwitchItem + { + EnableAutoSwitch = false, + }; + } if (config.guiItem == null) { config.guiItem = new() diff --git a/v2rayN/v2rayN/Handler/ServerAutoSwitch.cs b/v2rayN/v2rayN/Handler/ServerAutoSwitch.cs index 61394a34..06faf233 100644 --- a/v2rayN/v2rayN/Handler/ServerAutoSwitch.cs +++ b/v2rayN/v2rayN/Handler/ServerAutoSwitch.cs @@ -16,7 +16,7 @@ namespace v2rayN.Handler public string indexId { get; set; } public long latency { get; set; } } - internal class ServerAutoSwitch + public class ServerAutoSwitch { private List testResultItems= new List(); private static readonly object objLock = new(); @@ -70,6 +70,11 @@ namespace v2rayN.Handler if (taskmain != null) return; + var profiles = LazyConfig.Instance.ProfileItemsAutoSwitch(); + if (profiles.Count < 2) + return; + bStop = false; + taskmain = Task.Run(() => { int iFailTimeMax = 30; diff --git a/v2rayN/v2rayN/Mode/Config.cs b/v2rayN/v2rayN/Mode/Config.cs index fde5a14f..c7b580db 100644 --- a/v2rayN/v2rayN/Mode/Config.cs +++ b/v2rayN/v2rayN/Mode/Config.cs @@ -20,6 +20,7 @@ public CoreBasicItem coreBasicItem { get; set; } public TunModeItem tunModeItem { get; set; } + public AutoSwitchItem autoSwitchItem { get; set; } public KcpItem kcpItem { get; set; } public GrpcItem grpcItem { get; set; } public RoutingBasicItem routingBasicItem { get; set; } diff --git a/v2rayN/v2rayN/Mode/ConfigItems.cs b/v2rayN/v2rayN/Mode/ConfigItems.cs index 57068635..a4e20d12 100644 --- a/v2rayN/v2rayN/Mode/ConfigItems.cs +++ b/v2rayN/v2rayN/Mode/ConfigItems.cs @@ -167,6 +167,15 @@ namespace v2rayN.Mode public bool enableIPv6Address { get; set; } = true; } + [Serializable] + public class AutoSwitchItem + { + public bool EnableAutoSwitch { get; set; } + public int FailTimeMax { get; set; } = 60; + public int ServerSelectMode { get; set; } = 1; + public int mode { get; set; } = 0; + } + [Serializable] public class SpeedTestItem { diff --git a/v2rayN/v2rayN/Resx/ResUI.Designer.cs b/v2rayN/v2rayN/Resx/ResUI.Designer.cs index 60e21744..c7257aa2 100644 --- a/v2rayN/v2rayN/Resx/ResUI.Designer.cs +++ b/v2rayN/v2rayN/Resx/ResUI.Designer.cs @@ -2041,6 +2041,15 @@ namespace v2rayN.Resx { } } + /// + /// 查找类似 AutoSwitch 的本地化字符串。 + /// + public static string TbEnableAutoSwitchAs { + get { + return ResourceManager.GetString("TbEnableAutoSwitchAs", resourceCulture); + } + } + /// /// 查找类似 Enable advanced function 的本地化字符串。 /// diff --git a/v2rayN/v2rayN/Resx/ResUI.resx b/v2rayN/v2rayN/Resx/ResUI.resx index 95c42760..5d177076 100644 --- a/v2rayN/v2rayN/Resx/ResUI.resx +++ b/v2rayN/v2rayN/Resx/ResUI.resx @@ -988,6 +988,9 @@ Enable Tun + + AutoSwitch + New Port for LAN diff --git a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx index 42cac502..62e07027 100644 --- a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx @@ -988,6 +988,9 @@ 启用Tun模式 + + 自动切换 + 为局域网开启新的端口 diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index 989aaa6d..9a998a5d 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -21,6 +21,7 @@ using v2rayN.Views; namespace v2rayN.ViewModels { + public delegate void SetAutoSwitchTogDelegate(bool b); public class MainWindowViewModel : ReactiveObject { #region private prop @@ -35,7 +36,11 @@ namespace v2rayN.ViewModels private readonly PaletteHelper _paletteHelper = new(); private Dictionary _dicHeaderSort = new(); private Action _updateView; - + private SetAutoSwitchTogDelegate _setAutoSwitchTog; + public void SetDelegate(SetAutoSwitchTogDelegate s = null) + { + this._setAutoSwitchTog = s; + } #endregion private prop #region ObservableCollection @@ -228,6 +233,9 @@ namespace v2rayN.ViewModels [Reactive] public bool EnableTun { get; set; } + [Reactive] + public bool EnableAutoSwitch { get; set; } + [Reactive] public bool ColorModeDark { get; set; } @@ -250,7 +258,7 @@ namespace v2rayN.ViewModels - ServerAutoSwitch ServerAutoSwitchs= new ServerAutoSwitch(); + public ServerAutoSwitch ServerAutoSwitchs= new ServerAutoSwitch(); #region Init @@ -276,6 +284,8 @@ namespace v2rayN.ViewModels } _subId = _config.subIndexId; + EnableAutoSwitch = _config.autoSwitchItem.EnableAutoSwitch; + InitSubscriptionView(); RefreshRoutingsMenu(); RefreshServers(); @@ -319,6 +329,8 @@ namespace v2rayN.ViewModels y => y == true) .Subscribe(c => DoEnableTun(c)); + this.WhenAnyValue(x => x.EnableAutoSwitch).Subscribe(c => DoEnableAutoSwitch(c)); + BindingUI(); RestoreUI(); AutoHideStartup(); @@ -593,7 +605,8 @@ namespace v2rayN.ViewModels Reload(); ChangeSystemProxyStatus(_config.sysProxyType, true); ServerAutoSwitchs.SetDelegate(SetDefaultServer); - ServerAutoSwitchs.Start(); + if(_config.autoSwitchItem.EnableAutoSwitch) + ServerAutoSwitchs.Start(); } private void OnProgramStarted(object state, bool timeout) @@ -1644,6 +1657,37 @@ namespace v2rayN.ViewModels } } + private void DoEnableAutoSwitch(bool c) + { + if (_config.autoSwitchItem.EnableAutoSwitch != c) + { + _config.autoSwitchItem.EnableAutoSwitch = c; + + if (c) + { + var profiles = LazyConfig.Instance.ProfileItemsAutoSwitch(); + if (profiles.Count < 2) + { + MessageBox.Show("ѡлڵ2л!"); + _setAutoSwitchTog(false); + _config.autoSwitchItem.EnableAutoSwitch = false; + EnableAutoSwitch = false; + ConfigHandler.SaveConfig(ref _config); + return; + } + ServerAutoSwitchs.Start(); + } + else + { + Task.Run(() => + { + ServerAutoSwitchs.Stop(); + }); + } + ConfigHandler.SaveConfig(ref _config); + } + } + #endregion System proxy and Routings #region UI diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml b/v2rayN/v2rayN/Views/MainWindow.xaml index 90e483c6..8a801dc2 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml +++ b/v2rayN/v2rayN/Views/MainWindow.xaml @@ -431,6 +431,12 @@ materialDesign:HintAssist.Hint="{x:Static resx:ResUI.MsgServerTitle}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource DefTextBox}" /> + + + ToolTipText="v2rayN" d:IsHidden="True"> diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index e9e7a32d..1c3cd035 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -60,6 +60,8 @@ namespace v2rayN.Views ViewModel = new MainWindowViewModel(MainSnackbar.MessageQueue!, UpdateViewHandler); Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel)); + ViewModel.SetDelegate((bool b)=> togEnableAutoSwitch.IsChecked = b); + for (int i = Global.MinFontSize; i <= Global.MinFontSize + 8; i++) { cmbCurrentFontSize.Items.Add(i.ToString()); @@ -192,6 +194,7 @@ namespace v2rayN.Views this.OneWayBind(ViewModel, vm => vm.SpeedProxyDisplay, v => v.txtSpeedProxyDisplay.Text).DisposeWith(disposables); this.OneWayBind(ViewModel, vm => vm.SpeedDirectDisplay, v => v.txtSpeedDirectDisplay.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.EnableTun, v => v.togEnableTun.IsChecked).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.EnableAutoSwitch, v => v.togEnableAutoSwitch.IsChecked).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SystemProxySelected, v => v.cmbSystemProxy.SelectedIndex).DisposeWith(disposables); this.OneWayBind(ViewModel, vm => vm.RoutingItems, v => v.cmbRoutings2.ItemsSource).DisposeWith(disposables); @@ -466,7 +469,7 @@ namespace v2rayN.Views Width = _config.uiItem.mainWidth; Height = _config.uiItem.mainHeight; } - + var maxWidth = SystemParameters.WorkArea.Width; var maxHeight = SystemParameters.WorkArea.Height; if (Width > maxWidth) Width = maxWidth; @@ -569,6 +572,10 @@ namespace v2rayN.Views var item = LazyConfig.Instance.GetProfileItem(ViewModel.SelectedProfile.indexId); item.autoSwitch = ViewModel.SelectedProfile.autoSwitch; SqliteHelper.Instance.Update(item); + //if(ViewModel.EnableAutoSwitch) + //{ + // ViewModel.ServerAutoSwitchs.Start(); + //} } #endregion UI