jellyfin/MediaBrowser.Server.Mac/Main.cs

323 lines
10 KiB
C#
Raw Normal View History

2016-11-14 05:45:13 +00:00
using MediaBrowser.Model.Logging;
using System;
2014-11-24 00:02:01 +00:00
using System.Diagnostics;
2016-11-14 05:45:13 +00:00
using System.Globalization;
2014-11-24 00:02:01 +00:00
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
2016-11-14 05:45:13 +00:00
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Drawing;
2014-11-24 00:02:01 +00:00
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
2014-11-21 17:08:50 +00:00
using MonoMac.AppKit;
2014-11-24 00:02:01 +00:00
using MonoMac.Foundation;
2014-11-21 17:08:50 +00:00
using MonoMac.ObjCRuntime;
2016-11-14 04:50:23 +00:00
using Emby.Server.Core;
2017-05-29 20:07:05 +00:00
using Emby.Server.Core.Cryptography;
2016-11-19 18:20:26 +00:00
using Emby.Server.Implementations;
2017-02-24 18:00:18 +00:00
using Emby.Server.Implementations.Logging;
2016-11-14 04:50:23 +00:00
using Emby.Server.Mac.Native;
2016-11-14 05:41:57 +00:00
using Emby.Server.Implementations.IO;
2016-11-14 05:45:13 +00:00
using Mono.Unix.Native;
2016-11-14 06:00:53 +00:00
using MediaBrowser.Model.System;
2017-02-24 18:00:18 +00:00
using MediaBrowser.Model.IO;
2017-05-13 04:26:39 +00:00
using Emby.Drawing;
using Emby.Drawing.Skia;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Model.Drawing;
2017-08-22 18:47:53 +00:00
using Emby.Server.Implementations.EnvironmentInfo;
using Emby.Server.Implementations.Networking;
2014-11-21 17:08:50 +00:00
namespace MediaBrowser.Server.Mac
{
class MainClass
{
2016-11-14 04:50:23 +00:00
internal static MacAppHost AppHost;
2014-11-24 00:02:01 +00:00
private static ILogger _logger;
2017-02-24 18:00:18 +00:00
private static IFileSystem _fileSystem;
2014-11-24 00:02:01 +00:00
2014-11-21 17:08:50 +00:00
static void Main (string[] args)
{
2016-11-18 18:13:04 +00:00
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
var applicationPath = Assembly.GetEntryAssembly().Location;
2014-11-24 00:02:01 +00:00
2016-11-19 18:20:26 +00:00
var options = new StartupOptions(Environment.GetCommandLineArgs());
2014-11-24 00:02:01 +00:00
// Allow this to be specified on the command line.
var customProgramDataPath = options.GetOption("-programdata");
2016-11-14 04:50:23 +00:00
var appFolderPath = Path.GetDirectoryName(applicationPath);
var appPaths = CreateApplicationPaths(appFolderPath, customProgramDataPath);
2014-11-24 00:02:01 +00:00
2017-08-22 18:47:53 +00:00
var logManager = new SimpleLogManager(appPaths.LogDirectoryPath, "server");
2014-11-24 00:02:01 +00:00
logManager.ReloadLogger(LogSeverity.Info);
logManager.AddConsoleOutput();
var logger = _logger = logManager.GetLogger("Main");
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
StartApplication(appPaths, logManager, options);
2014-11-21 17:08:50 +00:00
NSApplication.Init ();
NSApplication.Main (args);
2014-11-26 04:27:55 +00:00
}
2016-11-14 04:50:23 +00:00
private static ServerApplicationPaths CreateApplicationPaths(string appFolderPath, string programDataPath)
2014-11-24 00:02:01 +00:00
{
if (string.IsNullOrEmpty(programDataPath))
{
2014-11-26 21:16:04 +00:00
// TODO: Use CommonApplicationData? Will we always have write access?
programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "mediabrowser-server");
2015-04-16 15:41:23 +00:00
if (!Directory.Exists (programDataPath)) {
programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "emby-server");
}
2014-11-24 00:02:01 +00:00
}
2014-11-27 01:56:14 +00:00
// Within the mac bundle, go uo two levels then down into Resources folder
2016-11-14 04:50:23 +00:00
var resourcesPath = Path.Combine(Path.GetDirectoryName(appFolderPath), "Resources");
2014-11-24 00:02:01 +00:00
2017-08-22 18:47:53 +00:00
return new ServerApplicationPaths(programDataPath, appFolderPath, resourcesPath);
2014-11-24 00:02:01 +00:00
}
/// <summary>
/// Runs the application.
/// </summary>
/// <param name="appPaths">The app paths.</param>
/// <param name="logManager">The log manager.</param>
/// <param name="options">The options.</param>
private static void StartApplication(ServerApplicationPaths appPaths,
ILogManager logManager,
StartupOptions options)
{
// Allow all https requests
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
2017-03-10 19:51:29 +00:00
var environmentInfo = GetEnvironmentInfo();
2014-11-24 00:02:01 +00:00
2017-03-10 19:51:29 +00:00
var fileSystem = new MonoFileSystem(logManager.GetLogger("FileSystem"), environmentInfo, appPaths.TempDirectory);
2017-02-24 18:00:18 +00:00
2017-03-10 19:51:29 +00:00
_fileSystem = fileSystem;
2016-11-14 05:41:57 +00:00
2017-05-13 04:26:39 +00:00
var imageEncoder = GetImageEncoder(appPaths, fileSystem, logManager);
2016-11-14 05:41:57 +00:00
AppHost = new MacAppHost(appPaths,
logManager,
options,
fileSystem,
new PowerManagement(),
"Emby.Server.Mac.pkg",
environmentInfo,
imageEncoder,
2017-08-13 20:30:53 +00:00
new SystemEvents(logManager.GetLogger("SystemEvents")),
2017-08-22 18:50:58 +00:00
new NetworkManager(logManager.GetLogger("NetworkManager")));
2014-11-24 00:02:01 +00:00
if (options.ContainsOption("-v")) {
2014-11-29 04:03:47 +00:00
Console.WriteLine (AppHost.ApplicationVersion.ToString());
2014-11-24 00:02:01 +00:00
return;
}
Console.WriteLine ("appHost.Init");
2014-11-28 17:40:20 +00:00
Task.Run (() => StartServer(CancellationToken.None));
2016-11-14 05:45:13 +00:00
}
2017-05-13 04:26:39 +00:00
private static IImageEncoder GetImageEncoder(ServerApplicationPaths appPaths, IFileSystem fileSystem, ILogManager logManager)
{
try
{
return new SkiaEncoder(logManager.GetLogger("Skia"), appPaths, () => AppHost.HttpClient, fileSystem);
}
catch (Exception ex)
{
return new NullImageEncoder();
}
}
2016-11-14 05:50:58 +00:00
private static EnvironmentInfo GetEnvironmentInfo()
2016-11-14 05:45:13 +00:00
{
2016-12-22 04:47:59 +00:00
var info = new EnvironmentInfo()
{
2017-08-22 18:47:53 +00:00
OperatingSystem = MediaBrowser.Model.System.OperatingSystem.OSX
2016-12-22 04:47:59 +00:00
};
2016-11-14 05:45:13 +00:00
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))
{
//info.OperatingSystem = Startup.Common.OperatingSystem.Bsd;
2016-11-14 05:50:58 +00:00
//info.IsBsd = true;
2016-11-14 05:45:13 +00:00
}
var archX86 = new Regex("(i|I)[3-6]86");
if (archX86.IsMatch(uname.machine))
{
2017-08-22 18:47:53 +00:00
info.SystemArchitecture = Architecture.X86;
2016-11-14 05:45:13 +00:00
}
else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
{
2017-08-22 18:47:53 +00:00
info.SystemArchitecture = Architecture.X64;
2016-11-14 05:45:13 +00:00
}
else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
{
2017-08-22 18:47:53 +00:00
info.SystemArchitecture = Architecture.Arm;
2016-11-14 05:45:13 +00:00
}
else if (System.Environment.Is64BitOperatingSystem)
{
2017-08-22 18:47:53 +00:00
info.SystemArchitecture = Architecture.X64;
2016-11-14 05:45:13 +00:00
}
else
{
2017-08-22 18:47:53 +00:00
info.SystemArchitecture = Architecture.X86;
2016-11-14 05:45:13 +00:00
}
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;
}
private static async void StartServer(CancellationToken cancellationToken)
2014-11-28 17:40:20 +00:00
{
var initProgress = new Progress<double>();
2014-11-24 00:02:01 +00:00
2014-11-29 04:03:47 +00:00
await AppHost.Init (initProgress).ConfigureAwait (false);
await AppHost.RunStartupTasks ().ConfigureAwait (false);
2014-11-27 01:56:14 +00:00
2014-11-29 04:03:47 +00:00
if (MenuBarIcon.Instance != null)
{
MenuBarIcon.Instance.Localize ();
}
2014-11-24 00:02:01 +00:00
}
public static void Shutdown()
{
ShutdownApp();
}
private static void ShutdownApp()
{
_logger.Info ("Calling ApplicationHost.Dispose");
2014-11-29 04:03:47 +00:00
AppHost.Dispose ();
2014-11-24 00:02:01 +00:00
_logger.Info("AppController.Terminate");
2014-11-28 17:40:20 +00:00
MenuBarIcon.Instance.Terminate ();
2014-11-24 00:02:01 +00:00
}
2015-05-23 22:01:13 +00:00
public static void Restart()
{
_logger.Info("Disposing app host");
AppHost.Dispose();
_logger.Info("Starting new instance");
var args = Environment.GetCommandLineArgs()
2015-05-23 22:27:05 +00:00
.Skip(1)
2015-05-23 22:01:13 +00:00
.Select(NormalizeCommandLineArgument);
var commandLineArgsString = string.Join(" ", args.ToArray());
2015-05-23 22:27:05 +00:00
var module = Environment.GetCommandLineArgs().First();
_logger.Info ("Executable: {0}", module);
_logger.Info ("Arguments: {0}", commandLineArgsString);
2015-05-23 22:01:13 +00:00
2015-05-23 22:27:05 +00:00
Process.Start(module, commandLineArgsString);
2015-05-23 22:01:13 +00:00
_logger.Info("AppController.Terminate");
MenuBarIcon.Instance.Terminate();
}
2015-05-23 22:08:50 +00:00
private static string NormalizeCommandLineArgument(string arg)
{
if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
{
return arg;
}
return "\"" + arg + "\"";
}
2014-11-24 00:02:01 +00:00
/// <summary>
/// Handles the UnhandledException event of the CurrentDomain control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = (Exception)e.ExceptionObject;
2017-02-24 18:00:18 +00:00
var consoleLogger = new ConsoleLogger();
new UnhandledExceptionWriter(AppHost.ServerConfigurationManager.ApplicationPaths, _logger, AppHost.LogManager, _fileSystem, consoleLogger).Log(exception);
2014-11-24 00:02:01 +00:00
if (!Debugger.IsAttached)
{
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
}
}
}
class NoCheckCertificatePolicy : ICertificatePolicy
{
2016-11-14 06:00:53 +00:00
public bool CheckValidationResult (ServicePoint srvPoint,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
WebRequest request,
int certificateProblem)
2014-11-24 00:02:01 +00:00
{
return true;
}
2016-11-14 05:50:27 +00:00
}
2014-11-21 17:08:50 +00:00
}