From 95341c5c96059e4bd70f290ff05ae3abc9c49257 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 12 Nov 2016 23:33:51 -0500 Subject: update .net core startup --- MediaBrowser.ServerApplication/MainStartup.cs | 38 ++-- .../MediaBrowser.ServerApplication.csproj | 2 +- .../Native/WindowsApp.cs | 240 --------------------- MediaBrowser.ServerApplication/WindowsAppHost.cs | 217 +++++++++++++++++++ 4 files changed, 234 insertions(+), 263 deletions(-) delete mode 100644 MediaBrowser.ServerApplication/Native/WindowsApp.cs create mode 100644 MediaBrowser.ServerApplication/WindowsAppHost.cs (limited to 'MediaBrowser.ServerApplication') diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index fa8cccf342..ab0a36affb 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -37,7 +37,7 @@ namespace MediaBrowser.ServerApplication private static ILogger _logger; - private static bool _isRunningAsService = false; + public static bool IsRunningAsService = false; private static bool _canRestartService = false; private static bool _appHostDisposed; @@ -72,9 +72,9 @@ namespace MediaBrowser.ServerApplication public static void Main() { var options = new StartupOptions(); - _isRunningAsService = options.ContainsOption("-service"); + IsRunningAsService = options.ContainsOption("-service"); - if (_isRunningAsService) + if (IsRunningAsService) { //_canRestartService = CanRestartWindowsService(); } @@ -88,7 +88,7 @@ namespace MediaBrowser.ServerApplication var success = SetDllDirectory(architecturePath); - var appPaths = CreateApplicationPaths(applicationPath, _isRunningAsService); + var appPaths = CreateApplicationPaths(applicationPath, IsRunningAsService); var logManager = new NlogManager(appPaths.LogDirectoryPath, "server"); logManager.ReloadLogger(LogSeverity.Debug); @@ -148,7 +148,7 @@ namespace MediaBrowser.ServerApplication try { - RunApplication(appPaths, logManager, _isRunningAsService, options); + RunApplication(appPaths, logManager, IsRunningAsService, options); } finally { @@ -204,7 +204,7 @@ namespace MediaBrowser.ServerApplication } } - if (!_isRunningAsService) + if (!IsRunningAsService) { return IsAlreadyRunningAsService(applicationPath); } @@ -272,7 +272,7 @@ namespace MediaBrowser.ServerApplication { get { - if (_isRunningAsService) + if (IsRunningAsService) { return _canRestartService; } @@ -295,7 +295,7 @@ namespace MediaBrowser.ServerApplication return false; #endif - if (_isRunningAsService) + if (IsRunningAsService) { return _canRestartService; } @@ -317,22 +317,16 @@ namespace MediaBrowser.ServerApplication /// The options. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, bool runService, StartupOptions options) { - var fileSystem = new WindowsFileSystem(logManager.GetLogger("FileSystem")); + var fileSystem = new ManagedFileSystem(logManager.GetLogger("FileSystem"), true, true, true); fileSystem.AddShortcutHandler(new LnkShortcutHandler()); fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem)); - var nativeApp = new WindowsApp(fileSystem, _logger) - { - IsRunningAsService = runService - }; - var imageEncoder = ImageEncoderHelper.GetImageEncoder(_logger, logManager, fileSystem, options, () => _appHost.HttpClient, appPaths); - _appHost = new ApplicationHost(appPaths, + _appHost = new WindowsAppHost(appPaths, logManager, options, fileSystem, - nativeApp, new PowerManagement(), "emby.windows.zip", new EnvironmentInfo(), @@ -440,7 +434,7 @@ namespace MediaBrowser.ServerApplication public static void Invoke(Action action) { - if (_isRunningAsService) + if (IsRunningAsService) { action(); } @@ -578,7 +572,7 @@ namespace MediaBrowser.ServerApplication /// The instance containing the event data. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e) { - if (e.Reason == SessionEndReasons.SystemShutdown || !_isRunningAsService) + if (e.Reason == SessionEndReasons.SystemShutdown || !IsRunningAsService) { Shutdown(); } @@ -595,7 +589,7 @@ namespace MediaBrowser.ServerApplication new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception); - if (!_isRunningAsService) + if (!IsRunningAsService) { MessageBox.Show("Unhandled exception: " + exception.Message); } @@ -623,7 +617,7 @@ namespace MediaBrowser.ServerApplication // Update is there - execute update try { - var serviceName = _isRunningAsService ? BackgroundService.GetExistingServiceName() : string.Empty; + var serviceName = IsRunningAsService ? BackgroundService.GetExistingServiceName() : string.Empty; new ApplicationUpdater().UpdateApplication(appPaths, updateArchive, logger, serviceName); // And just let the app exit so it can update @@ -642,7 +636,7 @@ namespace MediaBrowser.ServerApplication public static void Shutdown() { - if (_isRunningAsService) + if (IsRunningAsService) { ShutdownWindowsService(); } @@ -658,7 +652,7 @@ namespace MediaBrowser.ServerApplication { DisposeAppHost(); - if (_isRunningAsService) + if (IsRunningAsService) { RestartWindowsService(); } diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index f6aef57443..6984ff2bed 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -138,7 +138,6 @@ - @@ -156,6 +155,7 @@ SplashForm.cs + diff --git a/MediaBrowser.ServerApplication/Native/WindowsApp.cs b/MediaBrowser.ServerApplication/Native/WindowsApp.cs deleted file mode 100644 index babe952d6d..0000000000 --- a/MediaBrowser.ServerApplication/Native/WindowsApp.cs +++ /dev/null @@ -1,240 +0,0 @@ -using System; -using MediaBrowser.Common.Net; -using MediaBrowser.Model.Logging; -using MediaBrowser.Server.Startup.Common; -using MediaBrowser.ServerApplication.Networking; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Reflection; -using System.Windows.Forms; -using Emby.Server.Core; -using Emby.Server.Core.Data; -using Emby.Server.Core.FFMpeg; -using MediaBrowser.Common.IO; -using MediaBrowser.Controller.IO; -using MediaBrowser.Model.IO; - -namespace MediaBrowser.ServerApplication.Native -{ - public class WindowsApp : INativeApp - { - private readonly IFileSystem _fileSystem; - private readonly ILogger _logger; - - public WindowsApp(IFileSystem fileSystem, ILogger logger) - { - _fileSystem = fileSystem; - _logger = logger; - } - - public List GetAssembliesWithParts() - { - var list = new List(); - - if (!System.Environment.Is64BitProcess) - { - //list.Add(typeof(PismoIsoManager).Assembly); - } - - list.Add(GetType().Assembly); - - return list; - } - - public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory) - { - ServerAuthorization.AuthorizeServer(udpPort, httpServerPort, httpsPort, applicationPath, tempDirectory); - } - - public bool SupportsLibraryMonitor - { - get { return true; } - } - - public bool SupportsRunningAsService - { - get - { - return true; - } - } - - public bool IsRunningAsService - { - get; - set; - } - - public bool CanSelfRestart - { - get - { - return MainStartup.CanSelfRestart; - } - } - - public bool SupportsAutoRunAtStartup - { - get - { - return true; - } - } - - public bool CanSelfUpdate - { - get - { - return MainStartup.CanSelfUpdate; - } - } - - public void Shutdown() - { - MainStartup.Shutdown(); - } - - public void Restart(StartupOptions startupOptions) - { - MainStartup.Restart(); - } - - public void ConfigureAutoRun(bool autorun) - { - var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk"); - - var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup); - - if (autorun) - { - //Copy our shortut into the startup folder for this user - var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"); - _fileSystem.CreateDirectory(Path.GetDirectoryName(targetPath)); - File.Copy(shortcutPath, targetPath, true); - } - else - { - //Remove our shortcut from the startup folder for this user - _fileSystem.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk")); - } - } - - public INetworkManager CreateNetworkManager(ILogger logger) - { - return new NetworkManager(logger); - } - - public FFMpegInstallInfo GetFfmpegInstallInfo() - { - var info = new FFMpegInstallInfo(); - - info.FFMpegFilename = "ffmpeg.exe"; - info.FFProbeFilename = "ffprobe.exe"; - info.Version = "0"; - - return info; - } - - public void LaunchUrl(string url) - { - var process = new Process - { - StartInfo = new ProcessStartInfo - { - FileName = url - }, - - EnableRaisingEvents = true, - }; - - process.Exited += ProcessExited; - - try - { - process.Start(); - } - catch (Exception ex) - { - _logger.ErrorException("Error launching url: {0}", ex, url); - - throw; - } - } - - public IDbConnector GetDbConnector() - { - return new DbConnector(_logger); - } - - /// - /// Processes the exited. - /// - /// The sender. - /// The instance containing the event data. - private static void ProcessExited(object sender, EventArgs e) - { - ((Process)sender).Dispose(); - } - - public void EnableLoopback(string appName) - { - LoopUtil.Run(appName); - } - - public bool PortsRequireAuthorization(string applicationPath) - { - var appNameSrch = Path.GetFileName(applicationPath); - - var startInfo = new ProcessStartInfo - { - FileName = "netsh", - - Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"", - - CreateNoWindow = true, - UseShellExecute = false, - WindowStyle = ProcessWindowStyle.Hidden, - ErrorDialog = false, - RedirectStandardOutput = true - }; - - using (var process = Process.Start(startInfo)) - { - process.Start(); - - try - { - var data = process.StandardOutput.ReadToEnd() ?? string.Empty; - - if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1) - { - _logger.Info("Found potential windows firewall rule blocking Emby Server: " + data); - } - - //var parts = data.Split('\n'); - - //return parts.Length > 4; - //return Confirm(); - return false; - } - catch (Exception ex) - { - _logger.ErrorException("Error querying windows firewall", ex); - - // Hate having to do this - try - { - process.Kill(); - } - catch (Exception ex1) - { - _logger.ErrorException("Error killing process", ex1); - } - - throw; - } - } - } - } -} \ No newline at end of file diff --git a/MediaBrowser.ServerApplication/WindowsAppHost.cs b/MediaBrowser.ServerApplication/WindowsAppHost.cs new file mode 100644 index 0000000000..8fd7184321 --- /dev/null +++ b/MediaBrowser.ServerApplication/WindowsAppHost.cs @@ -0,0 +1,217 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using Emby.Server.Core; +using Emby.Server.Core.Data; +using Emby.Server.Core.FFMpeg; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.System; +using MediaBrowser.ServerApplication.Native; + +namespace MediaBrowser.ServerApplication +{ + public class WindowsAppHost : ApplicationHost + { + public WindowsAppHost(ServerApplicationPaths applicationPaths, ILogManager logManager, StartupOptions options, IFileSystem fileSystem, IPowerManagement powerManagement, string releaseAssetFilename, IEnvironmentInfo environmentInfo, MediaBrowser.Controller.Drawing.IImageEncoder imageEncoder, ISystemEvents systemEvents, IMemoryStreamFactory memoryStreamFactory, MediaBrowser.Common.Net.INetworkManager networkManager, Action certificateGenerator, Func defaultUsernameFactory) + : base(applicationPaths, logManager, options, fileSystem, powerManagement, releaseAssetFilename, environmentInfo, imageEncoder, systemEvents, memoryStreamFactory, networkManager, certificateGenerator, defaultUsernameFactory) + { + } + + public override bool IsRunningAsService + { + get { return MainStartup.IsRunningAsService; } + } + + protected override FFMpegInstallInfo GetFfmpegInstallInfo() + { + var info = new FFMpegInstallInfo(); + + info.FFMpegFilename = "ffmpeg.exe"; + info.FFProbeFilename = "ffprobe.exe"; + info.Version = "0"; + + return info; + } + + protected override void RestartInternal() + { + MainStartup.Restart(); + } + + protected override List GetAssembliesWithPartsInternal() + { + var list = new List(); + + if (!Environment.Is64BitProcess) + { + //list.Add(typeof(PismoIsoManager).Assembly); + } + + list.Add(GetType().Assembly); + + return list; + } + + protected override void ShutdownInternal() + { + MainStartup.Shutdown(); + } + + protected override void AuthorizeServer(int udpPort, int httpServerPort, int httpsServerPort, string applicationPath, string tempDirectory) + { + ServerAuthorization.AuthorizeServer(udpPort, httpServerPort, httpsServerPort, applicationPath, tempDirectory); + } + + protected override IDbConnector GetDbConnector() + { + return new DbConnector(Logger); + } + + protected override void ConfigureAutoRunInternal(bool autorun) + { + var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk"); + + var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup); + + if (autorun) + { + //Copy our shortut into the startup folder for this user + var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"); + FileSystemManager.CreateDirectory(Path.GetDirectoryName(targetPath)); + File.Copy(shortcutPath, targetPath, true); + } + else + { + //Remove our shortcut from the startup folder for this user + FileSystemManager.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk")); + } + } + + public override void LaunchUrl(string url) + { + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = url + }, + + EnableRaisingEvents = true, + }; + + process.Exited += ProcessExited; + + try + { + process.Start(); + } + catch (Exception ex) + { + Logger.ErrorException("Error launching url: {0}", ex, url); + + throw; + } + } + + private static void ProcessExited(object sender, EventArgs e) + { + ((Process)sender).Dispose(); + } + + protected override void EnableLoopbackInternal(string appName) + { + LoopUtil.Run(appName); + } + + public override bool SupportsRunningAsService + { + get + { + return true; + } + } + + public override bool CanSelfRestart + { + get + { + return MainStartup.CanSelfRestart; + } + } + + public override bool SupportsAutoRunAtStartup + { + get + { + return true; + } + } + + public override bool CanSelfUpdate + { + get + { + return MainStartup.CanSelfUpdate; + } + } + + public bool PortsRequireAuthorization(string applicationPath) + { + var appNameSrch = Path.GetFileName(applicationPath); + + var startInfo = new ProcessStartInfo + { + FileName = "netsh", + + Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"", + + CreateNoWindow = true, + UseShellExecute = false, + WindowStyle = ProcessWindowStyle.Hidden, + ErrorDialog = false, + RedirectStandardOutput = true + }; + + using (var process = Process.Start(startInfo)) + { + process.Start(); + + try + { + var data = process.StandardOutput.ReadToEnd() ?? string.Empty; + + if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1) + { + Logger.Info("Found potential windows firewall rule blocking Emby Server: " + data); + } + + //var parts = data.Split('\n'); + + //return parts.Length > 4; + //return Confirm(); + return false; + } + catch (Exception ex) + { + Logger.ErrorException("Error querying windows firewall", ex); + + // Hate having to do this + try + { + process.Kill(); + } + catch (Exception ex1) + { + Logger.ErrorException("Error killing process", ex1); + } + + throw; + } + } + } + + } +} -- cgit v1.2.3