First commit of SecureCRT import functionality.

This commit is contained in:
23439176+magriggs@users.noreply.github.com
2024-01-04 21:37:43 +08:00
parent 683f84f053
commit e1cac723d6
10 changed files with 4498 additions and 0 deletions

View File

@@ -33,6 +33,7 @@ namespace mRemoteNG.App
fileTypes.AddRange(new[] {Language.FilterRdgFiles, "*.rdg"}); fileTypes.AddRange(new[] {Language.FilterRdgFiles, "*.rdg"});
fileTypes.AddRange(new[] {Language.FilterPuttyConnectionManager, "*.dat"}); fileTypes.AddRange(new[] {Language.FilterPuttyConnectionManager, "*.dat"});
fileTypes.AddRange(new[] {Language.FilterAll, "*.*"}); fileTypes.AddRange(new[] {Language.FilterAll, "*.*"});
fileTypes.AddRange(new[] { Language.FilterSecureCRT, "*.crt" });
openFileDialog.Filter = string.Join("|", fileTypes.ToArray()); openFileDialog.Filter = string.Join("|", fileTypes.ToArray());
@@ -174,6 +175,8 @@ namespace mRemoteNG.App
return new RemoteDesktopConnectionManagerImporter(); return new RemoteDesktopConnectionManagerImporter();
case ".dat": case ".dat":
return new PuttyConnectionManagerImporter(); return new PuttyConnectionManagerImporter();
case ".crt":
return new SecureCRTImporter();
default: default:
throw new FileFormatException("Unrecognized file format."); throw new FileFormatException("Unrecognized file format.");
} }

View File

@@ -15,6 +15,19 @@ namespace mRemoteNG.App
private static Mutex _mutex; private static Mutex _mutex;
private static FrmSplashScreenNew _frmSplashScreen = null; private static FrmSplashScreenNew _frmSplashScreen = null;
public class LocalSettingsManager
{
internal bool DatabaseExists()
{
return true;
}
internal void CreateDatabase()
{
throw new NotImplementedException();
}
}
/// <summary> /// <summary>
/// The main entry point for the application. /// The main entry point for the application.
/// </summary> /// </summary>

View File

@@ -0,0 +1,46 @@
using mRemoteNG.App;
using mRemoteNG.Config.DataProviders;
using mRemoteNG.Config.Serializers;
using mRemoteNG.Config.Serializers.MiscSerializers;
using mRemoteNG.Container;
using mRemoteNG.Messages;
using mRemoteNG.Tree;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;
namespace mRemoteNG.Config.Import
{
[SupportedOSPlatform("windows")]
public class SecureCRTImporter : IConnectionImporter<string>
{
public void Import(string fileName, ContainerInfo destinationContainer)
{
if (fileName == null)
{
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, "Unable to import file. File path is null.");
return;
}
if (!File.Exists(fileName))
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg,
$"Unable to import file. File does not exist. Path: {fileName}");
var dataProvider = new FileDataProvider(fileName);
var content = dataProvider.Load();
var deserializer = new SecureCRTFileDeserializer();
var connectionTreeModel = deserializer.Deserialize(content);
var rootImportContainer = new ContainerInfo { Name = Path.GetFileNameWithoutExtension(fileName) };
rootImportContainer.AddChildRange(connectionTreeModel.RootNodes.First().Children.ToArray());
destinationContainer.AddChild(rootImportContainer);
}
}
}

View File

@@ -0,0 +1,170 @@
using mRemoteNG.App;
using mRemoteNG.Connection;
using mRemoteNG.Connection.Protocol;
using mRemoteNG.Container;
using mRemoteNG.Tree;
using mRemoteNG.Tree.Root;
using System;
using System.IO;
using System.Runtime.Versioning;
using System.Xml;
namespace mRemoteNG.Config.Serializers.MiscSerializers
{
[SupportedOSPlatform("windows")]
public class SecureCRTFileDeserializer
{
enum SecureCRTNodeType { folder, session };
public ConnectionTreeModel Deserialize(string content)
{
var connectionTreeModel = new ConnectionTreeModel();
var root = new RootNodeInfo(RootNodeType.Connection);
connectionTreeModel.AddRootNode(root);
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(content);
var sessionsNode = xmlDocument.SelectSingleNode("/VanDyke/key[@name=\"Sessions\"]");
ImportRootOrContainer(sessionsNode, root);
return connectionTreeModel;
}
private void ImportRootOrContainer(XmlNode rootNode, ContainerInfo parentContainer)
{
var newContainer = ImportContainer(rootNode, parentContainer);
if (rootNode.ChildNodes.Count == 0)
return;
foreach (XmlNode child in rootNode.ChildNodes)
{
var name = child.Attributes["name"].Value;
if (name == "Default" || name == "Default_LocalShell")
continue;
var nodeType = GetFolderOrSession(child);
switch (nodeType)
{
case SecureCRTNodeType.folder:
ImportRootOrContainer(child, newContainer);
break;
case SecureCRTNodeType.session:
ImportConnection(child, newContainer);
break;
}
}
}
private void ImportConnection(XmlNode childNode, ContainerInfo parentContainer)
{
var connectionInfo = ConnectionInfoFromXml(childNode);
if (connectionInfo == null)
return;
parentContainer.AddChild(connectionInfo);
}
private ContainerInfo ImportContainer(XmlNode containerNode, ContainerInfo parentContainer)
{
var containerInfo = new ContainerInfo
{
Name = containerNode.Attributes["name"].InnerText
};
parentContainer.AddChild(containerInfo);
return containerInfo;
}
private SecureCRTNodeType GetFolderOrSession(XmlNode xmlNode)
{
if (GetHostnameFromNode(xmlNode) == null)
return SecureCRTNodeType.folder;
return SecureCRTNodeType.session;
}
private ConnectionInfo ConnectionInfoFromXml(XmlNode xmlNode)
{
var connectionInfo = new ConnectionInfo();
try
{
connectionInfo.Name = xmlNode.Attributes["name"].InnerText;
connectionInfo.Hostname = GetHostnameFromNode(xmlNode);
connectionInfo.Protocol = GetProtocolFromNode(xmlNode);
connectionInfo.Port = GetPortFromNode(xmlNode, connectionInfo.Protocol);
connectionInfo.Username = GetUsernameFromNode(xmlNode);
connectionInfo.Description = GetDescriptionFromNode(xmlNode);
}
catch (FileFormatException e)
{
Runtime.MessageCollector.AddExceptionMessage("Error when parsing SecureCRT node: ", e);
return null;
}
return connectionInfo;
}
private string GetHostnameFromNode(XmlNode xmlNode)
{
return xmlNode.SelectSingleNode("string[@name=\"Hostname\"]")?.InnerText;
}
private string GetUsernameFromNode(XmlNode xmlNode)
{
return xmlNode.SelectSingleNode("string[@name=\"Username\"]")?.InnerText;
}
private int GetPortFromNode(XmlNode xmlNode, ProtocolType protocol)
{
switch (protocol)
{
case ProtocolType.SSH1:
return Convert.ToInt32(xmlNode.SelectSingleNode("dword[@name=\"[SSH1] Port\"]").InnerText);
case ProtocolType.SSH2:
return Convert.ToInt32(xmlNode.SelectSingleNode("dword[@name=\"[SSH2] Port\"]").InnerText);
default:
return Convert.ToInt32(xmlNode.SelectSingleNode("dword[@name=\"Port\"]")?.InnerText);
}
}
private ProtocolType GetProtocolFromNode(XmlNode xmlNode)
{
var protocolNode = xmlNode.SelectSingleNode("string[@name=\"Protocol Name\"]");
if (protocolNode == null)
throw new FileFormatException($"Protocol node not found");
var protocolText = protocolNode.InnerText.ToUpper();
switch (protocolText)
{
case "RDP":
return ProtocolType.RDP;
case "RAW":
return ProtocolType.RAW;
case "RLOGIN":
return ProtocolType.Rlogin;
case "SSH1":
return ProtocolType.SSH1;
case "SSH2":
return ProtocolType.SSH2;
case "TELNET":
return ProtocolType.Telnet;
default:
throw new FileFormatException($"Unrecognized protocol ({protocolText}).");
}
}
private string GetDescriptionFromNode(XmlNode xmlNode)
{
var description = string.Empty;
var descNode = xmlNode.SelectSingleNode("array[@name=\"Description\"]");
foreach(XmlNode n in descNode.ChildNodes)
{
description += n.InnerText + " ";
}
return description.TrimEnd();
}
}
}

View File

@@ -2360,6 +2360,15 @@ namespace mRemoteNG.Resources.Language {
} }
} }
/// <summary>
/// Looks up a localized string similar to SecureCRT (*.xml).
/// </summary>
internal static string FilterSecureCRT {
get {
return ResourceManager.GetString("FilterSecureCRT", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to First IP. /// Looks up a localized string similar to First IP.
/// </summary> /// </summary>

View File

@@ -2359,4 +2359,7 @@ Nightly Channel includes Alphas, Betas &amp; Release Candidates.</value>
<value>Maximum login attempts exceeded. Please connect again.</value> <value>Maximum login attempts exceeded. Please connect again.</value>
<comment>C# to Powershell transfer issue with encoding possible</comment> <comment>C# to Powershell transfer issue with encoding possible</comment>
</data> </data>
<data name="FilterSecureCRT" xml:space="preserve">
<value>SecureCRT (*.xml)</value>
</data>
</root> </root>

View File

@@ -0,0 +1,167 @@
using System.Collections.Generic;
using System.Linq;
using mRemoteNG.Config.Serializers.MiscSerializers;
using mRemoteNG.Connection;
using mRemoteNG.Connection.Protocol;
using mRemoteNG.Container;
using NUnit.Framework;
using mRemoteNGTests.Properties;
using mRemoteNG.Tree;
using System.Runtime.Versioning;
namespace mRemoteNGTests.Config.Serializers.MiscSerializers;
[SupportedOSPlatform("windows")]
public class SecureCRTFileDeserializerTests
{
private SecureCRTFileDeserializer _deserializer;
private ConnectionTreeModel _connectionTreeModel;
[OneTimeSetUp]
public void OnetimeSetup()
{
var fileContents = Resources.test_securecrt;
_deserializer = new SecureCRTFileDeserializer();
_connectionTreeModel = _deserializer.Deserialize(fileContents);
}
[Test]
public void HaveContainerNamedAllConnectionTypes()
{
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
var allConnectionTypesNode = GetContainerNamed("AllConnectionTypes", sessionsNode.Children);
Assert.That(allConnectionTypesNode, Is.Not.Null);
Assert.That(allConnectionTypesNode.IsContainer, Is.True);
Assert.That(allConnectionTypesNode.Name, Is.EqualTo("AllConnectionTypes"));
}
[Test]
public void TestRawConnectionInfo()
{
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
var allConnectionTypesNode = GetContainerNamed("AllConnectionTypes", sessionsNode.Children);
var rawNode = GetConnectionNamed("rawsession", allConnectionTypesNode.Children);
Assert.That(rawNode.Name, Is.EqualTo("rawsession"));
Assert.That(rawNode.Hostname, Is.EqualTo("rawhost"));
Assert.That(rawNode.Protocol, Is.EqualTo(ProtocolType.RAW));
Assert.That(rawNode.Port, Is.EqualTo(23));
Assert.That(rawNode.Username, Is.EqualTo(""));
}
[Test]
public void TestRDPConnectionInfo()
{
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
var allConnectionTypesNode = GetContainerNamed("AllConnectionTypes", sessionsNode.Children);
var rdpNode = GetConnectionNamed("RDPsession", allConnectionTypesNode.Children);
Assert.That(rdpNode.Name, Is.EqualTo("RDPsession"));
Assert.That(rdpNode.Hostname, Is.EqualTo("RDPhost"));
Assert.That(rdpNode.Protocol, Is.EqualTo(ProtocolType.RDP));
Assert.That(rdpNode.Port, Is.EqualTo(3389));
Assert.That(rdpNode.Username, Is.EqualTo("RDP\\rdp"));
}
[Test]
public void TestRloginConnection() {
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
var allConnectionTypesNode = GetContainerNamed("AllConnectionTypes", sessionsNode.Children);
var rloginNode = GetConnectionNamed("rloginsession", allConnectionTypesNode.Children);
Assert.That(rloginNode.Name, Is.EqualTo("rloginsession"));
Assert.That(rloginNode.Hostname, Is.EqualTo("rloginhost"));
Assert.That(rloginNode.Protocol, Is.EqualTo(ProtocolType.Rlogin));
Assert.That(rloginNode.Port, Is.EqualTo(0));
Assert.That(rloginNode.Username, Is.EqualTo("rloginuser"));
}
[Test]
public void TestSSH1Connection()
{
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
var allConnectionTypesNode = GetContainerNamed("AllConnectionTypes", sessionsNode.Children);
var ssh1Node = GetConnectionNamed("ssh1session", allConnectionTypesNode.Children);
Assert.That(ssh1Node.Name, Is.EqualTo("ssh1session"));
Assert.That(ssh1Node.Hostname, Is.EqualTo("ssh1host"));
Assert.That(ssh1Node.Protocol, Is.EqualTo(ProtocolType.SSH1));
Assert.That(ssh1Node.Port, Is.EqualTo(22));
Assert.That(ssh1Node.Username, Is.EqualTo("ssh1user"));
}
[Test]
public void TestSSH2Connection()
{
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
var allConnectionTypesNode = GetContainerNamed("AllConnectionTypes", sessionsNode.Children);
var ssh1Node = GetConnectionNamed("ssh2session", allConnectionTypesNode.Children);
Assert.That(ssh1Node.Name, Is.EqualTo("ssh2session"));
Assert.That(ssh1Node.Hostname, Is.EqualTo("ssh2host"));
Assert.That(ssh1Node.Protocol, Is.EqualTo(ProtocolType.SSH2));
Assert.That(ssh1Node.Port, Is.EqualTo(22));
Assert.That(ssh1Node.Username, Is.EqualTo("ssh2user"));
}
[Test]
public void TestTelnetConnection()
{
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
var allConnectionTypesNode = GetContainerNamed("AllConnectionTypes", sessionsNode.Children);
var ssh1Node = GetConnectionNamed("telnetsession", allConnectionTypesNode.Children);
Assert.That(ssh1Node.Name, Is.EqualTo("telnetsession"));
Assert.That(ssh1Node.Hostname, Is.EqualTo("telnethost"));
Assert.That(ssh1Node.Protocol, Is.EqualTo(ProtocolType.Telnet));
Assert.That(ssh1Node.Port, Is.EqualTo(23));
Assert.That(ssh1Node.Username, Is.EqualTo("telnetuser"));
}
[Test]
public void TestDescriptionField()
{
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
var host1Node = GetConnectionNamed("host1.org", sessionsNode.Children);
Assert.That(host1Node.Description, Is.EqualTo("First Second 123456"));
}
[Test]
public void TestValidateFileStructure()
{
var rootNode = GetContainerNamed("Connections", _connectionTreeModel.RootNodes);
var sessionsNode = GetContainerNamed("Sessions", rootNode.Children);
Assert.That(sessionsNode.Children.Count, Is.EqualTo(3));
var allConnectionTypesNode = GetContainerNamed("AllConnectionTypes", sessionsNode.Children);
Assert.That(allConnectionTypesNode.Children.Count, Is.EqualTo(6));
var serverSubfolderNode = GetContainerNamed("server_subfolder", sessionsNode.Children);
Assert.That(serverSubfolderNode, Is.Not.Null);
Assert.That(serverSubfolderNode.Children.Count, Is.EqualTo(2));
var serverSubSubfolderNode = GetContainerNamed("server_subsubfolder", serverSubfolderNode.Children);
Assert.That(serverSubSubfolderNode, Is.Not.Null);
Assert.That(serverSubSubfolderNode.Children.Count, Is.EqualTo(2));
}
private ContainerInfo GetContainerNamed(string name, IEnumerable<ConnectionInfo> list)
{
return list.First(node => node is ContainerInfo && node.Name == name) as ContainerInfo;
}
private ConnectionInfo GetConnectionNamed(string name, IEnumerable<ConnectionInfo> list)
{
return list.First(node => node is ConnectionInfo && node.Name == name);
}
private bool ContainsNodeNamed(string name, IEnumerable<ConnectionInfo> list)
{
return list.Any(node => node.Name == name);
}
}

View File

@@ -381,6 +381,23 @@ namespace mRemoteNGTests.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
///&lt;VanDyke version=&quot;3.0&quot;&gt;
/// &lt;key name=&quot;Security&quot;&gt;
/// &lt;string name=&quot;Passphrase&quot;&gt;03:637dca491ee5dc1979744cecc6d4b0d662285574c56fc6b2aa97afaa2b9a54291e6946f62b0dcb21be8ca375ed236e42694ef1eedebf2aeccf10d40013e6e81d&lt;/string&gt;
/// &lt;/key&gt;
/// &lt;key name=&quot;Global&quot;&gt;
/// &lt;string name=&quot;Active Sessions Manager Window State V2&quot;&gt;2,0,0,-209,117,41,411,0,0,0,0,0&lt;/string&gt;
/// &lt;dword name=&quot;Active Window Alpha Transparency&quot;&gt;255&lt;/dword&gt;
/// &lt;binary name=&quot;Add Keyword Dialog Size&quot;&gt;2c 00 00 00 00 00 00 00 01 0 [rest of string was truncated]&quot;;.
/// </summary>
internal static string test_securecrt {
get {
return ResourceManager.GetString("test_securecrt", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap. /// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary> /// </summary>

View File

@@ -184,6 +184,9 @@
<data name="test_remotedesktopconnection_rdp" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="test_remotedesktopconnection_rdp" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\test_remotedesktopconnection.rdp;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-16</value> <value>..\Resources\test_remotedesktopconnection.rdp;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-16</value>
</data> </data>
<data name="test_securecrt" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\test_securecrt.xml;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-16</value>
</data>
<data name="update" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="update" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\update.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value> <value>..\Resources\update.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data> </data>

File diff suppressed because one or more lines are too long