2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Drawing;
|
2019-10-03 10:18:20 +02:00
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAdmin.Dialogs;
|
2019-09-25 10:40:03 +02:00
|
|
|
|
using XenAdmin.Help;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.XenSearch;
|
|
|
|
|
using XenAPI;
|
2017-11-17 00:30:51 +01:00
|
|
|
|
using XenCenterLib;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin
|
|
|
|
|
{
|
|
|
|
|
public static class Program
|
|
|
|
|
{
|
|
|
|
|
public const int DEFAULT_WLB_PORT = 8012;
|
|
|
|
|
|
2016-10-12 20:09:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Module for authenticating with proxy server using the Basic authentication scheme.
|
|
|
|
|
/// </summary>
|
2018-09-28 12:38:09 +02:00
|
|
|
|
private static IAuthenticationModule BasicAuthenticationModule;
|
2016-10-12 20:09:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Module for authenticating with proxy server using the Digest authentication scheme.
|
|
|
|
|
/// </summary>
|
2018-09-28 12:38:09 +02:00
|
|
|
|
private static IAuthenticationModule DigestAuthenticationModule;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A UUID for the current instance of XenCenter. Used to identify our own task instances.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static readonly string XenCenterUUID = Guid.NewGuid().ToString();
|
|
|
|
|
|
|
|
|
|
private static NamedPipes.Pipe pipe;
|
|
|
|
|
private const string PIPE_PATH_PATTERN = @"\\.\pipe\XenCenter-{0}-{1}-{2}";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Color.Transparent on most platforms; SystemColors.Window when ClearType is enabled.
|
|
|
|
|
/// This is to work around a bug in TextRenderer.DrawText which causes text written to a double-buffer
|
|
|
|
|
/// using ClearType to anti-alias onto black rather than onto the background colour. In this case,
|
|
|
|
|
/// we use Window, because by luck those labels are always on top of that colour on Vista and XP.
|
|
|
|
|
/// We indicate that we're writing to a buffer (rather than the screen) by setting Graphics.TextContrast
|
|
|
|
|
/// to 5 (the default is 4). This hack was needed because there's no easy way to add info to
|
|
|
|
|
/// a Graphics object. (CA-22938).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Color TransparentUsually = Color.Transparent;
|
|
|
|
|
|
|
|
|
|
public static Font DefaultFont = FormFontFixer.DefaultFont;
|
|
|
|
|
public static Font DefaultFontBold;
|
|
|
|
|
public static Font DefaultFontUnderline;
|
|
|
|
|
public static Font DefaultFontItalic;
|
|
|
|
|
public static Font DefaultFontHeader;
|
|
|
|
|
|
|
|
|
|
public static MainWindow MainWindow = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static CollectionChangeEventHandler ProgramInvokeHandler(CollectionChangeEventHandler handler)
|
|
|
|
|
{
|
|
|
|
|
return delegate(object s, CollectionChangeEventArgs args)
|
2018-09-28 12:38:09 +02:00
|
|
|
|
{
|
|
|
|
|
if (MainWindow != null)
|
|
|
|
|
{
|
|
|
|
|
BeginInvoke(MainWindow, delegate
|
|
|
|
|
{
|
|
|
|
|
if (handler != null)
|
|
|
|
|
handler(s, args);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The secure hash of the master password used to load the client session.
|
|
|
|
|
/// If this is null then no prior session existed and the user should be prompted
|
|
|
|
|
/// to save his session when the UI is quit.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static byte[] MasterPassword = null;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A true value here indicates the user does not want to save session information for this
|
|
|
|
|
/// particular instance of the UI; but when the UI is restarted he should be prompted again.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool SkipSessionSave = false;
|
|
|
|
|
|
|
|
|
|
public static bool RunInAutomatedTestMode = false;
|
2018-09-28 12:38:09 +02:00
|
|
|
|
public static string TestExceptionString; // an exception passed back to the test framework
|
|
|
|
|
private static log4net.ILog log;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2018-09-28 12:38:09 +02:00
|
|
|
|
public static volatile bool Exiting;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public static readonly string AssemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
|
|
|
|
|
|
|
|
private static readonly System.Threading.Timer dailyTimer;
|
|
|
|
|
|
|
|
|
|
static Program()
|
|
|
|
|
{
|
|
|
|
|
XenAdminConfigManager.Provider = new WinformsXenAdminConfigProvider();
|
|
|
|
|
// Start timer to record resource usage every 24hrs
|
2018-09-28 12:38:09 +02:00
|
|
|
|
dailyTimer = new System.Threading.Timer(delegate
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-09-28 12:38:09 +02:00
|
|
|
|
logApplicationStats();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}, null, new TimeSpan(24, 0, 0), new TimeSpan(24, 0, 0));
|
|
|
|
|
|
|
|
|
|
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Assembly.GetCallingAssembly().Location + ".config"));
|
|
|
|
|
log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
|
|
SetDefaultFonts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The main entry point for the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
|
|
|
|
static public void Main(string[] Args)
|
|
|
|
|
{
|
|
|
|
|
//Upgrade settings
|
2020-12-21 16:55:03 +01:00
|
|
|
|
string appVersionString = Version.ToString();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
log.DebugFormat("Application version of new settings {0}", appVersionString);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-12-21 16:55:03 +01:00
|
|
|
|
if (Properties.Settings.Default.ApplicationVersion != appVersionString)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
log.Debug("Upgrading settings...");
|
|
|
|
|
Properties.Settings.Default.Upgrade();
|
2014-05-30 13:39:07 +02:00
|
|
|
|
|
|
|
|
|
// if program's hash has changed (e.g. by upgrading to .NET 4.0), then Upgrade() doesn't import the previous application settings
|
|
|
|
|
// because it cannot locate a previous user.config file. In this case a new user.config file is created with the default settings.
|
|
|
|
|
// We will try and find a config file from a previous installation and update the settings from it
|
|
|
|
|
if (Properties.Settings.Default.ApplicationVersion == "" && Properties.Settings.Default.DoUpgrade)
|
|
|
|
|
SettingsUpdate.Update();
|
|
|
|
|
log.DebugFormat("Settings upgraded from '{0}' to '{1}'", Properties.Settings.Default.ApplicationVersion, appVersionString);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Properties.Settings.Default.ApplicationVersion = appVersionString;
|
2014-05-30 13:39:07 +02:00
|
|
|
|
Settings.TrySaveSettings();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (ConfigurationErrorsException ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Could not load settings.", ex);
|
|
|
|
|
var msg = string.Format("{0}\n\n{1}", Messages.MESSAGEBOX_LOAD_CORRUPTED_TITLE,
|
|
|
|
|
string.Format(Messages.MESSAGEBOX_LOAD_CORRUPTED, Settings.GetUserConfigPath()));
|
2020-04-22 15:47:03 +02:00
|
|
|
|
using (var dlg = new ErrorDialog(msg)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
StartPosition = FormStartPosition.CenterScreen,
|
|
|
|
|
//For reasons I do not fully comprehend at the moment, the runtime
|
|
|
|
|
//overrides the above StartPosition with WindowsDefaultPosition if
|
|
|
|
|
//ShowInTaskbar is false. However it's a good idea anyway to show it
|
2020-04-22 15:47:03 +02:00
|
|
|
|
//in the taskbar since the main form is not launched at this point.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ShowInTaskbar = true
|
2016-06-20 11:49:12 +02:00
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
dlg.ShowDialog();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Application.Exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset statics, because XenAdminTests likes to call Main() twice.
|
|
|
|
|
TestExceptionString = null;
|
|
|
|
|
Exiting = false;
|
|
|
|
|
// Clear XenConnections and History so static classes like OtherConfigAndTagsWatcher
|
|
|
|
|
// listening to changes still work when Main is called more than once.
|
|
|
|
|
ConnectionsManager.XenConnections.Clear();
|
|
|
|
|
ConnectionsManager.History.Clear();
|
|
|
|
|
|
2020-01-22 15:47:39 +01:00
|
|
|
|
Search.InitSearch(BrandManager.ExtensionSearch);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
TreeSearch.InitSearch();
|
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
|
Application.ThreadException -= Application_ThreadException;
|
|
|
|
|
Application.ThreadException += Application_ThreadException;
|
2021-04-30 18:39:57 +02:00
|
|
|
|
|
|
|
|
|
// We must NEVER request ProfessionalColors before we have called Application.EnableVisualStyles
|
|
|
|
|
// as it may prevent the program from displayed as expected.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Application.EnableVisualStyles();
|
2021-04-30 18:39:57 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (SystemInformation.FontSmoothingType == 2) // ClearType
|
|
|
|
|
TransparentUsually = SystemColors.Window;
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException)
|
|
|
|
|
{
|
|
|
|
|
// Leave TransparentUsually == Color.Transparent. This is an old platform
|
|
|
|
|
// without FontSmoothingType support.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Force the current culture, to make the layout the same whatever the culture of the underlying OS (CA-46983).
|
|
|
|
|
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = new CultureInfo(InvisibleMessages.LOCALE, false);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
|
|
|
|
|
Thread.CurrentThread.Name = "Main program thread";
|
|
|
|
|
|
|
|
|
|
ServicePointManager.DefaultConnectionLimit = 20;
|
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = SSL.ValidateServerCertificate;
|
2015-07-30 13:47:14 +02:00
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
|
2021-04-13 13:02:49 +02:00
|
|
|
|
Session.UserAgent = $"{BrandManager.BrandConsole} {Version}";
|
2016-10-12 20:09:35 +02:00
|
|
|
|
RememberProxyAuthenticationModules();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ReconfigureConnectionSettings();
|
|
|
|
|
|
|
|
|
|
log.Info("Application started");
|
|
|
|
|
logSystemDetails();
|
2020-06-19 18:31:01 +02:00
|
|
|
|
Settings.Log();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// Remove the '--wait' argument, which may have been passed to the splash screen
|
2019-10-03 10:18:20 +02:00
|
|
|
|
var sanitizedArgs = Args.Where(ar => ar != "--wait").ToArray();
|
|
|
|
|
|
|
|
|
|
var firstArgType = ParseFileArgs(sanitizedArgs, out string[] tailArgs);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-10-03 10:18:20 +02:00
|
|
|
|
if (firstArgType == ArgType.Passwords)
|
|
|
|
|
{
|
|
|
|
|
try
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-10-03 10:18:20 +02:00
|
|
|
|
PasswordsRequest.HandleRequest(tailArgs[0]);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2019-10-03 10:18:20 +02:00
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
log.Fatal(exn, exn);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Application.Exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ConnectPipe();
|
|
|
|
|
}
|
2018-09-28 12:38:09 +02:00
|
|
|
|
catch (Win32Exception exn)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
log.Error("Creating named pipe failed. Continuing to launch XenCenter.", exn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Application.ApplicationExit -= Application_ApplicationExit;
|
|
|
|
|
Application.ApplicationExit += Application_ApplicationExit;
|
|
|
|
|
|
2019-10-03 10:18:20 +02:00
|
|
|
|
MainWindow mainWindow = new MainWindow(firstArgType, tailArgs);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Application.Run(mainWindow);
|
|
|
|
|
|
|
|
|
|
log.Info("Application main thread exited");
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-03 10:18:20 +02:00
|
|
|
|
private static ArgType ParseFileArgs(string[] args, out string[] tailArgs)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-10-03 10:18:20 +02:00
|
|
|
|
tailArgs = new string[0];
|
|
|
|
|
|
|
|
|
|
if (args == null || args.Length < 2)
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Too few args passed in via command line");
|
|
|
|
|
return ArgType.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var firstArgType = ArgType.None;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
switch (args[0])
|
|
|
|
|
{
|
|
|
|
|
case "import":
|
2019-10-03 10:18:20 +02:00
|
|
|
|
firstArgType = ArgType.Import;
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
case "license":
|
2019-10-03 10:18:20 +02:00
|
|
|
|
firstArgType = ArgType.License;
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
case "restore":
|
2019-10-03 10:18:20 +02:00
|
|
|
|
firstArgType = ArgType.Restore;
|
|
|
|
|
break;
|
2016-01-20 04:33:54 +01:00
|
|
|
|
case "search":
|
2019-10-03 10:18:20 +02:00
|
|
|
|
firstArgType = ArgType.XenSearch;
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
case "passwords":
|
2019-10-03 10:18:20 +02:00
|
|
|
|
firstArgType = ArgType.Passwords;
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
case "connect":
|
2019-10-03 10:18:20 +02:00
|
|
|
|
firstArgType = ArgType.Connect;
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
default:
|
2019-10-03 10:18:20 +02:00
|
|
|
|
log.Warn("Wrong syntax or unknown command line options.");
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2019-10-03 10:18:20 +02:00
|
|
|
|
|
|
|
|
|
if (firstArgType != ArgType.None)
|
|
|
|
|
{
|
|
|
|
|
log.InfoFormat("Command line option passed in: {0}", firstArgType.ToString());
|
|
|
|
|
tailArgs = args.Skip(1).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return firstArgType;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Connects to the XenCenter named pipe. If the pipe didn't already exist, a new thread is started
|
|
|
|
|
/// that listens for incoming data on the pipe (from new invocations of XenCenter) and deals
|
|
|
|
|
/// with the command line arguments of those instances. If the pipe does exist, a Win32Exception is thrown.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <exception cref="Win32Exception">If creating the pipe failed for any reason.</exception>
|
|
|
|
|
private static void ConnectPipe()
|
|
|
|
|
{
|
|
|
|
|
string pipe_path = string.Format(PIPE_PATH_PATTERN, Process.GetCurrentProcess().SessionId, Environment.UserName, Assembly.GetExecutingAssembly().Location.Replace('\\', '-'));
|
|
|
|
|
|
|
|
|
|
// Pipe path must be limited to 256 characters in length
|
|
|
|
|
if (pipe_path.Length > 256)
|
|
|
|
|
{
|
|
|
|
|
pipe_path = pipe_path.Substring(0, 256);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.InfoFormat(@"Connecting to pipe '{0}'", pipe_path);
|
|
|
|
|
// Line below may throw Win32Exception
|
|
|
|
|
pipe = new NamedPipes.Pipe(pipe_path);
|
|
|
|
|
|
|
|
|
|
log.InfoFormat(@"Successfully created pipe '{0}' - proceeding to launch XenCenter", pipe_path);
|
2019-10-03 10:18:20 +02:00
|
|
|
|
|
2018-09-28 12:38:09 +02:00
|
|
|
|
pipe.Read += delegate(object sender, NamedPipes.PipeReadEventArgs e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-09-28 12:38:09 +02:00
|
|
|
|
MainWindow m = MainWindow;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (m == null || RunInAutomatedTestMode)
|
|
|
|
|
return;
|
2019-10-03 10:18:20 +02:00
|
|
|
|
|
2021-04-26 11:19:17 +02:00
|
|
|
|
var bits = e.Message.Split(' ').Where(ar => ar != "--wait").ToArray();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-04-26 11:19:17 +02:00
|
|
|
|
var firstArgType = ParseFileArgs(bits, out string[] tailArgs);
|
2019-10-03 10:18:20 +02:00
|
|
|
|
|
|
|
|
|
if (firstArgType == ArgType.Passwords)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-02-26 02:26:53 +01:00
|
|
|
|
log.ErrorFormat("Refusing to accept passwords request down pipe. Use {0}Main.exe directly", BrandManager.BrandConsole.Replace(" ",""));
|
2019-10-03 10:18:20 +02:00
|
|
|
|
return;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2019-10-03 10:18:20 +02:00
|
|
|
|
if (firstArgType == ArgType.Connect)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-02-26 02:26:53 +01:00
|
|
|
|
log.ErrorFormat("Connect not supported down pipe. Use {0}Main.exe directly", BrandManager.BrandConsole.Replace(" ",""));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-03 10:18:20 +02:00
|
|
|
|
if (firstArgType == ArgType.None)
|
|
|
|
|
return;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-04-26 11:19:17 +02:00
|
|
|
|
// The C++ splash screen passes its command line as a literal string.
|
|
|
|
|
// This means we will get an e.Message like
|
|
|
|
|
// open "C:\Documents and Settings\foo.xva"
|
|
|
|
|
// INCLUDING the double quotes, thus we need to trim them
|
2019-10-03 10:18:20 +02:00
|
|
|
|
|
2021-04-26 11:19:17 +02:00
|
|
|
|
var argument = tailArgs[0];
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-04-26 11:19:17 +02:00
|
|
|
|
if (argument.StartsWith("\""))
|
|
|
|
|
{
|
|
|
|
|
var count = tailArgs.TakeWhile(t => !t.EndsWith("\"")).Count();
|
|
|
|
|
if (count < tailArgs.Length)
|
|
|
|
|
count++;
|
|
|
|
|
argument = string.Join(" ", tailArgs.Take(count).ToArray());
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-04-26 11:19:17 +02:00
|
|
|
|
argument = argument.Trim('"');
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-04-26 11:19:17 +02:00
|
|
|
|
Invoke(m, delegate
|
|
|
|
|
{
|
2019-10-03 10:18:20 +02:00
|
|
|
|
m.WindowState = FormWindowState.Normal;
|
2021-04-26 11:19:17 +02:00
|
|
|
|
m.ProcessCommand(firstArgType, argument);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
});
|
2018-09-28 12:38:09 +02:00
|
|
|
|
};
|
2021-04-26 11:19:17 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
pipe.BeginRead();
|
|
|
|
|
// We created the pipe successfully - i.e. nobody was listening, so go ahead and start XenCenter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void DisconnectPipe()
|
|
|
|
|
{
|
|
|
|
|
if (pipe != null)
|
|
|
|
|
{
|
|
|
|
|
log.Debug("Disconnecting from named pipe in Program.DisconnectPipe()");
|
2018-09-28 12:38:09 +02:00
|
|
|
|
ThreadPool.QueueUserWorkItem(state => pipe.Disconnect());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void logSystemDetails()
|
|
|
|
|
{
|
2020-12-21 16:55:03 +01:00
|
|
|
|
log.InfoFormat("Version: {0}", Version);
|
2018-09-28 12:38:09 +02:00
|
|
|
|
log.InfoFormat(".NET runtime version: {0}", Environment.Version.ToString(4));
|
|
|
|
|
log.InfoFormat("OS version: {0}", Environment.OSVersion);
|
|
|
|
|
log.InfoFormat("UI Culture: {0}", Thread.CurrentThread.CurrentUICulture.EnglishName);
|
|
|
|
|
log.InfoFormat("Bitness: {0}-bit", IntPtr.Size * 8);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void Application_ApplicationExit(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Exiting = true;
|
|
|
|
|
|
|
|
|
|
logApplicationStats();
|
|
|
|
|
|
|
|
|
|
Clip.UnregisterClipboardViewer();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Lets save the connections first, so we can save their connected state
|
2020-10-27 03:10:44 +01:00
|
|
|
|
Settings.SaveServerList(); //this calls Settings.TrySaveSettings()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
// Ignore here
|
|
|
|
|
}
|
|
|
|
|
// The application is about to exit - gracefully close connections to
|
|
|
|
|
// avoid a bunch of WinForms related race conditions...
|
2018-09-28 12:38:09 +02:00
|
|
|
|
foreach (IXenConnection conn in ConnectionsManager.XenConnectionsCopy)
|
2017-11-27 07:38:21 +01:00
|
|
|
|
conn.EndConnect(false, true);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void logApplicationStats()
|
|
|
|
|
{
|
|
|
|
|
Process p = Process.GetCurrentProcess();
|
|
|
|
|
|
|
|
|
|
log.Info("Log Application State");
|
|
|
|
|
log.InfoFormat("ExitCode: {0}", Environment.ExitCode);
|
|
|
|
|
|
|
|
|
|
log.InfoFormat("Time since process started: {0}", (DateTime.Now - Process.GetCurrentProcess().StartTime).ToString());
|
|
|
|
|
|
|
|
|
|
log.InfoFormat("Handles open: {0}", p.HandleCount.ToString());
|
|
|
|
|
log.InfoFormat("USER handles open: {0}", Win32.GetGuiResourcesUserCount(p.Handle));
|
|
|
|
|
log.InfoFormat("GDI handles open: {0}", Win32.GetGuiResourcesGDICount(p.Handle));
|
|
|
|
|
log.InfoFormat("Thread count: {0}", p.Threads.Count);
|
|
|
|
|
|
2015-09-11 13:05:25 +02:00
|
|
|
|
log.InfoFormat("Virtual memory size: {0} B({1})", p.VirtualMemorySize64, Util.MemorySizeStringSuitableUnits(p.VirtualMemorySize64, false));
|
|
|
|
|
log.InfoFormat("Working set: {0} B({1})", p.WorkingSet64, Util.MemorySizeStringSuitableUnits(p.WorkingSet64, false));
|
|
|
|
|
log.InfoFormat("Private memory size: {0} B({1})",p.PrivateMemorySize64, Util.MemorySizeStringSuitableUnits(p.PrivateMemorySize64, false));
|
|
|
|
|
log.InfoFormat("Nonpaged system memory size: {0} B({1})", p.NonpagedSystemMemorySize64, Util.MemorySizeStringSuitableUnits(p.NonpagedSystemMemorySize64, false));
|
|
|
|
|
log.InfoFormat("Paged memory size: {0} B({1})", p.PagedMemorySize64, Util.MemorySizeStringSuitableUnits(p.PagedMemorySize64, false));
|
|
|
|
|
log.InfoFormat("Paged system memory size: {0} B({1})", p.PagedSystemMemorySize64, Util.MemorySizeStringSuitableUnits(p.PagedSystemMemorySize64, false));
|
|
|
|
|
|
|
|
|
|
log.InfoFormat("Peak paged memory size: {0} B({1})", p.PeakPagedMemorySize64, Util.MemorySizeStringSuitableUnits(p.PeakPagedMemorySize64, false));
|
|
|
|
|
log.InfoFormat("Peak virtual memory size: {0} B({1})", p.PeakVirtualMemorySize64, Util.MemorySizeStringSuitableUnits(p.PeakVirtualMemorySize64, false));
|
|
|
|
|
log.InfoFormat("Peak working set: {0} B({1})", p.PeakWorkingSet64, Util.MemorySizeStringSuitableUnits(p.PeakWorkingSet64, false));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
log.InfoFormat("Process priority class: {0}", p.PriorityClass.ToString());
|
|
|
|
|
log.InfoFormat("Privileged processor time: {0}", p.PrivilegedProcessorTime.ToString());
|
|
|
|
|
|
|
|
|
|
log.InfoFormat("Total processor time: {0}", p.TotalProcessorTime.ToString());
|
|
|
|
|
log.InfoFormat("User processor time: {0}", p.UserProcessorTime.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 12:38:09 +02:00
|
|
|
|
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
ProcessUnhandledException(e.Exception);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
ProcessUnhandledException(e.ExceptionObject as Exception);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 17:27:45 +02:00
|
|
|
|
private static void ProcessUnhandledException(Exception e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (e != null)
|
|
|
|
|
{
|
|
|
|
|
log.Fatal("Uncaught exception", e);
|
|
|
|
|
if (e.InnerException != null)
|
|
|
|
|
log.Fatal("Inner exception", e.InnerException);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Fatal("Fatal error");
|
|
|
|
|
}
|
|
|
|
|
logSystemDetails();
|
|
|
|
|
logApplicationStats();
|
|
|
|
|
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (RunInAutomatedTestMode)
|
|
|
|
|
{
|
|
|
|
|
if (e != null && TestExceptionString == null)
|
|
|
|
|
TestExceptionString = e.GetBaseException().ToString();
|
|
|
|
|
}
|
|
|
|
|
else
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-09-25 00:05:58 +02:00
|
|
|
|
string filepath = GetLogFile() ?? Messages.MESSAGEBOX_LOGFILE_MISSING;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-04-22 15:47:03 +02:00
|
|
|
|
using (var d = new ErrorDialog(String.Format(Messages.MESSAGEBOX_PROGRAM_UNEXPECTED, HelpersGUI.DateTimeToString(DateTime.Now, "yyyy-MM-dd HH:mm:ss", false), filepath))
|
2021-03-16 02:50:45 +01:00
|
|
|
|
{WindowTitle = string.Format(Messages.MESSAGEBOX_PROGRAM_UNEXPECTED_TITLE, BrandManager.BrandConsole)})
|
2016-06-20 11:49:12 +02:00
|
|
|
|
{
|
|
|
|
|
// CA-44733
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(MainWindow) && !MainWindow.InvokeRequired)
|
2016-06-20 11:49:12 +02:00
|
|
|
|
d.ShowDialog(MainWindow);
|
|
|
|
|
else
|
|
|
|
|
d.ShowDialog();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
log.Fatal("Fatal error while handling fatal error!", exception);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (!RunInAutomatedTestMode)
|
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
|
using (var dlg = new ErrorDialog(exception.ToString()))
|
2016-06-20 11:49:12 +02:00
|
|
|
|
dlg.ShowDialog();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
// To be handled by WER
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 12:38:09 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the default fonts.
|
|
|
|
|
/// Some locales have a known correct font, which we just use. Otherwise (e.g. English),
|
|
|
|
|
/// we need to make sure that our fonts fit in with our panel layout. This can be a problem
|
|
|
|
|
/// on far-Eastern platforms in particular, because the default fonts there are wider in the
|
|
|
|
|
/// English range than Tahoma / Segoe UI. Anything bigger than them is going to cause us to
|
|
|
|
|
/// overflow on the panels, so we force it back to an appropriate font. We need to be careful
|
|
|
|
|
/// not to override the user's settings if they've deliberately scaled up the text for readability
|
|
|
|
|
/// reasons, so we only override if the font size is 9pt or smaller, which is the usual default.
|
|
|
|
|
/// We also define a registry key to turn this off, just in case someone in the field complains.
|
|
|
|
|
/// </summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private static void SetDefaultFonts()
|
|
|
|
|
{
|
|
|
|
|
if (Registry.ForceSystemFonts)
|
|
|
|
|
{
|
|
|
|
|
log.Debug("ForceSystemFonts registry key is defined");
|
|
|
|
|
}
|
|
|
|
|
else if (InvisibleMessages.LOCALE.StartsWith("ja-"))
|
|
|
|
|
{
|
|
|
|
|
Font new_font =
|
|
|
|
|
(Environment.OSVersion.Version.Major < 6 || Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 0) ?
|
|
|
|
|
new Font("MS UI Gothic", 9.0f) : // 2K (5.0), XP (5.1), 2K3 and XP Pro (5.2), Vista and 2K8 (6.0)
|
|
|
|
|
new Font("Meiryo UI", 9.0f); // Win 7 and 2K8R2 (6.1) and above
|
|
|
|
|
log.DebugFormat("Running on localized resources with Japanese fonts; switching the font from {0} {1}pt to {2} {3}pt.",
|
|
|
|
|
DefaultFont.Name, DefaultFont.Size, new_font.Name, new_font.Size);
|
|
|
|
|
SetDefaultFonts(new_font);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (InvisibleMessages.LOCALE.StartsWith("zh-"))
|
|
|
|
|
{
|
|
|
|
|
Font new_font =
|
|
|
|
|
Environment.OSVersion.Version.Major < 6 ?
|
|
|
|
|
new Font("MS Shell Dlg", 9.0f) : // 2K (5.0), XP (5.1), 2K3 and XP Pro (5.2)
|
|
|
|
|
new Font("Microsoft YaHei", 9.0f); // Vista and above
|
|
|
|
|
log.DebugFormat("Running on localized resources with Simplified Chinese fonts; switching the font from {0} {1}pt to {2} {3}pt.",
|
|
|
|
|
DefaultFont.Name, DefaultFont.Size, new_font.Name, new_font.Size);
|
|
|
|
|
SetDefaultFonts(new_font);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (DefaultFont.Size <= 9.0f)
|
|
|
|
|
{
|
|
|
|
|
Size s = Drawing.MeasureText("Lorum ipsum", DefaultFont);
|
|
|
|
|
// Segoe UI 9pt gives 78x15 here. Tahoma 8pt gives 66x13.
|
|
|
|
|
// We allow a bit of slop just in case antialias or hinting settings make a small difference.
|
|
|
|
|
if (s.Width > 80 || s.Height > 16)
|
|
|
|
|
{
|
|
|
|
|
Font new_font =
|
|
|
|
|
Environment.OSVersion.Version.Major < 6 ?
|
|
|
|
|
new Font("Tahoma", 8.0f) : // 2K (5.0), XP (5.1), 2K3 and XP Pro (5.2)
|
|
|
|
|
new Font("Segoe UI", 9.0f); // Vista and above
|
|
|
|
|
|
|
|
|
|
log.DebugFormat("Running on default resources with large default font; switching the font from {0} {1}pt to {2} {3}pt.",
|
|
|
|
|
DefaultFont.Name, DefaultFont.Size, new_font.Name, new_font.Size);
|
|
|
|
|
SetDefaultFonts(new_font);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Debug("Running on default resources and happy with the font.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Debug("Running on default resources but the font is bigger than usual.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.DebugFormat("Leaving the default font as {0} {1}pt.", DefaultFont.Name, DefaultFont.Size);
|
|
|
|
|
SetDefaultFonts(DefaultFont);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SetDefaultFonts(Font f)
|
|
|
|
|
{
|
|
|
|
|
DefaultFont = f;
|
|
|
|
|
DefaultFontBold = new Font(f, FontStyle.Bold);
|
|
|
|
|
DefaultFontUnderline = new Font(f, FontStyle.Underline);
|
|
|
|
|
DefaultFontItalic = new Font(f, FontStyle.Italic);
|
|
|
|
|
DefaultFontHeader = new Font(f.FontFamily, 9.75f, FontStyle.Bold);
|
|
|
|
|
|
|
|
|
|
FormFontFixer.DefaultFont = f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void AssertOffEventThread()
|
|
|
|
|
{
|
2018-09-28 12:38:09 +02:00
|
|
|
|
if (MainWindow.Visible && !MainWindow.InvokeRequired)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
FatalError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void AssertOnEventThread()
|
|
|
|
|
{
|
2018-09-28 12:38:09 +02:00
|
|
|
|
if (MainWindow != null && MainWindow.Visible && MainWindow.InvokeRequired)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
FatalError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void FatalError()
|
|
|
|
|
{
|
|
|
|
|
string msg = String.Format(Messages.MESSAGEBOX_PROGRAM_UNEXPECTED,
|
2019-09-25 00:05:58 +02:00
|
|
|
|
HelpersGUI.DateTimeToString(DateTime.Now, "yyyy-MM-dd HH:mm:ss", false), GetLogFile());
|
2018-09-28 12:38:09 +02:00
|
|
|
|
|
|
|
|
|
var msgWithStackTrace = string.Format("{0}\n{1}", msg, Environment.StackTrace);
|
|
|
|
|
log.Fatal(msgWithStackTrace);
|
|
|
|
|
|
|
|
|
|
MainWindow m = MainWindow;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (m == null)
|
|
|
|
|
{
|
|
|
|
|
log.Fatal("Program.MainWindow is null");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.FatalFormat("Program.MainWindow.Visible == {0}", m.Visible);
|
|
|
|
|
log.FatalFormat("Program.MainWindow.InvokeRequired == {0}", m.InvokeRequired);
|
|
|
|
|
log.FatalFormat("CurrentThread.Name == {0}", Thread.CurrentThread.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (RunInAutomatedTestMode)
|
|
|
|
|
{
|
|
|
|
|
if (TestExceptionString == null)
|
2018-09-28 12:38:09 +02:00
|
|
|
|
TestExceptionString = msgWithStackTrace;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-16 02:50:45 +01:00
|
|
|
|
using (var dlg = new ErrorDialog(msg)
|
|
|
|
|
{WindowTitle = string.Format(Messages.MESSAGEBOX_PROGRAM_UNEXPECTED_TITLE, BrandManager.BrandConsole)})
|
2016-06-20 11:49:12 +02:00
|
|
|
|
dlg.ShowDialog();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ViewLogFiles()
|
|
|
|
|
{
|
2019-09-25 00:05:58 +02:00
|
|
|
|
string s = GetLogFile();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (s == null)
|
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
|
using (var dlg = new ErrorDialog(Messages.MESSAGEBOX_LOGFILE_MISSING))
|
2016-06-20 11:49:12 +02:00
|
|
|
|
dlg.ShowDialog();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-09-28 12:38:09 +02:00
|
|
|
|
Process.Start(Path.GetDirectoryName(s));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 00:05:58 +02:00
|
|
|
|
public static string GetLogFile()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
foreach (log4net.Appender.IAppender appender in log.Logger.Repository.GetAppenders())
|
|
|
|
|
{
|
2018-09-28 12:38:09 +02:00
|
|
|
|
var fileAppender = appender as log4net.Appender.FileAppender;
|
|
|
|
|
if (fileAppender != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// Assume that the first FileAppender is the primary log file.
|
2018-09-28 12:38:09 +02:00
|
|
|
|
return fileAppender.File;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly List<string> HiddenObjects = new List<string>();
|
|
|
|
|
|
|
|
|
|
internal static void HideObject(string opaqueRef)
|
|
|
|
|
{
|
|
|
|
|
lock (HiddenObjects)
|
|
|
|
|
{
|
|
|
|
|
HiddenObjects.Add(opaqueRef);
|
|
|
|
|
}
|
|
|
|
|
if (MainWindow != null)
|
|
|
|
|
{
|
|
|
|
|
MainWindow.RequestRefreshTreeView();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void ShowObject(string opaqueRef)
|
|
|
|
|
{
|
|
|
|
|
lock (HiddenObjects)
|
|
|
|
|
{
|
|
|
|
|
HiddenObjects.Remove(opaqueRef);
|
|
|
|
|
}
|
|
|
|
|
if (MainWindow != null)
|
|
|
|
|
{
|
|
|
|
|
MainWindow.RequestRefreshTreeView();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static bool ObjectIsHidden(string opaqueRef)
|
|
|
|
|
{
|
|
|
|
|
lock (HiddenObjects)
|
|
|
|
|
{
|
|
|
|
|
return HiddenObjects.Contains(opaqueRef);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 17:27:45 +02:00
|
|
|
|
#region Invocations
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Safely invoke the given delegate on the given control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Invoke(Control c, MethodInvoker f)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (c.InvokeRequired)
|
|
|
|
|
{
|
|
|
|
|
MethodInvoker exceptionLogger = () =>
|
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
f();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.Error(string.Format("Exception in Invoke (ControlType={0}, MethodName={1})",
|
|
|
|
|
c.GetType(), f.Method.Name), e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
c.Invoke(exceptionLogger);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
f();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (ObjectDisposedException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (InvalidAsynchronousStateException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (InvalidOperationException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Safely invoke the given delegate on the given control.
|
|
|
|
|
/// </summary>
|
2018-10-05 17:27:45 +02:00
|
|
|
|
/// <returns>The result of the function call, or null if the control cannot be invoked.</returns>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public static object Invoke(Control c, Delegate f, params object[] p)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (c.InvokeRequired)
|
|
|
|
|
{
|
|
|
|
|
Func<object> exceptionLogger = () =>
|
2018-10-05 17:27:45 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return f.DynamicInvoke(p);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
log.Error(string.Format("Exception in Invoke (ControlType={0}, MethodName={1})",
|
|
|
|
|
c.GetType(), f.Method.Name), e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
return c.Invoke(exceptionLogger);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return f.DynamicInvoke(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (ObjectDisposedException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (InvalidAsynchronousStateException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (InvalidOperationException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IAsyncResult BeginInvoke(Control c, Delegate f, params object[] p)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return c.BeginInvoke(f, p);
|
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (ObjectDisposedException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (InvalidAsynchronousStateException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (InvalidOperationException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void BeginInvoke(Control c, MethodInvoker f)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
c.BeginInvoke(f);
|
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (ObjectDisposedException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (InvalidAsynchronousStateException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-10-05 17:27:45 +02:00
|
|
|
|
catch (InvalidOperationException e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-05 17:27:45 +02:00
|
|
|
|
if (IsInvokable(c))
|
|
|
|
|
throw;
|
|
|
|
|
log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 17:27:45 +02:00
|
|
|
|
private static bool IsInvokable(Control c)
|
|
|
|
|
{
|
|
|
|
|
return !Exiting && c != null && !c.Disposing && !c.IsDisposed && c.IsHandleCreated;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public static void ReconfigureConnectionSettings()
|
|
|
|
|
{
|
2017-02-13 21:28:35 +01:00
|
|
|
|
ReconfigureProxyAuthenticationSettings();
|
2018-09-28 12:38:09 +02:00
|
|
|
|
Session.Proxy = XenAdminConfigManager.Provider.GetProxyFromSettings(null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-12 20:09:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stores the Basic and Digest authentication modules, used for proxy server authentication,
|
|
|
|
|
/// for later use; this is needed because we cannot create new instances of them and it
|
|
|
|
|
/// saves us needing to create our own custom authentication modules.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static void RememberProxyAuthenticationModules()
|
|
|
|
|
{
|
|
|
|
|
var authModules = AuthenticationManager.RegisteredModules;
|
|
|
|
|
while (authModules.MoveNext())
|
|
|
|
|
{
|
|
|
|
|
var module = (IAuthenticationModule)authModules.Current;
|
|
|
|
|
if (module.AuthenticationType == "Basic")
|
|
|
|
|
BasicAuthenticationModule = module;
|
|
|
|
|
else if (module.AuthenticationType == "Digest")
|
|
|
|
|
DigestAuthenticationModule = module;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures .NET's AuthenticationManager to only use the authentication module that is
|
|
|
|
|
/// specified in the ProxyAuthenticationMethod setting. Also sets XenAPI's HTTP class to
|
|
|
|
|
/// use the same authentication method.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static void ReconfigureProxyAuthenticationSettings()
|
|
|
|
|
{
|
|
|
|
|
var authModules = AuthenticationManager.RegisteredModules;
|
|
|
|
|
var modulesToUnregister = new List<IAuthenticationModule>();
|
|
|
|
|
|
|
|
|
|
while (authModules.MoveNext())
|
|
|
|
|
{
|
|
|
|
|
var module = (IAuthenticationModule)authModules.Current;
|
|
|
|
|
modulesToUnregister.Add(module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var module in modulesToUnregister)
|
|
|
|
|
AuthenticationManager.Unregister(module);
|
|
|
|
|
|
2017-01-20 15:47:04 +01:00
|
|
|
|
var authSetting = (HTTP.ProxyAuthenticationMethod)Properties.Settings.Default.ProxyAuthenticationMethod;
|
|
|
|
|
if (authSetting == HTTP.ProxyAuthenticationMethod.Basic)
|
2016-10-12 20:09:35 +02:00
|
|
|
|
AuthenticationManager.Register(BasicAuthenticationModule);
|
|
|
|
|
else
|
|
|
|
|
AuthenticationManager.Register(DigestAuthenticationModule);
|
|
|
|
|
|
2018-09-28 12:38:09 +02:00
|
|
|
|
HTTP.CurrentProxyAuthenticationMethod = authSetting;
|
2016-10-12 20:09:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private const string SplashWindowClass = "XenCenterSplash0001";
|
|
|
|
|
|
|
|
|
|
internal static void CloseSplash()
|
|
|
|
|
{
|
|
|
|
|
IntPtr hWnd = Win32.FindWindow(SplashWindowClass, null);
|
|
|
|
|
|
|
|
|
|
if (hWnd == IntPtr.Zero)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!Win32.PostMessage(hWnd, Win32.WM_DESTROY, IntPtr.Zero, IntPtr.Zero))
|
|
|
|
|
{
|
2018-09-28 12:38:09 +02:00
|
|
|
|
log.Warn("PostMessage WM_DESTROY failed in CloseSplash()", new Win32Exception());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void OpenURL(string url)
|
|
|
|
|
{
|
2019-11-08 22:00:03 +01:00
|
|
|
|
if (RunInAutomatedTestMode || string.IsNullOrEmpty(url))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-09-28 12:38:09 +02:00
|
|
|
|
Process.Start(url);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.Debug(string.Format("Exception while opening url {0}:", url), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If true action threads will close themselves instantly...
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool ForcedExiting = false;
|
|
|
|
|
|
2020-12-21 16:55:03 +01:00
|
|
|
|
public static Version Version => Assembly.GetExecutingAssembly().GetName().Version;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-12-21 16:55:03 +01:00
|
|
|
|
public static string CurrentLanguage => Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-12-21 16:55:03 +01:00
|
|
|
|
public static string VersionAndLanguage => $"{Version}.{CurrentLanguage}";
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-12-21 16:55:03 +01:00
|
|
|
|
public static CultureInfo CurrentCulture => Thread.CurrentThread.CurrentCulture;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 16:13:45 +02:00
|
|
|
|
public enum ArgType { Import, License, Restore, None, XenSearch, Passwords, Connect }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|