WindowsApp.cs 7.2 KB

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