WindowsAppHost.cs 7.7 KB

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