diff --git a/mRemoteNGTests/Connection/Protocol/IntegratedProgramTests.cs b/mRemoteNGTests/Connection/Protocol/IntegratedProgramTests.cs index 777e0557..16bc5573 100644 --- a/mRemoteNGTests/Connection/Protocol/IntegratedProgramTests.cs +++ b/mRemoteNGTests/Connection/Protocol/IntegratedProgramTests.cs @@ -1,6 +1,8 @@ using System; +using System.Security; using System.Threading; using mRemoteNG.App; +using mRemoteNG.App.Update; using mRemoteNG.Config.Putty; using mRemoteNG.Config.Settings; using mRemoteNG.Connection; @@ -61,23 +63,26 @@ namespace mRemoteNGTests.Connection.Protocol private InterfaceControl BuildInterfaceControl(string extAppName, ProtocolBase sut) { var import = new Import(); + var connectionsService = new ConnectionsService(PuttySessionsManager.Instance, import); var configWindow = new ConfigWindow(new DockContent()); var sshTransferWindow = new SSHTransferWindow(); var connectionTreeWindow = new ConnectionTreeWindow(new DockContent(), _connectionInitiator); var connectionTree = connectionTreeWindow.ConnectionTree; - var export = new Export(new CredentialRepositoryList()); + var export = new Export(new CredentialRepositoryList(), connectionsService); var connectionTreeContextMenu = new ConnectionContextMenu(connectionTree, _connectionInitiator, sshTransferWindow, export, _externalToolsService, import); connectionTreeWindow.ConnectionTreeContextMenu = connectionTreeContextMenu; var errorAndInfoWindow = new ErrorAndInfoWindow(new DockContent(), connectionTreeWindow); var screenshotManagerWindow = new ScreenshotManagerWindow(new DockContent()); var shutdown = new Shutdown(new SettingsSaver(new ExternalToolsService()), new ConnectionsService(PuttySessionsManager.Instance, import)); - Func updateWindowBuilder = () => new UpdateWindow(new DockContent(), shutdown); + var appUpdater = new AppUpdater(() => connectionsService.EncryptionKey); + Func updateWindowBuilder = () => new UpdateWindow(new DockContent(), shutdown, appUpdater); Func notificationAreaIconBuilder = () => new NotificationAreaIcon(FrmMain.Default, _connectionInitiator, shutdown); Func externalToolsWindowBuilder = () => new ExternalToolsWindow(_connectionInitiator, _externalToolsService); Func portScanWindowBuilder = () => new PortScanWindow(() => connectionTreeWindow.SelectedNode, import); Func activeDirectoryImportWindowBuilder = () => new ActiveDirectoryImportWindow(() => connectionTreeWindow.SelectedNode, import); var windows = new Windows(_connectionInitiator, connectionTreeWindow, configWindow, errorAndInfoWindow, screenshotManagerWindow, - sshTransferWindow, updateWindowBuilder, notificationAreaIconBuilder, externalToolsWindowBuilder, Runtime.ConnectionsService, portScanWindowBuilder, activeDirectoryImportWindowBuilder); + sshTransferWindow, updateWindowBuilder, notificationAreaIconBuilder, externalToolsWindowBuilder, + connectionsService, portScanWindowBuilder, activeDirectoryImportWindowBuilder, appUpdater); var connectionWindow = new ConnectionWindow(new DockContent(), _connectionInitiator, windows, _externalToolsService); var connectionInfo = new ConnectionInfo {ExtApp = extAppName}; return new InterfaceControl(connectionWindow, sut, connectionInfo); diff --git a/mRemoteNGTests/UI/Forms/OptionsFormSetupAndTeardown.cs b/mRemoteNGTests/UI/Forms/OptionsFormSetupAndTeardown.cs index 1d9a87ee..de776ed7 100644 --- a/mRemoteNGTests/UI/Forms/OptionsFormSetupAndTeardown.cs +++ b/mRemoteNGTests/UI/Forms/OptionsFormSetupAndTeardown.cs @@ -1,5 +1,7 @@ using System; +using System.Security; using mRemoteNG.App; +using mRemoteNG.App.Update; using mRemoteNG.Config.Putty; using mRemoteNG.Config.Settings; using mRemoteNG.Connection; @@ -26,7 +28,9 @@ namespace mRemoteNGTests.UI.Forms var import = new Import(); var shutdown = new Shutdown(new SettingsSaver(new ExternalToolsService()), new ConnectionsService(PuttySessionsManager.Instance, import)); Func notificationIconBuilder = () => new NotificationAreaIcon(FrmMain.Default, connectionInitiator, shutdown); - _optionsForm = new frmOptions(connectionInitiator, type => {}, notificationIconBuilder, Runtime.ConnectionsService); + var connectionsService = new ConnectionsService(PuttySessionsManager.Instance, import); + var appUpdater = new AppUpdater(() => connectionsService.EncryptionKey); + _optionsForm = new frmOptions(connectionInitiator, type => {}, notificationIconBuilder, connectionsService, appUpdater); _optionsForm.Show(); } diff --git a/mRemoteNGTests/UI/Window/ConnectionTreeWindowTests.cs b/mRemoteNGTests/UI/Window/ConnectionTreeWindowTests.cs index b86c2d8c..ed817412 100644 --- a/mRemoteNGTests/UI/Window/ConnectionTreeWindowTests.cs +++ b/mRemoteNGTests/UI/Window/ConnectionTreeWindowTests.cs @@ -1,5 +1,6 @@ using System.Threading; using mRemoteNG.App; +using mRemoteNG.Config.Putty; using mRemoteNG.Connection; using mRemoteNG.Credential.Repositories; using mRemoteNG.Tools; @@ -23,7 +24,9 @@ namespace mRemoteNGTests.UI.Window var sshTransferWindow = new SSHTransferWindow(); var externalToolsService = new ExternalToolsService(); var import = new Import(); - var connectionContextMenu = new ConnectionContextMenu(connectionTree, connectionInitiator, sshTransferWindow, new Export(new CredentialRepositoryList()), externalToolsService, import); + var connectionsService = new ConnectionsService(PuttySessionsManager.Instance, import); + var export = new Export(new CredentialRepositoryList(), connectionsService); + var connectionContextMenu = new ConnectionContextMenu(connectionTree, connectionInitiator, sshTransferWindow, export, externalToolsService, import); _connectionTreeWindow = new ConnectionTreeWindow(new DockContent(), connectionInitiator) {ConnectionTreeContextMenu = connectionContextMenu}; } diff --git a/mRemoteV1/App/Export.cs b/mRemoteV1/App/Export.cs index 7aa5a70c..c5078b95 100644 --- a/mRemoteV1/App/Export.cs +++ b/mRemoteV1/App/Export.cs @@ -21,11 +21,13 @@ namespace mRemoteNG.App { public class Export { + private readonly ConnectionsService _connectionsService; private readonly ICredentialRepositoryList _credentialRepositoryList; - public Export(ICredentialRepositoryList credentialRepositoryList) + public Export(ICredentialRepositoryList credentialRepositoryList, ConnectionsService connectionsService) { _credentialRepositoryList = credentialRepositoryList.ThrowIfNull(nameof(credentialRepositoryList)); + _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); } public void ExportToFile(ConnectionInfo selectedNode, ConnectionTreeModel connectionTreeModel) @@ -110,7 +112,7 @@ namespace mRemoteNG.App } finally { - Runtime.ConnectionsService.RemoteConnectionsSyncronizer?.Enable(); + _connectionsService.RemoteConnectionsSyncronizer?.Enable(); } } } diff --git a/mRemoteV1/App/Startup.cs b/mRemoteV1/App/Startup.cs index c1910499..47c274c1 100644 --- a/mRemoteV1/App/Startup.cs +++ b/mRemoteV1/App/Startup.cs @@ -19,18 +19,18 @@ namespace mRemoteNG.App { public class Startup { - private AppUpdater _appUpdate; + private readonly AppUpdater _appUpdate; private readonly ConnectionIconLoader _connectionIconLoader; private readonly FrmMain _frmMain; private readonly Windows _windows; private readonly ConnectionsService _connectionsService; - public Startup(FrmMain frmMain, Windows windows, ConnectionsService connectionsService) + public Startup(FrmMain frmMain, Windows windows, ConnectionsService connectionsService, AppUpdater appUpdate) { _frmMain = frmMain.ThrowIfNull(nameof(frmMain)); _windows = windows.ThrowIfNull(nameof(windows)); _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); - _appUpdate = new AppUpdater(); + _appUpdate = appUpdate.ThrowIfNull(nameof(appUpdate)); _connectionIconLoader = new ConnectionIconLoader(GeneralAppInfo.HomePath + "\\Icons\\"); } @@ -64,11 +64,7 @@ namespace mRemoteNG.App public void CheckForUpdate() { - if (_appUpdate == null) - { - _appUpdate = new AppUpdater(); - } - else if (_appUpdate.IsGetUpdateInfoRunning) + if (_appUpdate.IsGetUpdateInfoRunning) { return; } diff --git a/mRemoteV1/App/Update/AppUpdater.cs b/mRemoteV1/App/Update/AppUpdater.cs index 8a87ea40..ef718e31 100644 --- a/mRemoteV1/App/Update/AppUpdater.cs +++ b/mRemoteV1/App/Update/AppUpdater.cs @@ -4,6 +4,7 @@ using System.Net; using System.ComponentModel; using System.Threading; using System.Reflection; +using System.Security; using mRemoteNG.App.Info; using mRemoteNG.Security.SymmetricEncryption; using System.Security.Cryptography; @@ -22,6 +23,7 @@ namespace mRemoteNG.App.Update private WebProxy _webProxy; private Thread _getUpdateInfoThread; private Thread _getChangeLogThread; + private readonly Func _encryptionKeyRetrievalFunc; #region Public Properties @@ -48,8 +50,9 @@ namespace mRemoteNG.App.Update #region Public Methods - public AppUpdater() + public AppUpdater(Func encryptionKeyRetrievalFunc) { + _encryptionKeyRetrievalFunc = encryptionKeyRetrievalFunc; SetProxySettings(); } @@ -61,7 +64,7 @@ namespace mRemoteNG.App.Update var useAuthentication = Settings.Default.UpdateProxyUseAuthentication; var username = Settings.Default.UpdateProxyAuthUser; var cryptographyProvider = new LegacyRijndaelCryptographyProvider(); - var password = cryptographyProvider.Decrypt(Settings.Default.UpdateProxyAuthPass, Runtime.ConnectionsService.EncryptionKey); + var password = cryptographyProvider.Decrypt(Settings.Default.UpdateProxyAuthPass, _encryptionKeyRetrievalFunc()); SetProxySettings(shouldWeUseProxy, proxyAddress, port, useAuthentication, username, password); } diff --git a/mRemoteV1/App/Windows.cs b/mRemoteV1/App/Windows.cs index 59322dcb..21865084 100644 --- a/mRemoteV1/App/Windows.cs +++ b/mRemoteV1/App/Windows.cs @@ -1,4 +1,5 @@ using System; +using mRemoteNG.App.Update; using mRemoteNG.Connection; using mRemoteNG.Messages; using mRemoteNG.Tools; @@ -25,6 +26,7 @@ namespace mRemoteNG.App private readonly Func _portScanWindowBuilder; private readonly Func _activeDirectoryImportWindowBuilder; private readonly ConnectionsService _connectionsService; + private readonly AppUpdater _appUpdater; internal ConnectionTreeWindow TreeForm { get; } internal ConfigWindow ConfigForm { get; } @@ -44,7 +46,8 @@ namespace mRemoteNG.App Func externalToolsWindowBuilder, ConnectionsService connectionsService, Func portScanWindowBuilder, - Func activeDirectoryImportWindowBuilder) + Func activeDirectoryImportWindowBuilder, + AppUpdater appUpdater) { _connectionInitiator = connectionInitiator.ThrowIfNull(nameof(connectionInitiator)); TreeForm = treeForm.ThrowIfNull(nameof(treeForm)); @@ -57,6 +60,7 @@ namespace mRemoteNG.App _externalToolsWindowBuilder = externalToolsWindowBuilder; _portScanWindowBuilder = portScanWindowBuilder; _activeDirectoryImportWindowBuilder = activeDirectoryImportWindowBuilder; + _appUpdater = appUpdater.ThrowIfNull(nameof(appUpdater)); _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); } @@ -79,7 +83,7 @@ namespace mRemoteNG.App _adimportForm.Show(dockPanel); break; case WindowType.Options: - using (var optionsForm = new frmOptions(_connectionInitiator, Show, _notificationAreaIconBuilder, _connectionsService)) + using (var optionsForm = new frmOptions(_connectionInitiator, Show, _notificationAreaIconBuilder, _connectionsService, _appUpdater)) { optionsForm.ShowDialog(dockPanel); } diff --git a/mRemoteV1/UI/Forms/OptionsPages/UpdatesPage.cs b/mRemoteV1/UI/Forms/OptionsPages/UpdatesPage.cs index 07bef3cf..ff62f8e3 100644 --- a/mRemoteV1/UI/Forms/OptionsPages/UpdatesPage.cs +++ b/mRemoteV1/UI/Forms/OptionsPages/UpdatesPage.cs @@ -12,11 +12,12 @@ namespace mRemoteNG.UI.Forms.OptionsPages { public partial class UpdatesPage { - private AppUpdater _appUpdate; + private readonly AppUpdater _appUpdate; private readonly Action _showWindowAction; - public UpdatesPage(Action showWindowAction) + public UpdatesPage(AppUpdater appUpdate, Action showWindowAction) { + _appUpdate = appUpdate; _showWindowAction = showWindowAction.ThrowIfNull(nameof(showWindowAction)); InitializeComponent(); base.ApplyTheme(); @@ -188,15 +189,11 @@ namespace mRemoteNG.UI.Forms.OptionsPages private void btnTestProxy_Click(object sender, EventArgs e) { - if (_appUpdate != null) + if (_appUpdate.IsGetUpdateInfoRunning) { - if (_appUpdate.IsGetUpdateInfoRunning) - { - return; - } + return; } - _appUpdate = new AppUpdater(); //_appUpdate.Load += _appUpdate.Update_Load; _appUpdate.SetProxySettings(chkUseProxyForAutomaticUpdates.Checked, txtProxyAddress.Text, (int) numProxyPort.Value, chkUseProxyAuthentication.Checked, txtProxyUsername.Text, diff --git a/mRemoteV1/UI/Forms/frmMain.cs b/mRemoteV1/UI/Forms/frmMain.cs index 62ca244a..8b11a178 100644 --- a/mRemoteV1/UI/Forms/frmMain.cs +++ b/mRemoteV1/UI/Forms/frmMain.cs @@ -12,6 +12,7 @@ using Microsoft.Win32; using mRemoteNG.App; using mRemoteNG.App.Info; using mRemoteNG.App.Initialization; +using mRemoteNG.App.Update; using mRemoteNG.Config; using mRemoteNG.Config.Connections; using mRemoteNG.Config.Putty; @@ -64,6 +65,7 @@ namespace mRemoteNG.UI.Forms private readonly Func _notificationAreaIconBuilder; private readonly ConnectionsService _connectionsService; private readonly Import _import; + private readonly AppUpdater _appUpdater; internal FullscreenHandler Fullscreen { get; set; } @@ -77,9 +79,9 @@ namespace mRemoteNG.UI.Forms var externalToolsService = new ExternalToolsService(); _import = new Import(); _connectionsService = new ConnectionsService(PuttySessionsManager.Instance, _import); - + _appUpdater = new AppUpdater(() => _connectionsService.EncryptionKey); ExternalToolsTypeConverter.ExternalToolsService = externalToolsService; - _export = new Export(_credentialRepositoryList); + _export = new Export(_credentialRepositoryList, _connectionsService); _connectionInitiator = new ConnectionInitiator(_windowList, externalToolsService); _webHelper = new WebHelper(_connectionInitiator); var configWindow = new ConfigWindow(new DockContent()); @@ -94,18 +96,18 @@ namespace mRemoteNG.UI.Forms var screenshotManagerWindow = new ScreenshotManagerWindow(new DockContent()); _settingsSaver = new SettingsSaver(externalToolsService); _shutdown = new Shutdown(_settingsSaver, _connectionsService); - Func updateWindowBuilder = () => new UpdateWindow(new DockContent(), _shutdown); + Func updateWindowBuilder = () => new UpdateWindow(new DockContent(), _shutdown, _appUpdater); _notificationAreaIconBuilder = () => new NotificationAreaIcon(this, _connectionInitiator, _shutdown); Func externalToolsWindowBuilder = () => new ExternalToolsWindow(_connectionInitiator, externalToolsService); Func portScanWindowBuilder = () => new PortScanWindow(() => connectionTreeWindow.SelectedNode, _import); Func activeDirectoryImportWindowBuilder = () => new ActiveDirectoryImportWindow(() => connectionTreeWindow.SelectedNode, _import); _windows = new Windows(_connectionInitiator, connectionTreeWindow, configWindow, errorAndInfoWindow, screenshotManagerWindow, - sshTransferWindow, updateWindowBuilder, _notificationAreaIconBuilder, externalToolsWindowBuilder, _connectionsService, portScanWindowBuilder, activeDirectoryImportWindowBuilder); + sshTransferWindow, updateWindowBuilder, _notificationAreaIconBuilder, externalToolsWindowBuilder, _connectionsService, portScanWindowBuilder, activeDirectoryImportWindowBuilder, _appUpdater); Func connectionWindowBuilder = () => new ConnectionWindow(new DockContent(), _connectionInitiator, _windows, externalToolsService); _panelAdder = new PanelAdder(_windowList, connectionWindowBuilder); _showFullPathInTitle = Settings.Default.ShowCompleteConsPathInTitle; _connectionInitiator.Adder = _panelAdder; - _startup = new Startup(this, _windows, _connectionsService); + _startup = new Startup(this, _windows, _connectionsService, _appUpdater); connectionTreeContextMenu.ShowWindowAction = _windows.Show; InitializeComponent(); @@ -341,7 +343,7 @@ namespace mRemoteNG.UI.Forms if (CTaskDialog.CommandButtonResult != 1) return; - using (var optionsForm = new frmOptions(_connectionInitiator, _windows.Show, _notificationAreaIconBuilder, _connectionsService, Language.strTabUpdates)) + using (var optionsForm = new frmOptions(_connectionInitiator, _windows.Show, _notificationAreaIconBuilder, _connectionsService, _appUpdater, Language.strTabUpdates)) { optionsForm.ShowDialog(this); } diff --git a/mRemoteV1/UI/Forms/frmOptions.cs b/mRemoteV1/UI/Forms/frmOptions.cs index f435f343..9c786501 100644 --- a/mRemoteV1/UI/Forms/frmOptions.cs +++ b/mRemoteV1/UI/Forms/frmOptions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Windows.Forms; +using mRemoteNG.App.Update; using mRemoteNG.Connection; using mRemoteNG.Tools; using mRemoteNG.UI.Forms.OptionsPages; @@ -18,18 +19,20 @@ namespace mRemoteNG.UI.Forms private readonly Action _showWindowAction; private readonly Func _notificationAreaIconBuilder; private readonly ConnectionsService _connectionsService; + private readonly AppUpdater _appUpdater; - public frmOptions(IConnectionInitiator connectionInitiator, Action showWindowAction, Func notificationAreaIconBuilder, ConnectionsService connectionsService) - : this(connectionInitiator, showWindowAction, notificationAreaIconBuilder, connectionsService, Language.strStartupExit) + public frmOptions(IConnectionInitiator connectionInitiator, Action showWindowAction, Func notificationAreaIconBuilder, ConnectionsService connectionsService, AppUpdater appUpdater) + : this(connectionInitiator, showWindowAction, notificationAreaIconBuilder, connectionsService, appUpdater, Language.strStartupExit) { } - public frmOptions(IConnectionInitiator connectionInitiator, Action showWindowAction, Func notificationAreaIconBuilder, ConnectionsService connectionsService, string pageName) + public frmOptions(IConnectionInitiator connectionInitiator, Action showWindowAction, Func notificationAreaIconBuilder, ConnectionsService connectionsService, AppUpdater appUpdater, string pageName) { _connectionInitiator = connectionInitiator.ThrowIfNull(nameof(connectionInitiator)); _showWindowAction = showWindowAction.ThrowIfNull(nameof(showWindowAction)); _notificationAreaIconBuilder = notificationAreaIconBuilder; _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); + _appUpdater = appUpdater.ThrowIfNull(nameof(appUpdater)); _pageName = pageName.ThrowIfNull(nameof(pageName)); InitializeComponent(); } @@ -76,7 +79,7 @@ namespace mRemoteNG.UI.Forms {typeof(ConnectionsPage).Name, new ConnectionsPage()}, {typeof(CredentialsPage).Name, new CredentialsPage()}, {typeof(SqlServerPage).Name, new SqlServerPage(_connectionsService)}, - {typeof(UpdatesPage).Name, new UpdatesPage(_showWindowAction)}, + {typeof(UpdatesPage).Name, new UpdatesPage(_appUpdater, _showWindowAction)}, {typeof(ThemePage).Name, new ThemePage()}, {typeof(SecurityPage).Name, new SecurityPage()}, {typeof(AdvancedPage).Name, new AdvancedPage()} diff --git a/mRemoteV1/UI/Window/UpdateWindow.cs b/mRemoteV1/UI/Window/UpdateWindow.cs index b2dc662b..537a12e8 100644 --- a/mRemoteV1/UI/Window/UpdateWindow.cs +++ b/mRemoteV1/UI/Window/UpdateWindow.cs @@ -15,15 +15,16 @@ namespace mRemoteNG.UI.Window { public partial class UpdateWindow : BaseWindow { - private AppUpdater _appUpdate; - private Shutdown _shutdown; + private readonly AppUpdater _appUpdate; + private readonly Shutdown _shutdown; private bool _isUpdateDownloadHandlerDeclared; - public UpdateWindow(DockContent panel, Shutdown shutdown) + public UpdateWindow(DockContent panel, Shutdown shutdown, AppUpdater appUpdate) { WindowType = WindowType.Update; DockPnl = panel; _shutdown = shutdown.ThrowIfNull(nameof(shutdown)); + _appUpdate = appUpdate.ThrowIfNull(nameof(appUpdate)); InitializeComponent(); FontOverrider.FontOverride(this); } @@ -85,12 +86,7 @@ namespace mRemoteNG.UI.Window #region Private Methods private void CheckForUpdate() { - if (_appUpdate == null) - { - _appUpdate = new AppUpdater(); - //_appUpdate.Load += _appUpdate.Update_Load; - } - else if (_appUpdate.IsGetUpdateInfoRunning) + if (_appUpdate.IsGetUpdateInfoRunning) { return; }