Add MusicBridge protocol documentation and implement system volume control
- Created PROTOCOL.md to outline the MusicBridge protocol v1, detailing setup, TLS, pairing, state management, and command structure. - Implemented SystemVolume class for managing system audio levels, including methods for setting, reading, changing, and muting volume. - Added unit tests for the MusicBridge agent, covering pairing, identity storage, command validation, and media state management. - Included tests for real-time media reading from Windows and ensured proper handling of artwork caching and retrieval.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace MusicBridge.Agent.Network;
|
||||
|
||||
// Windows DPAPI, scoped to the signed-in user. No UI or machine-wide key.
|
||||
internal static class UserProtection
|
||||
{
|
||||
public static byte[] Protect(byte[] bytes) => Transform(bytes, true);
|
||||
public static byte[] Unprotect(byte[] bytes) => Transform(bytes, false);
|
||||
|
||||
private static byte[] Transform(byte[] bytes, bool protect)
|
||||
{
|
||||
var input = new Blob { Size = bytes.Length, Data = Marshal.AllocHGlobal(bytes.Length) };
|
||||
var output = new Blob();
|
||||
try
|
||||
{
|
||||
Marshal.Copy(bytes, 0, input.Data, bytes.Length);
|
||||
var ok = protect
|
||||
? CryptProtectData(ref input, null, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 1, out output)
|
||||
: CryptUnprotectData(ref input, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 1, out output);
|
||||
if (!ok) throw new Win32Exception(Marshal.GetLastWin32Error(), "Не удалось открыть защищённое хранилище Windows.");
|
||||
var result = new byte[output.Size];
|
||||
Marshal.Copy(output.Data, result, 0, output.Size);
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.Copy(new byte[input.Size], 0, input.Data, input.Size);
|
||||
Marshal.FreeHGlobal(input.Data);
|
||||
if (output.Data != IntPtr.Zero)
|
||||
{
|
||||
Marshal.Copy(new byte[output.Size], 0, output.Data, output.Size);
|
||||
LocalFree(output.Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct Blob { public int Size; public IntPtr Data; }
|
||||
[DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool CryptProtectData(ref Blob input, string? description, IntPtr entropy, IntPtr reserved,
|
||||
IntPtr prompt, uint flags, out Blob output);
|
||||
[DllImport("crypt32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool CryptUnprotectData(ref Blob input, IntPtr description, IntPtr entropy, IntPtr reserved,
|
||||
IntPtr prompt, uint flags, out Blob output);
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern IntPtr LocalFree(IntPtr memory);
|
||||
}
|
||||
Reference in New Issue
Block a user