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; }
}
}
}