WindowsApp.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Startup.Common;
  5. using MediaBrowser.ServerApplication.Networking;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Reflection;
  10. using System.Windows.Forms;
  11. using CommonIO;
  12. using MediaBrowser.Model.System;
  13. using MediaBrowser.Server.Implementations.Persistence;
  14. using MediaBrowser.Server.Startup.Common.FFMpeg;
  15. using OperatingSystem = MediaBrowser.Server.Startup.Common.OperatingSystem;
  16. namespace MediaBrowser.ServerApplication.Native
  17. {
  18. public class WindowsApp : INativeApp
  19. {
  20. private readonly IFileSystem _fileSystem;
  21. private readonly ILogger _logger;
  22. public WindowsApp(IFileSystem fileSystem, ILogger logger)
  23. {
  24. _fileSystem = fileSystem;
  25. _logger = logger;
  26. }
  27. public List<Assembly> GetAssembliesWithParts()
  28. {
  29. var list = new List<Assembly>();
  30. if (!System.Environment.Is64BitProcess)
  31. {
  32. //list.Add(typeof(PismoIsoManager).Assembly);
  33. }
  34. list.Add(GetType().Assembly);
  35. return list;
  36. }
  37. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  38. {
  39. ServerAuthorization.AuthorizeServer(udpPort, httpServerPort, httpsPort, applicationPath, tempDirectory);
  40. }
  41. public NativeEnvironment Environment
  42. {
  43. get
  44. {
  45. return new NativeEnvironment
  46. {
  47. OperatingSystem = OperatingSystem.Windows,
  48. SystemArchitecture = System.Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86,
  49. OperatingSystemVersionString = System.Environment.OSVersion.VersionString
  50. };
  51. }
  52. }
  53. public bool SupportsLibraryMonitor
  54. {
  55. get { return true; }
  56. }
  57. public bool SupportsRunningAsService
  58. {
  59. get
  60. {
  61. return true;
  62. }
  63. }
  64. public bool IsRunningAsService
  65. {
  66. get;
  67. set;
  68. }
  69. public bool CanSelfRestart
  70. {
  71. get
  72. {
  73. return MainStartup.CanSelfRestart;
  74. }
  75. }
  76. public bool SupportsAutoRunAtStartup
  77. {
  78. get
  79. {
  80. return true;
  81. }
  82. }
  83. public bool CanSelfUpdate
  84. {
  85. get
  86. {
  87. return MainStartup.CanSelfUpdate;
  88. }
  89. }
  90. public void Shutdown()
  91. {
  92. MainStartup.Shutdown();
  93. }
  94. public void Restart(StartupOptions startupOptions)
  95. {
  96. MainStartup.Restart();
  97. }
  98. public void ConfigureAutoRun(bool autorun)
  99. {
  100. var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk");
  101. var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
  102. if (autorun)
  103. {
  104. //Copy our shortut into the startup folder for this user
  105. var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk");
  106. _fileSystem.CreateDirectory(Path.GetDirectoryName(targetPath));
  107. File.Copy(shortcutPath, targetPath, true);
  108. }
  109. else
  110. {
  111. //Remove our shortcut from the startup folder for this user
  112. _fileSystem.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"));
  113. }
  114. }
  115. public INetworkManager CreateNetworkManager(ILogger logger)
  116. {
  117. return new NetworkManager(logger);
  118. }
  119. public void PreventSystemStandby()
  120. {
  121. MainStartup.Invoke(Standby.PreventSleep);
  122. }
  123. public void AllowSystemStandby()
  124. {
  125. MainStartup.Invoke(Standby.AllowSleep);
  126. }
  127. public FFMpegInstallInfo GetFfmpegInstallInfo()
  128. {
  129. var info = new FFMpegInstallInfo();
  130. info.FFMpegFilename = "ffmpeg.exe";
  131. info.FFProbeFilename = "ffprobe.exe";
  132. info.Version = "0";
  133. return info;
  134. }
  135. public void LaunchUrl(string url)
  136. {
  137. var process = new Process
  138. {
  139. StartInfo = new ProcessStartInfo
  140. {
  141. FileName = url
  142. },
  143. EnableRaisingEvents = true,
  144. };
  145. process.Exited += ProcessExited;
  146. try
  147. {
  148. process.Start();
  149. }
  150. catch (Exception ex)
  151. {
  152. _logger.ErrorException("Error launching url: {0}", ex, url);
  153. throw;
  154. }
  155. }
  156. public IDbConnector GetDbConnector()
  157. {
  158. return new DbConnector(_logger);
  159. }
  160. /// <summary>
  161. /// Processes the exited.
  162. /// </summary>
  163. /// <param name="sender">The sender.</param>
  164. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  165. private static void ProcessExited(object sender, EventArgs e)
  166. {
  167. ((Process)sender).Dispose();
  168. }
  169. public void EnableLoopback(string appName)
  170. {
  171. LoopUtil.Run(appName);
  172. }
  173. private bool Confirm()
  174. {
  175. if (MainStartup._splash == null)
  176. {
  177. return false;
  178. }
  179. return MessageBox.Show(MainStartup._splash, "Emby has detected that a rule has been added to Windows Firewall that may prevent your other devices from accessing Emby Server. Click OK to remove this rule, or cancel to proceed anyway.", "Windows Firewall", MessageBoxButtons.OKCancel) == DialogResult.OK;
  180. }
  181. public bool PortsRequireAuthorization(string applicationPath)
  182. {
  183. var appNameSrch = Path.GetFileName(applicationPath);
  184. var startInfo = new ProcessStartInfo
  185. {
  186. FileName = "netsh",
  187. Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"",
  188. CreateNoWindow = true,
  189. UseShellExecute = false,
  190. WindowStyle = ProcessWindowStyle.Hidden,
  191. ErrorDialog = false,
  192. RedirectStandardOutput = true
  193. };
  194. using (var process = Process.Start(startInfo))
  195. {
  196. process.Start();
  197. try
  198. {
  199. var data = process.StandardOutput.ReadToEnd() ?? string.Empty;
  200. if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1)
  201. {
  202. _logger.Info("Found potential windows firewall rule blocking Emby Server: " + data);
  203. return Confirm();
  204. }
  205. //var parts = data.Split('\n');
  206. //return parts.Length > 4;
  207. //return Confirm();
  208. return false;
  209. }
  210. catch (Exception ex)
  211. {
  212. _logger.ErrorException("Error querying windows firewall", ex);
  213. // Hate having to do this
  214. try
  215. {
  216. process.Kill();
  217. }
  218. catch (Exception ex1)
  219. {
  220. _logger.ErrorException("Error killing process", ex1);
  221. }
  222. throw;
  223. }
  224. }
  225. }
  226. }
  227. }