From a6624e0370d23fa4896e2d9bc41d5195f95986c0 Mon Sep 17 00:00:00 2001
From: 2dust <31833384+2dust@users.noreply.github.com>
Date: Sun, 15 Nov 2026 14:56:32 +0800
Subject: [PATCH] Move DefaultServer function to ConfigTextHandler
---
.../Handler/Data/ConfigDbHandler.cs | 70 +------------------
.../Handler/Data/ConfigTextHandler.cs | 68 ++++++++++++++++++
.../ViewModels/MainWindowViewModel.cs | 2 +-
.../ViewModels/ProfilesViewModel.cs | 6 +-
.../ViewModels/StatusBarViewModel.cs | 4 +-
.../Services/V2rayRuntime.Profiles.cs | 2 +-
v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs | 6 +-
7 files changed, 78 insertions(+), 80 deletions(-)
diff --git a/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs b/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs
index d7510358..2e77feaf 100644
--- a/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs
+++ b/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs
@@ -187,73 +187,6 @@ public static class ConfigDbHandler
return 0;
}
- ///
- /// Set the default server by its index ID
- /// Updates the configuration to use the specified server as default
- ///
- /// Current configuration
- /// Index ID of the server to set as default
- /// 0 if successful, -1 if failed
- public static async Task SetDefaultServerIndex(Config config, string? indexId)
- {
- if (indexId.IsNullOrEmpty())
- {
- return -1;
- }
-
- config.IndexId = indexId;
-
- await SaveConfig();
-
- return 0;
- }
-
- ///
- /// Set a default server from the provided list of profiles
- /// Ensures there's always a valid default server selected
- ///
- /// Current configuration
- /// List of profile models to choose from
- /// Result of SetDefaultServerIndex operation
- public static async Task SetDefaultServer(Config config, List lstProfile)
- {
- if (lstProfile.Exists(t => t.IndexId == config.IndexId))
- {
- return 0;
- }
-
- if (await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync(t => t.IndexId == config.IndexId) != null)
- {
- return 0;
- }
- if (lstProfile.Count > 0)
- {
- return await SetDefaultServerIndex(config, lstProfile.FirstOrDefault(t => t.Port > 0)?.IndexId);
- }
-
- var item = await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync(t => t.Port > 0);
- return await SetDefaultServerIndex(config, item?.IndexId);
- }
-
- ///
- /// Get the current default server profile
- /// If the current default is invalid, selects a new default
- ///
- /// Current configuration
- /// The default profile item or null if none exists
- public static async Task GetDefaultServer(Config config)
- {
- var item = await ConfigDbQuery.GetProfileItem(config.IndexId);
- if (item is null)
- {
- var item2 = await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync();
- await SetDefaultServerIndex(config, item2?.IndexId);
- return item2;
- }
-
- return item;
- }
-
///
/// Move a server in the list to a different position
/// Supports moving to top, up, down, bottom or specific position
@@ -1415,7 +1348,6 @@ public static class ConfigDbHandler
return result;
}
-
///
/// Remove servers with invalid test results (timeout)
/// Useful for cleaning up subscription lists
@@ -1971,7 +1903,7 @@ public static class ConfigDbHandler
var existItem = FindMatchedProfileItem(lstSub, activeProfile);
if (existItem != null)
{
- await ConfigDbHandler.SetDefaultServerIndex(config, existItem.IndexId);
+ await ConfigTextHandler.SetDefaultServerIndex(existItem.IndexId);
}
}
diff --git a/v2rayN/ServiceLib/Handler/Data/ConfigTextHandler.cs b/v2rayN/ServiceLib/Handler/Data/ConfigTextHandler.cs
index b204ed05..80d03078 100644
--- a/v2rayN/ServiceLib/Handler/Data/ConfigTextHandler.cs
+++ b/v2rayN/ServiceLib/Handler/Data/ConfigTextHandler.cs
@@ -303,4 +303,72 @@ public static class ConfigTextHandler
}
#endregion UIItem
+
+ #region DefaultServer
+
+ ///
+ /// Set the default server by its index ID
+ /// Updates the configuration to use the specified server as default
+ ///
+ /// Index ID of the server to set as default
+ /// 0 if successful, -1 if failed
+ public static async Task SetDefaultServerIndex(string? indexId)
+ {
+ if (indexId.IsNullOrEmpty())
+ {
+ return -1;
+ }
+
+ Config.IndexId = indexId;
+
+ await SaveConfig();
+
+ return 0;
+ }
+
+ ///
+ /// Set a default server from the provided list of profiles
+ /// Ensures there's always a valid default server selected
+ ///
+ /// List of profile models to choose from
+ /// Result of SetDefaultServerIndex operation
+ public static async Task SetDefaultServer(List lstProfile)
+ {
+ if (lstProfile.Exists(t => t.IndexId == Config.IndexId))
+ {
+ return 0;
+ }
+
+ if (await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync(t => t.IndexId == Config.IndexId) != null)
+ {
+ return 0;
+ }
+ if (lstProfile.Count > 0)
+ {
+ return await SetDefaultServerIndex(lstProfile.FirstOrDefault(t => t.Port > 0)?.IndexId);
+ }
+
+ var item = await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync(t => t.Port > 0);
+ return await SetDefaultServerIndex(item?.IndexId);
+ }
+
+ ///
+ /// Get the current default server profile
+ /// If the current default is invalid, selects a new default
+ ///
+ /// The default profile item or null if none exists
+ public static async Task GetDefaultServer()
+ {
+ var item = await ConfigDbQuery.GetProfileItem(Config.IndexId);
+ if (item is null)
+ {
+ var item2 = await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync();
+ await SetDefaultServerIndex(item2?.IndexId);
+ return item2;
+ }
+
+ return item;
+ }
+
+ #endregion DefaultServer
}
diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
index a29eceff..c66b4bb3 100644
--- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
@@ -696,7 +696,7 @@ public partial class MainWindowViewModel : MyReactiveObject
TabMainSelectedIndex = 0;
}
});
- var profileItem = await ConfigDbHandler.GetDefaultServer(_config);
+ var profileItem = await ConfigTextHandler.GetDefaultServer();
if (profileItem == null)
{
NoticeManager.Instance.Enqueue(ResUI.CheckServerSettings);
diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs
index 49eca757..593a61cd 100644
--- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs
@@ -1,5 +1,3 @@
-
-
namespace ServiceLib.ViewModels;
public partial class ProfilesViewModel : MyReactiveObject
@@ -401,7 +399,7 @@ public partial class ProfilesViewModel : MyReactiveObject
{
var lstModel = await ConfigDbQuery.ProfileModels(_config.SubIndexId, filter);
- await ConfigDbHandler.SetDefaultServer(_config, lstModel);
+ await ConfigTextHandler.SetDefaultServer(lstModel);
var lstServerStat = (_config.GuiItem.EnableStatistics ? StatisticsManager.Instance.ServerStat : null) ?? [];
var lstProfileExs = await ProfileExManager.Instance.GetProfileExs();
@@ -585,7 +583,7 @@ public partial class ProfilesViewModel : MyReactiveObject
return;
}
- if (await ConfigDbHandler.SetDefaultServerIndex(_config, indexId) == 0)
+ if (await ConfigTextHandler.SetDefaultServerIndex(indexId) == 0)
{
await RefreshServers();
Reload();
diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
index e0f4846f..ddcfb489 100644
--- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
@@ -267,7 +267,7 @@ public partial class StatusBarViewModel : MyReactiveObject
await RefreshServersMenu();
//display running server
- var running = await ConfigDbHandler.GetDefaultServer(_config);
+ var running = await ConfigTextHandler.GetDefaultServer();
if (running != null)
{
RunningServerDisplay = running.GetSummary();
@@ -319,7 +319,7 @@ public partial class StatusBarViewModel : MyReactiveObject
public async Task TestServerAvailability()
{
- var item = await ConfigDbHandler.GetDefaultServer(_config);
+ var item = await ConfigTextHandler.GetDefaultServer();
if (item == null)
{
return null;
diff --git a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Profiles.cs b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Profiles.cs
index bfb98048..1950b4d5 100644
--- a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Profiles.cs
+++ b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Profiles.cs
@@ -269,7 +269,7 @@ public sealed partial class V2rayRuntime
}
if (removesCurrent)
{
- _ = await ConfigDbHandler.GetDefaultServer(Config);
+ _ = await ConfigTextHandler.GetDefaultServer();
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());
}
});
diff --git a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs
index ffa6f9a3..2dad72d7 100644
--- a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs
+++ b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs
@@ -268,7 +268,7 @@ public sealed partial class V2rayRuntime(
Config.SubIndexId = normalizedSubIndexId;
}
var selectedProfileId = Config.IndexId;
- var defaultProfile = await ConfigDbHandler.GetDefaultServer(Config);
+ var defaultProfile = await ConfigTextHandler.GetDefaultServer();
await _mutations.RunAsync(() => EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()));
await ProfileExManager.Instance.Init();
await CertPemManager.Instance.Init(Config);
@@ -502,7 +502,7 @@ public sealed partial class V2rayRuntime(
}
if (removesCurrentProfile)
{
- _ = await ConfigDbHandler.GetDefaultServer(Config);
+ _ = await ConfigTextHandler.GetDefaultServer();
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());
}
});
@@ -789,7 +789,7 @@ public sealed partial class V2rayRuntime(
private async Task GetDefaultProfileAsync() => await _mutations.RunAsync(async () =>
{
- var selected = await ConfigDbHandler.GetDefaultServer(Config);
+ var selected = await ConfigTextHandler.GetDefaultServer();
if (selected is not null)
{
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());