public override void Terminate() { GlobalWindowManager.WindowAdded -= WindowAddedHandler; MessageService.MessageShowing -= MessageService_MessageShowing; SprEngine.FilterCompile -= SprEngine_FilterCompile; SprEngine.FilterPlaceholderHints.Remove(keyFilePathSprPlaceholder); SprEngine.FilterPlaceholderHints.Remove(identFileOptSprPlaceholder); if (columnProvider != null) { pluginHost.ColumnProviderPool.Remove(columnProvider); columnProvider.Dispose(); columnProvider = null; } if (debug) Log("Terminating KeeAgent"); var agentModeAgent = agent as Agent; if (agentModeAgent != null) { // need to shutdown agent or app won't exit agentModeAgent.Dispose(); } RemoveMenuItems(); }
public override bool Initialize(IPluginHost host) { pluginHost = host; uiHelper = new UIHelper(pluginHost); removeKeyList = new List<ISshKey>(); debug = (pluginHost .CommandLineArgs[AppDefs.CommandLineOptions.Debug] != null); LoadOptions(); if (debug) Log("Loading KeeAgent..."); var isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT; var domainSocketPath = Environment.GetEnvironmentVariable (UnixClient.SshAuthSockName); try { if (Options.AgentMode != AgentMode.Client) { if (isWindows) { // In windows, try to start an agent. If Pageant is running, we will // get an exception. try { var pagent = new PageantAgent(); pagent.Locked += PageantAgent_Locked; pagent.KeyUsed += PageantAgent_KeyUsed; pagent.KeyAdded += PageantAgent_KeyAdded; pagent.KeyRemoved += PageantAgent_KeyRemoved; pagent.MessageReceived += PageantAgent_MessageReceived; // IMPORTANT: if you change either of these callbacks, you need // to make sure that they do not block the main event loop. pagent.FilterKeyListCallback = FilterKeyList; pagent.ConfirmUserPermissionCallback = Default.ConfirmCallback; agent = pagent; if (Options.UseCygwinSocket) { StartCygwinSocket(); } if (Options.UseMsysSocket) { StartMsysSocket(); } } catch (PageantRunningException) { if (Options.AgentMode != AgentMode.Auto) { throw; } } } else { // In Unix, we only try to start an agent if Agent mode was explicitly // selected or there is no agent running (indicated by environment variable). if (Options.AgentMode == AgentMode.Server || string.IsNullOrWhiteSpace (domainSocketPath)) { var unixAgent = new UnixAgent(); unixAgent.Locked += PageantAgent_Locked; unixAgent.KeyUsed += PageantAgent_KeyUsed; unixAgent.KeyAdded += PageantAgent_KeyAdded; unixAgent.KeyRemoved += PageantAgent_KeyRemoved; unixAgent.MessageReceived += PageantAgent_MessageReceived; // IMPORTANT: if you change either of these callbacks, you need // to make sure that they do not block the main event loop. unixAgent.FilterKeyListCallback = FilterKeyList; unixAgent.ConfirmUserPermissionCallback = Default.ConfirmCallback; agent = unixAgent; try { var socketPath = Options.UnixSocketPath.ExpandEnvironmentVariables(); unixAgent.StartUnixSocket (socketPath); } catch (ArgumentNullException) { var autoModeMessage = Options.AgentMode == AgentMode.Auto ? " to use KeeAgent in Agent mode or enable an external SSH agent in your " + "desktop session manager to use KeeAgent in Client mode." : "."; MessageService.ShowWarning("KeeAgent: No path specified for Agent socket file.", "Please enter a file in the KeeAgent options (Tools > Options... > KeeAgent tab)" + autoModeMessage); } catch (Exception ex) { MessageService.ShowWarning(ex.Message); } } } } if (agent == null) { if (isWindows) { agent = new PageantClient(); } else { agent = new UnixClient(); } } pluginHost.MainWindow.FileOpened += MainForm_FileOpened; pluginHost.MainWindow.FileClosingPost += MainForm_FileClosing; pluginHost.MainWindow.FileClosed += MainForm_FileClosed; // load all database that are already opened foreach (var database in pluginHost.MainWindow.DocumentManager.Documents) { MainForm_FileOpened(this, new FileOpenedEventArgs(database.Database)); } AddMenuItems(); GlobalWindowManager.WindowAdded += WindowAddedHandler; MessageService.MessageShowing += MessageService_MessageShowing; columnProvider = new KeeAgentColumnProvider(this); host.ColumnProviderPool.Add(columnProvider); SprEngine.FilterCompile += SprEngine_FilterCompile; SprEngine.FilterPlaceholderHints.Add(keyFilePathSprPlaceholder); SprEngine.FilterPlaceholderHints.Add(identFileOptSprPlaceholder); return true; } catch (PageantRunningException) { ShowPageantRunningErrorMessage(); } catch (Exception ex) { MessageService.ShowWarning("KeeAgent failed to load:", ex.Message); } Terminate(); return false; }