From fc2a5acfca4b12f1f5b7dc17ce34bdd890641dc6 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 10 Mar 2017 13:33:17 -0500 Subject: move loopback util --- .../Native/LoopbackUtil.cs | 358 --------------------- 1 file changed, 358 deletions(-) delete mode 100644 MediaBrowser.ServerApplication/Native/LoopbackUtil.cs (limited to 'MediaBrowser.ServerApplication/Native') diff --git a/MediaBrowser.ServerApplication/Native/LoopbackUtil.cs b/MediaBrowser.ServerApplication/Native/LoopbackUtil.cs deleted file mode 100644 index 5b260685b..000000000 --- a/MediaBrowser.ServerApplication/Native/LoopbackUtil.cs +++ /dev/null @@ -1,358 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - -namespace MediaBrowser.ServerApplication.Native -{ - /// - /// http://blogs.msdn.com/b/fiddler/archive/2011/12/10/fiddler-windows-8-apps-enable-LoopUtil-network-isolation-exemption.aspx - /// - public class LoopUtil - { - //http://msdn.microsoft.com/en-us/library/windows/desktop/aa379595(v=vs.85).aspx - [StructLayout(LayoutKind.Sequential)] - internal struct SID_AND_ATTRIBUTES - { - public IntPtr Sid; - public uint Attributes; - } - - [StructLayoutAttribute(LayoutKind.Sequential)] - internal struct INET_FIREWALL_AC_CAPABILITIES - { - public uint count; - public IntPtr capabilities; //SID_AND_ATTRIBUTES - } - - [StructLayoutAttribute(LayoutKind.Sequential)] - internal struct INET_FIREWALL_AC_BINARIES - { - public uint count; - public IntPtr binaries; - } - - [StructLayoutAttribute(LayoutKind.Sequential)] - internal struct INET_FIREWALL_APP_CONTAINER - { - internal IntPtr appContainerSid; - internal IntPtr userSid; - [MarshalAs(UnmanagedType.LPWStr)] - public string appContainerName; - [MarshalAs(UnmanagedType.LPWStr)] - public string displayName; - [MarshalAs(UnmanagedType.LPWStr)] - public string description; - internal INET_FIREWALL_AC_CAPABILITIES capabilities; - internal INET_FIREWALL_AC_BINARIES binaries; - [MarshalAs(UnmanagedType.LPWStr)] - public string workingDirectory; - [MarshalAs(UnmanagedType.LPWStr)] - public string packageFullName; - } - - - // Call this API to free the memory returned by the Enumeration API - [DllImport("FirewallAPI.dll")] - internal static extern void NetworkIsolationFreeAppContainers(IntPtr pACs); - - // Call this API to load the current list of LoopUtil-enabled AppContainers - [DllImport("FirewallAPI.dll")] - internal static extern uint NetworkIsolationGetAppContainerConfig(out uint pdwCntACs, out IntPtr appContainerSids); - - // Call this API to set the LoopUtil-exemption list - [DllImport("FirewallAPI.dll")] - private static extern uint NetworkIsolationSetAppContainerConfig(uint pdwCntACs, SID_AND_ATTRIBUTES[] appContainerSids); - - - // Use this API to convert a string SID into an actual SID - [DllImport("advapi32.dll", SetLastError = true)] - internal static extern bool ConvertStringSidToSid(string strSid, out IntPtr pSid); - - [DllImport("advapi32", CharSet = CharSet.Auto, SetLastError = true)] - static extern bool ConvertSidToStringSid( - [MarshalAs(UnmanagedType.LPArray)] byte[] pSID, - out IntPtr ptrSid); - - [DllImport("advapi32", CharSet = CharSet.Auto, SetLastError = true)] - static extern bool ConvertSidToStringSid(IntPtr pSid, out string strSid); - - // Use this API to convert a string reference (e.g. "@{blah.pri?ms-resource://whatever}") into a plain string - [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf); - - // Call this API to enumerate all of the AppContainers on the system - [DllImport("FirewallAPI.dll")] - internal static extern uint NetworkIsolationEnumAppContainers(uint Flags, out uint pdwCntPublicACs, out IntPtr ppACs); - // DWORD NetworkIsolationEnumAppContainers( - // _In_ DWORD Flags, - // _Out_ DWORD *pdwNumPublicAppCs, - // _Out_ PINET_FIREWALL_APP_CONTAINER *ppPublicAppCs - //); - - //http://msdn.microsoft.com/en-gb/library/windows/desktop/hh968116.aspx - enum NETISO_FLAG - { - NETISO_FLAG_FORCE_COMPUTE_BINARIES = 0x1, - NETISO_FLAG_MAX = 0x2 - } - - - public class AppContainer - { - public String appContainerName { get; set; } - public String displayName { get; set; } - public String workingDirectory { get; set; } - public String StringSid { get; set; } - public List capabilities { get; set; } - public bool LoopUtil { get; set; } - - public AppContainer(String _appContainerName, String _displayName, String _workingDirectory, IntPtr _sid) - { - this.appContainerName = _appContainerName; - this.displayName = _displayName; - this.workingDirectory = _workingDirectory; - String tempSid; - ConvertSidToStringSid(_sid, out tempSid); - this.StringSid = tempSid; - } - } - - internal List _AppList; - internal List _AppListConfig; - public List Apps = new List(); - internal IntPtr _pACs; - - public LoopUtil() - { - LoadApps(); - } - - public void LoadApps() - { - Apps.Clear(); - _pACs = IntPtr.Zero; - //Full List of Apps - _AppList = PI_NetworkIsolationEnumAppContainers(); - //List of Apps that have LoopUtil enabled. - _AppListConfig = PI_NetworkIsolationGetAppContainerConfig(); - foreach (var PI_app in _AppList) - { - AppContainer app = new AppContainer(PI_app.appContainerName, PI_app.displayName, PI_app.workingDirectory, PI_app.appContainerSid); - - var app_capabilities = LoopUtil.getCapabilites(PI_app.capabilities); - if (app_capabilities.Count > 0) - { - //var sid = new SecurityIdentifier(app_capabilities[0], 0); - - IntPtr arrayValue = IntPtr.Zero; - //var b = LoopUtil.ConvertStringSidToSid(app_capabilities[0].Sid, out arrayValue); - //string mysid; - //var b = LoopUtil.ConvertSidToStringSid(app_capabilities[0].Sid, out mysid); - } - app.LoopUtil = CheckLoopback(PI_app.appContainerSid); - Apps.Add(app); - } - } - private bool CheckLoopback(IntPtr intPtr) - { - foreach (SID_AND_ATTRIBUTES item in _AppListConfig) - { - string left, right; - ConvertSidToStringSid(item.Sid, out left); - ConvertSidToStringSid(intPtr, out right); - - if (left == right) - { - return true; - } - } - return false; - } - - private bool CreateExcemptions(string appName) - { - var hasChanges = false; - - foreach (var app in Apps) - { - if ((app.appContainerName ?? string.Empty).IndexOf(appName, StringComparison.OrdinalIgnoreCase) != -1 || - (app.displayName ?? string.Empty).IndexOf(appName, StringComparison.OrdinalIgnoreCase) != -1) - { - if (!app.LoopUtil) - { - app.LoopUtil = true; - hasChanges = true; - } - } - } - - return hasChanges; - } - - public static void Run(string appName) - { - var util = new LoopUtil(); - util.LoadApps(); - - var hasChanges = util.CreateExcemptions(appName); - - if (hasChanges) - { - util.SaveLoopbackState(); - } - util.SaveLoopbackState(); - } - - private static List getCapabilites(INET_FIREWALL_AC_CAPABILITIES cap) - { - List mycap = new List(); - - IntPtr arrayValue = cap.capabilities; - - var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); - for (var i = 0; i < cap.count; i++) - { - var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); - mycap.Add(cur); - arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); - } - - return mycap; - - } - - private static List getContainerSID(INET_FIREWALL_AC_CAPABILITIES cap) - { - List mycap = new List(); - - IntPtr arrayValue = cap.capabilities; - - var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); - for (var i = 0; i < cap.count; i++) - { - var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); - mycap.Add(cur); - arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); - } - - return mycap; - - } - - private static List PI_NetworkIsolationGetAppContainerConfig() - { - - IntPtr arrayValue = IntPtr.Zero; - uint size = 0; - var list = new List(); - - // Pin down variables - GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned); - GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned); - - uint retval = NetworkIsolationGetAppContainerConfig(out size, out arrayValue); - - var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); - for (var i = 0; i < size; i++) - { - var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); - list.Add(cur); - arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); - } - - //release pinned variables. - handle_pdwCntPublicACs.Free(); - handle_ppACs.Free(); - - return list; - - - } - - private List PI_NetworkIsolationEnumAppContainers() - { - - IntPtr arrayValue = IntPtr.Zero; - uint size = 0; - var list = new List(); - - // Pin down variables - GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned); - GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned); - - //uint retval2 = NetworkIsolationGetAppContainerConfig( out size, out arrayValue); - - uint retval = NetworkIsolationEnumAppContainers((Int32)NETISO_FLAG.NETISO_FLAG_MAX, out size, out arrayValue); - _pACs = arrayValue; //store the pointer so it can be freed when we close the form - - var structSize = Marshal.SizeOf(typeof(INET_FIREWALL_APP_CONTAINER)); - for (var i = 0; i < size; i++) - { - var cur = (INET_FIREWALL_APP_CONTAINER)Marshal.PtrToStructure(arrayValue, typeof(INET_FIREWALL_APP_CONTAINER)); - list.Add(cur); - arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); - } - - //release pinned variables. - handle_pdwCntPublicACs.Free(); - handle_ppACs.Free(); - - return list; - - - } - - public bool SaveLoopbackState() - { - var countEnabled = CountEnabledLoopUtil(); - SID_AND_ATTRIBUTES[] arr = new SID_AND_ATTRIBUTES[countEnabled]; - int count = 0; - - for (int i = 0; i < Apps.Count; i++) - { - if (Apps[i].LoopUtil) - { - arr[count].Attributes = 0; - //TO DO: - IntPtr ptr; - ConvertStringSidToSid(Apps[i].StringSid, out ptr); - arr[count].Sid = ptr; - count++; - } - - } - - - if (NetworkIsolationSetAppContainerConfig((uint)countEnabled, arr) == 0) - { - return true; - } - else - { return false; } - - } - - private int CountEnabledLoopUtil() - { - var count = 0; - for (int i = 0; i < Apps.Count; i++) - { - if (Apps[i].LoopUtil) - { - count++; - } - - } - return count; - } - - public void FreeResources() - { - NetworkIsolationFreeAppContainers(_pACs); - } - - } -} -- cgit v1.2.3 From 38badd4d28e6538e1c33b0d714b50cb24f0f6897 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 10 Mar 2017 14:51:29 -0500 Subject: rework file system libs --- .../IO/LnkShortcutHandler.cs | 333 ++++++++++++++++++++ .../IO/ManagedFileSystem.cs | 11 +- Emby.Server.Core/ApplicationHost.cs | 11 +- MediaBrowser.Model/IO/IFileSystem.cs | 2 + MediaBrowser.Server.Mac/Main.cs | 7 +- MediaBrowser.Server.Mono/Native/MonoFileSystem.cs | 5 +- MediaBrowser.Server.Mono/Program.cs | 7 +- MediaBrowser.ServerApplication/MainStartup.cs | 8 +- .../MediaBrowser.ServerApplication.csproj | 1 - .../Native/LnkShortcutHandler.cs | 334 --------------------- MediaBrowser.ServerApplication/WindowsAppHost.cs | 1 + 11 files changed, 364 insertions(+), 356 deletions(-) create mode 100644 Emby.Common.Implementations/IO/LnkShortcutHandler.cs delete mode 100644 MediaBrowser.ServerApplication/Native/LnkShortcutHandler.cs (limited to 'MediaBrowser.ServerApplication/Native') diff --git a/Emby.Common.Implementations/IO/LnkShortcutHandler.cs b/Emby.Common.Implementations/IO/LnkShortcutHandler.cs new file mode 100644 index 000000000..5d5f46057 --- /dev/null +++ b/Emby.Common.Implementations/IO/LnkShortcutHandler.cs @@ -0,0 +1,333 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.ComTypes; +using System.Security; +using System.Text; +using MediaBrowser.Model.IO; + +namespace Emby.Common.Implementations.IO +{ + public class LnkShortcutHandler :IShortcutHandler + { + public string Extension + { + get { return ".lnk"; } + } + + public string Resolve(string shortcutPath) + { + var link = new ShellLink(); + ((IPersistFile)link).Load(shortcutPath, NativeMethods.STGM_READ); + // ((IShellLinkW)link).Resolve(hwnd, 0) + var sb = new StringBuilder(NativeMethods.MAX_PATH); + WIN32_FIND_DATA data; + ((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0); + return sb.ToString(); + } + + public void Create(string shortcutPath, string targetPath) + { + throw new NotImplementedException(); + } + } + + /// + /// Class NativeMethods + /// + public static class NativeMethods + { + /// + /// The MA x_ PATH + /// + public const int MAX_PATH = 260; + /// + /// The MA x_ ALTERNATE + /// + public const int MAX_ALTERNATE = 14; + /// + /// The INVALI d_ HANDL e_ VALUE + /// + public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); + /// + /// The STG m_ READ + /// + public const int STGM_READ = 0; + } + + /// + /// Struct FILETIME + /// + [StructLayout(LayoutKind.Sequential)] + public struct FILETIME + { + /// + /// The dw low date time + /// + public uint dwLowDateTime; + /// + /// The dw high date time + /// + public uint dwHighDateTime; + } + + /// + /// Struct WIN32_FIND_DATA + /// + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public struct WIN32_FIND_DATA + { + /// + /// The dw file attributes + /// + public FileAttributes dwFileAttributes; + /// + /// The ft creation time + /// + public FILETIME ftCreationTime; + /// + /// The ft last access time + /// + public FILETIME ftLastAccessTime; + /// + /// The ft last write time + /// + public FILETIME ftLastWriteTime; + /// + /// The n file size high + /// + public int nFileSizeHigh; + /// + /// The n file size low + /// + public int nFileSizeLow; + /// + /// The dw reserved0 + /// + public int dwReserved0; + /// + /// The dw reserved1 + /// + public int dwReserved1; + + /// + /// The c file name + /// + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_PATH)] + public string cFileName; + + /// + /// This will always be null when FINDEX_INFO_LEVELS = basic + /// + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_ALTERNATE)] + public string cAlternate; + + /// + /// Gets or sets the path. + /// + /// The path. + public string Path { get; set; } + + /// + /// Returns a that represents this instance. + /// + /// A that represents this instance. + public override string ToString() + { + return Path ?? string.Empty; + } + } + + /// + /// Enum SLGP_FLAGS + /// + [Flags] + public enum SLGP_FLAGS + { + /// + /// Retrieves the standard short (8.3 format) file name + /// + SLGP_SHORTPATH = 0x1, + /// + /// Retrieves the Universal Naming Convention (UNC) path name of the file + /// + SLGP_UNCPRIORITY = 0x2, + /// + /// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded + /// + SLGP_RAWPATH = 0x4 + } + /// + /// Enum SLR_FLAGS + /// + [Flags] + public enum SLR_FLAGS + { + /// + /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set, + /// the high-order word of fFlags can be set to a time-out value that specifies the + /// maximum amount of time to be spent resolving the link. The function returns if the + /// link cannot be resolved within the time-out duration. If the high-order word is set + /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds + /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out + /// duration, in milliseconds. + /// + SLR_NO_UI = 0x1, + /// + /// Obsolete and no longer used + /// + SLR_ANY_MATCH = 0x2, + /// + /// If the link object has changed, update its path and list of identifiers. + /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine + /// whether or not the link object has changed. + /// + SLR_UPDATE = 0x4, + /// + /// Do not update the link information + /// + SLR_NOUPDATE = 0x8, + /// + /// Do not execute the search heuristics + /// + SLR_NOSEARCH = 0x10, + /// + /// Do not use distributed link tracking + /// + SLR_NOTRACK = 0x20, + /// + /// Disable distributed link tracking. By default, distributed link tracking tracks + /// removable media across multiple devices based on the volume name. It also uses the + /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter + /// has changed. Setting SLR_NOLINKINFO disables both types of tracking. + /// + SLR_NOLINKINFO = 0x40, + /// + /// Call the Microsoft Windows Installer + /// + SLR_INVOKE_MSI = 0x80 + } + + /// + /// The IShellLink interface allows Shell links to be created, modified, and resolved + /// + [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")] + public interface IShellLinkW + { + /// + /// Retrieves the path and file name of a Shell link object + /// + /// The PSZ file. + /// The CCH max path. + /// The PFD. + /// The f flags. + void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags); + /// + /// Retrieves the list of item identifiers for a Shell link object + /// + /// The ppidl. + void GetIDList(out IntPtr ppidl); + /// + /// Sets the pointer to an item identifier list (PIDL) for a Shell link object. + /// + /// The pidl. + void SetIDList(IntPtr pidl); + /// + /// Retrieves the description string for a Shell link object + /// + /// Name of the PSZ. + /// Name of the CCH max. + void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); + /// + /// Sets the description for a Shell link object. The description can be any application-defined string + /// + /// Name of the PSZ. + void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); + /// + /// Retrieves the name of the working directory for a Shell link object + /// + /// The PSZ dir. + /// The CCH max path. + void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); + /// + /// Sets the name of the working directory for a Shell link object + /// + /// The PSZ dir. + void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); + /// + /// Retrieves the command-line arguments associated with a Shell link object + /// + /// The PSZ args. + /// The CCH max path. + void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); + /// + /// Sets the command-line arguments for a Shell link object + /// + /// The PSZ args. + void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); + /// + /// Retrieves the hot key for a Shell link object + /// + /// The pw hotkey. + void GetHotkey(out short pwHotkey); + /// + /// Sets a hot key for a Shell link object + /// + /// The w hotkey. + void SetHotkey(short wHotkey); + /// + /// Retrieves the show command for a Shell link object + /// + /// The pi show CMD. + void GetShowCmd(out int piShowCmd); + /// + /// Sets the show command for a Shell link object. The show command sets the initial show state of the window. + /// + /// The i show CMD. + void SetShowCmd(int iShowCmd); + /// + /// Retrieves the location (path and index) of the icon for a Shell link object + /// + /// The PSZ icon path. + /// The CCH icon path. + /// The pi icon. + void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, + int cchIconPath, out int piIcon); + /// + /// Sets the location (path and index) of the icon for a Shell link object + /// + /// The PSZ icon path. + /// The i icon. + void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); + /// + /// Sets the relative path to the Shell link object + /// + /// The PSZ path rel. + /// The dw reserved. + void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); + /// + /// Attempts to find the target of a Shell link, even if it has been moved or renamed + /// + /// The HWND. + /// The f flags. + void Resolve(IntPtr hwnd, SLR_FLAGS fFlags); + /// + /// Sets the path and file name of a Shell link object + /// + /// The PSZ file. + void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); + + } + + // CLSID_ShellLink from ShlGuid.h + /// + /// Class ShellLink + /// + [ + ComImport, + Guid("00021401-0000-0000-C000-000000000046") + ] + public class ShellLink + { + } +} diff --git a/Emby.Common.Implementations/IO/ManagedFileSystem.cs b/Emby.Common.Implementations/IO/ManagedFileSystem.cs index 3fe20f659..2c9388a41 100644 --- a/Emby.Common.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Common.Implementations/IO/ManagedFileSystem.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.System; namespace Emby.Common.Implementations.IO { @@ -18,17 +19,17 @@ namespace Emby.Common.Implementations.IO private readonly bool _supportsAsyncFileStreams; private char[] _invalidFileNameChars; private readonly List _shortcutHandlers = new List(); - private bool EnableFileSystemRequestConcat = true; + private bool EnableFileSystemRequestConcat; private string _tempPath; - public ManagedFileSystem(ILogger logger, bool supportsAsyncFileStreams, bool enableManagedInvalidFileNameChars, bool enableFileSystemRequestConcat, string tempPath) + public ManagedFileSystem(ILogger logger, IEnvironmentInfo environmentInfo, string tempPath) { Logger = logger; - _supportsAsyncFileStreams = supportsAsyncFileStreams; + _supportsAsyncFileStreams = true; _tempPath = tempPath; - EnableFileSystemRequestConcat = enableFileSystemRequestConcat; - SetInvalidFileNameChars(enableManagedInvalidFileNameChars); + EnableFileSystemRequestConcat = false; + SetInvalidFileNameChars(environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows); } public void AddShortcutHandler(IShortcutHandler handler) diff --git a/Emby.Server.Core/ApplicationHost.cs b/Emby.Server.Core/ApplicationHost.cs index 7dbc7760b..6133a3343 100644 --- a/Emby.Server.Core/ApplicationHost.cs +++ b/Emby.Server.Core/ApplicationHost.cs @@ -61,7 +61,7 @@ using System.Threading; using System.Threading.Tasks; using Emby.Common.Implementations; using Emby.Common.Implementations.Archiving; -using Emby.Common.Implementations.Networking; +using Emby.Common.Implementations.IO; using Emby.Common.Implementations.Reflection; using Emby.Common.Implementations.Serialization; using Emby.Common.Implementations.TextEncoding; @@ -93,7 +93,7 @@ using Emby.Server.Implementations.Social; using Emby.Server.Implementations.Channels; using Emby.Server.Implementations.Collections; using Emby.Server.Implementations.Dto; -using Emby.Server.Implementations.EntryPoints; +using Emby.Server.Implementations.IO; using Emby.Server.Implementations.FileOrganization; using Emby.Server.Implementations.HttpServer; using Emby.Server.Implementations.HttpServer.Security; @@ -294,6 +294,13 @@ namespace Emby.Server.Core ImageEncoder = imageEncoder; SetBaseExceptionMessage(); + + if (environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows) + { + fileSystem.AddShortcutHandler(new LnkShortcutHandler()); + } + + fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem)); } private Version _version; diff --git a/MediaBrowser.Model/IO/IFileSystem.cs b/MediaBrowser.Model/IO/IFileSystem.cs index f6d1bb351..f90119cf3 100644 --- a/MediaBrowser.Model/IO/IFileSystem.cs +++ b/MediaBrowser.Model/IO/IFileSystem.cs @@ -10,6 +10,8 @@ namespace MediaBrowser.Model.IO /// public interface IFileSystem { + void AddShortcutHandler(IShortcutHandler handler); + /// /// Determines whether the specified filename is shortcut. /// diff --git a/MediaBrowser.Server.Mac/Main.cs b/MediaBrowser.Server.Mac/Main.cs index debd5f539..d703f7d0d 100644 --- a/MediaBrowser.Server.Mac/Main.cs +++ b/MediaBrowser.Server.Mac/Main.cs @@ -105,12 +105,11 @@ namespace MediaBrowser.Server.Mac // Allow all https requests ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); - var fileSystem = new MonoFileSystem(logManager.GetLogger("FileSystem"), false, false, appPaths.TempDirectory); - fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem)); + var environmentInfo = GetEnvironmentInfo(); - _fileSystem = fileSystem; + var fileSystem = new MonoFileSystem(logManager.GetLogger("FileSystem"), environmentInfo, appPaths.TempDirectory); - var environmentInfo = GetEnvironmentInfo(); + _fileSystem = fileSystem; var imageEncoder = ImageEncoderHelper.GetImageEncoder(_logger, logManager, diff --git a/MediaBrowser.Server.Mono/Native/MonoFileSystem.cs b/MediaBrowser.Server.Mono/Native/MonoFileSystem.cs index a5dc691a7..91c064efe 100644 --- a/MediaBrowser.Server.Mono/Native/MonoFileSystem.cs +++ b/MediaBrowser.Server.Mono/Native/MonoFileSystem.cs @@ -1,13 +1,14 @@ using Emby.Common.Implementations.IO; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.System; using Mono.Unix.Native; namespace MediaBrowser.Server.Mono.Native { public class MonoFileSystem : ManagedFileSystem { - public MonoFileSystem(ILogger logger, bool supportsAsyncFileStreams, bool enableManagedInvalidFileNameChars, string tempPath) - : base(logger, supportsAsyncFileStreams, enableManagedInvalidFileNameChars, true, tempPath) + public MonoFileSystem(ILogger logger, IEnvironmentInfo environment, string tempPath) + : base(logger, environment, tempPath) { } diff --git a/MediaBrowser.Server.Mono/Program.cs b/MediaBrowser.Server.Mono/Program.cs index 649283410..66851f7e9 100644 --- a/MediaBrowser.Server.Mono/Program.cs +++ b/MediaBrowser.Server.Mono/Program.cs @@ -114,12 +114,11 @@ namespace MediaBrowser.Server.Mono // Allow all https requests ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); - var fileSystem = new MonoFileSystem(logManager.GetLogger("FileSystem"), false, false, appPaths.TempDirectory); - fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem)); + var environmentInfo = GetEnvironmentInfo(); - FileSystem = fileSystem; + var fileSystem = new MonoFileSystem(logManager.GetLogger("FileSystem"), environmentInfo, appPaths.TempDirectory); - var environmentInfo = GetEnvironmentInfo(); + FileSystem = fileSystem; var imageEncoder = ImageEncoderHelper.GetImageEncoder(_logger, logManager, fileSystem, options, () => _appHost.HttpClient, appPaths); diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index b41e7607c..b02c5d6ac 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -331,9 +331,9 @@ namespace MediaBrowser.ServerApplication /// The options. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, bool runService, StartupOptions options) { - var fileSystem = new ManagedFileSystem(logManager.GetLogger("FileSystem"), true, true, false, appPaths.TempDirectory); - fileSystem.AddShortcutHandler(new LnkShortcutHandler()); - fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem)); + var environmentInfo = new EnvironmentInfo(); + + var fileSystem = new ManagedFileSystem(logManager.GetLogger("FileSystem"), environmentInfo, appPaths.TempDirectory); var imageEncoder = ImageEncoderHelper.GetImageEncoder(_logger, logManager, fileSystem, options, () => _appHost.HttpClient, appPaths); @@ -345,7 +345,7 @@ namespace MediaBrowser.ServerApplication fileSystem, new PowerManagement(), "emby.windows.zip", - new EnvironmentInfo(), + environmentInfo, imageEncoder, new Server.Startup.Common.SystemEvents(logManager.GetLogger("SystemEvents")), new RecyclableMemoryStreamProvider(), diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index 02916f555..63e10df76 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -142,7 +142,6 @@ MainForm.cs - diff --git a/MediaBrowser.ServerApplication/Native/LnkShortcutHandler.cs b/MediaBrowser.ServerApplication/Native/LnkShortcutHandler.cs deleted file mode 100644 index b4a87b9b4..000000000 --- a/MediaBrowser.ServerApplication/Native/LnkShortcutHandler.cs +++ /dev/null @@ -1,334 +0,0 @@ -using System; -using System.IO; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.ComTypes; -using System.Security; -using System.Text; -using MediaBrowser.Model.IO; - -namespace MediaBrowser.ServerApplication.Native -{ - public class LnkShortcutHandler :IShortcutHandler - { - public string Extension - { - get { return ".lnk"; } - } - - public string Resolve(string shortcutPath) - { - var link = new ShellLink(); - ((IPersistFile)link).Load(shortcutPath, NativeMethods.STGM_READ); - // ((IShellLinkW)link).Resolve(hwnd, 0) - var sb = new StringBuilder(NativeMethods.MAX_PATH); - WIN32_FIND_DATA data; - ((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0); - return sb.ToString(); - } - - public void Create(string shortcutPath, string targetPath) - { - throw new NotImplementedException(); - } - } - - /// - /// Class NativeMethods - /// - [SuppressUnmanagedCodeSecurity] - public static class NativeMethods - { - /// - /// The MA x_ PATH - /// - public const int MAX_PATH = 260; - /// - /// The MA x_ ALTERNATE - /// - public const int MAX_ALTERNATE = 14; - /// - /// The INVALI d_ HANDL e_ VALUE - /// - public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); - /// - /// The STG m_ READ - /// - public const int STGM_READ = 0; - } - - /// - /// Struct FILETIME - /// - [StructLayout(LayoutKind.Sequential)] - public struct FILETIME - { - /// - /// The dw low date time - /// - public uint dwLowDateTime; - /// - /// The dw high date time - /// - public uint dwHighDateTime; - } - - /// - /// Struct WIN32_FIND_DATA - /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - public struct WIN32_FIND_DATA - { - /// - /// The dw file attributes - /// - public FileAttributes dwFileAttributes; - /// - /// The ft creation time - /// - public FILETIME ftCreationTime; - /// - /// The ft last access time - /// - public FILETIME ftLastAccessTime; - /// - /// The ft last write time - /// - public FILETIME ftLastWriteTime; - /// - /// The n file size high - /// - public int nFileSizeHigh; - /// - /// The n file size low - /// - public int nFileSizeLow; - /// - /// The dw reserved0 - /// - public int dwReserved0; - /// - /// The dw reserved1 - /// - public int dwReserved1; - - /// - /// The c file name - /// - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_PATH)] - public string cFileName; - - /// - /// This will always be null when FINDEX_INFO_LEVELS = basic - /// - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_ALTERNATE)] - public string cAlternate; - - /// - /// Gets or sets the path. - /// - /// The path. - public string Path { get; set; } - - /// - /// Returns a that represents this instance. - /// - /// A that represents this instance. - public override string ToString() - { - return Path ?? string.Empty; - } - } - - /// - /// Enum SLGP_FLAGS - /// - [Flags] - public enum SLGP_FLAGS - { - /// - /// Retrieves the standard short (8.3 format) file name - /// - SLGP_SHORTPATH = 0x1, - /// - /// Retrieves the Universal Naming Convention (UNC) path name of the file - /// - SLGP_UNCPRIORITY = 0x2, - /// - /// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded - /// - SLGP_RAWPATH = 0x4 - } - /// - /// Enum SLR_FLAGS - /// - [Flags] - public enum SLR_FLAGS - { - /// - /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set, - /// the high-order word of fFlags can be set to a time-out value that specifies the - /// maximum amount of time to be spent resolving the link. The function returns if the - /// link cannot be resolved within the time-out duration. If the high-order word is set - /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds - /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out - /// duration, in milliseconds. - /// - SLR_NO_UI = 0x1, - /// - /// Obsolete and no longer used - /// - SLR_ANY_MATCH = 0x2, - /// - /// If the link object has changed, update its path and list of identifiers. - /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine - /// whether or not the link object has changed. - /// - SLR_UPDATE = 0x4, - /// - /// Do not update the link information - /// - SLR_NOUPDATE = 0x8, - /// - /// Do not execute the search heuristics - /// - SLR_NOSEARCH = 0x10, - /// - /// Do not use distributed link tracking - /// - SLR_NOTRACK = 0x20, - /// - /// Disable distributed link tracking. By default, distributed link tracking tracks - /// removable media across multiple devices based on the volume name. It also uses the - /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter - /// has changed. Setting SLR_NOLINKINFO disables both types of tracking. - /// - SLR_NOLINKINFO = 0x40, - /// - /// Call the Microsoft Windows Installer - /// - SLR_INVOKE_MSI = 0x80 - } - - /// - /// The IShellLink interface allows Shell links to be created, modified, and resolved - /// - [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")] - public interface IShellLinkW - { - /// - /// Retrieves the path and file name of a Shell link object - /// - /// The PSZ file. - /// The CCH max path. - /// The PFD. - /// The f flags. - void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags); - /// - /// Retrieves the list of item identifiers for a Shell link object - /// - /// The ppidl. - void GetIDList(out IntPtr ppidl); - /// - /// Sets the pointer to an item identifier list (PIDL) for a Shell link object. - /// - /// The pidl. - void SetIDList(IntPtr pidl); - /// - /// Retrieves the description string for a Shell link object - /// - /// Name of the PSZ. - /// Name of the CCH max. - void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); - /// - /// Sets the description for a Shell link object. The description can be any application-defined string - /// - /// Name of the PSZ. - void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); - /// - /// Retrieves the name of the working directory for a Shell link object - /// - /// The PSZ dir. - /// The CCH max path. - void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); - /// - /// Sets the name of the working directory for a Shell link object - /// - /// The PSZ dir. - void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); - /// - /// Retrieves the command-line arguments associated with a Shell link object - /// - /// The PSZ args. - /// The CCH max path. - void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); - /// - /// Sets the command-line arguments for a Shell link object - /// - /// The PSZ args. - void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); - /// - /// Retrieves the hot key for a Shell link object - /// - /// The pw hotkey. - void GetHotkey(out short pwHotkey); - /// - /// Sets a hot key for a Shell link object - /// - /// The w hotkey. - void SetHotkey(short wHotkey); - /// - /// Retrieves the show command for a Shell link object - /// - /// The pi show CMD. - void GetShowCmd(out int piShowCmd); - /// - /// Sets the show command for a Shell link object. The show command sets the initial show state of the window. - /// - /// The i show CMD. - void SetShowCmd(int iShowCmd); - /// - /// Retrieves the location (path and index) of the icon for a Shell link object - /// - /// The PSZ icon path. - /// The CCH icon path. - /// The pi icon. - void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, - int cchIconPath, out int piIcon); - /// - /// Sets the location (path and index) of the icon for a Shell link object - /// - /// The PSZ icon path. - /// The i icon. - void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); - /// - /// Sets the relative path to the Shell link object - /// - /// The PSZ path rel. - /// The dw reserved. - void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); - /// - /// Attempts to find the target of a Shell link, even if it has been moved or renamed - /// - /// The HWND. - /// The f flags. - void Resolve(IntPtr hwnd, SLR_FLAGS fFlags); - /// - /// Sets the path and file name of a Shell link object - /// - /// The PSZ file. - void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); - - } - - // CLSID_ShellLink from ShlGuid.h - /// - /// Class ShellLink - /// - [ - ComImport, - Guid("00021401-0000-0000-C000-000000000046") - ] - public class ShellLink - { - } -} diff --git a/MediaBrowser.ServerApplication/WindowsAppHost.cs b/MediaBrowser.ServerApplication/WindowsAppHost.cs index 915a2fa86..2d3d8a85b 100644 --- a/MediaBrowser.ServerApplication/WindowsAppHost.cs +++ b/MediaBrowser.ServerApplication/WindowsAppHost.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.InteropServices.ComTypes; +using Emby.Common.Implementations.IO; using Emby.Server.CinemaMode; using Emby.Server.Connect; using Emby.Server.Core; -- cgit v1.2.3 From 1823b4a6746670e310dac1ad431b741010f3616d Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 22 Mar 2017 14:37:04 -0400 Subject: 3.2.8.9 --- Emby.Server.Core/ApplicationHost.cs | 7 +- .../Emby.Server.Implementations.csproj | 1 - Emby.Server.Implementations/Windows/LoopUtil.cs | 358 -------------------- .../MediaBrowser.ServerApplication.csproj | 1 + MediaBrowser.ServerApplication/Native/LoopUtil.cs | 361 +++++++++++++++++++++ MediaBrowser.ServerApplication/WindowsAppHost.cs | 6 +- SharedVersion.cs | 2 +- 7 files changed, 369 insertions(+), 367 deletions(-) delete mode 100644 Emby.Server.Implementations/Windows/LoopUtil.cs create mode 100644 MediaBrowser.ServerApplication/Native/LoopUtil.cs (limited to 'MediaBrowser.ServerApplication/Native') diff --git a/Emby.Server.Core/ApplicationHost.cs b/Emby.Server.Core/ApplicationHost.cs index 971378ea7..50c572b8c 100644 --- a/Emby.Server.Core/ApplicationHost.cs +++ b/Emby.Server.Core/ApplicationHost.cs @@ -107,7 +107,6 @@ using Emby.Server.Implementations.Playlists; using Emby.Server.Implementations; using Emby.Server.Implementations.ServerManager; using Emby.Server.Implementations.Session; -using Emby.Server.Implementations.Windows; using Emby.Server.Implementations.TV; using Emby.Server.Implementations.Updates; using MediaBrowser.Model.Activity; @@ -1743,12 +1742,8 @@ namespace Emby.Server.Core ((IProcess)sender).Dispose(); } - public void EnableLoopback(string appName) + public virtual void EnableLoopback(string appName) { - if (EnvironmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows) - { - LoopUtil.Run(appName); - } } private void RegisterModules() diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csproj index afd437fe8..670acd37f 100644 --- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj +++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj @@ -267,7 +267,6 @@ - diff --git a/Emby.Server.Implementations/Windows/LoopUtil.cs b/Emby.Server.Implementations/Windows/LoopUtil.cs deleted file mode 100644 index 6eded2cec..000000000 --- a/Emby.Server.Implementations/Windows/LoopUtil.cs +++ /dev/null @@ -1,358 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - -namespace Emby.Server.Implementations.Windows -{ - /// - /// http://blogs.msdn.com/b/fiddler/archive/2011/12/10/fiddler-windows-8-apps-enable-LoopUtil-network-isolation-exemption.aspx - /// - public class LoopUtil - { - //http://msdn.microsoft.com/en-us/library/windows/desktop/aa379595(v=vs.85).aspx - [StructLayout(LayoutKind.Sequential)] - internal struct SID_AND_ATTRIBUTES - { - public IntPtr Sid; - public uint Attributes; - } - - [StructLayoutAttribute(LayoutKind.Sequential)] - internal struct INET_FIREWALL_AC_CAPABILITIES - { - public uint count; - public IntPtr capabilities; //SID_AND_ATTRIBUTES - } - - [StructLayoutAttribute(LayoutKind.Sequential)] - internal struct INET_FIREWALL_AC_BINARIES - { - public uint count; - public IntPtr binaries; - } - - [StructLayoutAttribute(LayoutKind.Sequential)] - internal struct INET_FIREWALL_APP_CONTAINER - { - internal IntPtr appContainerSid; - internal IntPtr userSid; - [MarshalAs(UnmanagedType.LPWStr)] - public string appContainerName; - [MarshalAs(UnmanagedType.LPWStr)] - public string displayName; - [MarshalAs(UnmanagedType.LPWStr)] - public string description; - internal INET_FIREWALL_AC_CAPABILITIES capabilities; - internal INET_FIREWALL_AC_BINARIES binaries; - [MarshalAs(UnmanagedType.LPWStr)] - public string workingDirectory; - [MarshalAs(UnmanagedType.LPWStr)] - public string packageFullName; - } - - - // Call this API to free the memory returned by the Enumeration API - [DllImport("FirewallAPI.dll")] - internal static extern void NetworkIsolationFreeAppContainers(IntPtr pACs); - - // Call this API to load the current list of LoopUtil-enabled AppContainers - [DllImport("FirewallAPI.dll")] - internal static extern uint NetworkIsolationGetAppContainerConfig(out uint pdwCntACs, out IntPtr appContainerSids); - - // Call this API to set the LoopUtil-exemption list - [DllImport("FirewallAPI.dll")] - private static extern uint NetworkIsolationSetAppContainerConfig(uint pdwCntACs, SID_AND_ATTRIBUTES[] appContainerSids); - - - // Use this API to convert a string SID into an actual SID - [DllImport("advapi32.dll", SetLastError = true)] - internal static extern bool ConvertStringSidToSid(string strSid, out IntPtr pSid); - - [DllImport("advapi32", /*CharSet = CharSet.Auto,*/ SetLastError = true)] - static extern bool ConvertSidToStringSid( - [MarshalAs(UnmanagedType.LPArray)] byte[] pSID, - out IntPtr ptrSid); - - [DllImport("advapi32", /*CharSet = CharSet.Auto,*/ SetLastError = true)] - static extern bool ConvertSidToStringSid(IntPtr pSid, out string strSid); - - // Use this API to convert a string reference (e.g. "@{blah.pri?ms-resource://whatever}") into a plain string - [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf); - - // Call this API to enumerate all of the AppContainers on the system - [DllImport("FirewallAPI.dll")] - internal static extern uint NetworkIsolationEnumAppContainers(uint Flags, out uint pdwCntPublicACs, out IntPtr ppACs); - // DWORD NetworkIsolationEnumAppContainers( - // _In_ DWORD Flags, - // _Out_ DWORD *pdwNumPublicAppCs, - // _Out_ PINET_FIREWALL_APP_CONTAINER *ppPublicAppCs - //); - - //http://msdn.microsoft.com/en-gb/library/windows/desktop/hh968116.aspx - enum NETISO_FLAG - { - NETISO_FLAG_FORCE_COMPUTE_BINARIES = 0x1, - NETISO_FLAG_MAX = 0x2 - } - - - public class AppContainer - { - public String appContainerName { get; set; } - public String displayName { get; set; } - public String workingDirectory { get; set; } - public String StringSid { get; set; } - public List capabilities { get; set; } - public bool LoopUtil { get; set; } - - public AppContainer(String _appContainerName, String _displayName, String _workingDirectory, IntPtr _sid) - { - this.appContainerName = _appContainerName; - this.displayName = _displayName; - this.workingDirectory = _workingDirectory; - String tempSid; - ConvertSidToStringSid(_sid, out tempSid); - this.StringSid = tempSid; - } - } - - internal List _AppList; - internal List _AppListConfig; - public List Apps = new List(); - internal IntPtr _pACs; - - public LoopUtil() - { - LoadApps(); - } - - public void LoadApps() - { - Apps.Clear(); - _pACs = IntPtr.Zero; - //Full List of Apps - _AppList = PI_NetworkIsolationEnumAppContainers(); - //List of Apps that have LoopUtil enabled. - _AppListConfig = PI_NetworkIsolationGetAppContainerConfig(); - foreach (var PI_app in _AppList) - { - AppContainer app = new AppContainer(PI_app.appContainerName, PI_app.displayName, PI_app.workingDirectory, PI_app.appContainerSid); - - var app_capabilities = LoopUtil.getCapabilites(PI_app.capabilities); - if (app_capabilities.Count > 0) - { - //var sid = new SecurityIdentifier(app_capabilities[0], 0); - - IntPtr arrayValue = IntPtr.Zero; - //var b = LoopUtil.ConvertStringSidToSid(app_capabilities[0].Sid, out arrayValue); - //string mysid; - //var b = LoopUtil.ConvertSidToStringSid(app_capabilities[0].Sid, out mysid); - } - app.LoopUtil = CheckLoopback(PI_app.appContainerSid); - Apps.Add(app); - } - } - private bool CheckLoopback(IntPtr intPtr) - { - foreach (SID_AND_ATTRIBUTES item in _AppListConfig) - { - string left, right; - ConvertSidToStringSid(item.Sid, out left); - ConvertSidToStringSid(intPtr, out right); - - if (left == right) - { - return true; - } - } - return false; - } - - private bool CreateExcemptions(string appName) - { - var hasChanges = false; - - foreach (var app in Apps) - { - if ((app.appContainerName ?? string.Empty).IndexOf(appName, StringComparison.OrdinalIgnoreCase) != -1 || - (app.displayName ?? string.Empty).IndexOf(appName, StringComparison.OrdinalIgnoreCase) != -1) - { - if (!app.LoopUtil) - { - app.LoopUtil = true; - hasChanges = true; - } - } - } - - return hasChanges; - } - - public static void Run(string appName) - { - var util = new LoopUtil(); - util.LoadApps(); - - var hasChanges = util.CreateExcemptions(appName); - - if (hasChanges) - { - util.SaveLoopbackState(); - } - util.SaveLoopbackState(); - } - - private static List getCapabilites(INET_FIREWALL_AC_CAPABILITIES cap) - { - List mycap = new List(); - - IntPtr arrayValue = cap.capabilities; - - var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); - for (var i = 0; i < cap.count; i++) - { - var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); - mycap.Add(cur); - arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); - } - - return mycap; - - } - - private static List getContainerSID(INET_FIREWALL_AC_CAPABILITIES cap) - { - List mycap = new List(); - - IntPtr arrayValue = cap.capabilities; - - var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); - for (var i = 0; i < cap.count; i++) - { - var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); - mycap.Add(cur); - arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); - } - - return mycap; - - } - - private static List PI_NetworkIsolationGetAppContainerConfig() - { - - IntPtr arrayValue = IntPtr.Zero; - uint size = 0; - var list = new List(); - - // Pin down variables - GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned); - GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned); - - uint retval = NetworkIsolationGetAppContainerConfig(out size, out arrayValue); - - var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); - for (var i = 0; i < size; i++) - { - var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); - list.Add(cur); - arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); - } - - //release pinned variables. - handle_pdwCntPublicACs.Free(); - handle_ppACs.Free(); - - return list; - - - } - - private List PI_NetworkIsolationEnumAppContainers() - { - - IntPtr arrayValue = IntPtr.Zero; - uint size = 0; - var list = new List(); - - // Pin down variables - GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned); - GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned); - - //uint retval2 = NetworkIsolationGetAppContainerConfig( out size, out arrayValue); - - uint retval = NetworkIsolationEnumAppContainers((Int32)NETISO_FLAG.NETISO_FLAG_MAX, out size, out arrayValue); - _pACs = arrayValue; //store the pointer so it can be freed when we close the form - - var structSize = Marshal.SizeOf(typeof(INET_FIREWALL_APP_CONTAINER)); - for (var i = 0; i < size; i++) - { - var cur = (INET_FIREWALL_APP_CONTAINER)Marshal.PtrToStructure(arrayValue, typeof(INET_FIREWALL_APP_CONTAINER)); - list.Add(cur); - arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); - } - - //release pinned variables. - handle_pdwCntPublicACs.Free(); - handle_ppACs.Free(); - - return list; - - - } - - public bool SaveLoopbackState() - { - var countEnabled = CountEnabledLoopUtil(); - SID_AND_ATTRIBUTES[] arr = new SID_AND_ATTRIBUTES[countEnabled]; - int count = 0; - - for (int i = 0; i < Apps.Count; i++) - { - if (Apps[i].LoopUtil) - { - arr[count].Attributes = 0; - //TO DO: - IntPtr ptr; - ConvertStringSidToSid(Apps[i].StringSid, out ptr); - arr[count].Sid = ptr; - count++; - } - - } - - - if (NetworkIsolationSetAppContainerConfig((uint)countEnabled, arr) == 0) - { - return true; - } - else - { return false; } - - } - - private int CountEnabledLoopUtil() - { - var count = 0; - for (int i = 0; i < Apps.Count; i++) - { - if (Apps[i].LoopUtil) - { - count++; - } - - } - return count; - } - - public void FreeResources() - { - NetworkIsolationFreeAppContainers(_pACs); - } - - } -} diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index 63e10df76..b968c2fb6 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -142,6 +142,7 @@ MainForm.cs + diff --git a/MediaBrowser.ServerApplication/Native/LoopUtil.cs b/MediaBrowser.ServerApplication/Native/LoopUtil.cs new file mode 100644 index 000000000..6160f853f --- /dev/null +++ b/MediaBrowser.ServerApplication/Native/LoopUtil.cs @@ -0,0 +1,361 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +/* + * Important - Even though this will compile in the shared projects, it will cause build failures within the mono runtime + */ +namespace MediaBrowser.ServerApplication.Native +{ + /// + /// http://blogs.msdn.com/b/fiddler/archive/2011/12/10/fiddler-windows-8-apps-enable-LoopUtil-network-isolation-exemption.aspx + /// + public class LoopUtil + { + //http://msdn.microsoft.com/en-us/library/windows/desktop/aa379595(v=vs.85).aspx + [StructLayout(LayoutKind.Sequential)] + internal struct SID_AND_ATTRIBUTES + { + public IntPtr Sid; + public uint Attributes; + } + + [StructLayoutAttribute(LayoutKind.Sequential)] + internal struct INET_FIREWALL_AC_CAPABILITIES + { + public uint count; + public IntPtr capabilities; //SID_AND_ATTRIBUTES + } + + [StructLayoutAttribute(LayoutKind.Sequential)] + internal struct INET_FIREWALL_AC_BINARIES + { + public uint count; + public IntPtr binaries; + } + + [StructLayoutAttribute(LayoutKind.Sequential)] + internal struct INET_FIREWALL_APP_CONTAINER + { + internal IntPtr appContainerSid; + internal IntPtr userSid; + [MarshalAs(UnmanagedType.LPWStr)] + public string appContainerName; + [MarshalAs(UnmanagedType.LPWStr)] + public string displayName; + [MarshalAs(UnmanagedType.LPWStr)] + public string description; + internal INET_FIREWALL_AC_CAPABILITIES capabilities; + internal INET_FIREWALL_AC_BINARIES binaries; + [MarshalAs(UnmanagedType.LPWStr)] + public string workingDirectory; + [MarshalAs(UnmanagedType.LPWStr)] + public string packageFullName; + } + + + // Call this API to free the memory returned by the Enumeration API + [DllImport("FirewallAPI.dll")] + internal static extern void NetworkIsolationFreeAppContainers(IntPtr pACs); + + // Call this API to load the current list of LoopUtil-enabled AppContainers + [DllImport("FirewallAPI.dll")] + internal static extern uint NetworkIsolationGetAppContainerConfig(out uint pdwCntACs, out IntPtr appContainerSids); + + // Call this API to set the LoopUtil-exemption list + [DllImport("FirewallAPI.dll")] + private static extern uint NetworkIsolationSetAppContainerConfig(uint pdwCntACs, SID_AND_ATTRIBUTES[] appContainerSids); + + + // Use this API to convert a string SID into an actual SID + [DllImport("advapi32.dll", SetLastError = true)] + internal static extern bool ConvertStringSidToSid(string strSid, out IntPtr pSid); + + [DllImport("advapi32", /*CharSet = CharSet.Auto,*/ SetLastError = true)] + static extern bool ConvertSidToStringSid( + [MarshalAs(UnmanagedType.LPArray)] byte[] pSID, + out IntPtr ptrSid); + + [DllImport("advapi32", /*CharSet = CharSet.Auto,*/ SetLastError = true)] + static extern bool ConvertSidToStringSid(IntPtr pSid, out string strSid); + + // Use this API to convert a string reference (e.g. "@{blah.pri?ms-resource://whatever}") into a plain string + [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] + internal static extern int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf); + + // Call this API to enumerate all of the AppContainers on the system + [DllImport("FirewallAPI.dll")] + internal static extern uint NetworkIsolationEnumAppContainers(uint Flags, out uint pdwCntPublicACs, out IntPtr ppACs); + // DWORD NetworkIsolationEnumAppContainers( + // _In_ DWORD Flags, + // _Out_ DWORD *pdwNumPublicAppCs, + // _Out_ PINET_FIREWALL_APP_CONTAINER *ppPublicAppCs + //); + + //http://msdn.microsoft.com/en-gb/library/windows/desktop/hh968116.aspx + enum NETISO_FLAG + { + NETISO_FLAG_FORCE_COMPUTE_BINARIES = 0x1, + NETISO_FLAG_MAX = 0x2 + } + + + public class AppContainer + { + public String appContainerName { get; set; } + public String displayName { get; set; } + public String workingDirectory { get; set; } + public String StringSid { get; set; } + public List capabilities { get; set; } + public bool LoopUtil { get; set; } + + public AppContainer(String _appContainerName, String _displayName, String _workingDirectory, IntPtr _sid) + { + this.appContainerName = _appContainerName; + this.displayName = _displayName; + this.workingDirectory = _workingDirectory; + String tempSid; + ConvertSidToStringSid(_sid, out tempSid); + this.StringSid = tempSid; + } + } + + internal List _AppList; + internal List _AppListConfig; + public List Apps = new List(); + internal IntPtr _pACs; + + public LoopUtil() + { + LoadApps(); + } + + public void LoadApps() + { + Apps.Clear(); + _pACs = IntPtr.Zero; + //Full List of Apps + _AppList = PI_NetworkIsolationEnumAppContainers(); + //List of Apps that have LoopUtil enabled. + _AppListConfig = PI_NetworkIsolationGetAppContainerConfig(); + foreach (var PI_app in _AppList) + { + AppContainer app = new AppContainer(PI_app.appContainerName, PI_app.displayName, PI_app.workingDirectory, PI_app.appContainerSid); + + var app_capabilities = LoopUtil.getCapabilites(PI_app.capabilities); + if (app_capabilities.Count > 0) + { + //var sid = new SecurityIdentifier(app_capabilities[0], 0); + + IntPtr arrayValue = IntPtr.Zero; + //var b = LoopUtil.ConvertStringSidToSid(app_capabilities[0].Sid, out arrayValue); + //string mysid; + //var b = LoopUtil.ConvertSidToStringSid(app_capabilities[0].Sid, out mysid); + } + app.LoopUtil = CheckLoopback(PI_app.appContainerSid); + Apps.Add(app); + } + } + private bool CheckLoopback(IntPtr intPtr) + { + foreach (SID_AND_ATTRIBUTES item in _AppListConfig) + { + string left, right; + ConvertSidToStringSid(item.Sid, out left); + ConvertSidToStringSid(intPtr, out right); + + if (left == right) + { + return true; + } + } + return false; + } + + private bool CreateExcemptions(string appName) + { + var hasChanges = false; + + foreach (var app in Apps) + { + if ((app.appContainerName ?? string.Empty).IndexOf(appName, StringComparison.OrdinalIgnoreCase) != -1 || + (app.displayName ?? string.Empty).IndexOf(appName, StringComparison.OrdinalIgnoreCase) != -1) + { + if (!app.LoopUtil) + { + app.LoopUtil = true; + hasChanges = true; + } + } + } + + return hasChanges; + } + + public static void Run(string appName) + { + var util = new LoopUtil(); + util.LoadApps(); + + var hasChanges = util.CreateExcemptions(appName); + + if (hasChanges) + { + util.SaveLoopbackState(); + } + util.SaveLoopbackState(); + } + + private static List getCapabilites(INET_FIREWALL_AC_CAPABILITIES cap) + { + List mycap = new List(); + + IntPtr arrayValue = cap.capabilities; + + var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); + for (var i = 0; i < cap.count; i++) + { + var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); + mycap.Add(cur); + arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); + } + + return mycap; + + } + + private static List getContainerSID(INET_FIREWALL_AC_CAPABILITIES cap) + { + List mycap = new List(); + + IntPtr arrayValue = cap.capabilities; + + var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); + for (var i = 0; i < cap.count; i++) + { + var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); + mycap.Add(cur); + arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); + } + + return mycap; + + } + + private static List PI_NetworkIsolationGetAppContainerConfig() + { + + IntPtr arrayValue = IntPtr.Zero; + uint size = 0; + var list = new List(); + + // Pin down variables + GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned); + GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned); + + uint retval = NetworkIsolationGetAppContainerConfig(out size, out arrayValue); + + var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES)); + for (var i = 0; i < size; i++) + { + var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES)); + list.Add(cur); + arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); + } + + //release pinned variables. + handle_pdwCntPublicACs.Free(); + handle_ppACs.Free(); + + return list; + + + } + + private List PI_NetworkIsolationEnumAppContainers() + { + + IntPtr arrayValue = IntPtr.Zero; + uint size = 0; + var list = new List(); + + // Pin down variables + GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned); + GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned); + + //uint retval2 = NetworkIsolationGetAppContainerConfig( out size, out arrayValue); + + uint retval = NetworkIsolationEnumAppContainers((Int32)NETISO_FLAG.NETISO_FLAG_MAX, out size, out arrayValue); + _pACs = arrayValue; //store the pointer so it can be freed when we close the form + + var structSize = Marshal.SizeOf(typeof(INET_FIREWALL_APP_CONTAINER)); + for (var i = 0; i < size; i++) + { + var cur = (INET_FIREWALL_APP_CONTAINER)Marshal.PtrToStructure(arrayValue, typeof(INET_FIREWALL_APP_CONTAINER)); + list.Add(cur); + arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize)); + } + + //release pinned variables. + handle_pdwCntPublicACs.Free(); + handle_ppACs.Free(); + + return list; + + + } + + public bool SaveLoopbackState() + { + var countEnabled = CountEnabledLoopUtil(); + SID_AND_ATTRIBUTES[] arr = new SID_AND_ATTRIBUTES[countEnabled]; + int count = 0; + + for (int i = 0; i < Apps.Count; i++) + { + if (Apps[i].LoopUtil) + { + arr[count].Attributes = 0; + //TO DO: + IntPtr ptr; + ConvertStringSidToSid(Apps[i].StringSid, out ptr); + arr[count].Sid = ptr; + count++; + } + + } + + + if (NetworkIsolationSetAppContainerConfig((uint)countEnabled, arr) == 0) + { + return true; + } + else + { return false; } + + } + + private int CountEnabledLoopUtil() + { + var count = 0; + for (int i = 0; i < Apps.Count; i++) + { + if (Apps[i].LoopUtil) + { + count++; + } + + } + return count; + } + + public void FreeResources() + { + NetworkIsolationFreeAppContainers(_pACs); + } + + } +} diff --git a/MediaBrowser.ServerApplication/WindowsAppHost.cs b/MediaBrowser.ServerApplication/WindowsAppHost.cs index 2d3d8a85b..8f1a88a74 100644 --- a/MediaBrowser.ServerApplication/WindowsAppHost.cs +++ b/MediaBrowser.ServerApplication/WindowsAppHost.cs @@ -11,7 +11,6 @@ using Emby.Server.Core; using Emby.Server.Implementations; using Emby.Server.Implementations.EntryPoints; using Emby.Server.Implementations.FFMpeg; -using Emby.Server.Implementations.Windows; using Emby.Server.Sync; using MediaBrowser.Controller.Connect; using MediaBrowser.Controller.Sync; @@ -49,6 +48,11 @@ namespace MediaBrowser.ServerApplication MainStartup.Restart(); } + public override void EnableLoopback(string appName) + { + LoopUtil.Run(appName); + } + protected override List GetAssembliesWithPartsInternal() { var list = new List(); diff --git a/SharedVersion.cs b/SharedVersion.cs index 28e89a683..3087dc497 100644 --- a/SharedVersion.cs +++ b/SharedVersion.cs @@ -1,3 +1,3 @@ using System.Reflection; -[assembly: AssemblyVersion("3.2.8.8")] +[assembly: AssemblyVersion("3.2.8.9")] -- cgit v1.2.3