forked from jaysonragasa/MultiRDPClient.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportWindow.cs
More file actions
165 lines (139 loc) · 6.37 KB
/
ImportWindow.cs
File metadata and controls
165 lines (139 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Database.Models;
using RDPFileReader;
using System;
using System.IO;
using System.Windows.Forms;
namespace MultiRemoteDesktopClient
{
public partial class ImportWindow : Form
{
OpenFileDialog ofd = null;
public ImportWindow()
{
InitializeComponent();
InitializeControls();
InitializeControlEvents();
}
public void InitializeControls()
{
}
public void InitializeControlEvents()
{
this.Shown += new EventHandler(ImportWindow_Shown);
this.btnStart.Click += new EventHandler(btnStart_Click);
this.btnBrowse.Click += new EventHandler(btnBrowse_Click);
}
void btnBrowse_Click(object sender, EventArgs e)
{
ofd = new OpenFileDialog();
ofd.Filter = "RDP File|*.rdp";
ofd.Multiselect = true;
ofd.Title = "Import RDP File";
ofd.ShowDialog();
foreach (string thisFile in ofd.FileNames)
{
System.Diagnostics.Debug.WriteLine("reading " + thisFile);
#region Read RDP File
RDPFile rdpfile;
{
try
{
rdpfile = new RDPFile();
rdpfile.Read(thisFile);
}
catch (Exception ex)
{
MessageBox.Show("An error occured while reading '" + Path.GetFileName(thisFile) + "' and it will be skipped.\r\n\r\nError Message: " + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
continue;
}
}
#endregion
Model_ServerDetails sd = new Model_ServerDetails();
sd.UID = DateTime.Now.Ticks.ToString();
sd.GroupID = 1;
sd.ServerName = System.IO.Path.GetFileNameWithoutExtension(thisFile);
sd.Server = rdpfile.FullAddress;
sd.Username = rdpfile.Username;
#region Try decrypting the password from RDP file
{
try
{
System.Diagnostics.Debug.WriteLine("reading password " + thisFile);
string RDPPassword = rdpfile.Password;
if (RDPPassword != string.Empty)
{
// based on http://www.remkoweijnen.nl/blog/2008/03/02/how-rdp-passwords-are-encrypted-2/
// he saids, MSTSC just add a ZERO number at the end of the hashed password.
// so let's just removed THAT!
RDPPassword = RDPPassword.Substring(0, RDPPassword.Length - 1);
// and decrypt it!
RDPPassword = DataProtection.DataProtectionForRDPWrapper.Decrypt(RDPPassword);
sd.Password = RDPPassword;
}
System.Diagnostics.Debug.WriteLine("reading password done");
}
catch (Exception Ex)
{
sd.Password = string.Empty;
if (Ex.Message == "Problem converting Hex to Bytes")
{
MessageBox.Show("This RDP File '" + Path.GetFileNameWithoutExtension(thisFile) + "' contains a secured password which is currently unsported by this application.\r\nThe importing can still continue but without the password.\r\nYou can edit the password later by selecting a server in 'All Listed Servers' and click 'Edit Settings' button on the toolbar", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else if (Ex.Message.Contains("Exception decrypting"))
{
MessageBox.Show("Failed to decrypt the password from '" + Path.GetFileNameWithoutExtension(thisFile) + "'", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("An unknown error occured while decrypting the password from '" + Path.GetFileNameWithoutExtension(thisFile) + "'", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
#endregion
sd.Description = "Imported from " + thisFile;
sd.ColorDepth = (int)rdpfile.SessionBPP;
sd.DesktopWidth = rdpfile.DesktopWidth;
sd.DesktopHeight = rdpfile.DesktopHeight;
sd.Fullscreen = false;
ListViewItem thisItem = new ListViewItem(Path.GetFileNameWithoutExtension(thisFile));
thisItem.SubItems.Add("OK");
thisItem.SubItems.Add(thisFile);
thisItem.Tag = sd;
thisItem.ImageIndex = 0;
lvRDPFiles.Items.Add(thisItem);
}
foreach (ColumnHeader ch in lvRDPFiles.Columns)
{
ch.Width = -1;
}
}
void btnStart_Click(object sender, EventArgs e)
{
foreach (ListViewItem thisItem in lvRDPFiles.Items)
{
thisItem.SubItems[1].Text = "Importing...";
Model_ServerDetails sd = (Model_ServerDetails)thisItem.Tag;
try
{
GlobalHelper.dbServers.Save(true, sd);
}
catch (Database.DatabaseException settingEx)
{
if (settingEx.ExceptionType == Database.DatabaseException.ExceptionTypes.DUPLICATE_ENTRY)
{
MessageBox.Show("Can't save '" + sd.ServerName + "' due to duplicate entry", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
thisItem.SubItems[1].Text = "Done!";
}
foreach (ColumnHeader ch in lvRDPFiles.Columns)
{
ch.Width = -1;
}
}
void ImportWindow_Shown(object sender, EventArgs e)
{
}
}
}