WindowsApp.cs 7.1 KB

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