diff --git a/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/Loopback/LoopbackManager.cs b/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/Loopback/LoopbackManager.cs index 607349aa..565c2698 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/Loopback/LoopbackManager.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/Loopback/LoopbackManager.cs @@ -18,7 +18,7 @@ internal sealed unsafe class LoopbackManager : ObservableObject private readonly RuntimeOptions runtimeOptions; private readonly ITaskContext taskContext; - private readonly SID hutaoContainerSID; + private readonly string hutaoContainerStringSID = default!; private bool isLoopbackEnabled; public LoopbackManager(IServiceProvider serviceProvider) @@ -35,30 +35,30 @@ internal sealed unsafe class LoopbackManager : ObservableObject { INET_FIREWALL_APP_CONTAINER* pContainer = pContainers + i; ReadOnlySpan appContainerName = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(pContainer->appContainerName); - if (appContainerName.Equals(runtimeOptions.FamilyName)) + if (appContainerName.Equals(runtimeOptions.FamilyName, StringComparison.Ordinal)) { - hutaoContainerSID = *pContainer->appContainerSid; + ConvertSidToStringSidW(pContainer->appContainerSid, out PWSTR stringSid); + hutaoContainerStringSID = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(stringSid).ToString(); break; } } } finally { - uint retVal = NetworkIsolationFreeAppContainers(pContainers); - Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(*(WIN32_ERROR*)&retVal)); + // This function returns 1 rather than 0 specfied in the document. + _ = NetworkIsolationFreeAppContainers(pContainers); } WIN32_ERROR error2 = NetworkIsolationGetAppContainerConfig(out uint accCount, out SID_AND_ATTRIBUTES* pSids); Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(error2)); - fixed (SID* phutaoContainerSID = &hutaoContainerSID) + for (uint i = 0; i < accCount; i++) { - for (uint i = 0; i < accCount; i++) + ConvertSidToStringSidW((pSids + i)->Sid, out PWSTR stringSid); + ReadOnlySpan stringSidSpan = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(stringSid); + if (stringSidSpan.Equals(hutaoContainerStringSID, StringComparison.Ordinal)) { - if (EqualSid(phutaoContainerSID, (pSids + i)->Sid)) - { - IsLoopbackEnabled = true; - break; - } + IsLoopbackEnabled = true; + break; } } } @@ -74,12 +74,10 @@ internal sealed unsafe class LoopbackManager : ObservableObject sids.Add(*(pSids + i)); } - fixed (SID* phutaoContainerSID = &hutaoContainerSID) - { - SID_AND_ATTRIBUTES sidAndAttributes = default; - sidAndAttributes.Sid = phutaoContainerSID; - sids.Add(sidAndAttributes); - IsLoopbackEnabled = NetworkIsolationSetAppContainerConfig(CollectionsMarshal.AsSpan(sids)) is WIN32_ERROR.ERROR_SUCCESS; - } + ConvertStringSidToSidW(hutaoContainerStringSID, out PSID pSid); + SID_AND_ATTRIBUTES sidAndAttributes = default; + sidAndAttributes.Sid = pSid; + sids.Add(sidAndAttributes); + IsLoopbackEnabled = NetworkIsolationSetAppContainerConfig(CollectionsMarshal.AsSpan(sids)) is WIN32_ERROR.ERROR_SUCCESS; } } diff --git a/src/Snap.Hutao/Snap.Hutao/Win32/AdvApi32.cs b/src/Snap.Hutao/Snap.Hutao/Win32/AdvApi32.cs index d95aa0e4..754f8ce8 100644 --- a/src/Snap.Hutao/Snap.Hutao/Win32/AdvApi32.cs +++ b/src/Snap.Hutao/Snap.Hutao/Win32/AdvApi32.cs @@ -10,9 +10,37 @@ using System.Runtime.Versioning; namespace Snap.Hutao.Win32; [SuppressMessage("", "SH002")] +[SuppressMessage("", "SA1313")] [SuppressMessage("", "SYSLIB1054")] internal static class AdvApi32 { + [DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)] + [SupportedOSPlatform("windows5.1.2600")] + public static unsafe extern BOOL ConvertSidToStringSidW(PSID Sid, PWSTR* StringSid); + + public static unsafe BOOL ConvertSidToStringSidW(PSID Sid, out PWSTR StringSid) + { + fixed (PWSTR* pStringSid = &StringSid) + { + return ConvertSidToStringSidW(Sid, pStringSid); + } + } + + [DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)] + [SupportedOSPlatform("windows5.1.2600")] + public static unsafe extern BOOL ConvertStringSidToSidW(PCWSTR StringSid, [Out] PSID* Sid); + + public static unsafe BOOL ConvertStringSidToSidW(ReadOnlySpan StringSid, out PSID Sid) + { + fixed (char* pStringSid = StringSid) + { + fixed (PSID* pSid = &Sid) + { + return ConvertStringSidToSidW(pStringSid, pSid); + } + } + } + [DllImport("ADVAPI32.dll", ExactSpelling = true, SetLastError = true)] [SupportedOSPlatform("windows5.1.2600")] public static extern BOOL EqualSid(PSID pSid1, PSID pSid2);