mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +08:00
changed more classes to not rely on Runtime for the connections service
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using mRemoteNG.Config.Serializers;
|
||||
using mRemoteNG.App;
|
||||
using mRemoteNG.Config.Putty;
|
||||
using mRemoteNG.Config.Serializers.Xml;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Container;
|
||||
@@ -19,7 +20,8 @@ namespace mRemoteNGTests.Config.Serializers.ConnectionSerializers.Xml
|
||||
|
||||
public void Setup(string confCons, string password)
|
||||
{
|
||||
_xmlConnectionsDeserializer = new XmlConnectionsDeserializer(password.ConvertToSecureString);
|
||||
var connectionsService = new ConnectionsService(PuttySessionsManager.Instance, new Import());
|
||||
_xmlConnectionsDeserializer = new XmlConnectionsDeserializer(connectionsService, password.ConvertToSecureString);
|
||||
_connectionTreeModel = _xmlConnectionsDeserializer.Deserialize(confCons);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Linq;
|
||||
using mRemoteNG.Config.Serializers;
|
||||
using mRemoteNG.App;
|
||||
using mRemoteNG.Config.Putty;
|
||||
using mRemoteNG.Config.Serializers.Xml;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Container;
|
||||
@@ -29,6 +30,7 @@ namespace mRemoteNGTests.IntegrationTests
|
||||
_originalModel.RootNodes.OfType<RootNodeInfo>().First().PasswordString.ConvertToSecureString(),
|
||||
new SaveFilter());
|
||||
_serializer = new XmlConnectionsSerializer(cryptoProvider, nodeSerializer);
|
||||
_deserializer = new XmlConnectionsDeserializer(new ConnectionsService(PuttySessionsManager.Instance, new Import()));
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
@@ -42,7 +44,6 @@ namespace mRemoteNGTests.IntegrationTests
|
||||
public void SerializeThenDeserialize()
|
||||
{
|
||||
var serializedContent = _serializer.Serialize(_originalModel);
|
||||
_deserializer = new XmlConnectionsDeserializer();
|
||||
var deserializedModel = _deserializer.Deserialize(serializedContent);
|
||||
var nodeNamesFromDeserializedModel = deserializedModel.GetRecursiveChildList().Select(node => node.Name);
|
||||
var nodeNamesFromOriginalModel = _originalModel.GetRecursiveChildList().Select(node => node.Name);
|
||||
@@ -54,7 +55,6 @@ namespace mRemoteNGTests.IntegrationTests
|
||||
{
|
||||
_serializer.UseFullEncryption = true;
|
||||
var serializedContent = _serializer.Serialize(_originalModel);
|
||||
_deserializer = new XmlConnectionsDeserializer();
|
||||
var deserializedModel = _deserializer.Deserialize(serializedContent);
|
||||
var nodeNamesFromDeserializedModel = deserializedModel.GetRecursiveChildList().Select(node => node.Name);
|
||||
var nodeNamesFromOriginalModel = _originalModel.GetRecursiveChildList().Select(node => node.Name);
|
||||
@@ -66,7 +66,6 @@ namespace mRemoteNGTests.IntegrationTests
|
||||
{
|
||||
var originalConnectionInfo = new ConnectionInfo {Name = "con1", Description = "£°úg¶┬ä" };
|
||||
var serializedContent = _serializer.Serialize(originalConnectionInfo);
|
||||
_deserializer = new XmlConnectionsDeserializer();
|
||||
var deserializedModel = _deserializer.Deserialize(serializedContent);
|
||||
var deserializedConnectionInfo = deserializedModel.GetRecursiveChildList().First(node => node.Name == originalConnectionInfo.Name);
|
||||
Assert.That(deserializedConnectionInfo.Description, Is.EqualTo(originalConnectionInfo.Description));
|
||||
@@ -84,7 +83,6 @@ namespace mRemoteNGTests.IntegrationTests
|
||||
new SaveFilter());
|
||||
_serializer = new XmlConnectionsSerializer(cryptoProvider, nodeSerializer);
|
||||
var serializedContent = _serializer.Serialize(_originalModel);
|
||||
_deserializer = new XmlConnectionsDeserializer();
|
||||
var deserializedModel = _deserializer.Deserialize(serializedContent);
|
||||
var nodeNamesFromDeserializedModel = deserializedModel.GetRecursiveChildList().Select(node => node.Name);
|
||||
var nodeNamesFromOriginalModel = _originalModel.GetRecursiveChildList().Select(node => node.Name);
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using mRemoteNG.Config.Import;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Connection.Protocol;
|
||||
using mRemoteNG.Container;
|
||||
using mRemoteNG.Tools;
|
||||
@@ -11,6 +12,9 @@ namespace mRemoteNG.App
|
||||
{
|
||||
public class Import
|
||||
{
|
||||
// TODO - this is only a property to break up a circular dependency. move this to ctor when able
|
||||
public ConnectionsService ConnectionsService { get; set; }
|
||||
|
||||
public void ImportFromFile(ContainerInfo importDestinationContainer)
|
||||
{
|
||||
try
|
||||
@@ -50,7 +54,7 @@ namespace mRemoteNG.App
|
||||
}
|
||||
}
|
||||
|
||||
Runtime.ConnectionsService.SaveConnectionsAsync();
|
||||
ConnectionsService.SaveConnectionsAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -64,7 +68,7 @@ namespace mRemoteNG.App
|
||||
try
|
||||
{
|
||||
ActiveDirectoryImporter.Import(ldapPath, importDestinationContainer, importSubOu);
|
||||
Runtime.ConnectionsService.SaveConnectionsAsync();
|
||||
ConnectionsService.SaveConnectionsAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -78,7 +82,7 @@ namespace mRemoteNG.App
|
||||
{
|
||||
var importer = new PortScanImporter(protocol);
|
||||
importer.Import(hosts, importDestinationContainer);
|
||||
Runtime.ConnectionsService.SaveConnectionsAsync();
|
||||
ConnectionsService.SaveConnectionsAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -93,7 +97,7 @@ namespace mRemoteNG.App
|
||||
switch (extension.ToLowerInvariant())
|
||||
{
|
||||
case ".xml":
|
||||
return new MRemoteNGXmlImporter();
|
||||
return new MRemoteNGXmlImporter(ConnectionsService);
|
||||
case ".csv":
|
||||
return new MRemoteNGCsvImporter();
|
||||
case ".rdp":
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace mRemoteNG.App
|
||||
messageCollector.AddMessage(MessageClass.DebugMsg, "Determining if we need a database syncronizer");
|
||||
if (!Settings.Default.UseSQLServer) return;
|
||||
messageCollector.AddMessage(MessageClass.DebugMsg, "Creating database syncronizer");
|
||||
_connectionsService.RemoteConnectionsSyncronizer = new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker());
|
||||
_connectionsService.RemoteConnectionsSyncronizer = new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker(_connectionsService), _connectionsService);
|
||||
_connectionsService.RemoteConnectionsSyncronizer.Enable();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Timers;
|
||||
using mRemoteNG.App;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Tools;
|
||||
|
||||
// ReSharper disable ArrangeAccessorOwnerBody
|
||||
|
||||
namespace mRemoteNG.Config.Connections.Multiuser
|
||||
@@ -9,15 +11,17 @@ namespace mRemoteNG.Config.Connections.Multiuser
|
||||
{
|
||||
private readonly Timer _updateTimer;
|
||||
private readonly IConnectionsUpdateChecker _updateChecker;
|
||||
private readonly ConnectionsService _connectionsService;
|
||||
|
||||
public double TimerIntervalInMilliseconds
|
||||
{
|
||||
get { return _updateTimer.Interval; }
|
||||
}
|
||||
|
||||
public RemoteConnectionsSyncronizer(IConnectionsUpdateChecker updateChecker)
|
||||
public RemoteConnectionsSyncronizer(IConnectionsUpdateChecker updateChecker, ConnectionsService connectionsService)
|
||||
{
|
||||
_updateChecker = updateChecker;
|
||||
_updateChecker = updateChecker.ThrowIfNull(nameof(updateChecker));
|
||||
_connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService));
|
||||
_updateTimer = new Timer(3000);
|
||||
SetEventListeners();
|
||||
}
|
||||
@@ -33,7 +37,7 @@ namespace mRemoteNG.Config.Connections.Multiuser
|
||||
|
||||
private void Load(object sender, ConnectionsUpdateAvailableEventArgs args)
|
||||
{
|
||||
Runtime.ConnectionsService.LoadConnections(true, false, "");
|
||||
_connectionsService.LoadConnections(true, false, "");
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ using System.Data.SqlClient;
|
||||
using System.Threading;
|
||||
using mRemoteNG.Config.Connections.Multiuser;
|
||||
using mRemoteNG.Config.DatabaseConnectors;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Tools;
|
||||
|
||||
namespace mRemoteNG.Config.Connections
|
||||
{
|
||||
@@ -13,12 +15,13 @@ namespace mRemoteNG.Config.Connections
|
||||
{
|
||||
private readonly SqlDatabaseConnector _sqlConnector;
|
||||
private readonly SqlCommand _sqlQuery;
|
||||
private readonly ConnectionsService _connectionsService;
|
||||
private DateTime _lastUpdateTime;
|
||||
private DateTime _lastDatabaseUpdateTime;
|
||||
|
||||
|
||||
public SqlConnectionsUpdateChecker()
|
||||
public SqlConnectionsUpdateChecker(ConnectionsService connectionsService)
|
||||
{
|
||||
_connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService));
|
||||
_sqlConnector = DatabaseConnectorFactory.SqlDatabaseConnectorFromSettings();
|
||||
_sqlQuery = new SqlCommand("SELECT * FROM tblUpdate", _sqlConnector.SqlConnection);
|
||||
_lastUpdateTime = default(DateTime);
|
||||
@@ -64,7 +67,7 @@ namespace mRemoteNG.Config.Connections
|
||||
|
||||
private bool CheckIfIAmTheLastOneUpdated(DateTime lastUpdateInDb)
|
||||
{
|
||||
DateTime LastSqlUpdateWithoutMilliseconds = new DateTime(Runtime.ConnectionsService.LastSqlUpdate.Ticks - (Runtime.ConnectionsService.LastSqlUpdate.Ticks % TimeSpan.TicksPerSecond), Runtime.ConnectionsService.LastSqlUpdate.Kind);
|
||||
DateTime LastSqlUpdateWithoutMilliseconds = new DateTime(_connectionsService.LastSqlUpdate.Ticks - (_connectionsService.LastSqlUpdate.Ticks % TimeSpan.TicksPerSecond), _connectionsService.LastSqlUpdate.Kind);
|
||||
return lastUpdateInDb == LastSqlUpdateWithoutMilliseconds;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,34 +1,28 @@
|
||||
using System;
|
||||
using System.Security;
|
||||
using System.Security;
|
||||
using mRemoteNG.Config.DataProviders;
|
||||
using mRemoteNG.Config.Serializers;
|
||||
using mRemoteNG.Tools;
|
||||
using mRemoteNG.Tree;
|
||||
using System.IO;
|
||||
using mRemoteNG.Config.Serializers.Xml;
|
||||
using mRemoteNG.Connection;
|
||||
|
||||
namespace mRemoteNG.Config.Connections
|
||||
{
|
||||
public class XmlConnectionsLoader
|
||||
{
|
||||
{
|
||||
private readonly ConnectionsService _connectionsService;
|
||||
private readonly string _connectionFilePath;
|
||||
|
||||
public XmlConnectionsLoader(string connectionFilePath)
|
||||
public XmlConnectionsLoader(string connectionFilePath, ConnectionsService connectionsService)
|
||||
{
|
||||
if (string.IsNullOrEmpty(connectionFilePath))
|
||||
throw new ArgumentException($"{nameof(connectionFilePath)} cannot be null or empty");
|
||||
|
||||
if (!File.Exists(connectionFilePath))
|
||||
throw new FileNotFoundException($"{connectionFilePath} does not exist");
|
||||
|
||||
_connectionFilePath = connectionFilePath;
|
||||
_connectionFilePath = connectionFilePath.ThrowIfNullOrEmpty(nameof(connectionFilePath));
|
||||
_connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService));
|
||||
}
|
||||
|
||||
public ConnectionTreeModel Load()
|
||||
{
|
||||
var dataProvider = new FileDataProvider(_connectionFilePath);
|
||||
var xmlString = dataProvider.Load();
|
||||
var deserializer = new XmlConnectionsDeserializer(PromptForPassword);
|
||||
var deserializer = new XmlConnectionsDeserializer(_connectionsService, PromptForPassword);
|
||||
return deserializer.Deserialize(xmlString);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,11 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using mRemoteNG.App;
|
||||
using mRemoteNG.Config.DataProviders;
|
||||
using mRemoteNG.Config.Serializers;
|
||||
using mRemoteNG.Config.Serializers.Xml;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Container;
|
||||
using mRemoteNG.Messages;
|
||||
using mRemoteNG.Tools;
|
||||
|
||||
|
||||
namespace mRemoteNG.Config.Import
|
||||
@@ -13,6 +14,13 @@ namespace mRemoteNG.Config.Import
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public class MRemoteNGXmlImporter : IConnectionImporter<string>
|
||||
{
|
||||
private readonly ConnectionsService _connectionsService;
|
||||
|
||||
public MRemoteNGXmlImporter(ConnectionsService connectionsService)
|
||||
{
|
||||
_connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService));
|
||||
}
|
||||
|
||||
public void Import(string fileName, ContainerInfo destinationContainer)
|
||||
{
|
||||
if (fileName == null)
|
||||
@@ -26,7 +34,7 @@ namespace mRemoteNG.Config.Import
|
||||
|
||||
var dataProvider = new FileDataProvider(fileName);
|
||||
var xmlString = dataProvider.Load();
|
||||
var xmlConnectionsDeserializer = new XmlConnectionsDeserializer();
|
||||
var xmlConnectionsDeserializer = new XmlConnectionsDeserializer(_connectionsService);
|
||||
var connectionTreeModel = xmlConnectionsDeserializer.Deserialize(xmlString, true);
|
||||
|
||||
var rootImportContainer = new ContainerInfo { Name = Path.GetFileNameWithoutExtension(fileName) };
|
||||
|
||||
@@ -13,6 +13,7 @@ using mRemoteNG.Connection.Protocol.VNC;
|
||||
using mRemoteNG.Container;
|
||||
using mRemoteNG.Messages;
|
||||
using mRemoteNG.Security;
|
||||
using mRemoteNG.Tools;
|
||||
using mRemoteNG.Tree;
|
||||
using mRemoteNG.Tree.Root;
|
||||
using mRemoteNG.UI.Forms;
|
||||
@@ -21,7 +22,8 @@ using mRemoteNG.UI.TaskDialog;
|
||||
namespace mRemoteNG.Config.Serializers.Xml
|
||||
{
|
||||
public class XmlConnectionsDeserializer : IDeserializer<string, ConnectionTreeModel>
|
||||
{
|
||||
{
|
||||
private readonly ConnectionsService _connectionsService;
|
||||
private XmlDocument _xmlDocument;
|
||||
private double _confVersion;
|
||||
private XmlConnectionsDecryptor _decryptor;
|
||||
@@ -31,8 +33,9 @@ namespace mRemoteNG.Config.Serializers.Xml
|
||||
|
||||
public Func<SecureString> AuthenticationRequestor { get; set; }
|
||||
|
||||
public XmlConnectionsDeserializer(Func<SecureString> authenticationRequestor = null)
|
||||
public XmlConnectionsDeserializer(ConnectionsService connectionsService, Func<SecureString> authenticationRequestor = null)
|
||||
{
|
||||
_connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService));
|
||||
AuthenticationRequestor = authenticationRequestor;
|
||||
}
|
||||
|
||||
@@ -48,7 +51,7 @@ namespace mRemoteNG.Config.Serializers.Xml
|
||||
LoadXmlConnectionData(xml);
|
||||
ValidateConnectionFileVersion();
|
||||
if (!import)
|
||||
Runtime.ConnectionsService.IsConnectionsFileLoaded = false;
|
||||
_connectionsService.IsConnectionsFileLoaded = false;
|
||||
|
||||
var rootXmlElement = _xmlDocument.DocumentElement;
|
||||
InitializeRootNode(rootXmlElement);
|
||||
@@ -81,13 +84,13 @@ namespace mRemoteNG.Config.Serializers.Xml
|
||||
AddNodesFromXmlRecursive(_xmlDocument.DocumentElement, _rootNodeInfo);
|
||||
|
||||
if (!import)
|
||||
Runtime.ConnectionsService.IsConnectionsFileLoaded = true;
|
||||
_connectionsService.IsConnectionsFileLoaded = true;
|
||||
|
||||
return connectionTreeModel;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Runtime.ConnectionsService.IsConnectionsFileLoaded = false;
|
||||
_connectionsService.IsConnectionsFileLoaded = false;
|
||||
Runtime.MessageCollector.AddExceptionStackTrace(Language.strLoadFromXmlFailed, ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ namespace mRemoteNG.Connection
|
||||
var newConnectionTreeModel =
|
||||
(useDatabase
|
||||
? new SqlConnectionsLoader().Load()
|
||||
: new XmlConnectionsLoader(connectionFileName).Load())
|
||||
: new XmlConnectionsLoader(connectionFileName, this).Load())
|
||||
?? new ConnectionTreeModel();
|
||||
|
||||
if (!import)
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace mRemoteNG.UI.Forms.OptionsPages
|
||||
private void ReinitializeSqlUpdater()
|
||||
{
|
||||
_connectionsService.RemoteConnectionsSyncronizer?.Dispose();
|
||||
_connectionsService.RemoteConnectionsSyncronizer = new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker());
|
||||
_connectionsService.RemoteConnectionsSyncronizer = new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker(_connectionsService), _connectionsService);
|
||||
_connectionsService.RemoteConnectionsSyncronizer.Enable();
|
||||
}
|
||||
|
||||
|
||||
@@ -79,6 +79,7 @@ namespace mRemoteNG.UI.Forms
|
||||
var externalToolsService = new ExternalToolsService();
|
||||
_import = new Import();
|
||||
_connectionsService = new ConnectionsService(PuttySessionsManager.Instance, _import);
|
||||
_import.ConnectionsService = _connectionsService;
|
||||
_appUpdater = new AppUpdater(() => _connectionsService.EncryptionKey);
|
||||
ExternalToolsTypeConverter.ExternalToolsService = externalToolsService;
|
||||
_export = new Export(_credentialRepositoryList, _connectionsService);
|
||||
|
||||
Reference in New Issue
Block a user