mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +08:00
removed xming sessions provider, fixes #1514
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Tree.Root;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
|
||||
// ReSharper disable ArrangeAccessorOwnerBody
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using mRemoteNG.Tools;
|
||||
@@ -24,7 +24,6 @@ namespace mRemoteNG.Config.Putty
|
||||
private PuttySessionsManager()
|
||||
{
|
||||
AddProvider(new PuttySessionsRegistryProvider());
|
||||
AddProvider(new PuttySessionsXmingProvider());
|
||||
}
|
||||
|
||||
|
||||
@@ -124,16 +123,7 @@ namespace mRemoteNG.Config.Putty
|
||||
private bool IsProviderEnabled(AbstractPuttySessionsProvider puttySessionsProvider)
|
||||
{
|
||||
var enabled = true;
|
||||
if (PuttyTypeDetector.GetPuttyType() == PuttyTypeDetector.PuttyType.Xming)
|
||||
{
|
||||
if (puttySessionsProvider is PuttySessionsRegistryProvider)
|
||||
enabled = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (puttySessionsProvider is PuttySessionsXmingProvider)
|
||||
enabled = false;
|
||||
}
|
||||
if (!(puttySessionsProvider is PuttySessionsRegistryProvider)) enabled = false;
|
||||
|
||||
return enabled;
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Management;
|
||||
using System.Net;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using Microsoft.Win32;
|
||||
using mRemoteNG.App;
|
||||
using mRemoteNG.Connection;
|
||||
|
||||
@@ -1,378 +0,0 @@
|
||||
using mRemoteNG.App;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Connection.Protocol;
|
||||
using mRemoteNG.Messages;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using mRemoteNG.Tree.Root;
|
||||
using System.Web;
|
||||
using System.Net;
|
||||
|
||||
namespace mRemoteNG.Config.Putty
|
||||
{
|
||||
public class PuttySessionsXmingProvider : AbstractPuttySessionsProvider
|
||||
{
|
||||
public override RootPuttySessionsNodeInfo RootInfo { get; } =
|
||||
new RootPuttySessionsNodeInfo {Name = "Xming Putty Sessions"};
|
||||
|
||||
private const string RegistrySessionNameFormat = "{0} [registry]";
|
||||
private const string RegistrySessionNamePattern = "(.*)\\ \\[registry\\]";
|
||||
|
||||
private static readonly PuttySessionsRegistryProvider PuttySessionsRegistryProvider =
|
||||
new PuttySessionsRegistryProvider();
|
||||
|
||||
private static FileSystemWatcher _eventWatcher;
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public override string[] GetSessionNames(bool raw = false)
|
||||
{
|
||||
var sessionsFolderPath = GetSessionsFolderPath();
|
||||
if (!Directory.Exists(sessionsFolderPath))
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
|
||||
var sessionNames = new List<string>();
|
||||
foreach (var sessionName in Directory.GetFiles(sessionsFolderPath))
|
||||
{
|
||||
var sessionFileName = Path.GetFileName(sessionName);
|
||||
// ReSharper disable once ConstantConditionalAccessQualifier
|
||||
sessionNames.Add(raw ? sessionFileName
|
||||
: WebUtility.UrlDecode(sessionFileName?.Replace("+", "%2B")));
|
||||
}
|
||||
|
||||
if (raw)
|
||||
{
|
||||
if (!sessionNames.Contains("Default%20Settings")) // Do not localize
|
||||
sessionNames.Insert(0, "Default%20Settings");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!sessionNames.Contains("Default Settings"))
|
||||
sessionNames.Insert(0, "Default Settings");
|
||||
}
|
||||
|
||||
var registrySessionNames =
|
||||
PuttySessionsRegistryProvider.GetSessionNames(raw).Select(sessionName => string.Format(RegistrySessionNameFormat, sessionName)).ToList();
|
||||
|
||||
sessionNames.AddRange(registrySessionNames);
|
||||
sessionNames.Sort();
|
||||
|
||||
return sessionNames.ToArray();
|
||||
}
|
||||
|
||||
public override PuttySessionInfo GetSession(string sessionName)
|
||||
{
|
||||
var registrySessionName = GetRegistrySessionName(sessionName);
|
||||
if (!string.IsNullOrEmpty(registrySessionName))
|
||||
{
|
||||
return ModifyRegistrySessionInfo(PuttySessionsRegistryProvider.GetSession(registrySessionName));
|
||||
}
|
||||
|
||||
var sessionsFolderPath = GetSessionsFolderPath();
|
||||
if (!Directory.Exists(sessionsFolderPath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var sessionFile = Path.Combine(sessionsFolderPath, sessionName);
|
||||
if (!File.Exists(sessionFile))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
sessionName = WebUtility.UrlDecode(sessionName.Replace("+", "%2B"));
|
||||
|
||||
var sessionFileReader = new SessionFileReader(sessionFile);
|
||||
var sessionInfo = new PuttySessionInfo
|
||||
{
|
||||
PuttySession = sessionName,
|
||||
Name = sessionName,
|
||||
Hostname = sessionFileReader.GetValue("HostName"),
|
||||
Username = sessionFileReader.GetValue("UserName")
|
||||
};
|
||||
var protocol = sessionFileReader.GetValue("Protocol") ?? "ssh";
|
||||
switch (protocol.ToLowerInvariant())
|
||||
{
|
||||
case "raw":
|
||||
sessionInfo.Protocol = ProtocolType.RAW;
|
||||
break;
|
||||
case "rlogin":
|
||||
sessionInfo.Protocol = ProtocolType.Rlogin;
|
||||
break;
|
||||
case "serial":
|
||||
return null;
|
||||
case "ssh":
|
||||
object sshVersionObject = sessionFileReader.GetValue("SshProt");
|
||||
if (sshVersionObject != null)
|
||||
{
|
||||
var sshVersion = Convert.ToInt32(sshVersionObject);
|
||||
sessionInfo.Protocol = sshVersion >= 2 ? ProtocolType.SSH2 : ProtocolType.SSH1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sessionInfo.Protocol = ProtocolType.SSH2;
|
||||
}
|
||||
|
||||
break;
|
||||
case "telnet":
|
||||
sessionInfo.Protocol = ProtocolType.Telnet;
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
sessionInfo.Port = Convert.ToInt32(sessionFileReader.GetValue("PortNumber"));
|
||||
|
||||
return sessionInfo;
|
||||
}
|
||||
|
||||
public override void StartWatcher()
|
||||
{
|
||||
if (_eventWatcher != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var sessionsFolderPath = GetSessionsFolderPath();
|
||||
|
||||
if (!Directory.Exists(sessionsFolderPath))
|
||||
{
|
||||
Runtime.MessageCollector.AddMessage(MessageClass.WarningMsg,
|
||||
$"XmingPortablePuttySessions.Watcher.StartWatching() failed: '{sessionsFolderPath}' does not exist.",
|
||||
true);
|
||||
return;
|
||||
}
|
||||
|
||||
_eventWatcher = new FileSystemWatcher(sessionsFolderPath)
|
||||
{
|
||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite
|
||||
};
|
||||
_eventWatcher.Changed += OnFileSystemEventArrived;
|
||||
_eventWatcher.Created += OnFileSystemEventArrived;
|
||||
_eventWatcher.Deleted += OnFileSystemEventArrived;
|
||||
_eventWatcher.Renamed += OnFileSystemEventArrived;
|
||||
_eventWatcher.EnableRaisingEvents = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Runtime.MessageCollector.AddExceptionMessage(
|
||||
"XmingPortablePuttySessions.Watcher.StartWatching() failed.",
|
||||
ex, MessageClass.WarningMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public override void StopWatcher()
|
||||
{
|
||||
PuttySessionsRegistryProvider.StopWatcher();
|
||||
PuttySessionsRegistryProvider.PuttySessionChanged -= OnRegistrySessionChanged;
|
||||
|
||||
if (_eventWatcher == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_eventWatcher.EnableRaisingEvents = false;
|
||||
_eventWatcher.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static string GetPuttyConfPath()
|
||||
{
|
||||
var puttyPath = mRemoteNG.Settings.Default.UseCustomPuttyPath
|
||||
? mRemoteNG.Settings.Default.CustomPuttyPath
|
||||
: App.Info.GeneralAppInfo.PuttyPath;
|
||||
puttyPath = Path.GetDirectoryName(puttyPath);
|
||||
return string.IsNullOrEmpty(puttyPath) ? null : Path.Combine(puttyPath, "putty.conf");
|
||||
}
|
||||
|
||||
private static string GetSessionsFolderPath()
|
||||
{
|
||||
var puttyConfPath = GetPuttyConfPath();
|
||||
var sessionFileReader = new PuttyConfFileReader(puttyConfPath);
|
||||
var basePath = Environment.ExpandEnvironmentVariables(sessionFileReader.GetValue("sshk&sess"));
|
||||
return Path.Combine(basePath, "sessions");
|
||||
}
|
||||
|
||||
private static string GetRegistrySessionName(string sessionName)
|
||||
{
|
||||
var regex = new Regex(RegistrySessionNamePattern);
|
||||
|
||||
var matches = regex.Matches(sessionName);
|
||||
if (matches.Count < 1)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var groups = matches[0].Groups;
|
||||
return groups.Count < 1 ? string.Empty : groups[1].Value;
|
||||
}
|
||||
|
||||
private static PuttySessionInfo ModifyRegistrySessionInfo(PuttySessionInfo sessionInfo)
|
||||
{
|
||||
if (sessionInfo == null)
|
||||
return null;
|
||||
|
||||
sessionInfo.Name = string.Format(RegistrySessionNameFormat, sessionInfo.Name);
|
||||
sessionInfo.PuttySession = string.Format(RegistrySessionNameFormat, sessionInfo.PuttySession);
|
||||
return sessionInfo;
|
||||
}
|
||||
|
||||
private void OnFileSystemEventArrived(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
RaiseSessionChangedEvent(new PuttySessionChangedEventArgs());
|
||||
}
|
||||
|
||||
private void OnRegistrySessionChanged(object sender, PuttySessionChangedEventArgs e)
|
||||
{
|
||||
RaiseSessionChangedEvent(new PuttySessionChangedEventArgs());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Classes
|
||||
|
||||
private class PuttyConfFileReader
|
||||
{
|
||||
public PuttyConfFileReader(string puttyConfFile)
|
||||
{
|
||||
_puttyConfFile = puttyConfFile;
|
||||
}
|
||||
|
||||
private string _puttyConfFile;
|
||||
private bool _configurationLoaded;
|
||||
private Dictionary<string, string> _configuration = new Dictionary<string, string>();
|
||||
|
||||
private void LoadConfiguration()
|
||||
{
|
||||
_configurationLoaded = true;
|
||||
try
|
||||
{
|
||||
if (!File.Exists(_puttyConfFile))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using (var streamReader = new StreamReader(_puttyConfFile))
|
||||
{
|
||||
do
|
||||
{
|
||||
var line = streamReader.ReadLine();
|
||||
if (line == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
line = line.Trim();
|
||||
if (line == string.Empty)
|
||||
{
|
||||
continue; // Blank line
|
||||
}
|
||||
|
||||
if (line.Substring(0, 1) == ";")
|
||||
{
|
||||
continue; // Comment
|
||||
}
|
||||
|
||||
var parts = line.Split(new[] {'='}, 2);
|
||||
if (parts.Length < 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_configuration.ContainsKey(parts[0]))
|
||||
{
|
||||
continue; // As per http://www.straightrunning.com/XmingNotes/portableputty.php only first entry is used
|
||||
}
|
||||
|
||||
_configuration.Add(parts[0], parts[1]);
|
||||
} while (true);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Runtime.MessageCollector.AddExceptionMessage("PuttyConfFileReader.LoadConfiguration() failed.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(string setting)
|
||||
{
|
||||
if (!_configurationLoaded)
|
||||
{
|
||||
LoadConfiguration();
|
||||
}
|
||||
|
||||
return !_configuration.ContainsKey(setting) ? string.Empty : _configuration[setting];
|
||||
}
|
||||
}
|
||||
|
||||
private class SessionFileReader
|
||||
{
|
||||
public SessionFileReader(string sessionFile)
|
||||
{
|
||||
_sessionFile = sessionFile;
|
||||
}
|
||||
|
||||
private string _sessionFile;
|
||||
private bool _sessionInfoLoaded;
|
||||
private Dictionary<string, string> _sessionInfo = new Dictionary<string, string>();
|
||||
|
||||
private void LoadSessionInfo()
|
||||
{
|
||||
_sessionInfoLoaded = true;
|
||||
try
|
||||
{
|
||||
if (!File.Exists(_sessionFile))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using (var streamReader = new StreamReader(_sessionFile))
|
||||
{
|
||||
do
|
||||
{
|
||||
var line = streamReader.ReadLine();
|
||||
if (line == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var parts = line.Split('\\');
|
||||
if (parts.Length < 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_sessionInfo.Add(parts[0], parts[1]);
|
||||
} while (true);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Runtime.MessageCollector.AddExceptionMessage("SessionFileReader.LoadSessionInfo() failed.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(string setting)
|
||||
{
|
||||
if (!_sessionInfoLoaded)
|
||||
{
|
||||
LoadSessionInfo();
|
||||
}
|
||||
|
||||
return !_sessionInfo.ContainsKey(setting) ? string.Empty : _sessionInfo[setting];
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -169,7 +169,6 @@
|
||||
<Compile Include="Config\Import\RemoteDesktopConnectionImporter.cs" />
|
||||
<Compile Include="Config\Import\RemoteDesktopConnectionManagerImporter.cs" />
|
||||
<Compile Include="Config\Putty\PuttySessionsRegistryProvider.cs" />
|
||||
<Compile Include="Config\Putty\PuttySessionsXmingProvider.cs" />
|
||||
<Compile Include="Config\Putty\PuttySessionsManager.cs" />
|
||||
<Compile Include="Config\Putty\AbstractPuttySessionsProvider.cs" />
|
||||
<Compile Include="Config\DatabaseConnectors\IDatabaseConnector.cs" />
|
||||
|
||||
Reference in New Issue
Block a user