WindowsAppHost.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. protected override bool SupportsDualModeSockets
  102. {
  103. get
  104. {
  105. return true;
  106. }
  107. }
  108. public override void LaunchUrl(string url)
  109. {
  110. var process = new Process
  111. {
  112. StartInfo = new ProcessStartInfo
  113. {
  114. FileName = url
  115. },
  116. EnableRaisingEvents = true,
  117. };
  118. process.Exited += ProcessExited;
  119. try
  120. {
  121. process.Start();
  122. }
  123. catch (Exception ex)
  124. {
  125. Console.WriteLine("Error launching url: {0}", url);
  126. Logger.ErrorException("Error launching url: {0}", ex, url);
  127. throw;
  128. }
  129. }
  130. private static void ProcessExited(object sender, EventArgs e)
  131. {
  132. ((Process)sender).Dispose();
  133. }
  134. protected override void EnableLoopbackInternal(string appName)
  135. {
  136. LoopUtil.Run(appName);
  137. }
  138. public override bool SupportsRunningAsService
  139. {
  140. get
  141. {
  142. return true;
  143. }
  144. }
  145. public override bool CanSelfRestart
  146. {
  147. get
  148. {
  149. return MainStartup.CanSelfRestart;
  150. }
  151. }
  152. public override bool SupportsAutoRunAtStartup
  153. {
  154. get
  155. {
  156. return true;
  157. }
  158. }
  159. public override bool CanSelfUpdate
  160. {
  161. get
  162. {
  163. return MainStartup.CanSelfUpdate;
  164. }
  165. }
  166. public bool PortsRequireAuthorization(string applicationPath)
  167. {
  168. var appNameSrch = Path.GetFileName(applicationPath);
  169. var startInfo = new ProcessStartInfo
  170. {
  171. FileName = "netsh",
  172. Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"",
  173. CreateNoWindow = true,
  174. UseShellExecute = false,
  175. WindowStyle = ProcessWindowStyle.Hidden,
  176. ErrorDialog = false,
  177. RedirectStandardOutput = true
  178. };
  179. using (var process = Process.Start(startInfo))
  180. {
  181. process.Start();
  182. try
  183. {
  184. var data = process.StandardOutput.ReadToEnd() ?? string.Empty;
  185. if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1)
  186. {
  187. Logger.Info("Found potential windows firewall rule blocking Emby Server: " + data);
  188. }
  189. //var parts = data.Split('\n');
  190. //return parts.Length > 4;
  191. //return Confirm();
  192. return false;
  193. }
  194. catch (Exception ex)
  195. {
  196. Logger.ErrorException("Error querying windows firewall", ex);
  197. // Hate having to do this
  198. try
  199. {
  200. process.Kill();
  201. }
  202. catch (Exception ex1)
  203. {
  204. Logger.ErrorException("Error killing process", ex1);
  205. }
  206. throw;
  207. }
  208. }
  209. }
  210. }
  211. }