WindowsAppHost.cs 6.7 KB

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