jellyfin/MediaBrowser.Server.Mac/Main.cs

312 lines
9.9 KiB
C#
Raw Normal View History

2016-11-14 06:45:13 +01:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Startup.Common;
using MediaBrowser.Server.Startup.Common.IO;
using MediaBrowser.Server.Implementations;
using System;
2014-11-24 01:02:01 +01:00
using System.Diagnostics;
2016-11-14 06:45:13 +01:00
using System.Globalization;
2014-11-24 01:02:01 +01:00
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
2016-11-14 06:45:13 +01:00
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Drawing;
2014-11-24 01:02:01 +01:00
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
2014-11-21 18:08:50 +01:00
using MonoMac.AppKit;
2014-11-24 01:02:01 +01:00
using MonoMac.Foundation;
2014-11-21 18:08:50 +01:00
using MonoMac.ObjCRuntime;
2016-11-14 05:50:23 +01:00
using Emby.Server.Core;
using Emby.Common.Implementations.Logging;
2016-11-14 07:00:53 +01:00
using Emby.Common.Implementations.EnvironmentInfo;
2016-11-14 05:50:23 +01:00
using Emby.Server.Mac.Native;
2016-11-14 06:41:57 +01:00
using Emby.Server.Implementations.IO;
using Emby.Common.Implementations.Networking;
2016-11-14 06:45:13 +01:00
using Emby.Common.Implementations.Security;
using Mono.Unix.Native;
2016-11-14 07:00:53 +01:00
using MediaBrowser.Model.System;
2014-11-21 18:08:50 +01:00
namespace MediaBrowser.Server.Mac
{
class MainClass
{
2016-11-14 05:50:23 +01:00
internal static MacAppHost AppHost;
2014-11-24 01:02:01 +01:00
private static ILogger _logger;
2014-11-21 18:08:50 +01:00
static void Main (string[] args)
{
2014-11-24 01:02:01 +01:00
var applicationPath = Assembly.GetEntryAssembly().Location;
var options = new StartupOptions();
// Allow this to be specified on the command line.
var customProgramDataPath = options.GetOption("-programdata");
2016-11-14 05:50:23 +01:00
var appFolderPath = Path.GetDirectoryName(applicationPath);
var appPaths = CreateApplicationPaths(appFolderPath, customProgramDataPath);
2014-11-24 01:02:01 +01:00
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
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 18:08:50 +01:00
NSApplication.Init ();
NSApplication.Main (args);
2014-11-26 05:27:55 +01:00
}
2016-11-14 05:50:23 +01:00
private static ServerApplicationPaths CreateApplicationPaths(string appFolderPath, string programDataPath)
2014-11-24 01:02:01 +01:00
{
if (string.IsNullOrEmpty(programDataPath))
{
2014-11-26 22:16:04 +01:00
// TODO: Use CommonApplicationData? Will we always have write access?
programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "mediabrowser-server");
2015-04-16 17:41:23 +02:00
if (!Directory.Exists (programDataPath)) {
programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "emby-server");
}
2014-11-24 01:02:01 +01:00
}
2014-11-27 02:56:14 +01:00
// Within the mac bundle, go uo two levels then down into Resources folder
2016-11-14 05:50:23 +01:00
var resourcesPath = Path.Combine(Path.GetDirectoryName(appFolderPath), "Resources");
2014-11-24 01:02:01 +01:00
2016-11-14 05:50:23 +01:00
return new ServerApplicationPaths(programDataPath, appFolderPath, resourcesPath);
2014-11-24 01:02:01 +01: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; });
2016-11-14 05:50:23 +01:00
var fileSystem = new MonoFileSystem(logManager.GetLogger("FileSystem"), false, false);
2015-10-30 02:55:42 +01:00
fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
2014-11-24 01:02:01 +01:00
2016-11-14 06:41:57 +01:00
var environmentInfo = GetEnvironmentInfo();
var imageEncoder = ImageEncoderHelper.GetImageEncoder(_logger,
logManager,
fileSystem,
options,
() => AppHost.HttpClient,
appPaths);
AppHost = new MacAppHost(appPaths,
logManager,
options,
fileSystem,
new PowerManagement(),
"Emby.Server.Mac.pkg",
environmentInfo,
imageEncoder,
new Startup.Common.SystemEvents(logManager.GetLogger("SystemEvents")),
new MemoryStreamProvider(),
new NetworkManager(logManager.GetLogger("NetworkManager")),
GenerateCertificate,
() => Environment.UserName);
2014-11-24 01:02:01 +01:00
if (options.ContainsOption("-v")) {
2014-11-29 05:03:47 +01:00
Console.WriteLine (AppHost.ApplicationVersion.ToString());
2014-11-24 01:02:01 +01:00
return;
}
Console.WriteLine ("appHost.Init");
2014-11-28 18:40:20 +01:00
Task.Run (() => StartServer(CancellationToken.None));
2016-11-14 06:45:13 +01:00
}
private static void GenerateCertificate(string certPath, string certHost)
{
CertificateGenerator.CreateSelfSignCertificatePfx(certPath, certHost, _logger);
}
2016-11-14 06:50:58 +01:00
private static EnvironmentInfo GetEnvironmentInfo()
2016-11-14 06:45:13 +01:00
{
2016-11-14 06:50:58 +01:00
var info = new EnvironmentInfo();
2016-11-14 06:45:13 +01: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 06:50:58 +01:00
//info.IsBsd = true;
2016-11-14 06:45:13 +01:00
}
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;
}
private static async void StartServer(CancellationToken cancellationToken)
2014-11-28 18:40:20 +01:00
{
var initProgress = new Progress<double>();
2014-11-24 01:02:01 +01:00
2014-11-29 05:03:47 +01:00
await AppHost.Init (initProgress).ConfigureAwait (false);
await AppHost.RunStartupTasks ().ConfigureAwait (false);
2014-11-27 02:56:14 +01:00
2014-11-29 05:03:47 +01:00
if (MenuBarIcon.Instance != null)
{
MenuBarIcon.Instance.Localize ();
}
2014-11-24 01:02:01 +01:00
}
public static void Shutdown()
{
ShutdownApp();
}
private static void ShutdownApp()
{
_logger.Info ("Calling ApplicationHost.Dispose");
2014-11-29 05:03:47 +01:00
AppHost.Dispose ();
2014-11-24 01:02:01 +01:00
_logger.Info("AppController.Terminate");
2014-11-28 18:40:20 +01:00
MenuBarIcon.Instance.Terminate ();
2014-11-24 01:02:01 +01:00
}
2015-05-24 00:01:13 +02:00
public static void Restart()
{
_logger.Info("Disposing app host");
AppHost.Dispose();
_logger.Info("Starting new instance");
var args = Environment.GetCommandLineArgs()
2015-05-24 00:27:05 +02:00
.Skip(1)
2015-05-24 00:01:13 +02:00
.Select(NormalizeCommandLineArgument);
var commandLineArgsString = string.Join(" ", args.ToArray());
2015-05-24 00:27:05 +02:00
var module = Environment.GetCommandLineArgs().First();
_logger.Info ("Executable: {0}", module);
_logger.Info ("Arguments: {0}", commandLineArgsString);
2015-05-24 00:01:13 +02:00
2015-05-24 00:27:05 +02:00
Process.Start(module, commandLineArgsString);
2015-05-24 00:01:13 +02:00
_logger.Info("AppController.Terminate");
MenuBarIcon.Instance.Terminate();
}
2015-05-24 00:08:50 +02:00
private static string NormalizeCommandLineArgument(string arg)
{
if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
{
return arg;
}
return "\"" + arg + "\"";
}
2014-11-24 01:02:01 +01: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;
2014-11-29 05:03:47 +01:00
new UnhandledExceptionWriter(AppHost.ServerConfigurationManager.ApplicationPaths, _logger, AppHost.LogManager).Log(exception);
2014-11-24 01:02:01 +01:00
if (!Debugger.IsAttached)
{
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
}
}
}
class NoCheckCertificatePolicy : ICertificatePolicy
{
2016-11-14 07:00:53 +01:00
public bool CheckValidationResult (ServicePoint srvPoint,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
WebRequest request,
int certificateProblem)
2014-11-24 01:02:01 +01:00
{
return true;
}
2016-11-14 06:50:27 +01:00
}
2014-11-21 18:08:50 +01:00
}