WindowsAppHost.cs 7.6 KB

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