diff options
Diffstat (limited to 'MediaBrowser.Server.Mono/Program.cs')
| -rw-r--r-- | MediaBrowser.Server.Mono/Program.cs | 173 |
1 files changed, 151 insertions, 22 deletions
diff --git a/MediaBrowser.Server.Mono/Program.cs b/MediaBrowser.Server.Mono/Program.cs index 32de45242..b3aeb2027 100644 --- a/MediaBrowser.Server.Mono/Program.cs +++ b/MediaBrowser.Server.Mono/Program.cs @@ -1,20 +1,29 @@ -using MediaBrowser.Common.Implementations.Logging; using MediaBrowser.Model.Logging; -using MediaBrowser.Server.Implementations; using MediaBrowser.Server.Mono.Native; using MediaBrowser.Server.Startup.Common; -using Microsoft.Win32; using System; using System.Diagnostics; +using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Reflection; -using System.Security.Cryptography.X509Certificates; +using System.Text.RegularExpressions; using System.Threading.Tasks; -using CommonIO; -using MediaBrowser.Server.Implementations.Logging; +using Emby.Common.Implementations.EnvironmentInfo; +using Emby.Common.Implementations.Logging; +using Emby.Common.Implementations.Networking; +using Emby.Common.Implementations.Security; +using Emby.Server.Core; +using Emby.Server.Implementations; +using Emby.Server.Implementations.IO; +using MediaBrowser.Model.System; +using MediaBrowser.Server.Startup.Common.IO; +using Mono.Unix.Native; +using NLog; +using ILogger = MediaBrowser.Model.Logging.ILogger; +using X509Certificate = System.Security.Cryptography.X509Certificates.X509Certificate; namespace MediaBrowser.Server.Mono { @@ -27,8 +36,12 @@ namespace MediaBrowser.Server.Mono public static void Main(string[] args) { var applicationPath = Assembly.GetEntryAssembly().Location; + var appFolderPath = Path.GetDirectoryName(applicationPath); - var options = new StartupOptions(); + TryCopySqliteConfigFile(appFolderPath); + SetSqliteProvider(); + + var options = new StartupOptions(Environment.GetCommandLineArgs()); // Allow this to be specified on the command line. var customProgramDataPath = options.GetOption("-programdata"); @@ -42,7 +55,7 @@ namespace MediaBrowser.Server.Mono var logger = _logger = logManager.GetLogger("Main"); ApplicationHost.LogEnvironmentInfo(logger, appPaths, true); - + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; try @@ -57,6 +70,25 @@ namespace MediaBrowser.Server.Mono } } + private static void TryCopySqliteConfigFile(string appFolderPath) + { + try + { + File.Copy(Path.Combine(appFolderPath, "System.Data.SQLite.dll.config"), + Path.Combine(appFolderPath, "SQLitePCLRaw.provider.sqlite3.dll.config"), + true); + } + catch + { + + } + } + + private static void SetSqliteProvider() + { + SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3()); + } + private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath) { if (string.IsNullOrEmpty(programDataPath)) @@ -64,24 +96,38 @@ namespace MediaBrowser.Server.Mono programDataPath = ApplicationPathHelper.GetProgramDataPath(applicationPath); } - return new ServerApplicationPaths(programDataPath, applicationPath, Path.GetDirectoryName(applicationPath)); + var appFolderPath = Path.GetDirectoryName(applicationPath); + + return new ServerApplicationPaths(programDataPath, appFolderPath, Path.GetDirectoryName(applicationPath)); } private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>(); private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options) { - SystemEvents.SessionEnding += SystemEvents_SessionEnding; - // Allow all https requests ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); - var fileSystem = new ManagedFileSystem(new PatternsLogger(logManager.GetLogger("FileSystem")), false, false); + var fileSystem = new MonoFileSystem(logManager.GetLogger("FileSystem"), false, false); fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem)); - var nativeApp = new NativeApp(options, logManager.GetLogger("App")); + var environmentInfo = GetEnvironmentInfo(); - _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "emby.mono.zip", nativeApp); + var imageEncoder = ImageEncoderHelper.GetImageEncoder(_logger, logManager, fileSystem, options, () => _appHost.HttpClient, appPaths); + + _appHost = new MonoAppHost(appPaths, + logManager, + options, + fileSystem, + new PowerManagement(), + "emby.mono.zip", + environmentInfo, + imageEncoder, + new Startup.Common.SystemEvents(logManager.GetLogger("SystemEvents")), + new MemoryStreamProvider(), + new NetworkManager(logManager.GetLogger("NetworkManager")), + GenerateCertificate, + () => Environment.UserName); if (options.ContainsOption("-v")) { @@ -106,17 +152,90 @@ namespace MediaBrowser.Server.Mono Task.WaitAll(task); } - /// <summary> - /// Handles the SessionEnding event of the SystemEvents control. - /// </summary> - /// <param name="sender">The source of the event.</param> - /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param> - static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e) + private static void GenerateCertificate(string certPath, string certHost) + { + CertificateGenerator.CreateSelfSignCertificatePfx(certPath, certHost, _logger); + } + + private static MonoEnvironmentInfo GetEnvironmentInfo() { - if (e.Reason == SessionEndReasons.SystemShutdown) + var info = new MonoEnvironmentInfo(); + + var uname = GetUnixName(); + + var sysName = uname.sysname ?? string.Empty; + + if (string.Equals(sysName, "Darwin", StringComparison.OrdinalIgnoreCase)) + { + //info.OperatingSystem = Startup.Common.OperatingSystem.Osx; + } + else if (string.Equals(sysName, "Linux", StringComparison.OrdinalIgnoreCase)) + { + //info.OperatingSystem = Startup.Common.OperatingSystem.Linux; + } + else if (string.Equals(sysName, "BSD", StringComparison.OrdinalIgnoreCase)) { - Shutdown(); + //info.OperatingSystem = Startup.Common.OperatingSystem.Bsd; + info.IsBsd = true; } + + var archX86 = new Regex("(i|I)[3-6]86"); + + if (archX86.IsMatch(uname.machine)) + { + info.CustomArchitecture = Architecture.X86; + } + else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase)) + { + info.CustomArchitecture = Architecture.X64; + } + else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase)) + { + info.CustomArchitecture = Architecture.Arm; + } + else if (System.Environment.Is64BitOperatingSystem) + { + info.CustomArchitecture = Architecture.X64; + } + else + { + info.CustomArchitecture = Architecture.X86; + } + + return info; + } + + private static Uname _unixName; + + private static Uname GetUnixName() + { + if (_unixName == null) + { + var uname = new Uname(); + try + { + Utsname utsname; + var callResult = Syscall.uname(out utsname); + if (callResult == 0) + { + uname.sysname = utsname.sysname ?? string.Empty; + uname.machine = utsname.machine ?? string.Empty; + } + + } + catch (Exception ex) + { + _logger.ErrorException("Error getting unix name", ex); + } + _unixName = uname; + } + return _unixName; + } + + public class Uname + { + public string sysname = string.Empty; + public string machine = string.Empty; } /// <summary> @@ -191,4 +310,14 @@ namespace MediaBrowser.Server.Mono return true; } } + + public class MonoEnvironmentInfo : EnvironmentInfo + { + public bool IsBsd { get; set; } + + public override string GetUserId() + { + return Syscall.getuid().ToString(CultureInfo.InvariantCulture); + } + } } |
