ApplicationHost.cs 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. using MediaBrowser.Api;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Common.Events;
  5. using MediaBrowser.Common.Extensions;
  6. using Emby.Common.Implementations.ScheduledTasks;
  7. using MediaBrowser.Common.Net;
  8. using MediaBrowser.Common.Progress;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Controller.Channels;
  11. using MediaBrowser.Controller.Chapters;
  12. using MediaBrowser.Controller.Collections;
  13. using MediaBrowser.Controller.Configuration;
  14. using MediaBrowser.Controller.Connect;
  15. using MediaBrowser.Controller.Devices;
  16. using MediaBrowser.Controller.Dlna;
  17. using MediaBrowser.Controller.Drawing;
  18. using MediaBrowser.Controller.Dto;
  19. using MediaBrowser.Controller.Entities;
  20. using MediaBrowser.Controller.FileOrganization;
  21. using MediaBrowser.Controller.Library;
  22. using MediaBrowser.Controller.LiveTv;
  23. using MediaBrowser.Controller.MediaEncoding;
  24. using MediaBrowser.Controller.Net;
  25. using MediaBrowser.Controller.Notifications;
  26. using MediaBrowser.Controller.Persistence;
  27. using MediaBrowser.Controller.Playlists;
  28. using MediaBrowser.Controller.Plugins;
  29. using MediaBrowser.Controller.Providers;
  30. using MediaBrowser.Controller.Resolvers;
  31. using MediaBrowser.Controller.Security;
  32. using MediaBrowser.Controller.Session;
  33. using MediaBrowser.Controller.Sorting;
  34. using MediaBrowser.Controller.Subtitles;
  35. using MediaBrowser.Controller.Sync;
  36. using MediaBrowser.Controller.TV;
  37. using MediaBrowser.LocalMetadata.Savers;
  38. using MediaBrowser.MediaEncoding.BdInfo;
  39. using MediaBrowser.MediaEncoding.Encoder;
  40. using MediaBrowser.MediaEncoding.Subtitles;
  41. using MediaBrowser.Model.Logging;
  42. using MediaBrowser.Model.MediaInfo;
  43. using MediaBrowser.Model.System;
  44. using MediaBrowser.Model.Updates;
  45. using MediaBrowser.Providers.Chapters;
  46. using MediaBrowser.Providers.Manager;
  47. using MediaBrowser.Providers.Subtitles;
  48. using MediaBrowser.WebDashboard.Api;
  49. using MediaBrowser.XbmcMetadata.Providers;
  50. using System;
  51. using System.Collections.Concurrent;
  52. using System.Collections.Generic;
  53. using System.Globalization;
  54. using System.IO;
  55. using System.Linq;
  56. using System.Net;
  57. using System.Net.Sockets;
  58. using System.Reflection;
  59. using System.Security.Cryptography.X509Certificates;
  60. using System.Threading;
  61. using System.Threading.Tasks;
  62. using Emby.Common.Implementations;
  63. using Emby.Common.Implementations.Archiving;
  64. using Emby.Common.Implementations.Networking;
  65. using Emby.Common.Implementations.Reflection;
  66. using Emby.Common.Implementations.Serialization;
  67. using Emby.Common.Implementations.TextEncoding;
  68. using Emby.Common.Implementations.Xml;
  69. using Emby.Photos;
  70. using MediaBrowser.Model.IO;
  71. using MediaBrowser.Api.Playback;
  72. using MediaBrowser.Common.Plugins;
  73. using MediaBrowser.Common.Security;
  74. using MediaBrowser.Common.Updates;
  75. using MediaBrowser.Controller.Entities.Audio;
  76. using MediaBrowser.Controller.Entities.Movies;
  77. using MediaBrowser.Controller.Entities.TV;
  78. using Emby.Dlna;
  79. using Emby.Dlna.ConnectionManager;
  80. using Emby.Dlna.ContentDirectory;
  81. using Emby.Dlna.Main;
  82. using Emby.Dlna.MediaReceiverRegistrar;
  83. using Emby.Dlna.Ssdp;
  84. using Emby.Server.Core;
  85. using Emby.Server.Implementations.Activity;
  86. using Emby.Server.Implementations.Devices;
  87. using Emby.Server.Implementations.FFMpeg;
  88. using Emby.Server.Core.IO;
  89. using Emby.Server.Core.Localization;
  90. using Emby.Server.Implementations.Migrations;
  91. using Emby.Server.Implementations.Security;
  92. using Emby.Server.Implementations.Social;
  93. using Emby.Server.Implementations.Channels;
  94. using Emby.Server.Implementations.Collections;
  95. using Emby.Server.Implementations.Dto;
  96. using Emby.Server.Implementations.EntryPoints;
  97. using Emby.Server.Implementations.FileOrganization;
  98. using Emby.Server.Implementations.HttpServer;
  99. using Emby.Server.Implementations.HttpServer.Security;
  100. using Emby.Server.Implementations.Library;
  101. using Emby.Server.Implementations.LiveTv;
  102. using Emby.Server.Implementations.Localization;
  103. using Emby.Server.Implementations.MediaEncoder;
  104. using Emby.Server.Implementations.Notifications;
  105. using Emby.Server.Implementations.Data;
  106. using Emby.Server.Implementations.Playlists;
  107. using Emby.Server.Implementations;
  108. using Emby.Server.Implementations.ServerManager;
  109. using Emby.Server.Implementations.Session;
  110. using Emby.Server.Implementations.Social;
  111. using Emby.Server.Implementations.TV;
  112. using Emby.Server.Implementations.Updates;
  113. using MediaBrowser.Model.Activity;
  114. using MediaBrowser.Model.Configuration;
  115. using MediaBrowser.Model.Dlna;
  116. using MediaBrowser.Model.Globalization;
  117. using MediaBrowser.Model.Net;
  118. using MediaBrowser.Model.News;
  119. using MediaBrowser.Model.Reflection;
  120. using MediaBrowser.Model.Serialization;
  121. using MediaBrowser.Model.Services;
  122. using MediaBrowser.Model.Social;
  123. using MediaBrowser.Model.Text;
  124. using MediaBrowser.Model.Xml;
  125. using OpenSubtitlesHandler;
  126. using ServiceStack;
  127. using SocketHttpListener.Primitives;
  128. using StringExtensions = MediaBrowser.Controller.Extensions.StringExtensions;
  129. using Emby.Drawing;
  130. using Emby.Server.Implementations.Migrations;
  131. using MediaBrowser.Model.Diagnostics;
  132. using Emby.Common.Implementations.Diagnostics;
  133. using Emby.Server.Implementations.Configuration;
  134. namespace Emby.Server.Core
  135. {
  136. /// <summary>
  137. /// Class CompositionRoot
  138. /// </summary>
  139. public abstract class ApplicationHost : BaseApplicationHost<ServerApplicationPaths>, IServerApplicationHost, IDependencyContainer
  140. {
  141. /// <summary>
  142. /// Gets the server configuration manager.
  143. /// </summary>
  144. /// <value>The server configuration manager.</value>
  145. public IServerConfigurationManager ServerConfigurationManager
  146. {
  147. get { return (IServerConfigurationManager)ConfigurationManager; }
  148. }
  149. /// <summary>
  150. /// Gets the configuration manager.
  151. /// </summary>
  152. /// <returns>IConfigurationManager.</returns>
  153. protected override IConfigurationManager GetConfigurationManager()
  154. {
  155. return new ServerConfigurationManager(ApplicationPaths, LogManager, XmlSerializer, FileSystemManager);
  156. }
  157. /// <summary>
  158. /// Gets or sets the server manager.
  159. /// </summary>
  160. /// <value>The server manager.</value>
  161. private IServerManager ServerManager { get; set; }
  162. /// <summary>
  163. /// Gets or sets the user manager.
  164. /// </summary>
  165. /// <value>The user manager.</value>
  166. public IUserManager UserManager { get; set; }
  167. /// <summary>
  168. /// Gets or sets the library manager.
  169. /// </summary>
  170. /// <value>The library manager.</value>
  171. internal ILibraryManager LibraryManager { get; set; }
  172. /// <summary>
  173. /// Gets or sets the directory watchers.
  174. /// </summary>
  175. /// <value>The directory watchers.</value>
  176. private ILibraryMonitor LibraryMonitor { get; set; }
  177. /// <summary>
  178. /// Gets or sets the provider manager.
  179. /// </summary>
  180. /// <value>The provider manager.</value>
  181. private IProviderManager ProviderManager { get; set; }
  182. /// <summary>
  183. /// Gets or sets the HTTP server.
  184. /// </summary>
  185. /// <value>The HTTP server.</value>
  186. private IHttpServer HttpServer { get; set; }
  187. private IDtoService DtoService { get; set; }
  188. private IImageProcessor ImageProcessor { get; set; }
  189. /// <summary>
  190. /// Gets or sets the media encoder.
  191. /// </summary>
  192. /// <value>The media encoder.</value>
  193. private IMediaEncoder MediaEncoder { get; set; }
  194. private ISubtitleEncoder SubtitleEncoder { get; set; }
  195. private IConnectManager ConnectManager { get; set; }
  196. private ISessionManager SessionManager { get; set; }
  197. private ILiveTvManager LiveTvManager { get; set; }
  198. public ILocalizationManager LocalizationManager { get; set; }
  199. private IEncodingManager EncodingManager { get; set; }
  200. private IChannelManager ChannelManager { get; set; }
  201. private ISyncManager SyncManager { get; set; }
  202. /// <summary>
  203. /// Gets or sets the user data repository.
  204. /// </summary>
  205. /// <value>The user data repository.</value>
  206. private IUserDataManager UserDataManager { get; set; }
  207. private IUserRepository UserRepository { get; set; }
  208. internal IDisplayPreferencesRepository DisplayPreferencesRepository { get; set; }
  209. internal IItemRepository ItemRepository { get; set; }
  210. private INotificationsRepository NotificationsRepository { get; set; }
  211. private IFileOrganizationRepository FileOrganizationRepository { get; set; }
  212. private INotificationManager NotificationManager { get; set; }
  213. private ISubtitleManager SubtitleManager { get; set; }
  214. private IChapterManager ChapterManager { get; set; }
  215. private IDeviceManager DeviceManager { get; set; }
  216. internal IUserViewManager UserViewManager { get; set; }
  217. private IAuthenticationRepository AuthenticationRepository { get; set; }
  218. private ISyncRepository SyncRepository { get; set; }
  219. private ITVSeriesManager TVSeriesManager { get; set; }
  220. private ICollectionManager CollectionManager { get; set; }
  221. private IMediaSourceManager MediaSourceManager { get; set; }
  222. private IPlaylistManager PlaylistManager { get; set; }
  223. /// <summary>
  224. /// Gets or sets the installation manager.
  225. /// </summary>
  226. /// <value>The installation manager.</value>
  227. protected IInstallationManager InstallationManager { get; private set; }
  228. /// <summary>
  229. /// Gets the security manager.
  230. /// </summary>
  231. /// <value>The security manager.</value>
  232. protected ISecurityManager SecurityManager { get; private set; }
  233. /// <summary>
  234. /// Gets or sets the zip client.
  235. /// </summary>
  236. /// <value>The zip client.</value>
  237. protected IZipClient ZipClient { get; private set; }
  238. protected IAuthService AuthService { get; private set; }
  239. protected readonly StartupOptions StartupOptions;
  240. private readonly string _releaseAssetFilename;
  241. internal IPowerManagement PowerManagement { get; private set; }
  242. internal IImageEncoder ImageEncoder { get; private set; }
  243. private readonly Action<string, string> _certificateGenerator;
  244. private readonly Func<string> _defaultUserNameFactory;
  245. /// <summary>
  246. /// Initializes a new instance of the <see cref="ApplicationHost" /> class.
  247. /// </summary>
  248. public ApplicationHost(ServerApplicationPaths applicationPaths,
  249. ILogManager logManager,
  250. StartupOptions options,
  251. IFileSystem fileSystem,
  252. IPowerManagement powerManagement,
  253. string releaseAssetFilename,
  254. IEnvironmentInfo environmentInfo,
  255. IImageEncoder imageEncoder,
  256. ISystemEvents systemEvents,
  257. IMemoryStreamFactory memoryStreamFactory,
  258. INetworkManager networkManager,
  259. Action<string, string> certificateGenerator,
  260. Func<string> defaultUsernameFactory)
  261. : base(applicationPaths,
  262. logManager,
  263. fileSystem,
  264. environmentInfo,
  265. systemEvents,
  266. memoryStreamFactory,
  267. networkManager)
  268. {
  269. StartupOptions = options;
  270. _certificateGenerator = certificateGenerator;
  271. _releaseAssetFilename = releaseAssetFilename;
  272. _defaultUserNameFactory = defaultUsernameFactory;
  273. PowerManagement = powerManagement;
  274. ImageEncoder = imageEncoder;
  275. SetBaseExceptionMessage();
  276. }
  277. private Version _version;
  278. /// <summary>
  279. /// Gets the current application version
  280. /// </summary>
  281. /// <value>The application version.</value>
  282. public override Version ApplicationVersion
  283. {
  284. get
  285. {
  286. return _version ?? (_version = GetAssembly(GetType()).GetName().Version);
  287. }
  288. }
  289. public virtual bool SupportsRunningAsService
  290. {
  291. get
  292. {
  293. return false;
  294. }
  295. }
  296. /// <summary>
  297. /// Gets the name.
  298. /// </summary>
  299. /// <value>The name.</value>
  300. public override string Name
  301. {
  302. get
  303. {
  304. return "Emby Server";
  305. }
  306. }
  307. public virtual bool IsRunningAsService
  308. {
  309. get
  310. {
  311. return false;
  312. }
  313. }
  314. private Assembly GetAssembly(Type type)
  315. {
  316. return type.GetTypeInfo().Assembly;
  317. }
  318. public virtual bool SupportsAutoRunAtStartup
  319. {
  320. get
  321. {
  322. return EnvironmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows;
  323. }
  324. }
  325. private void SetBaseExceptionMessage()
  326. {
  327. var builder = GetBaseExceptionMessage(ApplicationPaths);
  328. // Skip if plugins haven't been loaded yet
  329. //if (Plugins != null)
  330. //{
  331. // var pluginString = string.Join("|", Plugins.Select(i => i.Name + "-" + i.Version.ToString()).ToArray());
  332. // builder.Insert(0, string.Format("Plugins: {0}{1}", pluginString, Environment.NewLine));
  333. //}
  334. builder.Insert(0, string.Format("Version: {0}{1}", ApplicationVersion, Environment.NewLine));
  335. builder.Insert(0, "*** Error Report ***" + Environment.NewLine);
  336. LogManager.ExceptionMessagePrefix = builder.ToString();
  337. }
  338. /// <summary>
  339. /// Runs the startup tasks.
  340. /// </summary>
  341. public override async Task RunStartupTasks()
  342. {
  343. await PerformPreInitMigrations().ConfigureAwait(false);
  344. await base.RunStartupTasks().ConfigureAwait(false);
  345. await MediaEncoder.Init().ConfigureAwait(false);
  346. if (string.IsNullOrWhiteSpace(MediaEncoder.EncoderPath))
  347. {
  348. if (ServerConfigurationManager.Configuration.IsStartupWizardCompleted)
  349. {
  350. ServerConfigurationManager.Configuration.IsStartupWizardCompleted = false;
  351. ServerConfigurationManager.SaveConfiguration();
  352. }
  353. }
  354. Logger.Info("ServerId: {0}", SystemId);
  355. Logger.Info("Core startup complete");
  356. HttpServer.GlobalResponse = null;
  357. PerformPostInitMigrations();
  358. Logger.Info("Post-init migrations complete");
  359. foreach (var entryPoint in GetExports<IServerEntryPoint>().ToList())
  360. {
  361. var name = entryPoint.GetType().FullName;
  362. Logger.Info("Starting entry point {0}", name);
  363. var now = DateTime.UtcNow;
  364. try
  365. {
  366. entryPoint.Run();
  367. }
  368. catch (Exception ex)
  369. {
  370. Logger.ErrorException("Error in {0}", ex, name);
  371. }
  372. Logger.Info("Entry point completed: {0}. Duration: {1} seconds", name, (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
  373. }
  374. Logger.Info("All entry points have started");
  375. LogManager.RemoveConsoleOutput();
  376. }
  377. protected override IJsonSerializer CreateJsonSerializer()
  378. {
  379. try
  380. {
  381. // https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.IntegrationTests/Web.config#L4
  382. Licensing.RegisterLicense("1001-e1JlZjoxMDAxLE5hbWU6VGVzdCBCdXNpbmVzcyxUeXBlOkJ1c2luZXNzLEhhc2g6UHVNTVRPclhvT2ZIbjQ5MG5LZE1mUTd5RUMzQnBucTFEbTE3TDczVEF4QUNMT1FhNXJMOWkzVjFGL2ZkVTE3Q2pDNENqTkQyUktRWmhvUVBhYTBiekJGUUZ3ZE5aZHFDYm9hL3lydGlwUHI5K1JsaTBYbzNsUC85cjVJNHE5QVhldDN6QkE4aTlvdldrdTgyTk1relY2eis2dFFqTThYN2lmc0JveHgycFdjPSxFeHBpcnk6MjAxMy0wMS0wMX0=");
  383. }
  384. catch
  385. {
  386. // Failing under mono
  387. }
  388. var result = new JsonSerializer(FileSystemManager, LogManager.GetLogger("JsonSerializer"));
  389. ServiceStack.Text.JsConfig<LiveTvProgram>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  390. ServiceStack.Text.JsConfig<LiveTvChannel>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  391. ServiceStack.Text.JsConfig<LiveTvVideoRecording>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  392. ServiceStack.Text.JsConfig<LiveTvAudioRecording>.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ChannelMediaSources", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  393. ServiceStack.Text.JsConfig<Series>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  394. ServiceStack.Text.JsConfig<Audio>.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ChannelMediaSources", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  395. ServiceStack.Text.JsConfig<MusicAlbum>.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  396. ServiceStack.Text.JsConfig<MusicArtist>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  397. ServiceStack.Text.JsConfig<MusicGenre>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  398. ServiceStack.Text.JsConfig<MusicVideo>.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  399. ServiceStack.Text.JsConfig<Movie>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  400. ServiceStack.Text.JsConfig<Playlist>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  401. ServiceStack.Text.JsConfig<AudioPodcast>.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ChannelMediaSources", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  402. ServiceStack.Text.JsConfig<AudioBook>.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ChannelMediaSources", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  403. ServiceStack.Text.JsConfig<Trailer>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  404. ServiceStack.Text.JsConfig<BoxSet>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  405. ServiceStack.Text.JsConfig<Episode>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  406. ServiceStack.Text.JsConfig<Season>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  407. ServiceStack.Text.JsConfig<Book>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  408. ServiceStack.Text.JsConfig<CollectionFolder>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  409. ServiceStack.Text.JsConfig<Folder>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  410. ServiceStack.Text.JsConfig<Game>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  411. ServiceStack.Text.JsConfig<GameGenre>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  412. ServiceStack.Text.JsConfig<GameSystem>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  413. ServiceStack.Text.JsConfig<Genre>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  414. ServiceStack.Text.JsConfig<Person>.ExcludePropertyNames = new[] { "PlaceOfBirth", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  415. ServiceStack.Text.JsConfig<Photo>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  416. ServiceStack.Text.JsConfig<PhotoAlbum>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  417. ServiceStack.Text.JsConfig<Studio>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  418. ServiceStack.Text.JsConfig<UserRootFolder>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  419. ServiceStack.Text.JsConfig<UserView>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  420. ServiceStack.Text.JsConfig<Video>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  421. ServiceStack.Text.JsConfig<Year>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  422. ServiceStack.Text.JsConfig<Channel>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  423. ServiceStack.Text.JsConfig<AggregateFolder>.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "Taglines", "Keywords", "ExtraType" };
  424. return result;
  425. }
  426. public override Task Init(IProgress<double> progress)
  427. {
  428. HttpPort = ServerConfigurationManager.Configuration.HttpServerPortNumber;
  429. HttpsPort = ServerConfigurationManager.Configuration.HttpsPortNumber;
  430. // Safeguard against invalid configuration
  431. if (HttpPort == HttpsPort)
  432. {
  433. HttpPort = ServerConfiguration.DefaultHttpPort;
  434. HttpsPort = ServerConfiguration.DefaultHttpsPort;
  435. }
  436. return base.Init(progress);
  437. }
  438. private async Task PerformPreInitMigrations()
  439. {
  440. var migrations = new List<IVersionMigration>
  441. {
  442. new UpdateLevelMigration(ServerConfigurationManager, this, HttpClient, JsonSerializer, _releaseAssetFilename, Logger)
  443. };
  444. foreach (var task in migrations)
  445. {
  446. try
  447. {
  448. await task.Run().ConfigureAwait(false);
  449. }
  450. catch (Exception ex)
  451. {
  452. Logger.ErrorException("Error running migration", ex);
  453. }
  454. }
  455. }
  456. private void PerformPostInitMigrations()
  457. {
  458. var migrations = new List<IVersionMigration>
  459. {
  460. new LibraryScanMigration(ServerConfigurationManager, TaskManager),
  461. new GuideMigration(ServerConfigurationManager, TaskManager)
  462. };
  463. foreach (var task in migrations)
  464. {
  465. try
  466. {
  467. task.Run();
  468. }
  469. catch (Exception ex)
  470. {
  471. Logger.ErrorException("Error running migration", ex);
  472. }
  473. }
  474. }
  475. protected abstract IConnectManager CreateConnectManager();
  476. protected abstract ISyncManager CreateSyncManager();
  477. /// <summary>
  478. /// Registers resources that classes will depend on
  479. /// </summary>
  480. protected override async Task RegisterResources(IProgress<double> progress)
  481. {
  482. await base.RegisterResources(progress).ConfigureAwait(false);
  483. RegisterSingleInstance(PowerManagement);
  484. SecurityManager = new PluginSecurityManager(this, HttpClient, JsonSerializer, ApplicationPaths, LogManager, FileSystemManager, CryptographyProvider);
  485. RegisterSingleInstance(SecurityManager);
  486. InstallationManager = new InstallationManager(LogManager.GetLogger("InstallationManager"), this, ApplicationPaths, HttpClient, JsonSerializer, SecurityManager, ConfigurationManager, FileSystemManager, CryptographyProvider);
  487. RegisterSingleInstance(InstallationManager);
  488. ZipClient = new ZipClient(FileSystemManager);
  489. RegisterSingleInstance(ZipClient);
  490. RegisterSingleInstance<IHttpResultFactory>(new HttpResultFactory(LogManager, FileSystemManager, JsonSerializer, MemoryStreamFactory));
  491. RegisterSingleInstance<IServerApplicationHost>(this);
  492. RegisterSingleInstance<IServerApplicationPaths>(ApplicationPaths);
  493. RegisterSingleInstance(ServerConfigurationManager);
  494. IAssemblyInfo assemblyInfo = new AssemblyInfo();
  495. RegisterSingleInstance<IAssemblyInfo>(assemblyInfo);
  496. LocalizationManager = new LocalizationManager(ServerConfigurationManager, FileSystemManager, JsonSerializer, LogManager.GetLogger("LocalizationManager"), assemblyInfo, new TextLocalizer());
  497. StringExtensions.LocalizationManager = LocalizationManager;
  498. RegisterSingleInstance(LocalizationManager);
  499. ITextEncoding textEncoding = new TextEncoding(FileSystemManager);
  500. RegisterSingleInstance(textEncoding);
  501. Utilities.EncodingHelper = textEncoding;
  502. RegisterSingleInstance<IBlurayExaminer>(() => new BdInfoExaminer(FileSystemManager, textEncoding));
  503. RegisterSingleInstance<IXmlReaderSettingsFactory>(new XmlReaderSettingsFactory());
  504. UserDataManager = new UserDataManager(LogManager, ServerConfigurationManager);
  505. RegisterSingleInstance(UserDataManager);
  506. UserRepository = GetUserRepository();
  507. // This is only needed for disposal purposes. If removing this, make sure to have the manager handle disposing it
  508. RegisterSingleInstance(UserRepository);
  509. var displayPreferencesRepo = new SqliteDisplayPreferencesRepository(LogManager.GetLogger("SqliteDisplayPreferencesRepository"), JsonSerializer, ApplicationPaths, MemoryStreamFactory);
  510. DisplayPreferencesRepository = displayPreferencesRepo;
  511. RegisterSingleInstance(DisplayPreferencesRepository);
  512. var itemRepo = new SqliteItemRepository(ServerConfigurationManager, JsonSerializer, LogManager.GetLogger("SqliteItemRepository"), MemoryStreamFactory, assemblyInfo, FileSystemManager, EnvironmentInfo, TimerFactory);
  513. ItemRepository = itemRepo;
  514. RegisterSingleInstance(ItemRepository);
  515. FileOrganizationRepository = GetFileOrganizationRepository();
  516. RegisterSingleInstance(FileOrganizationRepository);
  517. AuthenticationRepository = await GetAuthenticationRepository().ConfigureAwait(false);
  518. RegisterSingleInstance(AuthenticationRepository);
  519. UserManager = new UserManager(LogManager.GetLogger("UserManager"), ServerConfigurationManager, UserRepository, XmlSerializer, NetworkManager, () => ImageProcessor, () => DtoService, () => ConnectManager, this, JsonSerializer, FileSystemManager, CryptographyProvider, _defaultUserNameFactory());
  520. RegisterSingleInstance(UserManager);
  521. LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataManager, () => LibraryMonitor, FileSystemManager, () => ProviderManager, () => UserViewManager);
  522. RegisterSingleInstance(LibraryManager);
  523. var musicManager = new MusicManager(LibraryManager);
  524. RegisterSingleInstance<IMusicManager>(new MusicManager(LibraryManager));
  525. LibraryMonitor = new LibraryMonitor(LogManager, TaskManager, LibraryManager, ServerConfigurationManager, FileSystemManager, TimerFactory, SystemEvents, EnvironmentInfo);
  526. RegisterSingleInstance(LibraryMonitor);
  527. ProviderManager = new ProviderManager(HttpClient, ServerConfigurationManager, LibraryMonitor, LogManager, FileSystemManager, ApplicationPaths, () => LibraryManager, JsonSerializer, MemoryStreamFactory);
  528. RegisterSingleInstance(ProviderManager);
  529. RegisterSingleInstance<ISearchEngine>(() => new SearchEngine(LogManager, LibraryManager, UserManager));
  530. CertificatePath = GetCertificatePath(true);
  531. Certificate = GetCertificate(CertificatePath);
  532. HttpServer = HttpServerFactory.CreateServer(this, LogManager, ServerConfigurationManager, NetworkManager, MemoryStreamFactory, "Emby", "web/index.html", textEncoding, SocketFactory, CryptographyProvider, JsonSerializer, XmlSerializer, EnvironmentInfo, Certificate, SupportsDualModeSockets);
  533. HttpServer.GlobalResponse = LocalizationManager.GetLocalizedString("StartupEmbyServerIsLoading");
  534. RegisterSingleInstance(HttpServer, false);
  535. progress.Report(10);
  536. ServerManager = new ServerManager(this, JsonSerializer, LogManager.GetLogger("ServerManager"), ServerConfigurationManager, MemoryStreamFactory, textEncoding);
  537. RegisterSingleInstance(ServerManager);
  538. var innerProgress = new ActionableProgress<double>();
  539. innerProgress.RegisterAction(p => progress.Report((.75 * p) + 15));
  540. ImageProcessor = GetImageProcessor();
  541. RegisterSingleInstance(ImageProcessor);
  542. TVSeriesManager = new TVSeriesManager(UserManager, UserDataManager, LibraryManager, ServerConfigurationManager);
  543. RegisterSingleInstance(TVSeriesManager);
  544. SyncManager = CreateSyncManager();
  545. RegisterSingleInstance(SyncManager);
  546. DtoService = new DtoService(LogManager.GetLogger("DtoService"), LibraryManager, UserDataManager, ItemRepository, ImageProcessor, ServerConfigurationManager, FileSystemManager, ProviderManager, () => ChannelManager, SyncManager, this, () => DeviceManager, () => MediaSourceManager, () => LiveTvManager);
  547. RegisterSingleInstance(DtoService);
  548. var encryptionManager = new EncryptionManager();
  549. RegisterSingleInstance<IEncryptionManager>(encryptionManager);
  550. ConnectManager = CreateConnectManager();
  551. RegisterSingleInstance(ConnectManager);
  552. DeviceManager = new DeviceManager(new DeviceRepository(ApplicationPaths, JsonSerializer, LogManager.GetLogger("DeviceManager"), FileSystemManager), UserManager, FileSystemManager, LibraryMonitor, ServerConfigurationManager, LogManager.GetLogger("DeviceManager"), NetworkManager);
  553. RegisterSingleInstance(DeviceManager);
  554. var newsService = new Emby.Server.Implementations.News.NewsService(ApplicationPaths, JsonSerializer);
  555. RegisterSingleInstance<INewsService>(newsService);
  556. var fileOrganizationService = new FileOrganizationService(TaskManager, FileOrganizationRepository, LogManager.GetLogger("FileOrganizationService"), LibraryMonitor, LibraryManager, ServerConfigurationManager, FileSystemManager, ProviderManager);
  557. RegisterSingleInstance<IFileOrganizationService>(fileOrganizationService);
  558. progress.Report(15);
  559. ChannelManager = new ChannelManager(UserManager, DtoService, LibraryManager, LogManager.GetLogger("ChannelManager"), ServerConfigurationManager, FileSystemManager, UserDataManager, JsonSerializer, LocalizationManager, HttpClient, ProviderManager);
  560. RegisterSingleInstance(ChannelManager);
  561. MediaSourceManager = new MediaSourceManager(ItemRepository, UserManager, LibraryManager, LogManager.GetLogger("MediaSourceManager"), JsonSerializer, FileSystemManager, UserDataManager, TimerFactory);
  562. RegisterSingleInstance(MediaSourceManager);
  563. SessionManager = new SessionManager(UserDataManager, LogManager.GetLogger("SessionManager"), LibraryManager, UserManager, musicManager, DtoService, ImageProcessor, JsonSerializer, this, HttpClient, AuthenticationRepository, DeviceManager, MediaSourceManager, TimerFactory);
  564. RegisterSingleInstance(SessionManager);
  565. var dlnaManager = new DlnaManager(XmlSerializer, FileSystemManager, ApplicationPaths, LogManager.GetLogger("Dlna"), JsonSerializer, this, assemblyInfo);
  566. RegisterSingleInstance<IDlnaManager>(dlnaManager);
  567. var connectionManager = new ConnectionManager(dlnaManager, ServerConfigurationManager, LogManager.GetLogger("UpnpConnectionManager"), HttpClient, new XmlReaderSettingsFactory());
  568. RegisterSingleInstance<IConnectionManager>(connectionManager);
  569. CollectionManager = new CollectionManager(LibraryManager, FileSystemManager, LibraryMonitor, LogManager.GetLogger("CollectionManager"), ProviderManager);
  570. RegisterSingleInstance(CollectionManager);
  571. PlaylistManager = new PlaylistManager(LibraryManager, FileSystemManager, LibraryMonitor, LogManager.GetLogger("PlaylistManager"), UserManager, ProviderManager);
  572. RegisterSingleInstance<IPlaylistManager>(PlaylistManager);
  573. LiveTvManager = new LiveTvManager(this, ServerConfigurationManager, Logger, ItemRepository, ImageProcessor, UserDataManager, DtoService, UserManager, LibraryManager, TaskManager, LocalizationManager, JsonSerializer, ProviderManager, FileSystemManager, SecurityManager);
  574. RegisterSingleInstance(LiveTvManager);
  575. UserViewManager = new UserViewManager(LibraryManager, LocalizationManager, UserManager, ChannelManager, LiveTvManager, ServerConfigurationManager);
  576. RegisterSingleInstance(UserViewManager);
  577. var contentDirectory = new ContentDirectory(dlnaManager, UserDataManager, ImageProcessor, LibraryManager, ServerConfigurationManager, UserManager, LogManager.GetLogger("UpnpContentDirectory"), HttpClient, LocalizationManager, ChannelManager, MediaSourceManager, UserViewManager, () => MediaEncoder, new XmlReaderSettingsFactory());
  578. RegisterSingleInstance<IContentDirectory>(contentDirectory);
  579. var mediaRegistrar = new MediaReceiverRegistrar(LogManager.GetLogger("MediaReceiverRegistrar"), HttpClient, ServerConfigurationManager, new XmlReaderSettingsFactory());
  580. RegisterSingleInstance<IMediaReceiverRegistrar>(mediaRegistrar);
  581. NotificationManager = new NotificationManager(LogManager, UserManager, ServerConfigurationManager);
  582. RegisterSingleInstance(NotificationManager);
  583. SubtitleManager = new SubtitleManager(LogManager.GetLogger("SubtitleManager"), FileSystemManager, LibraryMonitor, LibraryManager, MediaSourceManager);
  584. RegisterSingleInstance(SubtitleManager);
  585. RegisterSingleInstance<IDeviceDiscovery>(new DeviceDiscovery(LogManager.GetLogger("IDeviceDiscovery"), ServerConfigurationManager, SocketFactory, TimerFactory));
  586. ChapterManager = new ChapterManager(LibraryManager, LogManager.GetLogger("ChapterManager"), ServerConfigurationManager, ItemRepository);
  587. RegisterSingleInstance(ChapterManager);
  588. await RegisterMediaEncoder(innerProgress).ConfigureAwait(false);
  589. progress.Report(90);
  590. EncodingManager = new EncodingManager(FileSystemManager, Logger, MediaEncoder, ChapterManager, LibraryManager);
  591. RegisterSingleInstance(EncodingManager);
  592. var sharingRepo = new SharingRepository(LogManager.GetLogger("SharingRepository"), ApplicationPaths);
  593. sharingRepo.Initialize();
  594. // This is only needed for disposal purposes. If removing this, make sure to have the manager handle disposing it
  595. RegisterSingleInstance<ISharingRepository>(sharingRepo);
  596. RegisterSingleInstance<ISharingManager>(new SharingManager(sharingRepo, ServerConfigurationManager, LibraryManager, this));
  597. var activityLogRepo = GetActivityLogRepository();
  598. RegisterSingleInstance(activityLogRepo);
  599. RegisterSingleInstance<IActivityManager>(new ActivityManager(LogManager.GetLogger("ActivityManager"), activityLogRepo, UserManager));
  600. var authContext = new AuthorizationContext(AuthenticationRepository, ConnectManager);
  601. RegisterSingleInstance<IAuthorizationContext>(authContext);
  602. RegisterSingleInstance<ISessionContext>(new SessionContext(UserManager, authContext, SessionManager));
  603. AuthService = new AuthService(UserManager, authContext, ServerConfigurationManager, ConnectManager, SessionManager, DeviceManager);
  604. RegisterSingleInstance<IAuthService>(AuthService);
  605. SubtitleEncoder = new SubtitleEncoder(LibraryManager, LogManager.GetLogger("SubtitleEncoder"), ApplicationPaths, FileSystemManager, MediaEncoder, JsonSerializer, HttpClient, MediaSourceManager, MemoryStreamFactory, ProcessFactory, textEncoding);
  606. RegisterSingleInstance(SubtitleEncoder);
  607. displayPreferencesRepo.Initialize();
  608. var userDataRepo = new SqliteUserDataRepository(LogManager.GetLogger("SqliteUserDataRepository"), ApplicationPaths, FileSystemManager);
  609. ((UserDataManager)UserDataManager).Repository = userDataRepo;
  610. itemRepo.Initialize(userDataRepo);
  611. ((LibraryManager)LibraryManager).ItemRepository = ItemRepository;
  612. ConfigureNotificationsRepository();
  613. progress.Report(100);
  614. SetStaticProperties();
  615. await ((UserManager)UserManager).Initialize().ConfigureAwait(false);
  616. }
  617. protected virtual bool SupportsDualModeSockets
  618. {
  619. get
  620. {
  621. return true;
  622. }
  623. }
  624. private ICertificate GetCertificate(string certificateLocation)
  625. {
  626. if (string.IsNullOrWhiteSpace(certificateLocation))
  627. {
  628. return null;
  629. }
  630. try
  631. {
  632. if (!FileSystemManager.FileExists(certificateLocation))
  633. {
  634. return null;
  635. }
  636. X509Certificate2 localCert = new X509Certificate2(certificateLocation);
  637. //localCert.PrivateKey = PrivateKey.CreateFromFile(pvk_file).RSA;
  638. if (!localCert.HasPrivateKey)
  639. {
  640. //throw new FileNotFoundException("Secure requested, no private key included", certificateLocation);
  641. return null;
  642. }
  643. return new Certificate(localCert);
  644. }
  645. catch (Exception ex)
  646. {
  647. Logger.ErrorException("Error loading cert from {0}", ex, certificateLocation);
  648. return null;
  649. }
  650. }
  651. private IImageProcessor GetImageProcessor()
  652. {
  653. var maxConcurrentImageProcesses = Math.Max(Environment.ProcessorCount, 4);
  654. if (StartupOptions.ContainsOption("-imagethreads"))
  655. {
  656. int.TryParse(StartupOptions.GetOption("-imagethreads"), NumberStyles.Any, CultureInfo.InvariantCulture, out maxConcurrentImageProcesses);
  657. }
  658. return new ImageProcessor(LogManager.GetLogger("ImageProcessor"), ServerConfigurationManager.ApplicationPaths, FileSystemManager, JsonSerializer, ImageEncoder, maxConcurrentImageProcesses, () => LibraryManager, TimerFactory);
  659. }
  660. protected virtual FFMpegInstallInfo GetFfmpegInstallInfo()
  661. {
  662. var info = new FFMpegInstallInfo();
  663. // Windows builds: http://ffmpeg.zeranoe.com/builds/
  664. // Linux builds: http://johnvansickle.com/ffmpeg/
  665. // OS X builds: http://ffmpegmac.net/
  666. // OS X x64: http://www.evermeet.cx/ffmpeg/
  667. if (EnvironmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Linux)
  668. {
  669. info.FFMpegFilename = "ffmpeg";
  670. info.FFProbeFilename = "ffprobe";
  671. info.ArchiveType = "7z";
  672. info.Version = "20160215";
  673. info.DownloadUrls = GetLinuxDownloadUrls();
  674. }
  675. else if (EnvironmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows)
  676. {
  677. info.FFMpegFilename = "ffmpeg.exe";
  678. info.FFProbeFilename = "ffprobe.exe";
  679. info.Version = "20160410";
  680. info.ArchiveType = "7z";
  681. info.DownloadUrls = GetWindowsDownloadUrls();
  682. }
  683. else
  684. {
  685. // No version available - user requirement
  686. info.DownloadUrls = new string[] { };
  687. }
  688. return info;
  689. }
  690. private string[] GetWindowsDownloadUrls()
  691. {
  692. switch (EnvironmentInfo.SystemArchitecture)
  693. {
  694. case Architecture.X64:
  695. return new[]
  696. {
  697. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/windows/ffmpeg-20160410-win64.7z"
  698. };
  699. case Architecture.X86:
  700. return new[]
  701. {
  702. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/windows/ffmpeg-20160410-win32.7z"
  703. };
  704. }
  705. return new string[] { };
  706. }
  707. private string[] GetLinuxDownloadUrls()
  708. {
  709. switch (EnvironmentInfo.SystemArchitecture)
  710. {
  711. case Architecture.X64:
  712. return new[]
  713. {
  714. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/linux/ffmpeg-git-20160215-64bit-static.7z"
  715. };
  716. case Architecture.X86:
  717. return new[]
  718. {
  719. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/linux/ffmpeg-git-20160215-32bit-static.7z"
  720. };
  721. }
  722. return new string[] { };
  723. }
  724. /// <summary>
  725. /// Registers the media encoder.
  726. /// </summary>
  727. /// <returns>Task.</returns>
  728. private async Task RegisterMediaEncoder(IProgress<double> progress)
  729. {
  730. string encoderPath = null;
  731. string probePath = null;
  732. var info = await new FFMpegLoader(Logger, ApplicationPaths, HttpClient, ZipClient, FileSystemManager, GetFfmpegInstallInfo())
  733. .GetFFMpegInfo(StartupOptions, progress).ConfigureAwait(false);
  734. encoderPath = info.EncoderPath;
  735. probePath = info.ProbePath;
  736. var hasExternalEncoder = string.Equals(info.Version, "external", StringComparison.OrdinalIgnoreCase);
  737. var mediaEncoder = new MediaEncoder(LogManager.GetLogger("MediaEncoder"),
  738. JsonSerializer,
  739. encoderPath,
  740. probePath,
  741. hasExternalEncoder,
  742. ServerConfigurationManager,
  743. FileSystemManager,
  744. LiveTvManager,
  745. IsoManager,
  746. LibraryManager,
  747. ChannelManager,
  748. SessionManager,
  749. () => SubtitleEncoder,
  750. () => MediaSourceManager,
  751. HttpClient,
  752. ZipClient,
  753. MemoryStreamFactory,
  754. ProcessFactory,
  755. (Environment.ProcessorCount > 2 ? 14000 : 40000),
  756. EnvironmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows,
  757. EnvironmentInfo);
  758. MediaEncoder = mediaEncoder;
  759. RegisterSingleInstance(MediaEncoder);
  760. }
  761. /// <summary>
  762. /// Gets the user repository.
  763. /// </summary>
  764. /// <returns>Task{IUserRepository}.</returns>
  765. private IUserRepository GetUserRepository()
  766. {
  767. var repo = new SqliteUserRepository(LogManager.GetLogger("SqliteUserRepository"), ApplicationPaths, JsonSerializer, MemoryStreamFactory);
  768. repo.Initialize();
  769. return repo;
  770. }
  771. /// <summary>
  772. /// Gets the file organization repository.
  773. /// </summary>
  774. /// <returns>Task{IUserRepository}.</returns>
  775. private IFileOrganizationRepository GetFileOrganizationRepository()
  776. {
  777. var repo = new SqliteFileOrganizationRepository(LogManager.GetLogger("SqliteFileOrganizationRepository"), ServerConfigurationManager.ApplicationPaths);
  778. repo.Initialize();
  779. return repo;
  780. }
  781. private async Task<IAuthenticationRepository> GetAuthenticationRepository()
  782. {
  783. var repo = new AuthenticationRepository(LogManager.GetLogger("AuthenticationRepository"), ServerConfigurationManager.ApplicationPaths);
  784. repo.Initialize();
  785. return repo;
  786. }
  787. private IActivityRepository GetActivityLogRepository()
  788. {
  789. var repo = new ActivityRepository(LogManager.GetLogger("ActivityRepository"), ServerConfigurationManager.ApplicationPaths);
  790. repo.Initialize();
  791. return repo;
  792. }
  793. /// <summary>
  794. /// Configures the repositories.
  795. /// </summary>
  796. private void ConfigureNotificationsRepository()
  797. {
  798. var repo = new SqliteNotificationsRepository(LogManager.GetLogger("SqliteNotificationsRepository"), ServerConfigurationManager.ApplicationPaths, FileSystemManager);
  799. repo.Initialize();
  800. NotificationsRepository = repo;
  801. RegisterSingleInstance(NotificationsRepository);
  802. }
  803. /// <summary>
  804. /// Dirty hacks
  805. /// </summary>
  806. private void SetStaticProperties()
  807. {
  808. // For now there's no real way to inject these properly
  809. BaseItem.Logger = LogManager.GetLogger("BaseItem");
  810. BaseItem.ConfigurationManager = ServerConfigurationManager;
  811. BaseItem.LibraryManager = LibraryManager;
  812. BaseItem.ProviderManager = ProviderManager;
  813. BaseItem.LocalizationManager = LocalizationManager;
  814. BaseItem.ItemRepository = ItemRepository;
  815. User.XmlSerializer = XmlSerializer;
  816. User.UserManager = UserManager;
  817. Folder.UserManager = UserManager;
  818. BaseItem.FileSystem = FileSystemManager;
  819. BaseItem.UserDataManager = UserDataManager;
  820. BaseItem.ChannelManager = ChannelManager;
  821. BaseItem.LiveTvManager = LiveTvManager;
  822. Folder.UserViewManager = UserViewManager;
  823. UserView.TVSeriesManager = TVSeriesManager;
  824. UserView.PlaylistManager = PlaylistManager;
  825. BaseItem.CollectionManager = CollectionManager;
  826. BaseItem.MediaSourceManager = MediaSourceManager;
  827. CollectionFolder.XmlSerializer = XmlSerializer;
  828. BaseStreamingService.AppHost = this;
  829. BaseStreamingService.HttpClient = HttpClient;
  830. Utilities.CryptographyProvider = CryptographyProvider;
  831. AuthenticatedAttribute.AuthService = AuthService;
  832. }
  833. /// <summary>
  834. /// Finds the parts.
  835. /// </summary>
  836. protected override void FindParts()
  837. {
  838. if (!ServerConfigurationManager.Configuration.IsPortAuthorized)
  839. {
  840. RegisterServerWithAdministratorAccess();
  841. ServerConfigurationManager.Configuration.IsPortAuthorized = true;
  842. ConfigurationManager.SaveConfiguration();
  843. }
  844. RegisterModules();
  845. base.FindParts();
  846. HttpServer.Init(GetExports<IService>(false));
  847. ServerManager.AddWebSocketListeners(GetExports<IWebSocketListener>(false));
  848. StartServer();
  849. LibraryManager.AddParts(GetExports<IResolverIgnoreRule>(),
  850. GetExports<IVirtualFolderCreator>(),
  851. GetExports<IItemResolver>(),
  852. GetExports<IIntroProvider>(),
  853. GetExports<IBaseItemComparer>(),
  854. GetExports<ILibraryPostScanTask>());
  855. ProviderManager.AddParts(GetExports<IImageProvider>(),
  856. GetExports<IMetadataService>(),
  857. GetExports<IMetadataProvider>(),
  858. GetExports<IMetadataSaver>(),
  859. GetExports<IExternalId>());
  860. ImageProcessor.AddParts(GetExports<IImageEnhancer>());
  861. LiveTvManager.AddParts(GetExports<ILiveTvService>(), GetExports<ITunerHost>(), GetExports<IListingsProvider>());
  862. SubtitleManager.AddParts(GetExports<ISubtitleProvider>());
  863. SessionManager.AddParts(GetExports<ISessionControllerFactory>());
  864. ChannelManager.AddParts(GetExports<IChannel>());
  865. MediaSourceManager.AddParts(GetExports<IMediaSourceProvider>());
  866. NotificationManager.AddParts(GetExports<INotificationService>(), GetExports<INotificationTypeFactory>());
  867. SyncManager.AddParts(GetExports<ISyncProvider>());
  868. }
  869. private string CertificatePath { get; set; }
  870. private ICertificate Certificate { get; set; }
  871. private IEnumerable<string> GetUrlPrefixes()
  872. {
  873. var hosts = new List<string>();
  874. hosts.Add("+");
  875. return hosts.SelectMany(i =>
  876. {
  877. var prefixes = new List<string>
  878. {
  879. "http://"+i+":" + HttpPort + "/"
  880. };
  881. if (!string.IsNullOrWhiteSpace(CertificatePath))
  882. {
  883. prefixes.Add("https://" + i + ":" + HttpsPort + "/");
  884. }
  885. return prefixes;
  886. });
  887. }
  888. /// <summary>
  889. /// Starts the server.
  890. /// </summary>
  891. private void StartServer()
  892. {
  893. try
  894. {
  895. ServerManager.Start(GetUrlPrefixes());
  896. return;
  897. }
  898. catch (Exception ex)
  899. {
  900. Logger.ErrorException("Error starting http server", ex);
  901. if (HttpPort == ServerConfiguration.DefaultHttpPort)
  902. {
  903. throw;
  904. }
  905. }
  906. HttpPort = ServerConfiguration.DefaultHttpPort;
  907. try
  908. {
  909. ServerManager.Start(GetUrlPrefixes());
  910. }
  911. catch (Exception ex)
  912. {
  913. Logger.ErrorException("Error starting http server", ex);
  914. throw;
  915. }
  916. }
  917. private string GetCertificatePath(bool generateCertificate)
  918. {
  919. if (!string.IsNullOrWhiteSpace(ServerConfigurationManager.Configuration.CertificatePath))
  920. {
  921. // Custom cert
  922. return ServerConfigurationManager.Configuration.CertificatePath;
  923. }
  924. // Generate self-signed cert
  925. var certHost = GetHostnameFromExternalDns(ServerConfigurationManager.Configuration.WanDdns);
  926. var certPath = Path.Combine(ServerConfigurationManager.ApplicationPaths.ProgramDataPath, "ssl", "cert_" + (certHost + "1").GetMD5().ToString("N") + ".pfx");
  927. if (generateCertificate)
  928. {
  929. if (!FileSystemManager.FileExists(certPath))
  930. {
  931. FileSystemManager.CreateDirectory(Path.GetDirectoryName(certPath));
  932. try
  933. {
  934. _certificateGenerator(certPath, certHost);
  935. }
  936. catch (Exception ex)
  937. {
  938. Logger.ErrorException("Error creating ssl cert", ex);
  939. return null;
  940. }
  941. }
  942. }
  943. return certPath;
  944. }
  945. /// <summary>
  946. /// Called when [configuration updated].
  947. /// </summary>
  948. /// <param name="sender">The sender.</param>
  949. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  950. protected override void OnConfigurationUpdated(object sender, EventArgs e)
  951. {
  952. base.OnConfigurationUpdated(sender, e);
  953. var requiresRestart = false;
  954. // Don't do anything if these haven't been set yet
  955. if (HttpPort != 0 && HttpsPort != 0)
  956. {
  957. // Need to restart if ports have changed
  958. if (ServerConfigurationManager.Configuration.HttpServerPortNumber != HttpPort ||
  959. ServerConfigurationManager.Configuration.HttpsPortNumber != HttpsPort)
  960. {
  961. if (ServerConfigurationManager.Configuration.IsPortAuthorized)
  962. {
  963. ServerConfigurationManager.Configuration.IsPortAuthorized = false;
  964. ServerConfigurationManager.SaveConfiguration();
  965. requiresRestart = true;
  966. }
  967. }
  968. }
  969. if (!HttpServer.UrlPrefixes.SequenceEqual(GetUrlPrefixes(), StringComparer.OrdinalIgnoreCase))
  970. {
  971. requiresRestart = true;
  972. }
  973. if (!string.Equals(CertificatePath, GetCertificatePath(false), StringComparison.OrdinalIgnoreCase))
  974. {
  975. requiresRestart = true;
  976. }
  977. if (requiresRestart)
  978. {
  979. Logger.Info("App needs to be restarted due to configuration change.");
  980. NotifyPendingRestart();
  981. }
  982. }
  983. /// <summary>
  984. /// Restarts this instance.
  985. /// </summary>
  986. public override async Task Restart()
  987. {
  988. if (!CanSelfRestart)
  989. {
  990. throw new PlatformNotSupportedException("The server is unable to self-restart. Please restart manually.");
  991. }
  992. try
  993. {
  994. await SessionManager.SendServerRestartNotification(CancellationToken.None).ConfigureAwait(false);
  995. }
  996. catch (Exception ex)
  997. {
  998. Logger.ErrorException("Error sending server restart notification", ex);
  999. }
  1000. Logger.Info("Calling RestartInternal");
  1001. RestartInternal();
  1002. }
  1003. protected abstract void RestartInternal();
  1004. /// <summary>
  1005. /// Gets the composable part assemblies.
  1006. /// </summary>
  1007. /// <returns>IEnumerable{Assembly}.</returns>
  1008. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  1009. {
  1010. var list = GetPluginAssemblies()
  1011. .ToList();
  1012. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  1013. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  1014. // Include composable parts in the Api assembly
  1015. list.Add(GetAssembly(typeof(ApiEntryPoint)));
  1016. // Include composable parts in the Dashboard assembly
  1017. list.Add(GetAssembly(typeof(DashboardService)));
  1018. // Include composable parts in the Model assembly
  1019. list.Add(GetAssembly(typeof(SystemInfo)));
  1020. // Include composable parts in the Common assembly
  1021. list.Add(GetAssembly(typeof(IApplicationHost)));
  1022. // Include composable parts in the Controller assembly
  1023. list.Add(GetAssembly(typeof(IServerApplicationHost)));
  1024. // Include composable parts in the Providers assembly
  1025. list.Add(GetAssembly(typeof(ProviderUtils)));
  1026. // Include composable parts in the Photos assembly
  1027. list.Add(GetAssembly(typeof(PhotoProvider)));
  1028. // Common implementations
  1029. list.Add(GetAssembly(typeof(TaskManager)));
  1030. // Emby.Server implementations
  1031. list.Add(GetAssembly(typeof(InstallationManager)));
  1032. // Emby.Server.Core
  1033. list.Add(GetAssembly(typeof(ApplicationHost)));
  1034. // MediaEncoding
  1035. list.Add(GetAssembly(typeof(MediaEncoder)));
  1036. // Dlna
  1037. list.Add(GetAssembly(typeof(DlnaEntryPoint)));
  1038. // Local metadata
  1039. list.Add(GetAssembly(typeof(BoxSetXmlSaver)));
  1040. // Xbmc
  1041. list.Add(GetAssembly(typeof(ArtistNfoProvider)));
  1042. list.AddRange(GetAssembliesWithPartsInternal());
  1043. // Include composable parts in the running assembly
  1044. list.Add(GetAssembly(typeof(ApplicationHost)));
  1045. return list;
  1046. }
  1047. protected abstract List<Assembly> GetAssembliesWithPartsInternal();
  1048. /// <summary>
  1049. /// Gets the plugin assemblies.
  1050. /// </summary>
  1051. /// <returns>IEnumerable{Assembly}.</returns>
  1052. private IEnumerable<Assembly> GetPluginAssemblies()
  1053. {
  1054. try
  1055. {
  1056. return Directory.EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  1057. .Where(EnablePlugin)
  1058. .Select(LoadAssembly)
  1059. .Where(a => a != null)
  1060. .ToList();
  1061. }
  1062. catch (DirectoryNotFoundException)
  1063. {
  1064. return new List<Assembly>();
  1065. }
  1066. }
  1067. private bool EnablePlugin(string path)
  1068. {
  1069. var filename = Path.GetFileName(path);
  1070. var exclude = new[]
  1071. {
  1072. "mbplus.dll",
  1073. "mbintros.dll",
  1074. "embytv.dll"
  1075. };
  1076. return !exclude.Contains(filename ?? string.Empty, StringComparer.OrdinalIgnoreCase);
  1077. }
  1078. /// <summary>
  1079. /// Gets the system status.
  1080. /// </summary>
  1081. /// <returns>SystemInfo.</returns>
  1082. public async Task<SystemInfo> GetSystemInfo()
  1083. {
  1084. var localAddress = await GetLocalApiUrl().ConfigureAwait(false);
  1085. return new SystemInfo
  1086. {
  1087. HasPendingRestart = HasPendingRestart,
  1088. Version = ApplicationVersion.ToString(),
  1089. WebSocketPortNumber = HttpPort,
  1090. FailedPluginAssemblies = FailedAssemblies.ToList(),
  1091. InProgressInstallations = InstallationManager.CurrentInstallations.Select(i => i.Item1).ToList(),
  1092. CompletedInstallations = InstallationManager.CompletedInstallations.ToList(),
  1093. Id = SystemId,
  1094. ProgramDataPath = ApplicationPaths.ProgramDataPath,
  1095. LogPath = ApplicationPaths.LogDirectoryPath,
  1096. ItemsByNamePath = ApplicationPaths.ItemsByNamePath,
  1097. InternalMetadataPath = ApplicationPaths.InternalMetadataPath,
  1098. CachePath = ApplicationPaths.CachePath,
  1099. MacAddress = GetMacAddress(),
  1100. HttpServerPortNumber = HttpPort,
  1101. SupportsHttps = SupportsHttps,
  1102. HttpsPortNumber = HttpsPort,
  1103. OperatingSystem = EnvironmentInfo.OperatingSystem.ToString(),
  1104. OperatingSystemDisplayName = OperatingSystemDisplayName,
  1105. CanSelfRestart = CanSelfRestart,
  1106. CanSelfUpdate = CanSelfUpdate,
  1107. WanAddress = ConnectManager.WanApiAddress,
  1108. HasUpdateAvailable = HasUpdateAvailable,
  1109. SupportsAutoRunAtStartup = SupportsAutoRunAtStartup,
  1110. TranscodingTempPath = ApplicationPaths.TranscodingTempPath,
  1111. SupportsRunningAsService = SupportsRunningAsService,
  1112. ServerName = FriendlyName,
  1113. LocalAddress = localAddress,
  1114. SupportsLibraryMonitor = true,
  1115. EncoderLocationType = MediaEncoder.EncoderLocationType,
  1116. SystemArchitecture = EnvironmentInfo.SystemArchitecture,
  1117. SystemUpdateLevel = ConfigurationManager.CommonConfiguration.SystemUpdateLevel,
  1118. PackageName = StartupOptions.GetOption("-package")
  1119. };
  1120. }
  1121. public bool EnableHttps
  1122. {
  1123. get
  1124. {
  1125. return SupportsHttps && ServerConfigurationManager.Configuration.EnableHttps;
  1126. }
  1127. }
  1128. public bool SupportsHttps
  1129. {
  1130. get { return Certificate != null; }
  1131. }
  1132. public async Task<string> GetLocalApiUrl()
  1133. {
  1134. try
  1135. {
  1136. // Return the first matched address, if found, or the first known local address
  1137. var address = (await GetLocalIpAddresses().ConfigureAwait(false)).FirstOrDefault(i => !i.Equals(IpAddressInfo.Loopback) && !i.Equals(IpAddressInfo.IPv6Loopback));
  1138. if (address != null)
  1139. {
  1140. return GetLocalApiUrl(address);
  1141. }
  1142. return null;
  1143. }
  1144. catch (Exception ex)
  1145. {
  1146. Logger.ErrorException("Error getting local Ip address information", ex);
  1147. }
  1148. return null;
  1149. }
  1150. public string GetLocalApiUrl(IpAddressInfo ipAddress)
  1151. {
  1152. if (ipAddress.AddressFamily == IpAddressFamily.InterNetworkV6)
  1153. {
  1154. return GetLocalApiUrl("[" + ipAddress.Address + "]");
  1155. }
  1156. return GetLocalApiUrl(ipAddress.Address);
  1157. }
  1158. public string GetLocalApiUrl(string host)
  1159. {
  1160. return string.Format("http://{0}:{1}",
  1161. host,
  1162. HttpPort.ToString(CultureInfo.InvariantCulture));
  1163. }
  1164. public async Task<List<IpAddressInfo>> GetLocalIpAddresses()
  1165. {
  1166. var addresses = ServerConfigurationManager
  1167. .Configuration
  1168. .LocalNetworkAddresses
  1169. .Select(NormalizeConfiguredLocalAddress)
  1170. .Where(i => i != null)
  1171. .ToList();
  1172. if (addresses.Count == 0)
  1173. {
  1174. addresses.AddRange(NetworkManager.GetLocalIpAddresses());
  1175. var list = new List<IpAddressInfo>();
  1176. foreach (var address in addresses)
  1177. {
  1178. var valid = await IsIpAddressValidAsync(address).ConfigureAwait(false);
  1179. if (valid)
  1180. {
  1181. list.Add(address);
  1182. }
  1183. }
  1184. addresses = list;
  1185. }
  1186. return addresses;
  1187. }
  1188. private IpAddressInfo NormalizeConfiguredLocalAddress(string address)
  1189. {
  1190. var index = address.Trim('/').IndexOf('/');
  1191. if (index != -1)
  1192. {
  1193. address = address.Substring(index + 1);
  1194. }
  1195. IpAddressInfo result;
  1196. if (NetworkManager.TryParseIpAddress(address.Trim('/'), out result))
  1197. {
  1198. return result;
  1199. }
  1200. return null;
  1201. }
  1202. private readonly ConcurrentDictionary<string, bool> _validAddressResults = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
  1203. private DateTime _lastAddressCacheClear;
  1204. private async Task<bool> IsIpAddressValidAsync(IpAddressInfo address)
  1205. {
  1206. if (address.Equals(IpAddressInfo.Loopback) ||
  1207. address.Equals(IpAddressInfo.IPv6Loopback))
  1208. {
  1209. return true;
  1210. }
  1211. var apiUrl = GetLocalApiUrl(address);
  1212. apiUrl += "/system/ping";
  1213. if ((DateTime.UtcNow - _lastAddressCacheClear).TotalMinutes >= 15)
  1214. {
  1215. _lastAddressCacheClear = DateTime.UtcNow;
  1216. _validAddressResults.Clear();
  1217. }
  1218. bool cachedResult;
  1219. if (_validAddressResults.TryGetValue(apiUrl, out cachedResult))
  1220. {
  1221. return cachedResult;
  1222. }
  1223. try
  1224. {
  1225. using (var response = await HttpClient.SendAsync(new HttpRequestOptions
  1226. {
  1227. Url = apiUrl,
  1228. LogErrorResponseBody = false,
  1229. LogErrors = false,
  1230. LogRequest = false,
  1231. TimeoutMs = 30000,
  1232. BufferContent = false
  1233. }, "POST").ConfigureAwait(false))
  1234. {
  1235. using (var reader = new StreamReader(response.Content))
  1236. {
  1237. var result = reader.ReadToEnd();
  1238. var valid = string.Equals(Name, result, StringComparison.OrdinalIgnoreCase);
  1239. _validAddressResults.AddOrUpdate(apiUrl, valid, (k, v) => valid);
  1240. //Logger.Debug("Ping test result to {0}. Success: {1}", apiUrl, valid);
  1241. return valid;
  1242. }
  1243. }
  1244. }
  1245. catch
  1246. {
  1247. //Logger.Debug("Ping test result to {0}. Success: {1}", apiUrl, false);
  1248. _validAddressResults.AddOrUpdate(apiUrl, false, (k, v) => false);
  1249. return false;
  1250. }
  1251. }
  1252. public string FriendlyName
  1253. {
  1254. get
  1255. {
  1256. return string.IsNullOrWhiteSpace(ServerConfigurationManager.Configuration.ServerName)
  1257. ? Environment.MachineName
  1258. : ServerConfigurationManager.Configuration.ServerName;
  1259. }
  1260. }
  1261. public int HttpPort { get; private set; }
  1262. public int HttpsPort { get; private set; }
  1263. /// <summary>
  1264. /// Gets the mac address.
  1265. /// </summary>
  1266. /// <returns>System.String.</returns>
  1267. private string GetMacAddress()
  1268. {
  1269. try
  1270. {
  1271. return NetworkManager.GetMacAddress();
  1272. }
  1273. catch (Exception ex)
  1274. {
  1275. Logger.ErrorException("Error getting mac address", ex);
  1276. return null;
  1277. }
  1278. }
  1279. /// <summary>
  1280. /// Shuts down.
  1281. /// </summary>
  1282. public override async Task Shutdown()
  1283. {
  1284. try
  1285. {
  1286. await SessionManager.SendServerShutdownNotification(CancellationToken.None).ConfigureAwait(false);
  1287. }
  1288. catch (Exception ex)
  1289. {
  1290. Logger.ErrorException("Error sending server shutdown notification", ex);
  1291. }
  1292. ShutdownInternal();
  1293. }
  1294. protected abstract void ShutdownInternal();
  1295. /// <summary>
  1296. /// Registers the server with administrator access.
  1297. /// </summary>
  1298. private void RegisterServerWithAdministratorAccess()
  1299. {
  1300. Logger.Info("Requesting administrative access to authorize http server");
  1301. try
  1302. {
  1303. AuthorizeServer();
  1304. }
  1305. catch (NotImplementedException)
  1306. {
  1307. }
  1308. catch (Exception ex)
  1309. {
  1310. Logger.ErrorException("Error authorizing server", ex);
  1311. }
  1312. }
  1313. protected virtual void AuthorizeServer()
  1314. {
  1315. throw new NotImplementedException();
  1316. }
  1317. public event EventHandler HasUpdateAvailableChanged;
  1318. private bool _hasUpdateAvailable;
  1319. public bool HasUpdateAvailable
  1320. {
  1321. get { return _hasUpdateAvailable; }
  1322. set
  1323. {
  1324. var fireEvent = value && !_hasUpdateAvailable;
  1325. _hasUpdateAvailable = value;
  1326. if (fireEvent)
  1327. {
  1328. EventHelper.FireEventIfNotNull(HasUpdateAvailableChanged, this, EventArgs.Empty, Logger);
  1329. }
  1330. }
  1331. }
  1332. /// <summary>
  1333. /// Checks for update.
  1334. /// </summary>
  1335. /// <param name="cancellationToken">The cancellation token.</param>
  1336. /// <param name="progress">The progress.</param>
  1337. /// <returns>Task{CheckForUpdateResult}.</returns>
  1338. public override async Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
  1339. {
  1340. var cacheLength = TimeSpan.FromHours(3);
  1341. var updateLevel = ConfigurationManager.CommonConfiguration.SystemUpdateLevel;
  1342. if (updateLevel == PackageVersionClass.Beta)
  1343. {
  1344. cacheLength = TimeSpan.FromHours(1);
  1345. }
  1346. else if (updateLevel == PackageVersionClass.Dev)
  1347. {
  1348. cacheLength = TimeSpan.FromMinutes(5);
  1349. }
  1350. var result = await new GithubUpdater(HttpClient, JsonSerializer).CheckForUpdateResult("MediaBrowser", "Emby", ApplicationVersion, updateLevel, _releaseAssetFilename,
  1351. "MBServer", "Mbserver.zip", cacheLength, cancellationToken).ConfigureAwait(false);
  1352. HasUpdateAvailable = result.IsUpdateAvailable;
  1353. return result;
  1354. }
  1355. /// <summary>
  1356. /// Updates the application.
  1357. /// </summary>
  1358. /// <param name="package">The package that contains the update</param>
  1359. /// <param name="cancellationToken">The cancellation token.</param>
  1360. /// <param name="progress">The progress.</param>
  1361. public override async Task UpdateApplication(PackageVersionInfo package, CancellationToken cancellationToken, IProgress<double> progress)
  1362. {
  1363. await InstallationManager.InstallPackage(package, false, progress, cancellationToken).ConfigureAwait(false);
  1364. HasUpdateAvailable = false;
  1365. OnApplicationUpdated(package);
  1366. }
  1367. /// <summary>
  1368. /// Configures the automatic run at startup.
  1369. /// </summary>
  1370. /// <param name="autorun">if set to <c>true</c> [autorun].</param>
  1371. protected override void ConfigureAutoRunAtStartup(bool autorun)
  1372. {
  1373. if (SupportsAutoRunAtStartup)
  1374. {
  1375. ConfigureAutoRunInternal(autorun);
  1376. }
  1377. }
  1378. protected virtual void ConfigureAutoRunInternal(bool autorun)
  1379. {
  1380. throw new NotImplementedException();
  1381. }
  1382. /// <summary>
  1383. /// This returns localhost in the case of no external dns, and the hostname if the
  1384. /// dns is prefixed with a valid Uri prefix.
  1385. /// </summary>
  1386. /// <param name="externalDns">The external dns prefix to get the hostname of.</param>
  1387. /// <returns>The hostname in <paramref name="externalDns"/></returns>
  1388. private static string GetHostnameFromExternalDns(string externalDns)
  1389. {
  1390. if (string.IsNullOrWhiteSpace(externalDns))
  1391. {
  1392. return "localhost";
  1393. }
  1394. try
  1395. {
  1396. return new Uri(externalDns).Host;
  1397. }
  1398. catch
  1399. {
  1400. return externalDns;
  1401. }
  1402. }
  1403. public void LaunchUrl(string url)
  1404. {
  1405. if (EnvironmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows &&
  1406. EnvironmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.OSX)
  1407. {
  1408. throw new NotImplementedException();
  1409. }
  1410. var process = ProcessFactory.Create(new ProcessOptions
  1411. {
  1412. FileName = url,
  1413. //EnableRaisingEvents = true,
  1414. UseShellExecute = true,
  1415. ErrorDialog = false
  1416. });
  1417. process.Exited += ProcessExited;
  1418. try
  1419. {
  1420. process.Start();
  1421. }
  1422. catch (Exception ex)
  1423. {
  1424. Console.WriteLine("Error launching url: {0}", url);
  1425. Logger.ErrorException("Error launching url: {0}", ex, url);
  1426. throw;
  1427. }
  1428. }
  1429. private static void ProcessExited(object sender, EventArgs e)
  1430. {
  1431. ((IProcess)sender).Dispose();
  1432. }
  1433. public void EnableLoopback(string appName)
  1434. {
  1435. EnableLoopbackInternal(appName);
  1436. }
  1437. protected virtual void EnableLoopbackInternal(string appName)
  1438. {
  1439. }
  1440. private void RegisterModules()
  1441. {
  1442. var moduleTypes = GetExportTypes<IDependencyModule>();
  1443. foreach (var type in moduleTypes)
  1444. {
  1445. try
  1446. {
  1447. var instance = Activator.CreateInstance(type) as IDependencyModule;
  1448. if (instance != null)
  1449. instance.BindDependencies(this);
  1450. }
  1451. catch (Exception ex)
  1452. {
  1453. Logger.ErrorException("Error setting up dependency bindings for " + type.Name, ex);
  1454. }
  1455. }
  1456. }
  1457. void IDependencyContainer.RegisterSingleInstance<T>(T obj, bool manageLifetime)
  1458. {
  1459. RegisterSingleInstance(obj, manageLifetime);
  1460. }
  1461. void IDependencyContainer.RegisterSingleInstance<T>(Func<T> func)
  1462. {
  1463. RegisterSingleInstance(func);
  1464. }
  1465. void IDependencyContainer.Register(Type typeInterface, Type typeImplementation)
  1466. {
  1467. Container.Register(typeInterface, typeImplementation);
  1468. }
  1469. }
  1470. }