WindowsApp.cs 6.5 KB

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