v2rayN/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs
Miheichev Aleksandr Sergeevich 62b17203a5 Tighten platform annotations and finalize SQLite native sync
This commit refines the platform-aware bits of the .NET 10 dependency
modernization stack added earlier in this PR. It introduces no new
NuGet packages and does not change any pinned versions: dotnet list
package --outdated against v2rayN.sln returns only the eight Avalonia
12.x packages, all of which are explicitly out of scope for this PR.

ServiceLib/Common/Utils.cs:
- Annotate IsLinux() with [SupportedOSPlatformGuard("linux")] and
  IsMacOS() with [SupportedOSPlatformGuard("macos")] so the analyzer
  can narrow the platform context inside Linux/macOS guards in the same
  way it already does for Utils.IsWindows().
- Rewrite GetSystemHosts() as a cross-platform helper. Previously it
  unconditionally tried to read C:\Windows\System32\drivers\etc\{hosts,
  hosts.ics}, which silently returned an empty dictionary on
  Linux/macOS. The new implementation reads /etc/hosts on those
  platforms, fixing UseSystemHosts=true being a silent no-op outside
  Windows. Behavior on Windows is unchanged (still merges hosts and
  hosts.ics).

ServiceLib/Handler/AutoStartupHandler.cs:
- Mark the Linux helpers (ClearTaskLinux, SetTaskLinux,
  GetHomePathLinux) with [SupportedOSPlatform("linux")] and the macOS
  helpers (ClearTaskOSX, SetTaskOSX, GetLaunchAgentPathMacOS,
  GenerateLaunchAgentPlist) with [SupportedOSPlatform("macos")] for
  symmetry with the Windows helpers in the same file. The dispatch in
  UpdateTask is already gated by Utils.IsWindows / IsLinux / IsMacOS,
  whose new SupportedOSPlatformGuard attributes make these annotations
  enforceable by the analyzer.

ServiceLib/Handler/SysProxy/ProxySettingLinux.cs and
ServiceLib/Handler/SysProxy/ProxySettingOSX.cs:
- Mark the classes with [SupportedOSPlatform("linux")] and
  [SupportedOSPlatform("macos")] respectively. Both are reachable only
  through SysProxyHandler.UpdateSysProxy under the corresponding
  platform guard.

ServiceLib/Manager/AppManager.cs:
- Call SQLitePCL.Batteries_V2.Init() at the top of InitApp() in
  addition to the existing call in SQLiteHelper's static constructor.
  The call is idempotent, so it cooperates with that static
  constructor; the duplication is intentional defense-in-depth so that
  any future code path which reaches the database without going through
  SQLiteHelper (background services, tests, future utilities) will
  still find an initialized native provider rather than throwing
  "Library not initialized" at runtime.

ServiceLib/Manager/CoreManager.cs:
- Add a clarifying comment above the existing
  [SupportedOSPlatform("windows")] field-level attribute on
  _processJob, explaining that the attribute narrows the analyzer's
  platform context for every read/assign of the field and that runtime
  safety is preserved by the IsWindows() guard inside AddProcessJob. No
  code change.

package-rhel-riscv.sh:
- Promote the previously hard-coded SQLite amalgamation version
  (sqlite_year / sqlite_ver) inside build_sqlite_native_riscv64 to two
  env-overridable variables (SQLITE_AMALGAMATION_YEAR and
  SQLITE_AMALGAMATION_VER) declared at the top of the script next to
  SKIA_VER and HARFBUZZ_VER.
- Set the defaults to 2025 / 3500400, which corresponds to SQLite
  3.50.4 and matches SourceGear.sqlite3 3.50.4.5 used on the other
  RIDs (x64/arm64/macOS). Previously the script downloaded SQLite
  3.53.0 amalgamation on RISC-V, which left RISC-V users on a
  different SQLite minor version than every other platform shipped
  from this repository. The accompanying comment block documents the
  mapping rule between SourceGear.sqlite3 X.Y.Z.W and the
  SQLITE_AMALGAMATION_* values, so the next bump can be done in two
  coordinated places (this script and Directory.Packages.props)
  without drift. The version can still be overridden per-build via
  environment variables for ad-hoc testing on RISC-V.

Verification
------------
- dotnet restore + dotnet build v2rayN.sln -c Release: 0 errors,
  1 warning (CS8625 in GlobalHotKeys submodule, pre-existing,
  out-of-scope of this PR).
- dotnet test ServiceLib.Tests: 41/41 passed.
- dotnet publish v2rayN.Desktop.csproj for linux-x64 and osx-x64,
  and v2rayN.csproj for win-x64: all succeed with no new warnings.
- dotnet list package --outdated against v2rayN.sln: only the eight
  Avalonia 12.x packages appear; none are eligible while this PR
  stays on the Avalonia 11.x branch.
2026-05-03 01:28:51 +03:00

37 lines
1.3 KiB
C#

namespace ServiceLib.Handler.SysProxy;
[SupportedOSPlatform("macos")]
public static class ProxySettingOSX
{
private static readonly string _proxySetFileName = $"{Global.ProxySetOSXShellFileName.Replace(Global.NamespaceSample, "")}.sh";
public static async Task SetProxy(string host, int port, string exceptions)
{
List<string> args = ["set", host, port.ToString()];
if (exceptions.IsNotEmpty())
{
args.AddRange(exceptions.Split(','));
}
await ExecCmd(args);
}
public static async Task UnsetProxy()
{
List<string> args = ["clear"];
await ExecCmd(args);
}
private static async Task ExecCmd(List<string> args)
{
var customSystemProxyScriptPath = AppManager.Instance.Config.SystemProxyItem?.CustomSystemProxyScriptPath;
var fileName = (customSystemProxyScriptPath.IsNotEmpty() && File.Exists(customSystemProxyScriptPath))
? customSystemProxyScriptPath
: await FileUtils.CreateLinuxShellFile(_proxySetFileName, EmbedUtils.GetEmbedText(Global.ProxySetOSXShellFileName), false);
// TODO: temporarily notify which script is being used
NoticeManager.Instance.SendMessage(fileName);
await Utils.GetCliWrapOutput(fileName, args);
}
}