From 83ad83b13577d0567c96789897eb2f62b2f04ca5 Mon Sep 17 00:00:00 2001
From: 2dust <31833384+2dust@users.noreply.github.com>
Date: Sat, 7 Dec 2024 14:07:51 +0800
Subject: [PATCH 1/3] Optimize save config files
---
v2rayN/ServiceLib/Common/JsonUtils.cs | 40 ++------------
v2rayN/ServiceLib/Handler/ConfigHandler.cs | 55 +++++++++----------
.../CoreConfig/CoreConfigSingboxService.cs | 4 +-
.../ViewModels/MainWindowViewModel.cs | 6 +-
4 files changed, 35 insertions(+), 70 deletions(-)
diff --git a/v2rayN/ServiceLib/Common/JsonUtils.cs b/v2rayN/ServiceLib/Common/JsonUtils.cs
index 4d0f8e71..cf5ec25c 100644
--- a/v2rayN/ServiceLib/Common/JsonUtils.cs
+++ b/v2rayN/ServiceLib/Common/JsonUtils.cs
@@ -70,8 +70,9 @@ namespace ServiceLib.Common
///
///
///
+ ///
///
- public static string Serialize(object? obj, bool indented = true)
+ public static string Serialize(object? obj, bool indented = true, bool nullValue = false)
{
var result = string.Empty;
try
@@ -82,8 +83,8 @@ namespace ServiceLib.Common
}
var options = new JsonSerializerOptions
{
- WriteIndented = indented ? true : false,
- DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
+ WriteIndented = indented,
+ DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull
};
result = JsonSerializer.Serialize(obj, options);
}
@@ -100,38 +101,5 @@ namespace ServiceLib.Common
///
///
public static JsonNode? SerializeToNode(object? obj) => JsonSerializer.SerializeToNode(obj);
-
- ///
- /// Save as json file
- ///
- ///
- ///
- ///
- ///
- public static int ToFile(object? obj, string? filePath, bool nullValue = true)
- {
- if (filePath is null)
- {
- return -1;
- }
- try
- {
- using var file = File.Create(filePath);
-
- var options = new JsonSerializerOptions
- {
- WriteIndented = true,
- DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull
- };
-
- JsonSerializer.Serialize(file, obj, options);
- return 0;
- }
- catch (Exception ex)
- {
- Logging.SaveLog(ex.Message, ex);
- return -1;
- }
- }
}
}
\ No newline at end of file
diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs
index 8c066868..39d6cb1c 100644
--- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs
+++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs
@@ -9,7 +9,6 @@ namespace ServiceLib.Handler
public class ConfigHandler
{
private static readonly string _configRes = Global.ConfigFileName;
- private static readonly object _objLock = new();
#region ConfigHandler
@@ -67,7 +66,6 @@ namespace ServiceLib.Handler
}
config.RoutingBasicItem ??= new();
-
if (Utils.IsNullOrEmpty(config.RoutingBasicItem.DomainStrategy))
{
config.RoutingBasicItem.DomainStrategy = Global.DomainStrategies.First();//"IPIfNonMatch";
@@ -120,15 +118,6 @@ namespace ServiceLib.Handler
}
config.ConstItem ??= new ConstItem();
- if (Utils.IsNotEmpty(config.ConstItem.DefIEProxyExceptions))
- {
- config.SystemProxyItem.SystemProxyExceptions = $"{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}";
- config.ConstItem.DefIEProxyExceptions = string.Empty;
- }
- if (config.SystemProxyItem.SystemProxyExceptions.IsNullOrEmpty())
- {
- config.SystemProxyItem.SystemProxyExceptions = Utils.IsWindows() ? Global.SystemProxyExceptionsWindows : Global.SystemProxyExceptionsLinux;
- }
config.SpeedTestItem ??= new();
if (config.SpeedTestItem.SpeedTestTimeout < 10)
@@ -167,6 +156,16 @@ namespace ServiceLib.Handler
config.WebDavItem ??= new();
config.CheckUpdateItem ??= new();
+ if (Utils.IsNotEmpty(config.ConstItem.DefIEProxyExceptions))
+ {
+ config.SystemProxyItem.SystemProxyExceptions = $"{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}";
+ config.ConstItem.DefIEProxyExceptions = string.Empty;
+ }
+ if (config.SystemProxyItem.SystemProxyExceptions.IsNullOrEmpty())
+ {
+ config.SystemProxyItem.SystemProxyExceptions = Utils.IsWindows() ? Global.SystemProxyExceptionsWindows : Global.SystemProxyExceptionsLinux;
+ }
+
return config;
}
@@ -177,30 +176,26 @@ namespace ServiceLib.Handler
///
public static async Task SaveConfig(Config config)
{
- lock (_objLock)
+ try
{
- try
- {
- //save temp file
- var resPath = Utils.GetConfigPath(_configRes);
- var tempPath = $"{resPath}_temp";
- if (JsonUtils.ToFile(config, tempPath) != 0)
- {
- return -1;
- }
+ //save temp file
+ var resPath = Utils.GetConfigPath(_configRes);
+ var tempPath = $"{resPath}_temp";
- if (File.Exists(resPath))
- {
- File.Delete(resPath);
- }
- //rename
- File.Move(tempPath, resPath);
- }
- catch (Exception ex)
+ var content = JsonUtils.Serialize(config, true, true);
+ if (content.IsNullOrEmpty())
{
- Logging.SaveLog("ToJsonFile", ex);
return -1;
}
+ await File.WriteAllTextAsync(tempPath, content);
+
+ //rename
+ File.Move(tempPath, resPath, true);
+ }
+ catch (Exception ex)
+ {
+ Logging.SaveLog("ToJsonFile", ex);
+ return -1;
}
return 0;
diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs
index 202b5089..e1f26b1b 100644
--- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs
+++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs
@@ -409,7 +409,9 @@ namespace ServiceLib.Services.CoreConfig
{
await GenInbounds(singboxConfig);
await GenExperimental(singboxConfig);
- JsonUtils.ToFile(singboxConfig, fileName, false);
+
+ var content = JsonUtils.Serialize(singboxConfig, true);
+ await File.WriteAllTextAsync(fileName, content);
}
}
else
diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
index 9b016ca6..ff680c3d 100644
--- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
@@ -282,16 +282,16 @@ namespace ServiceLib.ViewModels
{
try
{
- Logging.SaveLog("MyAppExit Begin");
- await SysProxyHandler.UpdateSysProxy(_config, true);
+ Logging.SaveLog("MyAppExitAsync Begin");
await ConfigHandler.SaveConfig(_config);
+ await SysProxyHandler.UpdateSysProxy(_config, true);
await ProfileExHandler.Instance.SaveTo();
await StatisticsHandler.Instance.SaveTo();
StatisticsHandler.Instance.Close();
await CoreHandler.Instance.CoreStop();
- Logging.SaveLog("MyAppExit End");
+ Logging.SaveLog("MyAppExitAsync End");
}
catch { }
finally
From bf8bbbdcb0c08917c7581f325f8a1bd005f78064 Mon Sep 17 00:00:00 2001
From: 2dust <31833384+2dust@users.noreply.github.com>
Date: Sat, 7 Dec 2024 16:29:58 +0800
Subject: [PATCH 2/3] Code clean
---
.../ServiceLib/Services/Statistics/StatisticsSingboxService.cs | 2 +-
v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs | 2 +-
v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs | 2 +-
v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs | 1 +
4 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs b/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs
index e8f294e4..ef70e3f7 100644
--- a/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs
+++ b/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs
@@ -10,7 +10,7 @@ namespace ServiceLib.Services.Statistics
private ClientWebSocket? webSocket;
private Action? _updateFunc;
private string Url => $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic";
-
+
public StatisticsSingboxService(Config config, Action updateFunc)
{
_config = config;
diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
index e999576f..f66a15c3 100644
--- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
@@ -459,7 +459,7 @@ namespace ServiceLib.ViewModels
if (_config.Inbound.First().NewPort4LAN)
{
StringBuilder sb2 = new();
- sb2.Append($"[{EInboundProtocol.mixed}:{AppHandler.Instance.GetLocalPort(EInboundProtocol.socks2)}]");
+ sb2.Append($"[{EInboundProtocol.mixed}:{AppHandler.Instance.GetLocalPort(EInboundProtocol.socks2)}]");
InboundLanDisplay = $"{ResUI.LabLAN}:{sb2}";
}
else
diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
index fa214786..9bd228af 100644
--- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
+++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
@@ -152,7 +152,7 @@ namespace v2rayN.Desktop.Views
menuGlobalHotkeySetting.IsVisible = false;
}
menuAddServerViaScan.IsVisible = false;
-
+
RestoreUI();
AddHelpMenuItem();
//WindowsHandler.Instance.RegisterGlobalHotkey(_config, OnHotkeyHandler, null);
diff --git a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs
index 9de1ceae..acc0cfec 100644
--- a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs
+++ b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs
@@ -51,6 +51,7 @@ namespace v2rayN.Views
{
AutofitColumnWidth();
}
+
private void AutofitColumnWidth()
{
foreach (var it in lstConnections.Columns)
From 78a28fbdb32fe8d1e48c63e65bb075c759b8e444 Mon Sep 17 00:00:00 2001
From: 2dust <31833384+2dust@users.noreply.github.com>
Date: Sat, 7 Dec 2024 16:30:32 +0800
Subject: [PATCH 3/3] up 7.3.1
---
v2rayN/ServiceLib/ServiceLib.csproj | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/v2rayN/ServiceLib/ServiceLib.csproj b/v2rayN/ServiceLib/ServiceLib.csproj
index fb012498..cdaf8ed3 100644
--- a/v2rayN/ServiceLib/ServiceLib.csproj
+++ b/v2rayN/ServiceLib/ServiceLib.csproj
@@ -4,7 +4,7 @@
net8.0
enable
enable
- 7.3.0
+ 7.3.1