WindowsAppHost.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using Emby.Server.Core;
  7. using Emby.Server.Core.Data;
  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 IDbConnector GetDbConnector()
  81. {
  82. return new DbConnector(Logger);
  83. }
  84. protected override void ConfigureAutoRunInternal(bool autorun)
  85. {
  86. var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk");
  87. var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
  88. if (autorun)
  89. {
  90. //Copy our shortut into the startup folder for this user
  91. var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk");
  92. FileSystemManager.CreateDirectory(Path.GetDirectoryName(targetPath));
  93. File.Copy(shortcutPath, targetPath, true);
  94. }
  95. else
  96. {
  97. //Remove our shortcut from the startup folder for this user
  98. FileSystemManager.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"));
  99. }
  100. }
  101. public override void LaunchUrl(string url)
  102. {
  103. var process = new Process
  104. {
  105. StartInfo = new ProcessStartInfo
  106. {
  107. FileName = url
  108. },
  109. EnableRaisingEvents = true,
  110. };
  111. process.Exited += ProcessExited;
  112. try
  113. {
  114. process.Start();
  115. }
  116. catch (Exception ex)
  117. {
  118. Logger.ErrorException("Error launching url: {0}", ex, url);
  119. throw;
  120. }
  121. }
  122. private static void ProcessExited(object sender, EventArgs e)
  123. {
  124. ((Process)sender).Dispose();
  125. }
  126. protected override void EnableLoopbackInternal(string appName)
  127. {
  128. LoopUtil.Run(appName);
  129. }
  130. public override bool SupportsRunningAsService
  131. {
  132. get
  133. {
  134. return true;
  135. }
  136. }
  137. public override bool CanSelfRestart
  138. {
  139. get
  140. {
  141. return MainStartup.CanSelfRestart;
  142. }
  143. }
  144. public override bool SupportsAutoRunAtStartup
  145. {
  146. get
  147. {
  148. return true;
  149. }
  150. }
  151. public override bool CanSelfUpdate
  152. {
  153. get
  154. {
  155. return MainStartup.CanSelfUpdate;
  156. }
  157. }
  158. public bool PortsRequireAuthorization(string applicationPath)
  159. {
  160. var appNameSrch = Path.GetFileName(applicationPath);
  161. var startInfo = new ProcessStartInfo
  162. {
  163. FileName = "netsh",
  164. Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"",
  165. CreateNoWindow = true,
  166. UseShellExecute = false,
  167. WindowStyle = ProcessWindowStyle.Hidden,
  168. ErrorDialog = false,
  169. RedirectStandardOutput = true
  170. };
  171. using (var process = Process.Start(startInfo))
  172. {
  173. process.Start();
  174. try
  175. {
  176. var data = process.StandardOutput.ReadToEnd() ?? string.Empty;
  177. if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1)
  178. {
  179. Logger.Info("Found potential windows firewall rule blocking Emby Server: " + data);
  180. }
  181. //var parts = data.Split('\n');
  182. //return parts.Length > 4;
  183. //return Confirm();
  184. return false;
  185. }
  186. catch (Exception ex)
  187. {
  188. Logger.ErrorException("Error querying windows firewall", ex);
  189. // Hate having to do this
  190. try
  191. {
  192. process.Kill();
  193. }
  194. catch (Exception ex1)
  195. {
  196. Logger.ErrorException("Error killing process", ex1);
  197. }
  198. throw;
  199. }
  200. }
  201. }
  202. }
  203. }