[go: up one dir, main page]

Menu

[r1]: / Program.cs  Maximize  Restore  History

Download this file

353 lines (312 with data), 14.0 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
namespace RDPManager
{
static class Program
{
private static NotifyIcon _icon = new NotifyIcon();
private static Dictionary<String, Credentials> Users = null; // = new Dictionary<String, Credentials>();
private const String passFileName = "passwords.bin";
private static List<String> configFiles = new List<String>();
private static String version = "1.05";
static string screenMode = null;
static string desktopWidth = null;
static string desktopHeight = null;
static string compression = null;
static string keyboardHook = null;
static string audioMode = null;
static string redirectDrives = null;
static string redirectPrinters = null;
static string redirectComPorts = null;
static string redirectSmartCards = null;
static string disableWallpaper = null;
static string disableWindowDrag = null;
static string disableAnims = null;
static string disableThemes = null;
static string disableCursor = null;
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//load the icon
using (Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("RDPManager.RDPManager.ico"))
{
_icon.Icon = new Icon(s);
}
_icon.ContextMenu = new ContextMenu();
if (args.Length > 0)
{
for (int i = 0; i < args.Length; i++)
{
configFiles.Add(args[i]);
}
}
else
{
configFiles.Add("RDPManager.xml");
}
foreach( String File in configFiles)
{
if (!CheckPath(File))
{
MessageBox.Show("Config file " + File + " not found, exiting...", "Config File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
_icon.Visible = false;
_icon.Dispose();
Environment.Exit(0);
}
LoadFile(File);
}
AddDefaultMenuItems();
_icon.Visible = true;
LoadPasswords();
Application.Run();
}
private static void AddDefaultMenuItems()
{
_icon.ContextMenu.MenuItems.Add(new MenuItem("-"));
MenuItem item = new MenuItem("Manual");
item.Click += new EventHandler(manualItem_Click);
_icon.ContextMenu.MenuItems.Add(item);
item = new MenuItem("Reload XML");
item.Click += new EventHandler(reloadItem_Click);
_icon.ContextMenu.MenuItems.Add(item);
item = new MenuItem("Manage Passwords");
item.Click += new EventHandler(managePwordItem_Click);
_icon.ContextMenu.MenuItems.Add(item);
_icon.ContextMenu.MenuItems.Add(new MenuItem("-"));
item = new MenuItem("About");
item.Click += new EventHandler(aboutItem_Click);
_icon.ContextMenu.MenuItems.Add(item);
item = new MenuItem("Exit");
item.Click += new EventHandler(exitItem_Click);
_icon.ContextMenu.MenuItems.Add(item);
}
static void exitItem_Click(object sender, EventArgs e)
{
_icon.Visible = false;
_icon.Dispose();
Environment.Exit(0);
}
static void reloadItem_Click(object sender, EventArgs e)
{
_icon.ContextMenu.MenuItems.Clear();
foreach (String File in configFiles)
{
if (!CheckPath(File))
{
MessageBox.Show("Config file " + File + " not found, exiting...", "Config File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
_icon.Visible = false;
_icon.Dispose();
Environment.Exit(0);
}
LoadFile(File);
}
AddDefaultMenuItems();
}
static void aboutItem_Click(object sender, EventArgs e)
{
About aboutFrm = new About();
aboutFrm.Version = version;
aboutFrm.ShowDialog();
}
static void manualItem_Click(object sender, EventArgs e)
{
dlgInputBox InputBox = new dlgInputBox();
InputBox.Text = "Machine Name";
InputBox.Label = "Machine Name or IP Address";
InputBox.ShowDialog();
if (InputBox.DialogResult == DialogResult.OK)
{
Connect(InputBox.InputTxt);
}
}
static void LoadFile(string FileName)
{
XmlDocument doc = new XmlDocument();
using (Stream fileIn = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
doc.Load(fileIn);
}
screenMode = doc.SelectSingleNode("RDPConnections/@ScreenMode").InnerText;
desktopWidth = doc.SelectSingleNode("RDPConnections/@DesktopWidth").InnerText;
desktopHeight = doc.SelectSingleNode("RDPConnections/@DesktopHeight").InnerText;
compression = doc.SelectSingleNode("RDPConnections/@Compression").InnerText;
keyboardHook = doc.SelectSingleNode("RDPConnections/@KeyboardHook").InnerText;
audioMode = doc.SelectSingleNode("RDPConnections/@AudioMode").InnerText;
redirectDrives = doc.SelectSingleNode("RDPConnections/@RedirectDrives").InnerText;
redirectPrinters = doc.SelectSingleNode("RDPConnections/@RedirectPrinters").InnerText;
redirectComPorts = doc.SelectSingleNode("RDPConnections/@RedirectComPorts").InnerText;
redirectSmartCards = doc.SelectSingleNode("RDPConnections/@RedirectSmartCards").InnerText;
disableWallpaper = doc.SelectSingleNode("RDPConnections/@DisableWallpaper").InnerText;
disableWindowDrag = doc.SelectSingleNode("RDPConnections/@DisableWindowDrag").InnerText;
disableAnims = doc.SelectSingleNode("RDPConnections/@DisableAnims").InnerText;
disableThemes = doc.SelectSingleNode("RDPConnections/@DisableThemes").InnerText;
disableCursor = doc.SelectSingleNode("RDPConnections/@DisableCursor").InnerText;
ProcessElement(doc.DocumentElement, _icon.ContextMenu.MenuItems);
}
private static void ProcessElement(XmlElement xmlElement, Menu.MenuItemCollection menuItems)
{
foreach (XmlElement subElement in xmlElement) {
MenuItem m = new MenuItem(subElement.GetAttribute("DisplayName"));
menuItems.Add(m);
if (subElement.Name == "RDPConnection")
{
m.Tag = subElement.GetAttribute("MachineName");
m.Click += new EventHandler(m_Click);
}
else
{
//is a folder node
ProcessElement(subElement, m.MenuItems);
}
}
}
static void m_Click(object sender, EventArgs e)
{
Connect((sender as MenuItem).Tag as string);
}
static void managePwordItem_Click(object sender, EventArgs e)
{
PassManage passManageFrm = new PassManage();
passManageFrm.Users = Users;
passManageFrm.ShowDialog();
Users = passManageFrm.Users;
SavePasswords();
}
private static void Connect(string machineName)
{
//we create and use an RDP file
String fileName = "";
if (machineName.Contains(":"))
{
fileName = machineName.Substring(0,machineName.IndexOf(':')) + ".rdp";
}
else
{
fileName = machineName + ".rdp";
}
using (FileStream fileStream = new FileStream(fileName , FileMode.Create, FileAccess.Write, FileShare.None))
{
using (StreamWriter sw = new StreamWriter(fileStream))
{
sw.WriteLine("screen mode id:i:{0}", screenMode);
sw.WriteLine("desktopwidth:i:{0}", desktopWidth);
sw.WriteLine("desktopheight:i:{0}", desktopHeight);
sw.WriteLine("session bpp:i:16");
sw.WriteLine("full address:s:{0}", machineName);
sw.WriteLine("compression:i:{0}", compression);
sw.WriteLine("keyboardhook:i:{0}", keyboardHook);
sw.WriteLine("audiomode:i:{0}", audioMode);
sw.WriteLine("drivestoredirect:s:{0}", redirectDrives);
sw.WriteLine("redirectprinters:i:{0}", redirectPrinters);
sw.WriteLine("redirectcomports:i:{0}", redirectComPorts);
sw.WriteLine("redirectsmartcards:i:{0}", redirectSmartCards);
sw.WriteLine("displayconnectionbar:i:1");
sw.WriteLine("autoreconnection enabled:i:1");
sw.WriteLine("authentication level:i:0");
if( Users.ContainsKey(machineName.ToUpper()) )
{
sw.WriteLine("username:s:{0}", Users[machineName.ToUpper()].UserName);
sw.WriteLine("password 51:b:{0}",Users[machineName.ToUpper()].Password);
if( Users[machineName.ToUpper()].Domain != "" )
sw.WriteLine("domain:s:{0}", Users[machineName.ToUpper()].Domain);
} else {
sw.WriteLine("username:s:{0}", Users["Default"].UserName);
sw.WriteLine("password 51:b:{0}", Users["Default"].Password);
if (Users["Default"].Domain != "")
sw.WriteLine("domain:s:{0}", Users["Default"].Domain);
}
sw.WriteLine("disable wallpaper:i:{0}", disableWallpaper);
sw.WriteLine("disable full window drag:i:{0}", disableWindowDrag);
sw.WriteLine("disable menu anims:i:{0}", disableAnims);
sw.WriteLine("disable themes:i:{0}", disableThemes);
sw.WriteLine("disable cursor setting:i:{0}", disableCursor);
sw.WriteLine("bitmapcachepersistenable:i:1");
sw.WriteLine("auto connect:i:1");
sw.Flush();
}
}
Process.Start(fileName);
}
public static string GetEncodedPassword(String passWord)
{
byte[] encoded = DPAPI.Encrypt(DPAPI.KeyType.UserKey, Encoding.Unicode.GetBytes(passWord), null, null);
string temp = BitConverter.ToString(encoded).Replace("-", "");
return temp;
}
private static bool CheckPath(string pathToCheck)
{
FileInfo fInfo = new FileInfo(pathToCheck);
if (!fInfo.Exists)
{
return false;
}
else
{
return true;
}
}
private static void LoadPasswords()
{
FileInfo fInfo = new FileInfo(passFileName);
if (fInfo.Exists)
{
BinaryFormatter binFormat = new BinaryFormatter();
FileStream fStream = File.OpenRead(passFileName);
Users = (Dictionary<String, Credentials>)binFormat.Deserialize(fStream);
fStream.Close();
}
else
{
Credentials cred = new Credentials();
cred.UserName = "Administrator";
Users = new Dictionary<String, Credentials>();
Users["Default"] = cred;
SavePasswords();
MessageBox.Show("New Password file created, remember to set your default password");
}
}
private static void SavePasswords()
{
FileInfo fInfo = new FileInfo(passFileName);
if (fInfo.Exists)
{
fInfo.Delete();
}
BinaryFormatter binFormat = new BinaryFormatter();
Stream fStream = new FileStream(passFileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
binFormat.Serialize(fStream, Users);
fStream.Close();
}
}
[Serializable]
public class Credentials
{
private String userName = "";
public String UserName
{
get { return userName; }
set { userName = value; }
}
private String passWord = "";
public String Password
{
get { return passWord; }
set { passWord = value; }
}
private String domain = "";
public String Domain
{
get { return domain; }
set { domain = value; }
}
}
}