2015-05-22 11:29:53 +02:00
|
|
|
|
/* Copyright (c) Citrix Systems Inc.
|
|
|
|
|
* 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 XenAdmin.Core;
|
|
|
|
|
using XenAdmin.Network;
|
2015-07-01 08:29:27 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
|
|
|
|
namespace XenServerHealthCheck
|
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
public class ServerInfo
|
|
|
|
|
{
|
|
|
|
|
public string HostName { get; set; }
|
|
|
|
|
public string UserName { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
public Task task { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
public class ServerListHelper
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
private ServerListHelper() { }
|
|
|
|
|
public static readonly ServerListHelper instance = new ServerListHelper();
|
2015-05-22 11:29:53 +02:00
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
2015-07-01 08:29:27 +02:00
|
|
|
|
private List<ServerInfo> serverList;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
private static readonly Object serverListLock = new Object();
|
2015-07-01 08:29:27 +02:00
|
|
|
|
public List<ServerInfo> GetServerList()
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
List<ServerInfo> currentList;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
lock (serverListLock)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
currentList = new List<ServerInfo>(serverList);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
2015-06-26 07:36:30 +02:00
|
|
|
|
return currentList;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
public void Init()
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
lock (serverListLock)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
serverList = new List<ServerInfo>();
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string[] encServerList = Properties.Settings.Default.ServerList ?? new string[0];
|
|
|
|
|
foreach (string encServer in encServerList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string decryptCredential = EncryptionUtils.Unprotect(encServer);
|
|
|
|
|
string[] decryptCredentialComps = decryptCredential.Split(SEPARATOR);
|
2015-07-01 08:29:27 +02:00
|
|
|
|
ServerInfo connection = new ServerInfo();
|
|
|
|
|
connection.HostName = decryptCredentialComps[0];
|
|
|
|
|
connection.UserName = decryptCredentialComps[1];
|
2015-06-26 07:36:30 +02:00
|
|
|
|
connection.Password = decryptCredentialComps[2];
|
2015-07-01 08:29:27 +02:00
|
|
|
|
connection.task = null;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Add(connection);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
private const char SEPARATOR = '\x202f'; // narrow non-breaking space.
|
2015-07-01 08:29:27 +02:00
|
|
|
|
private string ProtectCredential(ServerInfo connection)
|
2015-06-26 07:36:30 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
string Host = connection.HostName ?? string.Empty;
|
|
|
|
|
string username = connection.UserName ?? string.Empty;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string passwordSecret = connection.Password ?? string.Empty;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
return EncryptionUtils.Protect(String.Join(SEPARATOR.ToString(), new[] { Host, username, passwordSecret }));
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-01 08:29:27 +02:00
|
|
|
|
public void removeServerCredential(string HostName)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
lock (serverListLock)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
foreach (ServerInfo con in serverList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
if (HostName == con.HostName)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Remove(con);
|
2015-07-01 08:29:27 +02:00
|
|
|
|
updateServerList();
|
|
|
|
|
return;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
private void updateServerList()
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
List<string> encList = new List<string>();
|
2015-07-01 08:29:27 +02:00
|
|
|
|
foreach (ServerInfo connection in serverList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
encList.Add(ProtectCredential(connection));
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
2015-06-26 07:36:30 +02:00
|
|
|
|
Properties.Settings.Default.ServerList = encList.ToArray();
|
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
}
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2015-07-01 08:29:27 +02:00
|
|
|
|
public bool UpdateServerInfo(ServerInfo server)
|
|
|
|
|
{
|
|
|
|
|
lock (serverListLock)
|
|
|
|
|
{
|
|
|
|
|
foreach (ServerInfo con in serverList)
|
|
|
|
|
{
|
|
|
|
|
if (server.HostName == con.HostName)
|
|
|
|
|
{
|
|
|
|
|
serverList.Remove(con);
|
|
|
|
|
serverList.Add(server);
|
|
|
|
|
updateServerList();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
public void UpdateServerCredential(string credential)
|
|
|
|
|
{
|
|
|
|
|
log.Info("Receive credential update message");
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string decryptCredential = EncryptionUtils.UnprotectForLocalMachine(credential);
|
|
|
|
|
string[] decryptCredentialComps = decryptCredential.Split(SEPARATOR);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
lock (serverListLock)
|
|
|
|
|
{
|
|
|
|
|
if (decryptCredentialComps.Length == 3)
|
|
|
|
|
{//Add credential
|
2015-07-01 08:29:27 +02:00
|
|
|
|
Task originalTask = null;
|
|
|
|
|
foreach (ServerInfo connection in serverList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
if (connection.HostName == decryptCredentialComps[0])
|
2015-06-26 07:36:30 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
originalTask = connection.task;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Remove(connection);
|
|
|
|
|
log.Info("Refresh credential");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
2015-07-01 08:29:27 +02:00
|
|
|
|
ServerInfo newConnection = new ServerInfo();
|
|
|
|
|
newConnection.HostName = decryptCredentialComps[0];
|
|
|
|
|
newConnection.UserName = decryptCredentialComps[1];
|
2015-06-26 07:36:30 +02:00
|
|
|
|
newConnection.Password = decryptCredentialComps[2];
|
2015-07-01 08:29:27 +02:00
|
|
|
|
newConnection.task = originalTask;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Add(newConnection);
|
|
|
|
|
log.InfoFormat("Add credential, current credential size is {0}", serverList.Count);
|
|
|
|
|
}
|
|
|
|
|
else if (decryptCredentialComps.Length == 1)
|
|
|
|
|
{//remove credential
|
2015-07-01 08:29:27 +02:00
|
|
|
|
foreach (ServerInfo connection in serverList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
if (connection.HostName == decryptCredentialComps[0])
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Remove(connection);
|
2015-07-01 04:35:59 +02:00
|
|
|
|
log.InfoFormat("Remove credential, current credential size is {0}", serverList.Count);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-26 07:36:30 +02:00
|
|
|
|
|
|
|
|
|
updateServerList();
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|