ApplicationHost.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. using MediaBrowser.Api;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Common.Constants;
  5. using MediaBrowser.Common.Extensions;
  6. using MediaBrowser.Common.Implementations;
  7. using MediaBrowser.Common.Implementations.IO;
  8. using MediaBrowser.Common.Implementations.ScheduledTasks;
  9. using MediaBrowser.Common.Implementations.Updates;
  10. using MediaBrowser.Common.MediaInfo;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Controller;
  13. using MediaBrowser.Controller.Configuration;
  14. using MediaBrowser.Controller.Drawing;
  15. using MediaBrowser.Controller.Entities;
  16. using MediaBrowser.Controller.IO;
  17. using MediaBrowser.Controller.Library;
  18. using MediaBrowser.Controller.Localization;
  19. using MediaBrowser.Controller.MediaInfo;
  20. using MediaBrowser.Controller.Notifications;
  21. using MediaBrowser.Controller.Persistence;
  22. using MediaBrowser.Controller.Plugins;
  23. using MediaBrowser.Controller.Providers;
  24. using MediaBrowser.Controller.Resolvers;
  25. using MediaBrowser.Controller.Session;
  26. using MediaBrowser.Controller.Sorting;
  27. using MediaBrowser.IsoMounter;
  28. using MediaBrowser.Model.IO;
  29. using MediaBrowser.Model.MediaInfo;
  30. using MediaBrowser.Model.System;
  31. using MediaBrowser.Providers;
  32. using MediaBrowser.Server.Implementations;
  33. using MediaBrowser.Server.Implementations.BdInfo;
  34. using MediaBrowser.Server.Implementations.Configuration;
  35. using MediaBrowser.Server.Implementations.HttpServer;
  36. using MediaBrowser.Server.Implementations.IO;
  37. using MediaBrowser.Server.Implementations.Library;
  38. using MediaBrowser.Server.Implementations.Localization;
  39. using MediaBrowser.Server.Implementations.MediaEncoder;
  40. using MediaBrowser.Server.Implementations.Persistence;
  41. using MediaBrowser.Server.Implementations.Providers;
  42. using MediaBrowser.Server.Implementations.ServerManager;
  43. using MediaBrowser.Server.Implementations.Session;
  44. using MediaBrowser.Server.Implementations.WebSocket;
  45. using MediaBrowser.ServerApplication.Implementations;
  46. using MediaBrowser.WebDashboard.Api;
  47. using System;
  48. using System.Collections.Generic;
  49. using System.Data.SQLite;
  50. using System.Diagnostics;
  51. using System.IO;
  52. using System.Linq;
  53. using System.Reflection;
  54. using System.Threading.Tasks;
  55. namespace MediaBrowser.ServerApplication
  56. {
  57. /// <summary>
  58. /// Class CompositionRoot
  59. /// </summary>
  60. public class ApplicationHost : BaseApplicationHost<ServerApplicationPaths>, IServerApplicationHost
  61. {
  62. internal const int UdpServerPort = 7359;
  63. /// <summary>
  64. /// Gets the server kernel.
  65. /// </summary>
  66. /// <value>The server kernel.</value>
  67. protected Kernel ServerKernel { get; set; }
  68. /// <summary>
  69. /// Gets the server configuration manager.
  70. /// </summary>
  71. /// <value>The server configuration manager.</value>
  72. public IServerConfigurationManager ServerConfigurationManager
  73. {
  74. get { return (IServerConfigurationManager)ConfigurationManager; }
  75. }
  76. /// <summary>
  77. /// Gets the name of the log file prefix.
  78. /// </summary>
  79. /// <value>The name of the log file prefix.</value>
  80. protected override string LogFilePrefixName
  81. {
  82. get { return "server"; }
  83. }
  84. /// <summary>
  85. /// Gets the name of the web application that can be used for url building.
  86. /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
  87. /// </summary>
  88. /// <value>The name of the web application.</value>
  89. public string WebApplicationName
  90. {
  91. get { return "mediabrowser"; }
  92. }
  93. /// <summary>
  94. /// Gets the HTTP server URL prefix.
  95. /// </summary>
  96. /// <value>The HTTP server URL prefix.</value>
  97. public string HttpServerUrlPrefix
  98. {
  99. get
  100. {
  101. return "http://+:" + ServerConfigurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/";
  102. }
  103. }
  104. /// <summary>
  105. /// Gets the configuration manager.
  106. /// </summary>
  107. /// <returns>IConfigurationManager.</returns>
  108. protected override IConfigurationManager GetConfigurationManager()
  109. {
  110. return new ServerConfigurationManager(ApplicationPaths, LogManager, XmlSerializer);
  111. }
  112. /// <summary>
  113. /// Gets or sets the server manager.
  114. /// </summary>
  115. /// <value>The server manager.</value>
  116. private IServerManager ServerManager { get; set; }
  117. /// <summary>
  118. /// Gets or sets the user manager.
  119. /// </summary>
  120. /// <value>The user manager.</value>
  121. public IUserManager UserManager { get; set; }
  122. /// <summary>
  123. /// Gets or sets the library manager.
  124. /// </summary>
  125. /// <value>The library manager.</value>
  126. internal ILibraryManager LibraryManager { get; set; }
  127. /// <summary>
  128. /// Gets or sets the directory watchers.
  129. /// </summary>
  130. /// <value>The directory watchers.</value>
  131. private IDirectoryWatchers DirectoryWatchers { get; set; }
  132. /// <summary>
  133. /// Gets or sets the provider manager.
  134. /// </summary>
  135. /// <value>The provider manager.</value>
  136. private IProviderManager ProviderManager { get; set; }
  137. /// <summary>
  138. /// Gets or sets the zip client.
  139. /// </summary>
  140. /// <value>The zip client.</value>
  141. private IZipClient ZipClient { get; set; }
  142. /// <summary>
  143. /// Gets or sets the HTTP server.
  144. /// </summary>
  145. /// <value>The HTTP server.</value>
  146. private IHttpServer HttpServer { get; set; }
  147. /// <summary>
  148. /// Gets or sets the media encoder.
  149. /// </summary>
  150. /// <value>The media encoder.</value>
  151. private IMediaEncoder MediaEncoder { get; set; }
  152. private IIsoManager IsoManager { get; set; }
  153. private ILocalizationManager LocalizationManager { get; set; }
  154. /// <summary>
  155. /// Gets or sets the user data repository.
  156. /// </summary>
  157. /// <value>The user data repository.</value>
  158. private IUserDataRepository UserDataRepository { get; set; }
  159. private IUserRepository UserRepository { get; set; }
  160. internal IDisplayPreferencesRepository DisplayPreferencesRepository { get; set; }
  161. private IItemRepository ItemRepository { get; set; }
  162. private INotificationsRepository NotificationsRepository { get; set; }
  163. /// <summary>
  164. /// The full path to our startmenu shortcut
  165. /// </summary>
  166. protected override string ProductShortcutPath
  167. {
  168. get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Media Browser 3", "Media Browser Server.lnk"); }
  169. }
  170. private Task<IHttpServer> _httpServerCreationTask;
  171. /// <summary>
  172. /// Runs the startup tasks.
  173. /// </summary>
  174. /// <returns>Task.</returns>
  175. public override async Task RunStartupTasks()
  176. {
  177. await base.RunStartupTasks().ConfigureAwait(false);
  178. DirectoryWatchers.Start();
  179. Logger.Info("Core startup complete");
  180. Parallel.ForEach(GetExports<IServerEntryPoint>(), entryPoint =>
  181. {
  182. try
  183. {
  184. entryPoint.Run();
  185. }
  186. catch (Exception ex)
  187. {
  188. Logger.ErrorException("Error in {0}", ex, entryPoint.GetType().Name);
  189. }
  190. });
  191. }
  192. /// <summary>
  193. /// Called when [logger loaded].
  194. /// </summary>
  195. protected override void OnLoggerLoaded()
  196. {
  197. base.OnLoggerLoaded();
  198. _httpServerCreationTask = Task.Run(() => ServerFactory.CreateServer(this, LogManager, "Media Browser", "dashboard/index.html"));
  199. }
  200. /// <summary>
  201. /// Registers resources that classes will depend on
  202. /// </summary>
  203. /// <returns>Task.</returns>
  204. protected override async Task RegisterResources()
  205. {
  206. ServerKernel = new Kernel();
  207. await base.RegisterResources().ConfigureAwait(false);
  208. RegisterSingleInstance<IHttpResultFactory>(new HttpResultFactory(LogManager));
  209. RegisterSingleInstance<IServerApplicationHost>(this);
  210. RegisterSingleInstance<IServerApplicationPaths>(ApplicationPaths);
  211. RegisterSingleInstance(ServerKernel);
  212. RegisterSingleInstance(ServerConfigurationManager);
  213. RegisterSingleInstance<IWebSocketServer>(() => new AlchemyServer(Logger));
  214. IsoManager = new IsoManager();
  215. RegisterSingleInstance(IsoManager);
  216. RegisterSingleInstance<IBlurayExaminer>(() => new BdInfoExaminer());
  217. ZipClient = new DotNetZipClient();
  218. RegisterSingleInstance(ZipClient);
  219. UserDataRepository = new SqliteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager);
  220. RegisterSingleInstance(UserDataRepository);
  221. UserRepository = await GetUserRepository().ConfigureAwait(false);
  222. RegisterSingleInstance(UserRepository);
  223. DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
  224. RegisterSingleInstance(DisplayPreferencesRepository);
  225. ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
  226. RegisterSingleInstance(ItemRepository);
  227. UserManager = new UserManager(Logger, ServerConfigurationManager, UserRepository);
  228. RegisterSingleInstance(UserManager);
  229. LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataRepository, () => DirectoryWatchers);
  230. RegisterSingleInstance(LibraryManager);
  231. DirectoryWatchers = new DirectoryWatchers(LogManager, TaskManager, LibraryManager, ServerConfigurationManager);
  232. RegisterSingleInstance(DirectoryWatchers);
  233. ProviderManager = new ProviderManager(HttpClient, ServerConfigurationManager, DirectoryWatchers, LogManager, LibraryManager);
  234. RegisterSingleInstance(ProviderManager);
  235. RegisterSingleInstance<ILibrarySearchEngine>(() => new LuceneSearchEngine(ApplicationPaths, LogManager, LibraryManager));
  236. MediaEncoder = new MediaEncoder(LogManager.GetLogger("MediaEncoder"), ZipClient, ApplicationPaths, JsonSerializer);
  237. RegisterSingleInstance(MediaEncoder);
  238. var clientConnectionManager = new SessionManager(UserDataRepository, ServerConfigurationManager, Logger, UserRepository);
  239. RegisterSingleInstance<ISessionManager>(clientConnectionManager);
  240. HttpServer = await _httpServerCreationTask.ConfigureAwait(false);
  241. RegisterSingleInstance(HttpServer, false);
  242. ServerManager = new ServerManager(this, JsonSerializer, Logger, ServerConfigurationManager);
  243. RegisterSingleInstance(ServerManager);
  244. LocalizationManager = new LocalizationManager(ServerConfigurationManager);
  245. RegisterSingleInstance(LocalizationManager);
  246. var displayPreferencesTask = Task.Run(async () => await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false));
  247. var itemsTask = Task.Run(async () => await ConfigureItemRepositories().ConfigureAwait(false));
  248. var userdataTask = Task.Run(async () => await ConfigureUserDataRepositories().ConfigureAwait(false));
  249. await ConfigureNotificationsRepository().ConfigureAwait(false);
  250. await Task.WhenAll(itemsTask, displayPreferencesTask, userdataTask).ConfigureAwait(false);
  251. SetKernelProperties();
  252. }
  253. /// <summary>
  254. /// Sets the kernel properties.
  255. /// </summary>
  256. private void SetKernelProperties()
  257. {
  258. ServerKernel.ImageManager = new ImageManager(LogManager.GetLogger("ImageManager"),
  259. ApplicationPaths, ItemRepository);
  260. Parallel.Invoke(
  261. () => ServerKernel.FFMpegManager = new FFMpegManager(ApplicationPaths, MediaEncoder, LibraryManager, Logger, ItemRepository),
  262. () => ServerKernel.ImageManager.ImageEnhancers = GetExports<IImageEnhancer>().OrderBy(e => e.Priority).ToArray(),
  263. () => LocalizedStrings.StringFiles = GetExports<LocalizedStringData>(),
  264. SetStaticProperties
  265. );
  266. }
  267. private async Task<IUserRepository> GetUserRepository()
  268. {
  269. var dbFile = Path.Combine(ApplicationPaths.DataPath, "users.db");
  270. var connection = await ConnectToDb(dbFile).ConfigureAwait(false);
  271. var repo = new SqliteUserRepository(connection, ApplicationPaths, JsonSerializer, LogManager);
  272. repo.Initialize();
  273. return repo;
  274. }
  275. /// <summary>
  276. /// Configures the repositories.
  277. /// </summary>
  278. /// <returns>Task.</returns>
  279. private async Task ConfigureNotificationsRepository()
  280. {
  281. var dbFile = Path.Combine(ApplicationPaths.DataPath, "notifications.db");
  282. var connection = await ConnectToDb(dbFile).ConfigureAwait(false);
  283. var repo = new SqliteNotificationsRepository(connection, LogManager);
  284. repo.Initialize();
  285. NotificationsRepository = repo;
  286. RegisterSingleInstance(NotificationsRepository);
  287. }
  288. /// <summary>
  289. /// Configures the repositories.
  290. /// </summary>
  291. /// <returns>Task.</returns>
  292. private async Task ConfigureDisplayPreferencesRepositories()
  293. {
  294. await DisplayPreferencesRepository.Initialize().ConfigureAwait(false);
  295. }
  296. /// <summary>
  297. /// Configures the item repositories.
  298. /// </summary>
  299. /// <returns>Task.</returns>
  300. private async Task ConfigureItemRepositories()
  301. {
  302. await ItemRepository.Initialize().ConfigureAwait(false);
  303. ((LibraryManager)LibraryManager).ItemRepository = ItemRepository;
  304. }
  305. /// <summary>
  306. /// Configures the user data repositories.
  307. /// </summary>
  308. /// <returns>Task.</returns>
  309. private Task ConfigureUserDataRepositories()
  310. {
  311. return UserDataRepository.Initialize();
  312. }
  313. /// <summary>
  314. /// Connects to db.
  315. /// </summary>
  316. /// <param name="dbPath">The db path.</param>
  317. /// <returns>Task{IDbConnection}.</returns>
  318. /// <exception cref="System.ArgumentNullException">dbPath</exception>
  319. private static async Task<SQLiteConnection> ConnectToDb(string dbPath)
  320. {
  321. if (string.IsNullOrEmpty(dbPath))
  322. {
  323. throw new ArgumentNullException("dbPath");
  324. }
  325. var connectionstr = new SQLiteConnectionStringBuilder
  326. {
  327. PageSize = 4096,
  328. CacheSize = 4096,
  329. SyncMode = SynchronizationModes.Normal,
  330. DataSource = dbPath,
  331. JournalMode = SQLiteJournalModeEnum.Wal
  332. };
  333. var connection = new SQLiteConnection(connectionstr.ConnectionString);
  334. await connection.OpenAsync().ConfigureAwait(false);
  335. return connection;
  336. }
  337. /// <summary>
  338. /// Dirty hacks
  339. /// </summary>
  340. private void SetStaticProperties()
  341. {
  342. // For now there's no real way to inject these properly
  343. BaseItem.Logger = LogManager.GetLogger("BaseItem");
  344. BaseItem.ConfigurationManager = ServerConfigurationManager;
  345. BaseItem.LibraryManager = LibraryManager;
  346. BaseItem.ProviderManager = ProviderManager;
  347. BaseItem.LocalizationManager = LocalizationManager;
  348. BaseItem.ItemRepository = ItemRepository;
  349. User.XmlSerializer = XmlSerializer;
  350. User.UserManager = UserManager;
  351. LocalizedStrings.ApplicationPaths = ApplicationPaths;
  352. }
  353. /// <summary>
  354. /// Finds the parts.
  355. /// </summary>
  356. protected override void FindParts()
  357. {
  358. if (IsFirstRun)
  359. {
  360. RegisterServerWithAdministratorAccess();
  361. }
  362. base.FindParts();
  363. HttpServer.Init(GetExports<IRestfulService>(false));
  364. ServerManager.AddWebSocketListeners(GetExports<IWebSocketListener>(false));
  365. StartServer(true);
  366. LibraryManager.AddParts(GetExports<IResolverIgnoreRule>(),
  367. GetExports<IVirtualFolderCreator>(),
  368. GetExports<IItemResolver>(),
  369. GetExports<IIntroProvider>(),
  370. GetExports<IBaseItemComparer>(),
  371. GetExports<ILibraryPrescanTask>(),
  372. GetExports<ILibraryPostScanTask>(),
  373. GetExports<IMetadataSaver>());
  374. ProviderManager.AddParts(GetExports<BaseMetadataProvider>().ToArray());
  375. IsoManager.AddParts(GetExports<IIsoMounter>().ToArray());
  376. }
  377. /// <summary>
  378. /// Starts the server.
  379. /// </summary>
  380. /// <param name="retryOnFailure">if set to <c>true</c> [retry on failure].</param>
  381. private void StartServer(bool retryOnFailure)
  382. {
  383. try
  384. {
  385. ServerManager.Start();
  386. }
  387. catch
  388. {
  389. if (retryOnFailure)
  390. {
  391. RegisterServerWithAdministratorAccess();
  392. StartServer(false);
  393. }
  394. else
  395. {
  396. throw;
  397. }
  398. }
  399. }
  400. /// <summary>
  401. /// Called when [configuration updated].
  402. /// </summary>
  403. /// <param name="sender">The sender.</param>
  404. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  405. protected override void OnConfigurationUpdated(object sender, EventArgs e)
  406. {
  407. base.OnConfigurationUpdated(sender, e);
  408. if (!string.Equals(HttpServer.UrlPrefix, HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase))
  409. {
  410. NotifyPendingRestart();
  411. }
  412. else if (!ServerManager.SupportsNativeWebSocket && ServerManager.WebSocketPortNumber != ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber)
  413. {
  414. NotifyPendingRestart();
  415. }
  416. }
  417. /// <summary>
  418. /// Restarts this instance.
  419. /// </summary>
  420. public override void Restart()
  421. {
  422. App.Instance.Restart();
  423. }
  424. /// <summary>
  425. /// Gets or sets a value indicating whether this instance can self update.
  426. /// </summary>
  427. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  428. public override bool CanSelfUpdate
  429. {
  430. get
  431. {
  432. #if DEBUG
  433. return false;
  434. #endif
  435. return ConfigurationManager.CommonConfiguration.EnableAutoUpdate;
  436. }
  437. }
  438. /// <summary>
  439. /// Gets the composable part assemblies.
  440. /// </summary>
  441. /// <returns>IEnumerable{Assembly}.</returns>
  442. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  443. {
  444. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  445. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  446. foreach (var pluginAssembly in Directory
  447. .EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  448. .Select(LoadAssembly).Where(a => a != null))
  449. {
  450. yield return pluginAssembly;
  451. }
  452. // Include composable parts in the Api assembly
  453. yield return typeof(ApiEntryPoint).Assembly;
  454. // Include composable parts in the Dashboard assembly
  455. yield return typeof(DashboardInfo).Assembly;
  456. // Include composable parts in the Model assembly
  457. yield return typeof(SystemInfo).Assembly;
  458. // Include composable parts in the Common assembly
  459. yield return typeof(IApplicationHost).Assembly;
  460. // Include composable parts in the Controller assembly
  461. yield return typeof(Kernel).Assembly;
  462. // Include composable parts in the Providers assembly
  463. yield return typeof(ImagesByNameProvider).Assembly;
  464. // Common implementations
  465. yield return typeof(TaskManager).Assembly;
  466. // Server implementations
  467. yield return typeof(ServerApplicationPaths).Assembly;
  468. // Pismo
  469. yield return typeof(PismoIsoManager).Assembly;
  470. // Include composable parts in the running assembly
  471. yield return GetType().Assembly;
  472. }
  473. private readonly string _systemId = Environment.MachineName.GetMD5().ToString();
  474. /// <summary>
  475. /// Gets the system status.
  476. /// </summary>
  477. /// <returns>SystemInfo.</returns>
  478. public virtual SystemInfo GetSystemInfo()
  479. {
  480. return new SystemInfo
  481. {
  482. HasPendingRestart = HasPendingRestart,
  483. Version = ApplicationVersion.ToString(),
  484. IsNetworkDeployed = CanSelfUpdate,
  485. WebSocketPortNumber = ServerManager.WebSocketPortNumber,
  486. SupportsNativeWebSocket = ServerManager.SupportsNativeWebSocket,
  487. FailedPluginAssemblies = FailedAssemblies.ToArray(),
  488. InProgressInstallations = InstallationManager.CurrentInstallations.Select(i => i.Item1).ToArray(),
  489. CompletedInstallations = InstallationManager.CompletedInstallations.ToArray(),
  490. Id = _systemId,
  491. ProgramDataPath = ApplicationPaths.ProgramDataPath,
  492. MacAddress = GetMacAddress()
  493. };
  494. }
  495. /// <summary>
  496. /// Gets the mac address.
  497. /// </summary>
  498. /// <returns>System.String.</returns>
  499. private string GetMacAddress()
  500. {
  501. try
  502. {
  503. return NetworkManager.GetMacAddress();
  504. }
  505. catch (Exception ex)
  506. {
  507. Logger.ErrorException("Error getting mac address", ex);
  508. return null;
  509. }
  510. }
  511. /// <summary>
  512. /// Shuts down.
  513. /// </summary>
  514. public override void Shutdown()
  515. {
  516. App.Instance.Dispatcher.Invoke(App.Instance.Shutdown);
  517. }
  518. /// <summary>
  519. /// Registers the server with administrator access.
  520. /// </summary>
  521. private void RegisterServerWithAdministratorAccess()
  522. {
  523. Logger.Info("Requesting administrative access to authorize http server");
  524. // Create a temp file path to extract the bat file to
  525. var tmpFile = Path.Combine(ConfigurationManager.CommonApplicationPaths.TempDirectory, Guid.NewGuid() + ".bat");
  526. // Extract the bat file
  527. using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.ServerApplication.RegisterServer.bat"))
  528. {
  529. using (var fileStream = File.Create(tmpFile))
  530. {
  531. stream.CopyTo(fileStream);
  532. }
  533. }
  534. var startInfo = new ProcessStartInfo
  535. {
  536. FileName = tmpFile,
  537. Arguments = string.Format("{0} {1} {2} {3}", ServerConfigurationManager.Configuration.HttpServerPortNumber,
  538. HttpServerUrlPrefix,
  539. UdpServerPort,
  540. ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber),
  541. CreateNoWindow = true,
  542. WindowStyle = ProcessWindowStyle.Hidden,
  543. Verb = "runas",
  544. ErrorDialog = false
  545. };
  546. using (var process = Process.Start(startInfo))
  547. {
  548. process.WaitForExit();
  549. }
  550. }
  551. protected override string ApplicationUpdatePackageName
  552. {
  553. get { return Constants.MbServerPkgName; }
  554. }
  555. }
  556. }