WindowsAppHost.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices.ComTypes;
  7. using Emby.Server.Core;
  8. using Emby.Server.Implementations;
  9. using Emby.Server.Implementations.EntryPoints;
  10. using Emby.Server.Implementations.FFMpeg;
  11. using MediaBrowser.Model.IO;
  12. using MediaBrowser.Model.Logging;
  13. using MediaBrowser.Model.System;
  14. using MediaBrowser.ServerApplication.Native;
  15. namespace MediaBrowser.ServerApplication
  16. {
  17. public class WindowsAppHost : ApplicationHost
  18. {
  19. public WindowsAppHost(ServerApplicationPaths applicationPaths, ILogManager logManager, StartupOptions options, IFileSystem fileSystem, IPowerManagement powerManagement, string releaseAssetFilename, IEnvironmentInfo environmentInfo, MediaBrowser.Controller.Drawing.IImageEncoder imageEncoder, ISystemEvents systemEvents, IMemoryStreamFactory memoryStreamFactory, MediaBrowser.Common.Net.INetworkManager networkManager, Action<string, string> certificateGenerator, Func<string> defaultUsernameFactory)
  20. : base(applicationPaths, logManager, options, fileSystem, powerManagement, releaseAssetFilename, environmentInfo, imageEncoder, systemEvents, memoryStreamFactory, networkManager, certificateGenerator, defaultUsernameFactory)
  21. {
  22. }
  23. public override bool IsRunningAsService
  24. {
  25. get { return MainStartup.IsRunningAsService; }
  26. }
  27. protected override FFMpegInstallInfo GetFfmpegInstallInfo()
  28. {
  29. var info = new FFMpegInstallInfo();
  30. info.FFMpegFilename = "ffmpeg.exe";
  31. info.FFProbeFilename = "ffprobe.exe";
  32. info.Version = "20160410";
  33. info.ArchiveType = "7z";
  34. info.DownloadUrls = GetDownloadUrls();
  35. return info;
  36. }
  37. private string[] GetDownloadUrls()
  38. {
  39. switch (EnvironmentInfo.SystemArchitecture)
  40. {
  41. case Architecture.X64:
  42. return new[]
  43. {
  44. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/windows/ffmpeg-20160410-win64.7z"
  45. };
  46. case Architecture.X86:
  47. return new[]
  48. {
  49. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/windows/ffmpeg-20160410-win32.7z"
  50. };
  51. }
  52. return new string[] { };
  53. }
  54. protected override void RestartInternal()
  55. {
  56. MainStartup.Restart();
  57. }
  58. protected override List<Assembly> GetAssembliesWithPartsInternal()
  59. {
  60. var list = new List<Assembly>();
  61. if (!Environment.Is64BitProcess)
  62. {
  63. //list.Add(typeof(PismoIsoManager).Assembly);
  64. }
  65. list.Add(GetType().Assembly);
  66. return list;
  67. }
  68. protected override void ShutdownInternal()
  69. {
  70. MainStartup.Shutdown();
  71. }
  72. protected override void AuthorizeServer()
  73. {
  74. ServerAuthorization.AuthorizeServer(UdpServerEntryPoint.PortNumber,
  75. ServerConfigurationManager.Configuration.HttpServerPortNumber,
  76. ServerConfigurationManager.Configuration.HttpsPortNumber,
  77. MainStartup.ApplicationPath,
  78. ConfigurationManager.CommonApplicationPaths.TempDirectory);
  79. }
  80. protected override void ConfigureAutoRunInternal(bool autorun)
  81. {
  82. var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
  83. if (autorun && !MainStartup.IsRunningAsService)
  84. {
  85. //Copy our shortut into the startup folder for this user
  86. var targetPath = Path.Combine(startupPath, "Emby Server.lnk");
  87. IShellLinkW link = (IShellLinkW)new ShellLink();
  88. var appPath = Process.GetCurrentProcess().MainModule.FileName;
  89. // setup shortcut information
  90. link.SetDescription(Name);
  91. link.SetPath(appPath);
  92. link.SetWorkingDirectory(Path.GetDirectoryName(appPath));
  93. // save it
  94. IPersistFile file = (IPersistFile)link;
  95. file.Save(targetPath, false);
  96. }
  97. else
  98. {
  99. //Remove our shortcut from the startup folder for this user
  100. FileSystemManager.DeleteFile(Path.Combine(startupPath, "Emby Server.lnk"));
  101. }
  102. }
  103. protected override bool SupportsDualModeSockets
  104. {
  105. get
  106. {
  107. return true;
  108. }
  109. }
  110. protected override void EnableLoopbackInternal(string appName)
  111. {
  112. LoopUtil.Run(appName);
  113. }
  114. public override bool SupportsRunningAsService
  115. {
  116. get
  117. {
  118. return true;
  119. }
  120. }
  121. public override bool CanSelfRestart
  122. {
  123. get
  124. {
  125. return MainStartup.CanSelfRestart;
  126. }
  127. }
  128. public override bool SupportsAutoRunAtStartup
  129. {
  130. get
  131. {
  132. return true;
  133. }
  134. }
  135. public override bool CanSelfUpdate
  136. {
  137. get
  138. {
  139. return MainStartup.CanSelfUpdate;
  140. }
  141. }
  142. public bool PortsRequireAuthorization(string applicationPath)
  143. {
  144. var appNameSrch = Path.GetFileName(applicationPath);
  145. var startInfo = new ProcessStartInfo
  146. {
  147. FileName = "netsh",
  148. Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"",
  149. CreateNoWindow = true,
  150. UseShellExecute = false,
  151. WindowStyle = ProcessWindowStyle.Hidden,
  152. ErrorDialog = false,
  153. RedirectStandardOutput = true
  154. };
  155. using (var process = Process.Start(startInfo))
  156. {
  157. process.Start();
  158. try
  159. {
  160. var data = process.StandardOutput.ReadToEnd() ?? string.Empty;
  161. if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1)
  162. {
  163. Logger.Info("Found potential windows firewall rule blocking Emby Server: " + data);
  164. }
  165. //var parts = data.Split('\n');
  166. //return parts.Length > 4;
  167. //return Confirm();
  168. return false;
  169. }
  170. catch (Exception ex)
  171. {
  172. Logger.ErrorException("Error querying windows firewall", ex);
  173. // Hate having to do this
  174. try
  175. {
  176. process.Kill();
  177. }
  178. catch (Exception ex1)
  179. {
  180. Logger.ErrorException("Error killing process", ex1);
  181. }
  182. throw;
  183. }
  184. }
  185. }
  186. }
  187. }