2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.Drawing.Drawing2D;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using DotNetVnc;
|
|
|
|
|
using log4net;
|
|
|
|
|
using XenAdmin.Controls.ConsoleTab;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using XenAdmin.Dialogs;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using XenAdmin.Network;
|
2017-11-17 02:04:45 +01:00
|
|
|
|
using XenCenterLib;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using Console = XenAPI.Console;
|
|
|
|
|
using Message = System.Windows.Forms.Message;
|
|
|
|
|
using Timer = System.Threading.Timer;
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.ConsoleView
|
|
|
|
|
{
|
|
|
|
|
public class XSVNCScreen : UserControl
|
|
|
|
|
{
|
|
|
|
|
private const int SHORT_RETRY_COUNT = 10;
|
|
|
|
|
private const int SHORT_RETRY_SLEEP_TIME = 100;
|
|
|
|
|
private const int RETRY_SLEEP_TIME = 5000;
|
|
|
|
|
private const int RDP_POLL_INTERVAL = 30000;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
public const int RDPPort = 3389;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private const int VNC_PORT = 5900;
|
2017-02-27 19:00:46 +01:00
|
|
|
|
private const int CONSOLE_SIZE_OFFSET = 6;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:34:02 +02:00
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.DeclaringType);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private int _connectionRetries;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private volatile bool _useVNC = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private bool _autoCaptureKeyboardAndMouse = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private readonly Color _focusColor = SystemColors.MenuHighlight;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// May only be written on the event thread. May be read off the event thread, to check whether
|
|
|
|
|
/// the VNC source has been switched during connection.
|
|
|
|
|
/// </summary>
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private volatile VNCGraphicsClient _vncClient;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private RdpClient _rdpClient;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private Timer _connectionPoller;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private VM _sourceVm;
|
|
|
|
|
private bool _sourceIsPv;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private readonly object _hostedConsolesLock = new object();
|
|
|
|
|
private List<XenRef<Console>> _hostedConsoles;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is assigned when the hosted connection connects up. It's used by PollPort to check for
|
|
|
|
|
/// the IP address from the guest metrics, so for the lifetime of that hosted connection, we can
|
|
|
|
|
/// poll for the in-guest VNC using the same session. activeSession must be accessed only under
|
|
|
|
|
/// the activeSessionLock.
|
|
|
|
|
/// </summary>
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private Session _activeSession;
|
|
|
|
|
private readonly object _activeSessionLock = new object();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-08-31 12:31:16 +02:00
|
|
|
|
/// Xvnc will block us if we're too quick with the disconnect and reconnect that we do
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// when polling for the VNC port. To get around this, we keep the connection open between poll
|
|
|
|
|
/// and proper connection. pendingVNCConnection must be accessed only under the
|
|
|
|
|
/// pendingVNCConnectionLock. Work under this lock must be non-blocking, because it's used on
|
|
|
|
|
/// Dispose.
|
|
|
|
|
/// </summary>
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private Stream _pendingVNCConnection;
|
|
|
|
|
private readonly object _pendingVNCConnectionLock = new object();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
internal EventHandler ResizeHandler;
|
|
|
|
|
|
|
|
|
|
public event EventHandler UserCancelledAuth;
|
2015-04-30 12:37:39 +02:00
|
|
|
|
public event EventHandler VncConnectionAttemptCancelled;
|
2016-08-24 12:48:42 +02:00
|
|
|
|
public event Action<bool> GpuStatusChanged;
|
2016-08-24 14:51:50 +02:00
|
|
|
|
public event Action<string> ConnectionNameChanged;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
public bool RdpVersionWarningNeeded => _rdpClient != null && _rdpClient.needsRdpVersionWarning;
|
2017-11-13 17:51:56 +01:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
internal readonly VNCTabView ParentVNCTabView;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
public bool UserWantsToSwitchProtocol { get; set; }
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private bool HasRDP => Source != null && Source.HasRDP();
|
2014-08-22 18:36:41 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether we have tried to login without providing a password (covers the case where the user
|
|
|
|
|
/// has configured VNC not to require a login password). If no password is saved, passwordless
|
|
|
|
|
/// login is tried once.
|
|
|
|
|
/// </summary>
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private bool _haveTriedLoginWithoutPassword;
|
|
|
|
|
private bool _ignoreNextError;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private Dictionary<string, string> _cachedNetworks;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The last known VNC password for this VM.
|
|
|
|
|
/// </summary>
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private char[] _vncPassword;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
internal ConsoleKeyHandler KeyHandler;
|
|
|
|
|
|
|
|
|
|
internal string ElevatedUsername;
|
|
|
|
|
internal string ElevatedPassword;
|
|
|
|
|
|
|
|
|
|
internal XSVNCScreen(VM source, EventHandler resizeHandler, VNCTabView parent, string elevatedUsername, string elevatedPassword)
|
|
|
|
|
{
|
2023-07-11 11:27:23 +02:00
|
|
|
|
ResizeHandler = resizeHandler;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
ParentVNCTabView = parent;
|
2023-07-11 11:27:23 +02:00
|
|
|
|
Source = source;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
KeyHandler = ParentVNCTabView.KeyHandler;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ElevatedUsername = elevatedUsername;
|
|
|
|
|
ElevatedPassword = elevatedPassword;
|
|
|
|
|
|
|
|
|
|
#pragma warning disable 0219
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var _ = Handle;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
#pragma warning restore 0219
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
InitSubControl();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
//We're going to try and catch when the IP address changes for the VM, and re-scan for ports.
|
|
|
|
|
if (source == null)
|
|
|
|
|
return;
|
|
|
|
|
Properties.Settings.Default.PropertyChanged += Default_PropertyChanged;
|
2023-07-11 11:30:47 +02:00
|
|
|
|
var guestMetrics = Source.Connection.Resolve(Source.guest_metrics);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (guestMetrics == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_cachedNetworks = guestMetrics.networks;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:30:26 +02:00
|
|
|
|
guestMetrics.PropertyChanged += guestMetrics_PropertyChanged;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.PropertyName == "EnableRDPPolling")
|
2022-03-29 16:40:34 +02:00
|
|
|
|
Program.Invoke(this, StartPolling);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UnregisterEventListeners()
|
|
|
|
|
{
|
|
|
|
|
if (Source == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-07-11 11:30:26 +02:00
|
|
|
|
Source.PropertyChanged -= VM_PropertyChanged;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:30:47 +02:00
|
|
|
|
var guestMetrics = Source.Connection.Resolve(Source.guest_metrics);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (guestMetrics == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-07-11 11:30:26 +02:00
|
|
|
|
guestMetrics.PropertyChanged -= guestMetrics_PropertyChanged;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void guestMetrics_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (Source == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (e.PropertyName == "networks")
|
|
|
|
|
{
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var newNetworks = (sender as VM_guest_metrics).networks;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (!EquateDictionary(newNetworks, _cachedNetworks))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
Log.InfoFormat("Detected IP address change in vm {0}, repolling for VNC/RDP...", Source.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_cachedNetworks = newNetworks;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
Program.Invoke(this, StartPolling);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private static bool EquateDictionary<T, TS>(Dictionary<T, TS> d1, Dictionary<T, TS> d2) where TS : IEquatable<TS>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (d1.Count != d2.Count)
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
foreach (var key in d1.Keys)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (!d2.ContainsKey(key) || !d2[key].Equals(d1[key]))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private bool _wasPaused = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public void Pause()
|
|
|
|
|
{
|
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_wasPaused = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
RemoteConsole.Pause();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unpause()
|
|
|
|
|
{
|
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_wasPaused = false;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
RemoteConsole.UnPause();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:31:20 +02:00
|
|
|
|
public Size DesktopSize => RemoteConsole?.DesktopSize ?? Size.Empty;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Nothrow guarantee.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
UnregisterEventListeners();
|
|
|
|
|
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_connectionPoller != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_connectionPoller.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
_connectionPoller.Dispose();
|
|
|
|
|
_connectionPoller = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
{
|
|
|
|
|
RemoteConsole.DisconnectAndDispose();
|
|
|
|
|
RemoteConsole = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.DebugFormat("Set Pending Vnc connection to null");
|
|
|
|
|
SetPendingVNCConnection(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodInvoker OnDetectRDP = null;
|
|
|
|
|
public MethodInvoker OnDetectVNC = null;
|
2022-03-29 16:40:34 +02:00
|
|
|
|
public string RdpIp;
|
|
|
|
|
public string VncIp;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
private void PollRDPPort(object sender)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (HasRDP)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (OnDetectRDP != null)
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, OnDetectRDP);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-29 16:40:34 +02:00
|
|
|
|
RdpIp = null;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
var openIp = PollPort(RDPPort, false);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
if (openIp == null)
|
|
|
|
|
return;
|
|
|
|
|
RdpIp = openIp;
|
|
|
|
|
if (OnDetectRDP != null)
|
|
|
|
|
Program.Invoke(this, OnDetectRDP);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
private void PollVNCPort(object sender)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-29 16:40:34 +02:00
|
|
|
|
VncIp = null;
|
|
|
|
|
var openIp = PollPort(VNC_PORT, true);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
if (openIp == null)
|
|
|
|
|
return;
|
|
|
|
|
VncIp = openIp;
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_connectionPoller?.Change(Timeout.Infinite, Timeout.Infinite);
|
2022-03-29 16:40:34 +02:00
|
|
|
|
|
|
|
|
|
if (OnDetectVNC != null)
|
|
|
|
|
Program.Invoke(this, OnDetectVNC);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// scan each ip address (from the guest agent) for an open port
|
|
|
|
|
/// </summary>
|
2022-03-29 16:40:34 +02:00
|
|
|
|
public string PollPort(int port, bool vnc)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (Source == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
var vm = Source;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
var guestMetricsRef = vm.guest_metrics;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (guestMetricsRef == null || Helper.IsNullOrEmptyOpaqueRef(guestMetricsRef.opaque_ref))
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
var metrics = vm.Connection.Resolve(guestMetricsRef);
|
|
|
|
|
var networks = metrics?.networks;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (networks == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
var ipAddresses = new List<string>();
|
|
|
|
|
var ipv6Addresses = new List<string>();
|
|
|
|
|
var ipAddressesForNetworksWithoutPifs = new List<string>();
|
|
|
|
|
var ipv6AddressesForNetworksWithoutPifs = new List<string>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
foreach (var vif in vm.Connection.ResolveAll(vm.VIFs))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-29 16:40:34 +02:00
|
|
|
|
var network = vif.Connection.Resolve(vif.network);
|
|
|
|
|
var host = vm.Connection.Resolve(vm.resident_on);
|
|
|
|
|
var pif = Helpers.FindPIF(network, host);
|
|
|
|
|
foreach (var networkInfo in networks.Where(n => n.Key.StartsWith($"{vif.device}/ip")))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-06-23 10:53:15 +02:00
|
|
|
|
if (networkInfo.Key.EndsWith("ip") || networkInfo.Key.Contains("ipv4")) // IPv4 address
|
2015-03-27 04:23:49 +01:00
|
|
|
|
{
|
2016-10-10 10:57:50 +02:00
|
|
|
|
if (pif == null)
|
2016-10-07 18:01:09 +02:00
|
|
|
|
ipAddressesForNetworksWithoutPifs.Add(networkInfo.Value);
|
2017-09-03 04:33:29 +02:00
|
|
|
|
else if (pif.LinkStatus() == PIF.LinkState.Connected)
|
2016-10-10 10:57:50 +02:00
|
|
|
|
ipAddresses.Add(networkInfo.Value);
|
2016-10-07 18:01:09 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (networkInfo.Key.Contains("ipv6")) // IPv6 address, enclose in square brackets
|
2015-03-27 04:23:49 +01:00
|
|
|
|
{
|
2016-10-10 10:57:50 +02:00
|
|
|
|
if (pif == null)
|
2022-03-29 16:40:34 +02:00
|
|
|
|
ipv6AddressesForNetworksWithoutPifs.Add($"[{networkInfo.Value}]");
|
2017-09-03 04:33:29 +02:00
|
|
|
|
else if (pif.LinkStatus() == PIF.LinkState.Connected)
|
2022-03-29 16:40:34 +02:00
|
|
|
|
ipv6Addresses.Add($"[{networkInfo.Value}]");
|
2015-03-27 04:23:49 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-23 10:53:15 +02:00
|
|
|
|
ipAddresses = ipAddresses.Distinct().ToList();
|
2015-03-27 04:23:49 +01:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ipAddresses.AddRange(ipv6Addresses); // make sure IPv4 addresses are scanned first (CA-102755)
|
2016-10-07 18:01:09 +02:00
|
|
|
|
// add IP addresses for networks without PIFs
|
|
|
|
|
ipAddresses.AddRange(ipAddressesForNetworksWithoutPifs);
|
|
|
|
|
ipAddresses.AddRange(ipv6AddressesForNetworksWithoutPifs);
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
foreach (var ipAddress in ipAddresses)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.DebugFormat("Poll port {0}:{1}", ipAddress, port);
|
2023-07-11 11:37:06 +02:00
|
|
|
|
var s = ConnectGuest(ipAddress, port, vm.Connection);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (vnc)
|
|
|
|
|
{
|
|
|
|
|
Log.DebugFormat("Connected. Set Pending Vnc connection {0}:{1}", ipAddress, port);
|
|
|
|
|
SetPendingVNCConnection(s);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s.Close();
|
|
|
|
|
}
|
|
|
|
|
return ipAddress;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
Log.Debug(exn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (WebException)
|
|
|
|
|
{
|
|
|
|
|
// xapi has gone away.
|
|
|
|
|
}
|
|
|
|
|
catch (IOException)
|
|
|
|
|
{
|
|
|
|
|
// xapi has gone away.
|
|
|
|
|
}
|
|
|
|
|
catch (Failure exn)
|
|
|
|
|
{
|
2022-03-29 16:40:34 +02:00
|
|
|
|
switch (exn.ErrorDescription[0])
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-29 16:40:34 +02:00
|
|
|
|
case Failure.HANDLE_INVALID:
|
|
|
|
|
// HANDLE_INVALID is fine -- the guest metrics are not there yet.
|
|
|
|
|
break;
|
|
|
|
|
case Failure.SESSION_INVALID:
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-29 16:40:34 +02:00
|
|
|
|
// SESSION_INVALID is fine -- these will expire from time to time.
|
|
|
|
|
// We need to invalidate the session though.
|
2023-07-11 11:37:06 +02:00
|
|
|
|
lock (_activeSessionLock)
|
2022-03-29 16:40:34 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_activeSession = null;
|
2022-03-29 16:40:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-03-29 16:40:34 +02:00
|
|
|
|
default:
|
|
|
|
|
Log.Warn("Exception while polling VM for port " + port + ".", exn);
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn("Exception while polling VM for port " + port + ".", e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Exchange the current value of pendingVNCConnection with the given one, and close the old
|
|
|
|
|
/// connection, if any.
|
|
|
|
|
/// Nothrow guarantee.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s">May be null</param>
|
|
|
|
|
private void SetPendingVNCConnection(Stream s)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
Stream oldPending;
|
|
|
|
|
lock (_pendingVNCConnectionLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
oldPending = _pendingVNCConnection;
|
|
|
|
|
_pendingVNCConnection = s;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (oldPending != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
oldPending.Close();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2021-09-10 15:19:08 +02:00
|
|
|
|
// ignored
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private bool _scaling;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public bool Scaling
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
2023-07-11 11:37:06 +02:00
|
|
|
|
return _scaling;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_scaling = value;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
RemoteConsole.Scaling = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IRemoteConsole RemoteConsole
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
get => _vncClient != null ? (IRemoteConsole)_vncClient : _rdpClient;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_vncClient != null)
|
|
|
|
|
_vncClient = (VNCGraphicsClient) value ;
|
|
|
|
|
else if (_rdpClient != null)
|
|
|
|
|
_rdpClient = (RdpClient)value;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:24:21 +02:00
|
|
|
|
private readonly object _rdpConnectionLock = new object();
|
|
|
|
|
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the actual VNC or RDP client control.
|
|
|
|
|
/// </summary>
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private void InitSubControl()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
2015-03-16 07:56:45 +01:00
|
|
|
|
//When switch to RDP from VNC, if RDP IP is empty, do not try to switch.
|
2023-07-11 11:28:19 +02:00
|
|
|
|
if (string.IsNullOrEmpty(RdpIp) && !UseVNC && RemoteConsole != null)
|
2015-03-16 07:56:45 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var wasFocused = false;
|
2023-07-11 11:27:23 +02:00
|
|
|
|
Controls.Clear();
|
2017-02-27 19:00:46 +01:00
|
|
|
|
//console size with some offset to accomodate focus rectangle
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var currentConsoleSize = new Size(Size.Width - CONSOLE_SIZE_OFFSET, Size.Height - CONSOLE_SIZE_OFFSET);
|
2023-07-11 11:24:21 +02:00
|
|
|
|
|
|
|
|
|
lock (_rdpConnectionLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:24:21 +02:00
|
|
|
|
// Stop the old client.
|
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
{
|
|
|
|
|
var preventResetConsole = false;
|
|
|
|
|
wasFocused = RemoteConsole.ConsoleControl != null && RemoteConsole.ConsoleControl.Focused;
|
|
|
|
|
if (RemoteConsole is RdpClient client && client.IsAttemptingConnection)
|
|
|
|
|
{
|
|
|
|
|
preventResetConsole = true;
|
|
|
|
|
}
|
|
|
|
|
if(!preventResetConsole)
|
|
|
|
|
{
|
|
|
|
|
RemoteConsole.DisconnectAndDispose();
|
|
|
|
|
RemoteConsole = null;
|
|
|
|
|
}
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_vncPassword = null;
|
2023-07-11 11:24:21 +02:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2023-07-11 11:24:21 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// Reset
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_haveTriedLoginWithoutPassword = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:28:19 +02:00
|
|
|
|
if (UseVNC || string.IsNullOrEmpty(RdpIp))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:27:23 +02:00
|
|
|
|
AutoScroll = false;
|
|
|
|
|
AutoScrollMinSize = new Size(0, 0);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_vncClient = new VNCGraphicsClient(this);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_vncClient.UseSource = UseSource;
|
|
|
|
|
_vncClient.DesktopResized += ResizeHandler;
|
|
|
|
|
_vncClient.Resize += ResizeHandler;
|
|
|
|
|
_vncClient.ErrorOccurred += ErrorHandler;
|
|
|
|
|
_vncClient.ConnectionSuccess += ConnectionSuccess;
|
|
|
|
|
_vncClient.Dock = DockStyle.Fill;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_rdpClient == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:32:48 +02:00
|
|
|
|
if (ParentForm is FullScreenForm form)
|
|
|
|
|
currentConsoleSize = form.GetContentSize();
|
2023-07-11 11:27:23 +02:00
|
|
|
|
AutoScroll = true;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
AutoScrollMinSize = _oldSize;
|
|
|
|
|
_rdpClient = new RdpClient(this, currentConsoleSize, ResizeHandler);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_rdpClient.OnDisconnected += ParentVNCTabView.RdpDisconnectedHandler;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:34:02 +02:00
|
|
|
|
if (RemoteConsole?.ConsoleControl != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:27:23 +02:00
|
|
|
|
RemoteConsole.KeyHandler = KeyHandler;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
RemoteConsole.SendScanCodes = !_sourceIsPv;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
RemoteConsole.Scaling = Scaling;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
RemoteConsole.DisplayBorder = _displayFocusRectangle;
|
|
|
|
|
SetKeyboardAndMouseCapture(_autoCaptureKeyboardAndMouse);
|
|
|
|
|
if (_wasPaused)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
RemoteConsole.Pause();
|
|
|
|
|
else
|
2022-04-07 14:51:09 +02:00
|
|
|
|
RemoteConsole.UnPause();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ConnectToRemoteConsole();
|
|
|
|
|
|
|
|
|
|
if (wasFocused)
|
|
|
|
|
RemoteConsole.Activate();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 12:48:42 +02:00
|
|
|
|
if (GpuStatusChanged != null)
|
|
|
|
|
GpuStatusChanged(MustConnectRemoteDesktop());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool MustConnectRemoteDesktop()
|
|
|
|
|
{
|
2022-03-29 16:40:34 +02:00
|
|
|
|
return (UseVNC || string.IsNullOrEmpty(RdpIp)) &&
|
2017-09-03 04:33:29 +02:00
|
|
|
|
Source.HasGPUPassthrough() && Source.power_state == vm_power_state.Running;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetKeyboardAndMouseCapture(bool value)
|
|
|
|
|
{
|
2023-07-11 11:34:02 +02:00
|
|
|
|
if (RemoteConsole?.ConsoleControl != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
RemoteConsole.ConsoleControl.TabStop = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConnectToRemoteConsole()
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_vncClient != null)
|
|
|
|
|
ThreadPool.QueueUserWorkItem(Connect, new KeyValuePair<VNCGraphicsClient, Exception>(_vncClient, null));
|
|
|
|
|
else if (_rdpClient != null)
|
|
|
|
|
_rdpClient.Connect(RdpIp);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionSuccess(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_connectionRetries = 0;
|
2015-05-06 13:11:50 +02:00
|
|
|
|
if (AutoSwitchRDPLater)
|
|
|
|
|
{
|
|
|
|
|
if (OnDetectRDP != null)
|
|
|
|
|
Program.Invoke(this, OnDetectRDP);
|
|
|
|
|
AutoSwitchRDPLater = false;
|
|
|
|
|
}
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (ParentVNCTabView.IsRDPControlEnabled())
|
|
|
|
|
ParentVNCTabView.EnableToggleVNCButton();
|
2015-05-06 13:11:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool AutoSwitchRDPLater
|
|
|
|
|
{
|
2015-06-04 12:13:58 +02:00
|
|
|
|
get;
|
|
|
|
|
set;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool UseVNC
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
get => _useVNC;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (value != _useVNC)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_connectionRetries = 0;
|
|
|
|
|
_useVNC = value;
|
|
|
|
|
_scaling = false;
|
|
|
|
|
InitSubControl();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
// Check if we have really switched. If not, change useVNC back (CA-102755)
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var switched = true;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_useVNC) // we wanted VNC
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_vncClient == null && _rdpClient != null) // it is actually RDP
|
2013-06-24 13:41:48 +02:00
|
|
|
|
switched = false;
|
|
|
|
|
}
|
|
|
|
|
else // we wanted RDP
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_rdpClient == null && _vncClient != null) // it is actually VNC
|
2013-06-24 13:41:48 +02:00
|
|
|
|
switched = false;
|
|
|
|
|
}
|
|
|
|
|
if (!switched)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_useVNC = !_useVNC;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private volatile bool _useSource = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates whether to use the source or the detected vncIP
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool UseSource
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
get => _useSource;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (value != _useSource)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_useSource = value;
|
|
|
|
|
_connectionRetries = 0;
|
|
|
|
|
InitSubControl();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 14:51:50 +02:00
|
|
|
|
private VM Source
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
get => _sourceVm;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_connectionPoller != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_connectionPoller.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
_connectionPoller = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_sourceVm != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_sourceVm.PropertyChanged -= VM_PropertyChanged;
|
|
|
|
|
_sourceVm = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_sourceVm = value;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
2023-07-11 11:30:26 +02:00
|
|
|
|
value.PropertyChanged += VM_PropertyChanged;
|
2014-10-08 11:18:40 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_sourceIsPv = !value.IsHVM();
|
2014-08-22 18:36:41 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
StartPolling();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
lock (_hostedConsolesLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_hostedConsoles = Source.consoles;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VM_PropertyChanged(value, new PropertyChangedEventArgs("consoles"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 14:51:50 +02:00
|
|
|
|
public string ConnectionName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Source == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
if (Source.IsControlDomainZero(out var host))
|
2021-02-24 16:44:38 +01:00
|
|
|
|
return string.Format(Messages.CONSOLE_HOST, host.Name());
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
if (Source.IsSrDriverDomain(out var sr))
|
2021-02-24 16:44:38 +01:00
|
|
|
|
return string.Format(Messages.CONSOLE_SR_DRIVER_DOMAIN, sr.Name());
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return Source.Name();
|
2016-08-24 14:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private bool InDefaultConsole()
|
|
|
|
|
{
|
|
|
|
|
// Windows VMs:
|
|
|
|
|
// - UseVNC indicates whether to use the default desktop (true) or the remote desktop (false);
|
|
|
|
|
// - UseSource is true by default and not used
|
|
|
|
|
// Linux VMs:
|
|
|
|
|
// - UseVNC is true by default and not used;
|
|
|
|
|
// - UseSource indicates whether to use the text console (true) or the graphical vnc console (false);
|
|
|
|
|
|
|
|
|
|
return UseVNC && UseSource;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
private void StartPolling()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
//Disable the button first, but only if in text/default console (to allow user to return to the text console - ref. CA-70314)
|
2015-05-06 13:11:50 +02:00
|
|
|
|
if (InDefaultConsole())
|
2014-12-04 09:35:36 +01:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
ParentVNCTabView.DisableToggleVNCButton();
|
2014-12-04 09:35:36 +01:00
|
|
|
|
}
|
2015-05-06 13:11:50 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (ParentVNCTabView.IsRDPControlEnabled())
|
2022-03-29 16:40:34 +02:00
|
|
|
|
return;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-03-29 16:40:34 +02:00
|
|
|
|
if (InDefaultConsole())
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
ParentVNCTabView.DisableToggleVNCButton();
|
2016-07-04 12:58:59 +02:00
|
|
|
|
}
|
2022-03-29 16:40:34 +02:00
|
|
|
|
|
|
|
|
|
if (Source == null || Source.IsControlDomainZero(out _))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//Start the polling again
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_connectionPoller = !Source.IsHVM() ? new Timer(PollVNCPort, null, RETRY_SLEEP_TIME, RDP_POLL_INTERVAL) : new Timer(PollRDPPort, null, RETRY_SLEEP_TIME, RDP_POLL_INTERVAL);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VM_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var vm = (VM)sender;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (vm.uuid != Source.uuid)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//We only want to reconnect the consoles when they change
|
|
|
|
|
//if the vm is running and if we are using vncTerm / qemu
|
|
|
|
|
if (e.PropertyName == "consoles" && vm.power_state == vm_power_state.Running && UseSource)
|
|
|
|
|
{
|
|
|
|
|
ConnectNewHostedConsole();
|
|
|
|
|
}
|
|
|
|
|
//If the consoles change under us then refresh hostedConsoles
|
|
|
|
|
else if (e.PropertyName == "consoles" && vm.power_state == vm_power_state.Running && !UseSource)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
lock (_hostedConsolesLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_hostedConsoles = Source.consoles;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//Or if the VM legitimately turns on
|
|
|
|
|
else if (e.PropertyName == "power_state" && vm.power_state == vm_power_state.Running)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
ParentVNCTabView.VMPowerOn();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ConnectNewHostedConsole();
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_connectionPoller != null)
|
|
|
|
|
_connectionPoller.Change(RETRY_SLEEP_TIME, RDP_POLL_INTERVAL);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else if (e.PropertyName == "power_state" &&
|
|
|
|
|
(vm.power_state == vm_power_state.Halted || vm.power_state == vm_power_state.Suspended))
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
ParentVNCTabView.VMPowerOff();
|
|
|
|
|
if (_connectionPoller != null)
|
|
|
|
|
_connectionPoller.Change(Timeout.Infinite, Timeout.Infinite);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else if (e.PropertyName == "domid")
|
|
|
|
|
{
|
|
|
|
|
// Reboot / start / shutdown
|
2022-03-29 16:40:34 +02:00
|
|
|
|
Program.Invoke(this, StartPolling);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.PropertyName == "power_state" || e.PropertyName == "VGPUs")
|
2016-08-24 12:48:42 +02:00
|
|
|
|
{
|
|
|
|
|
Program.Invoke(this, () =>
|
|
|
|
|
{
|
|
|
|
|
if (GpuStatusChanged != null)
|
|
|
|
|
GpuStatusChanged(MustConnectRemoteDesktop());
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-08-24 14:51:50 +02:00
|
|
|
|
|
|
|
|
|
if (e.PropertyName == "name_label" && ConnectionNameChanged != null)
|
|
|
|
|
ConnectionNameChanged(ConnectionName);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
internal void ImediatelyPollForConsole()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_connectionPoller != null)
|
|
|
|
|
_connectionPoller.Change(0, RDP_POLL_INTERVAL);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConnectNewHostedConsole()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
lock (_hostedConsolesLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_hostedConsoles = Source.consoles;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (UseVNC && _vncClient != null && ConnectionSuperceded())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
InitSubControl();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A connection is superceded if it's connected to a console that's no longer being
|
|
|
|
|
/// advertised by the server and there's a replacement that _is_ being advertised, or
|
|
|
|
|
/// if its not connected at all.
|
|
|
|
|
///
|
|
|
|
|
/// The server will leave the console open until we close, so that the user may see any crash messages.
|
|
|
|
|
/// For this reason, we need to close down ourselves when we see that the console has
|
|
|
|
|
/// been replaced by a newer one (i.e. after a reboot).
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool ConnectionSuperceded()
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
return !_vncClient.Connected || ConsoleSuperceded((Console)_vncClient.Console);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private bool ConsoleSuperceded(Console oldConsole)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (oldConsole == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
List<Console> consoles;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
lock (_hostedConsolesLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
consoles = Source.Connection.ResolveAll(_hostedConsoles);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2023-07-11 11:37:06 +02:00
|
|
|
|
var goodConsole = false;
|
2023-07-11 11:29:26 +02:00
|
|
|
|
foreach (var console in consoles)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (console.opaque_ref == oldConsole.opaque_ref &&
|
|
|
|
|
console.location == oldConsole.location)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return false;
|
|
|
|
|
else if (console.protocol == console_protocol.rfb)
|
2023-07-11 11:37:06 +02:00
|
|
|
|
goodConsole = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2023-07-11 11:37:06 +02:00
|
|
|
|
return goodConsole;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// CA-11201: GUI logs are being massively spammed. Prevent "INTERNAL_ERROR Host has disappeared"
|
|
|
|
|
/// appearing more than once.
|
|
|
|
|
/// </summary>
|
2023-07-11 11:27:57 +02:00
|
|
|
|
private bool _suppressHostGoneMessage;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private void Connect(object o)
|
|
|
|
|
{
|
|
|
|
|
if (Program.RunInAutomatedTestMode)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var kvp = (KeyValuePair<VNCGraphicsClient, Exception>)o;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var v = kvp.Key;
|
|
|
|
|
var error = kvp.Value;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (UseSource)
|
|
|
|
|
{
|
|
|
|
|
List<Console> consoles;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
lock (_hostedConsolesLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
consoles = _sourceVm.Connection.ResolveAll(_hostedConsoles);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
foreach (var console in consoles)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_vncClient != v)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// We've been replaced. Give up.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (console.protocol == console_protocol.rfb)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ConnectHostedConsole(v, console);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var failure = exn as Failure;
|
|
|
|
|
var isHostGoneMessage = failure != null
|
|
|
|
|
&& failure.ErrorDescription.Count == 2
|
|
|
|
|
&& failure.ErrorDescription[0] == Failure.INTERNAL_ERROR
|
|
|
|
|
&& failure.ErrorDescription[1] == string.Format(Messages.HOST_GONE, BrandManager.BrandConsole);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (isHostGoneMessage)
|
|
|
|
|
{
|
|
|
|
|
if (!_suppressHostGoneMessage)
|
|
|
|
|
{
|
|
|
|
|
Log.Debug(exn, exn);
|
|
|
|
|
_suppressHostGoneMessage = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Debug(exn, exn);
|
|
|
|
|
_suppressHostGoneMessage = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.Debug("Did not find any hosted consoles");
|
|
|
|
|
SleepAndRetryConnection(v);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-29 16:40:34 +02:00
|
|
|
|
if (VncIp == null)
|
2015-04-30 12:37:39 +02:00
|
|
|
|
{
|
|
|
|
|
Log.DebugFormat("vncIP is null. Abort VNC connection attempt");
|
|
|
|
|
OnVncConnectionAttemptCancelled();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_vncPassword = Settings.GetVNCPassword(_sourceVm.uuid);
|
|
|
|
|
if (_vncPassword == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
var lifecycleOperationInProgress = _sourceVm.current_operations.Values.Any(VM.is_lifecycle_operation);
|
|
|
|
|
if (_haveTriedLoginWithoutPassword && !lifecycleOperationInProgress)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, delegate
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
PromptForPassword(_ignoreNextError ? null : error);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
});
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_ignoreNextError = false;
|
|
|
|
|
if (_vncPassword == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("User cancelled VNC password prompt: aborting connection attempt");
|
|
|
|
|
OnUserCancelledAuth();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Debug("Attempting passwordless VNC login");
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_vncPassword = Array.Empty<char>();
|
|
|
|
|
_ignoreNextError = true;
|
|
|
|
|
_haveTriedLoginWithoutPassword = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream s;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
lock (_pendingVNCConnectionLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
s = _pendingVNCConnection;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Log.DebugFormat("Using pending VNC connection");
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_pendingVNCConnection = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
if (s == null)
|
|
|
|
|
{
|
2023-07-11 11:27:23 +02:00
|
|
|
|
Log.DebugFormat("Connecting to vncIP={0}, port={1}", VncIp, VNC_PORT);
|
2023-07-11 11:37:06 +02:00
|
|
|
|
s = ConnectGuest(VncIp, VNC_PORT, _sourceVm.Connection);
|
2023-07-11 11:27:23 +02:00
|
|
|
|
Log.DebugFormat("Connected to vncIP={0}, port={1}", VncIp, VNC_PORT);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
InvokeConnection(v, s, null);
|
2015-04-30 12:37:39 +02:00
|
|
|
|
|
|
|
|
|
// store the empty vnc password after a successful passwordless login
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_haveTriedLoginWithoutPassword && _vncPassword.Length == 0)
|
|
|
|
|
Program.Invoke(this, () => Settings.SetVNCPassword(_sourceVm.uuid, _vncPassword));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(exn, exn);
|
|
|
|
|
SleepAndRetryConnection(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private void PromptForPassword(Exception error)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
// Prompt for password
|
2023-07-11 11:37:06 +02:00
|
|
|
|
var f = new VNCPasswordDialog(error, _sourceVm);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (f.ShowDialog(this) == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
// Store password for next time
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_vncPassword = f.Password;
|
|
|
|
|
Settings.SetVNCPassword(_sourceVm.uuid, _vncPassword);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// User cancelled
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
f.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnUserCancelledAuth()
|
|
|
|
|
{
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, delegate
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("User cancelled during VNC authentication");
|
|
|
|
|
if (UserCancelledAuth != null)
|
|
|
|
|
UserCancelledAuth(this, null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 12:37:39 +02:00
|
|
|
|
private void OnVncConnectionAttemptCancelled()
|
|
|
|
|
{
|
|
|
|
|
Program.Invoke(this, delegate
|
|
|
|
|
{
|
|
|
|
|
Log.Debug("Cancelled VNC connection attempt");
|
|
|
|
|
if (VncConnectionAttemptCancelled != null)
|
|
|
|
|
VncConnectionAttemptCancelled(this, null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private Stream ConnectGuest(string ipAddress, int port, IXenConnection connection)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
var uriString = string.Format("http://{0}:{1}/", ipAddress, port);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Log.DebugFormat("Trying to connect to: {0}", uriString);
|
|
|
|
|
return HTTP.ConnectStream(new Uri(uriString), XenAdminConfigManager.Provider.GetProxyFromSettings(connection), true, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConnectHostedConsole(VNCGraphicsClient v, Console console)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var host = console.Connection.Resolve(Source.resident_on);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (host == null)
|
|
|
|
|
{
|
2021-03-16 02:50:45 +01:00
|
|
|
|
throw new Failure(Failure.INTERNAL_ERROR, string.Format(Messages.HOST_GONE, BrandManager.BrandConsole));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var uri = new Uri(console.location);
|
2023-07-11 11:33:21 +02:00
|
|
|
|
string sessionRef;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
lock (_activeSessionLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// use the elevated credentials, if provided, for connecting to the console (CA-91132)
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_activeSession = (string.IsNullOrEmpty(ElevatedUsername) || string.IsNullOrEmpty(ElevatedPassword)) ?
|
2013-06-24 13:41:48 +02:00
|
|
|
|
console.Connection.DuplicateSession() : console.Connection.ElevatedSession(ElevatedUsername, ElevatedPassword);
|
2023-07-11 11:37:06 +02:00
|
|
|
|
sessionRef = _activeSession.opaque_ref;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:33:21 +02:00
|
|
|
|
var stream = HTTPHelper.CONNECT(uri, console.Connection, sessionRef, false);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
InvokeConnection(v, stream, console);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InvokeConnection(VNCGraphicsClient v, Stream stream, Console console)
|
|
|
|
|
{
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, delegate()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// This is the last chance that we have to make sure that we've not already
|
|
|
|
|
// connected this VNCGraphicsClient. Now that we are back on the event thread,
|
|
|
|
|
// we're guaranteed that no-one will beat us to the v.connect() call. We
|
|
|
|
|
// hand over responsibility for closing the stream at that point, so we have to
|
|
|
|
|
// close it ourselves if the client is already connected.
|
|
|
|
|
if (v.Connected || v.Terminated)
|
|
|
|
|
{
|
|
|
|
|
stream.Close();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
v.SendScanCodes = UseSource && !_sourceIsPv;
|
|
|
|
|
v.SourceVM = _sourceVm;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
v.Console = console;
|
2023-07-11 11:37:06 +02:00
|
|
|
|
v.UseQemuExtKeyEncoding = _sourceVm != null && Helpers.InvernessOrGreater(_sourceVm.Connection);
|
|
|
|
|
v.Connect(stream, _vncPassword);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RetryConnection(VNCGraphicsClient v, Exception exn)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_vncClient == v && !v.Terminated && Source.power_state == vm_power_state.Running)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:30:26 +02:00
|
|
|
|
ThreadPool.QueueUserWorkItem(Connect, new KeyValuePair<VNCGraphicsClient, Exception>(v, exn));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendCAD()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
{
|
|
|
|
|
RemoteConsole.SendCAD();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private string _errorMessage;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private void ErrorHandler(object sender, Exception exn)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
2023-07-11 11:27:23 +02:00
|
|
|
|
if (Disposing || IsDisposed)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, delegate()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var v = (VNCGraphicsClient)sender;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (exn is VNCAuthenticationException || exn is CryptographicException)
|
|
|
|
|
{
|
|
|
|
|
Log.Debug(exn, exn);
|
|
|
|
|
|
|
|
|
|
// Clear the stored VNC password for this server.
|
2023-07-11 11:37:06 +02:00
|
|
|
|
Settings.SetVNCPassword(_sourceVm.uuid, null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
RetryConnection(v, exn);
|
|
|
|
|
}
|
|
|
|
|
else if (exn is IOException || exn is Failure)
|
|
|
|
|
{
|
|
|
|
|
Log.Debug(exn, exn);
|
|
|
|
|
SleepAndRetryConnection_(v);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(exn, exn);
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_errorMessage = exn.Message;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SleepAndRetryConnection_(VNCGraphicsClient v)
|
|
|
|
|
{
|
2023-07-11 11:30:26 +02:00
|
|
|
|
ThreadPool.QueueUserWorkItem(SleepAndRetryConnection, v);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SleepAndRetryConnection(object o)
|
|
|
|
|
{
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var v = (VNCGraphicsClient)o;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_connectionRetries++;
|
|
|
|
|
Thread.Sleep(_connectionRetries < SHORT_RETRY_COUNT ? SHORT_RETRY_SLEEP_TIME : RETRY_SLEEP_TIME);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
RetryConnection(v, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnPaint(e);
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_errorMessage != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
var size = e.Graphics.MeasureString(_errorMessage, Font);
|
|
|
|
|
e.Graphics.DrawString(_errorMessage, Font, Brushes.Black,
|
2023-07-11 11:27:23 +02:00
|
|
|
|
((Width - size.Width) / 2), ((Height - size.Height) / 2));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// draw focus rectangle
|
2023-07-11 11:27:23 +02:00
|
|
|
|
if (DisplayFocusRectangle && ContainsFocus && RemoteConsole != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var focusRect = Rectangle.Inflate(RemoteConsole.ConsoleBounds, VNCGraphicsClient.BORDER_PADDING / 2,
|
2013-06-24 13:41:48 +02:00
|
|
|
|
VNCGraphicsClient.BORDER_PADDING / 2);
|
2023-07-11 11:37:06 +02:00
|
|
|
|
using (var pen = new Pen(_focusColor, VNCGraphicsClient.BORDER_WIDTH))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:27:23 +02:00
|
|
|
|
if (Focused)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
pen.DashStyle = DashStyle.Dash;
|
|
|
|
|
e.Graphics.DrawRectangle(pen, focusRect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save this for when we init a new vncClient.
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private bool _displayFocusRectangle = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public bool DisplayFocusRectangle
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
get => _displayFocusRectangle;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_displayFocusRectangle = value;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
RemoteConsole.DisplayBorder = _displayFocusRectangle;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal Image Snapshot()
|
|
|
|
|
{
|
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
return RemoteConsole.Snapshot();
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void RefreshScreen()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
2023-07-11 11:34:02 +02:00
|
|
|
|
if (RemoteConsole?.ConsoleControl != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
RemoteConsole.ConsoleControl.Refresh();
|
|
|
|
|
}
|
|
|
|
|
Invalidate();
|
|
|
|
|
Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnGotFocus(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
base.OnGotFocus(e);
|
|
|
|
|
|
|
|
|
|
RefreshScreen();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnLostFocus(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
base.OnLostFocus(e);
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_pressedKeys = new Set<Keys>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// reset tab stop
|
2023-07-11 11:37:06 +02:00
|
|
|
|
SetKeyboardAndMouseCapture(_autoCaptureKeyboardAndMouse);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
RefreshScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnEnter(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
base.OnEnter(e);
|
|
|
|
|
|
|
|
|
|
CaptureKeyboardAndMouse();
|
|
|
|
|
RefreshScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnLeave(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
base.OnLeave(e);
|
|
|
|
|
|
|
|
|
|
// reset tab stop
|
2023-07-11 11:37:06 +02:00
|
|
|
|
SetKeyboardAndMouseCapture(_autoCaptureKeyboardAndMouse);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
RefreshScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void UncaptureKeyboardAndMouse()
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_autoCaptureKeyboardAndMouse)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
SetKeyboardAndMouseCapture(false);
|
|
|
|
|
}
|
|
|
|
|
ActiveControl = null;
|
|
|
|
|
|
|
|
|
|
EnableMenuShortcuts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void CaptureKeyboardAndMouse()
|
|
|
|
|
{
|
|
|
|
|
if (RemoteConsole != null)
|
|
|
|
|
{
|
|
|
|
|
RemoteConsole.Activate();
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_autoCaptureKeyboardAndMouse)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
SetKeyboardAndMouseCapture(true);
|
|
|
|
|
}
|
|
|
|
|
Unpause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DisableMenuShortcuts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void DisableMenuShortcuts()
|
|
|
|
|
{
|
2018-10-01 18:01:59 +02:00
|
|
|
|
Program.MainWindow.MenuShortcutsEnabled = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void EnableMenuShortcuts()
|
|
|
|
|
{
|
2018-10-01 18:01:59 +02:00
|
|
|
|
Program.MainWindow.MenuShortcutsEnabled = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private Set<Keys> _pressedKeys = new Set<Keys>();
|
|
|
|
|
private bool _modifierKeyPressedAlone;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
|
|
|
|
{
|
|
|
|
|
const int WM_KEYDOWN = 0x100;
|
|
|
|
|
const int WM_SYSKEYDOWN = 0x104;
|
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var down = ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var key = keyData;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if ((key & Keys.Control) == Keys.Control)
|
|
|
|
|
key = key & ~Keys.Control;
|
|
|
|
|
|
|
|
|
|
if ((key & Keys.Alt) == Keys.Alt)
|
|
|
|
|
key = key & ~Keys.Alt;
|
|
|
|
|
|
|
|
|
|
if ((key & Keys.Shift) == Keys.Shift)
|
|
|
|
|
key = key & ~Keys.Shift;
|
|
|
|
|
|
|
|
|
|
// use TranslateKeyMessage to identify if Left or Right modifier keys have been pressed/released
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var extKey = ConsoleKeyHandler.TranslateKeyMessage(msg);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
return Keysym(down, key, extKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnKeyUp(KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
e.Handled = Keysym(false, e.KeyCode, e.KeyCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Keysym(bool pressed, Keys key, Keys extendedKey)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (!pressed && _pressedKeys.Count == 0) // we received key-up, but not key-down - ignore
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (KeyHandler.handleExtras(pressed, _pressedKeys, KeyHandler.ExtraKeys, extendedKey, KeyHandler.ModifierKeys, ref _modifierKeyPressedAlone))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-11 11:27:23 +02:00
|
|
|
|
Focus();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// on keyup, try to remove extended keys (i.e. LControlKey, LControlKey, RShiftKey, LShiftKey, RMenu, LMenu)
|
|
|
|
|
// we need to do this here, because we cannot otherwise distinguish between Left and Right modifier keys on KeyUp
|
|
|
|
|
if (!pressed)
|
|
|
|
|
{
|
2023-07-11 11:29:26 +02:00
|
|
|
|
var extendedKeys = ConsoleKeyHandler.GetExtendedKeys(key);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
foreach (var k in extendedKeys)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_pressedKeys.Remove(k);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (key == Keys.Tab || (key == (Keys.Tab | Keys.Shift)))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-02-27 19:00:46 +01:00
|
|
|
|
|
2023-07-11 11:37:06 +02:00
|
|
|
|
private Size _oldSize;
|
2017-02-27 19:00:46 +01:00
|
|
|
|
public void UpdateRDPResolution(bool fullscreen = false)
|
|
|
|
|
{
|
2023-07-11 11:37:06 +02:00
|
|
|
|
if (_rdpClient == null || _oldSize.Equals(Size))
|
2017-02-27 19:00:46 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2017-03-03 18:29:19 +01:00
|
|
|
|
//no offsets in fullscreen mode because there is no need to accomodate focus border
|
2017-02-27 19:00:46 +01:00
|
|
|
|
if (fullscreen)
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_rdpClient.UpdateDisplay(Size.Width, Size.Height, new Point(0,0));
|
2017-04-25 15:07:29 +02:00
|
|
|
|
else
|
2023-07-11 11:37:06 +02:00
|
|
|
|
_rdpClient.UpdateDisplay(Size.Width - CONSOLE_SIZE_OFFSET, Size.Height - CONSOLE_SIZE_OFFSET, new Point(3,3));
|
|
|
|
|
_oldSize = new Size(Size.Width, Size.Height);
|
2017-04-25 15:07:29 +02:00
|
|
|
|
Refresh();
|
2017-02-27 19:00:46 +01:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|