fix: tighten Unix platform handling

Adds Linux and macOS platform guards so the analyzer can narrow calls through Utils.IsLinux() and Utils.IsMacOS().

Marks the Linux/macOS autostart and system proxy helpers with explicit platform attributes.

Updates Utils.GetSystemHosts() to read /etc/hosts on Linux and macOS while keeping the existing Windows hosts and hosts.ics merge behavior.
This commit is contained in:
Miheichev Aleksandr Sergeevich 2026-05-13 15:41:52 +03:00
parent 06796e474a
commit 25c7c69886
4 changed files with 27 additions and 6 deletions

View file

@ -863,6 +863,8 @@ public class Utils
} }
public static Dictionary<string, string> GetSystemHosts() public static Dictionary<string, string> GetSystemHosts()
{
if (IsWindows())
{ {
var hosts = GetSystemHosts(@"C:\Windows\System32\drivers\etc\hosts"); var hosts = GetSystemHosts(@"C:\Windows\System32\drivers\etc\hosts");
var hostsIcs = GetSystemHosts(@"C:\Windows\System32\drivers\etc\hosts.ics"); var hostsIcs = GetSystemHosts(@"C:\Windows\System32\drivers\etc\hosts.ics");
@ -875,6 +877,14 @@ public class Utils
return hosts; return hosts;
} }
if (IsLinux() || IsMacOS())
{
return GetSystemHosts("/etc/hosts");
}
return new Dictionary<string, string>();
}
public static async Task<string?> GetCliWrapOutput(string filePath, string? arg) public static async Task<string?> GetCliWrapOutput(string filePath, string? arg)
{ {
return await GetCliWrapOutput(filePath, arg != null ? new List<string>() { arg } : null); return await GetCliWrapOutput(filePath, arg != null ? new List<string>() { arg } : null);
@ -1117,8 +1127,10 @@ public class Utils
[SupportedOSPlatformGuard("windows")] [SupportedOSPlatformGuard("windows")]
public static bool IsWindows() => OperatingSystem.IsWindows(); public static bool IsWindows() => OperatingSystem.IsWindows();
[SupportedOSPlatformGuard("linux")]
public static bool IsLinux() => OperatingSystem.IsLinux(); public static bool IsLinux() => OperatingSystem.IsLinux();
[SupportedOSPlatformGuard("macos")]
public static bool IsMacOS() => OperatingSystem.IsMacOS(); public static bool IsMacOS() => OperatingSystem.IsMacOS();
[UnsupportedOSPlatformGuard("windows")] [UnsupportedOSPlatformGuard("windows")]

View file

@ -127,6 +127,7 @@ public static class AutoStartupHandler
#region Linux #region Linux
[SupportedOSPlatform("linux")]
private static async Task ClearTaskLinux() private static async Task ClearTaskLinux()
{ {
try try
@ -140,6 +141,7 @@ public static class AutoStartupHandler
await Task.CompletedTask; await Task.CompletedTask;
} }
[SupportedOSPlatform("linux")]
private static async Task SetTaskLinux() private static async Task SetTaskLinux()
{ {
try try
@ -160,6 +162,7 @@ public static class AutoStartupHandler
} }
} }
[SupportedOSPlatform("linux")]
private static string GetHomePathLinux() private static string GetHomePathLinux()
{ {
var homePath = Path.Combine(Utils.GetHomePath(), ".config", "autostart", $"{Global.AppName}.desktop"); var homePath = Path.Combine(Utils.GetHomePath(), ".config", "autostart", $"{Global.AppName}.desktop");
@ -171,6 +174,7 @@ public static class AutoStartupHandler
#region macOS #region macOS
[SupportedOSPlatform("macos")]
private static async Task ClearTaskOSX() private static async Task ClearTaskOSX()
{ {
try try
@ -190,6 +194,7 @@ public static class AutoStartupHandler
} }
} }
[SupportedOSPlatform("macos")]
private static async Task SetTaskOSX() private static async Task SetTaskOSX()
{ {
try try
@ -207,6 +212,7 @@ public static class AutoStartupHandler
} }
} }
[SupportedOSPlatform("macos")]
private static string GetLaunchAgentPathMacOS() private static string GetLaunchAgentPathMacOS()
{ {
var homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); var homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
@ -215,6 +221,7 @@ public static class AutoStartupHandler
return launchAgentPath; return launchAgentPath;
} }
[SupportedOSPlatform("macos")]
private static string GenerateLaunchAgentPlist() private static string GenerateLaunchAgentPlist()
{ {
var exePath = Utils.GetExePath(); var exePath = Utils.GetExePath();

View file

@ -1,5 +1,6 @@
namespace ServiceLib.Handler.SysProxy; namespace ServiceLib.Handler.SysProxy;
[SupportedOSPlatform("linux")]
public static class ProxySettingLinux public static class ProxySettingLinux
{ {
private static readonly string _proxySetFileName = $"{Global.ProxySetLinuxShellFileName.Replace(Global.NamespaceSample, "")}.sh"; private static readonly string _proxySetFileName = $"{Global.ProxySetLinuxShellFileName.Replace(Global.NamespaceSample, "")}.sh";

View file

@ -1,5 +1,6 @@
namespace ServiceLib.Handler.SysProxy; namespace ServiceLib.Handler.SysProxy;
[SupportedOSPlatform("macos")]
public static class ProxySettingOSX public static class ProxySettingOSX
{ {
private static readonly string _proxySetFileName = $"{Global.ProxySetOSXShellFileName.Replace(Global.NamespaceSample, "")}.sh"; private static readonly string _proxySetFileName = $"{Global.ProxySetOSXShellFileName.Replace(Global.NamespaceSample, "")}.sh";