Skip to content

Commit

Permalink
Merge branch '2dust:master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix6936 authored Dec 7, 2024
2 parents 46c301d + 78a28fb commit 3257976
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 74 deletions.
40 changes: 4 additions & 36 deletions v2rayN/ServiceLib/Common/JsonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public static T DeepCopy<T>(T obj)
/// </summary>
/// <param name="obj"></param>
/// <param name="indented"></param>
/// <param name="nullValue"></param>
/// <returns></returns>
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
Expand All @@ -82,8 +83,8 @@ public static string Serialize(object? obj, bool indented = true)
}
var options = new JsonSerializerOptions
{
WriteIndented = indented ? true : false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
WriteIndented = indented,
DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull
};
result = JsonSerializer.Serialize(obj, options);
}
Expand All @@ -100,38 +101,5 @@ public static string Serialize(object? obj, bool indented = true)
/// <param name="obj"></param>
/// <returns></returns>
public static JsonNode? SerializeToNode(object? obj) => JsonSerializer.SerializeToNode(obj);

/// <summary>
/// Save as json file
/// </summary>
/// <param name="obj"></param>
/// <param name="filePath"></param>
/// <param name="nullValue"></param>
/// <returns></returns>
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;
}
}
}
}
55 changes: 25 additions & 30 deletions v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -67,7 +66,6 @@ public class ConfigHandler
}

config.RoutingBasicItem ??= new();

if (Utils.IsNullOrEmpty(config.RoutingBasicItem.DomainStrategy))
{
config.RoutingBasicItem.DomainStrategy = Global.DomainStrategies.First();//"IPIfNonMatch";
Expand Down Expand Up @@ -120,15 +118,6 @@ public class ConfigHandler
}

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)
Expand Down Expand Up @@ -167,6 +156,16 @@ public class ConfigHandler
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;
}

Expand All @@ -177,30 +176,26 @@ public class ConfigHandler
/// <returns></returns>
public static async Task<int> 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;
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/ServiceLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>7.3.0</Version>
<Version>7.3.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ public async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string
{
await GenInbounds(singboxConfig);
await GenExperimental(singboxConfig);
JsonUtils.ToFile(singboxConfig, fileName, false);

var content = JsonUtils.Serialize(singboxConfig, true);
await File.WriteAllTextAsync(fileName, content);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class StatisticsSingboxService
private ClientWebSocket? webSocket;
private Action<ServerSpeedItem>? _updateFunc;
private string Url => $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic";

public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc)
{
_config = config;
Expand Down
6 changes: 3 additions & 3 deletions v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,16 @@ public async Task MyAppExitAsync(bool blWindowsShutDown)
{
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
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public async Task InboundDisplayStatus()
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
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public MainWindow()
menuGlobalHotkeySetting.IsVisible = false;
}
menuAddServerViaScan.IsVisible = false;

RestoreUI();
AddHelpMenuItem();
//WindowsHandler.Instance.RegisterGlobalHotkey(_config, OnHotkeyHandler, null);
Expand Down
1 change: 1 addition & 0 deletions v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private void BtnAutofitColumnWidth_Click(object sender, RoutedEventArgs e)
{
AutofitColumnWidth();
}

private void AutofitColumnWidth()
{
foreach (var it in lstConnections.Columns)
Expand Down

0 comments on commit 3257976

Please sign in to comment.