public void Connect(string type)
{
Debug.WriteLine("Connect : type - " + type + " " + (type != "-1" ? types[Convert.ToInt16(type)] : ""));
// browsing files with OpenFileDialog() fucks with CurrentDirectory, lets fix it
Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (lbList.SelectedItems == null) return;
if (lbList.SelectedItems.Count > 0)
{
if (lbList.SelectedItems.Count > 5)
{
if (MessageBox.Show(this, "Are you sure you want to connect to the " + lbList.SelectedItems.Count + " selected items ?", "Connection confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) return;
}
foreach (object item in lbList.SelectedItems)
{
ArrayList server = XmlGetServer(item.ToString());
string winscpprot = "sftp://";
string _host = Decrypt(server[1].ToString());
string _user = Decrypt(server[2].ToString());
string _pass = Decrypt(server[3].ToString());
string _type = type == "-1" ? server[4].ToString() : type;
string[] f = { "\\", "/", ":", "*", "?", "\"", "<", ">", "|" };
string[] ps = { "/", "\\\\" };
string[] pr = { "\\", "\\" };
switch (_type)
{
case "1": //RDP
string[] rdpextractpath = ExtractFilePath(Settings.Default.rdpath);
string rdpath = rdpextractpath[0];
string rdpargs = rdpextractpath[1];
if (File.Exists(rdpath))
{
Mstscpw mstscpw = new Mstscpw();
string rdppass = mstscpw.encryptpw(_pass);
ArrayList arraylist = new ArrayList();
string[] size = Settings.Default.rdsize.Split('x');
string rdpout = "";
if (Settings.Default.rdfilespath != "" && ReplaceA(ps, pr, Settings.Default.rdfilespath) != "\\")
{
rdpout = ReplaceA(ps, pr, Settings.Default.rdfilespath + "\\");
try
{
Directory.CreateDirectory(rdpout);
}
catch
{
MessageBox.Show(this, "Output path for generated \".rdp\" connection files doesn't exist.\nFiles will be generated in the current path.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
rdpout = "";
}
}
foreach (string width in size)
{
int num;
if (Int32.TryParse(width.Trim(), out num)) arraylist.Add(width.Trim());
}
TextWriter rdpfile = new StreamWriter(rdpout + ReplaceU(f, server[0].ToString()) + ".rdp");
if (Settings.Default.rdsize == "Full screen") rdpfile.WriteLine("screen mode id:i:2");
else rdpfile.WriteLine("screen mode id:i:1");
if (arraylist.Count == 2)
{
rdpfile.WriteLine("desktopwidth:i:" + arraylist[0]);
rdpfile.WriteLine("desktopheight:i:" + arraylist[1]);
}
if (_host != "") rdpfile.WriteLine("full address:s:" + _host);
if (_user != "")
{
rdpfile.WriteLine("username:s:" + _user);
if (_pass != "") rdpfile.WriteLine("password 51:b:" + rdppass);
}
if (Settings.Default.rddrives) rdpfile.WriteLine("redirectdrives:i:1");
if (Settings.Default.rdadmin) rdpfile.WriteLine("administrative session:i:1");
if (Settings.Default.rdspan) rdpfile.WriteLine("use multimon:i:1");
rdpfile.Close();
Process myProc = new Process();
myProc.StartInfo.FileName = rdpath;
myProc.StartInfo.Arguments = "\"" + rdpout + ReplaceU(f, server[0].ToString()) + ".rdp\"";
if (rdpargs != "") myProc.StartInfo.Arguments += " " + rdpargs;
//MessageBox.Show(myProc.StartInfo.FileName + myProc.StartInfo.FileName.IndexOf('"').ToString() + File.Exists(myProc.StartInfo.FileName).ToString());
try
{
myProc.Start();
}
catch (System.ComponentModel.Win32Exception)
{
//user canceled
}
}
else
{
if (MessageBox.Show(this, "Could not find file \"" + rdpath + "\".\nDo you want to change the configuration ?", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK) optionsform.bRDPath_Click(type);
}
break;
case "2": //VNC
string[] vncextractpath = ExtractFilePath(Settings.Default.vncpath);
string vncpath = vncextractpath[0];
string vncargs = vncextractpath[1];
if (File.Exists(vncpath))
{
string host;
string port;
string[] hostport = _host.Split(':');
int split = hostport.Length;
if (split == 2)
{
host = hostport[0];
port = hostport[1];
}
else
{
host = _host;
port = "5900";
}
string vncout = "";
if (Settings.Default.vncfilespath != "" && ReplaceA(ps, pr, Settings.Default.vncfilespath) != "\\")
{
vncout = ReplaceA(ps, pr, Settings.Default.vncfilespath + "\\");
try
{
Directory.CreateDirectory(vncout);
}
catch
{
MessageBox.Show(this, "Output path for generated \".vnc\" connection files doesn't exist.\nFiles will be generated in the current path.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
vncout = "";
}
}
TextWriter vncfile = new StreamWriter(vncout + ReplaceU(f, server[0].ToString()) + ".vnc");
vncfile.WriteLine("[Connection]");
if (host != "") vncfile.WriteLine("host=" + host.Trim());
if (port != "") vncfile.WriteLine("port=" + port.Trim());
if (_user != "") vncfile.WriteLine("username=" + _user);
if (_pass != "") vncfile.WriteLine("password=" + cryptVNC.EncryptPassword(_pass));
vncfile.WriteLine("[Options]");
if (Settings.Default.vncfullscreen) vncfile.WriteLine("fullscreen=1");
if (Settings.Default.vncviewonly)
{
vncfile.WriteLine("viewonly=1"); //ultravnc
vncfile.WriteLine("sendptrevents=0"); //realvnc
vncfile.WriteLine("sendkeyevents=0"); //realvnc
vncfile.WriteLine("sendcuttext=0"); //realvnc
vncfile.WriteLine("acceptcuttext=0"); //realvnc
vncfile.WriteLine("sharefiles=0"); //realvnc
}
if (_pass != "" && _pass.Length > 8) vncfile.WriteLine("protocol3.3=1"); // fuckin vnc 4.0 auth
vncfile.Close();
Process myProc = new Process();
myProc.StartInfo.FileName = Settings.Default.vncpath;
myProc.StartInfo.Arguments = "-config \"" + vncout + ReplaceU(f, server[0].ToString()) + ".vnc\"";
if (vncargs != "") myProc.StartInfo.Arguments += " " + vncargs;
try
{
myProc.Start();
}
catch (System.ComponentModel.Win32Exception)
{
//user canceled
}
}
else
{
if (MessageBox.Show(this, "Could not find file \"" + vncpath + "\".\nDo you want to change the configuration ?", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK) optionsform.bVNCPath_Click(type);
}
break;
case "3": //WinSCP (SCP)
winscpprot = "scp://";
goto case "4";
case "4": //WinSCP (SFTP)
string[] winscpextractpath = ExtractFilePath(Settings.Default.winscppath);
string winscppath = winscpextractpath[0];
string winscpargs = winscpextractpath[1];
if (File.Exists(winscppath))
{
string host;
string port;
string[] hostport = _host.Split(':');
int split = hostport.Length;
if (split == 2)
{
host = hostport[0];
port = hostport[1];
}
else
{
host = _host;
port = "";
}
Process myProc = new Process();
myProc.StartInfo.FileName = Settings.Default.winscppath;
myProc.StartInfo.Arguments = winscpprot;
if (_user != "")
{
string[] s = { "%", " ", "+", "/", "@", "\"", ":", ";" };
_user = ReplaceU(s, _user);
_pass = ReplaceU(s, _pass);
myProc.StartInfo.Arguments += _user;
if (_pass != "") myProc.StartInfo.Arguments += ":" + _pass;
myProc.StartInfo.Arguments += "@";
}
if (host != "") myProc.StartInfo.Arguments += HttpUtility.UrlEncode(host);
if (port != "") myProc.StartInfo.Arguments += ":" + port;
if (winscpprot == "ftp://") myProc.StartInfo.Arguments += " /passive=" + (Settings.Default.winscppassive ? "on" : "off");
if (Settings.Default.winscpkey && Settings.Default.winscpkeyfile != "") myProc.StartInfo.Arguments += " /privatekey=\"" + Settings.Default.winscpkeyfile + "\"";
Debug.WriteLine(myProc.StartInfo.Arguments);
if (winscpargs != "") myProc.StartInfo.Arguments += " " + winscpargs;
try
{
myProc.Start();
}
catch (System.ComponentModel.Win32Exception)
{
//user canceled
}
}
else
{
if (MessageBox.Show(this, "Could not find file \"" + winscppath + "\".\nDo you want to change the configuration ?", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK) optionsform.bWSCPPath_Click(type);
}
break;
case "5": //WinSCP (FTP)
winscpprot = "ftp://";
goto case "4";
default: //PuTTY
string[] puttyextractpath = ExtractFilePath(Settings.Default.puttypath);
string puttypath = puttyextractpath[0];
string puttyargs = puttyextractpath[1];
// for some reason you only have escape \ if it's followed by "
// will "fix" up to 3 \ in a password like \\\", then screw you with your maniac passwords
string[] passs = { "\"", "\\\\\"", "\\\\\\\\\"", "\\\\\\\\\\\\\"", };
string[] passr = { "\\\"", "\\\\\\\"", "\\\\\\\\\\\"", "\\\\\\\\\\\\\\\"", };
if (File.Exists(puttypath))
{
string host;
string port;
string[] hostport = _host.Split(':');
int split = hostport.Length;
if (split == 2)
{
host = hostport[0];
port = hostport[1];
}
else
{
host = _host;
port = "";
}
Process myProc = new Process();
myProc.StartInfo.FileName = Settings.Default.puttypath;
myProc.StartInfo.Arguments = "-ssh ";
if (_user != "") myProc.StartInfo.Arguments += _user + "@";
if (host != "") myProc.StartInfo.Arguments += host;
if (port != "") myProc.StartInfo.Arguments += " " + port;
if (_user != "" && _pass != "") myProc.StartInfo.Arguments += " -pw \"" + ReplaceA(passs, passr, _pass) + "\"";
if (Settings.Default.puttyexecute && Settings.Default.puttycommand != "") myProc.StartInfo.Arguments += " -m \"" + Settings.Default.puttycommand + "\"";
if (Settings.Default.puttykey && Settings.Default.puttykeyfile != "") myProc.StartInfo.Arguments += " -i \"" + Settings.Default.puttykeyfile + "\"";
if (Settings.Default.puttyforward) myProc.StartInfo.Arguments += " -X";
//MessageBox.Show(this, myProc.StartInfo.Arguments);
if (puttyargs != "") myProc.StartInfo.Arguments += " " + puttyargs;
try
{
myProc.Start();
}
catch (System.ComponentModel.Win32Exception)
{
//user canceled
}
}
else
{
if (MessageBox.Show(this, "Could not find file \"" + puttypath + "\".\nDo you want to change the configuration ?", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK) optionsform.bPuTTYPath_Click(type);
}
break;
}
}
}
}