|
@@ -19,45 +19,141 @@
|
|
* limitations under the License.
|
|
* limitations under the License.
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
+using System;
|
|
using System.Diagnostics;
|
|
using System.Diagnostics;
|
|
|
|
+using System.IO;
|
|
using System.Text;
|
|
using System.Text;
|
|
|
|
+using System.Text.RegularExpressions;
|
|
|
|
+using System.Windows.Forms;
|
|
|
|
+using Microsoft.Win32;
|
|
|
|
|
|
public class SshHandler
|
|
public class SshHandler
|
|
{
|
|
{
|
|
|
|
+ private enum Handler { Unspecified, Putty };
|
|
|
|
+ private static Handler handler = Handler.Unspecified;
|
|
|
|
+ private static string puttyPath = null;
|
|
|
|
+
|
|
public static void Main(string[] args)
|
|
public static void Main(string[] args)
|
|
{
|
|
{
|
|
- foreach (string arg in args)
|
|
+ Application.EnableVisualStyles();
|
|
- Ssh(arg);
|
|
+
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ Regex usage = new Regex(@"^[-/](?:help|usage|\?)$", RegexOptions.IgnoreCase);
|
|
|
|
+ Regex putty = new Regex(@"^[-/]putty(?:[:=](?<putty_path>.*))?$", RegexOptions.IgnoreCase);
|
|
|
|
+ Uri uri = null;
|
|
|
|
+
|
|
|
|
+ foreach (string arg in args)
|
|
|
|
+ {
|
|
|
|
+ if (usage.IsMatch(arg))
|
|
|
|
+ {
|
|
|
|
+ Usage();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Match match;
|
|
|
|
+
|
|
|
|
+ if ((match = putty.Match(arg)).Success)
|
|
|
|
+ {
|
|
|
|
+ handler = Handler.Putty;
|
|
|
|
+ Group group = match.Groups["putty_path"];
|
|
|
|
+ if (group.Success)
|
|
|
|
+ puttyPath = group.Value;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ uri = new Uri(arg, UriKind.Absolute);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (uri != null)
|
|
|
|
+ switch (handler)
|
|
|
|
+ {
|
|
|
|
+ case Handler.Unspecified:
|
|
|
|
+ if (FindPutty())
|
|
|
|
+ Putty(uri);
|
|
|
|
+ else
|
|
|
|
+ throw new Exception("Could not find a suitable SSH application.");
|
|
|
|
+ break;
|
|
|
|
+ case Handler.Putty:
|
|
|
|
+ Putty(uri);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ Usage();
|
|
|
|
+ }
|
|
|
|
+ catch (Exception exception)
|
|
|
|
+ {
|
|
|
|
+ MessageBox.Show(exception.Message, "SSH Handler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- private static void Ssh(string arg)
|
|
+ private static void Usage()
|
|
{
|
|
{
|
|
- System.Uri uri = new System.Uri(arg);
|
|
+ MessageBox.Show("ssh-handler [/putty[:<putty-path>]] <ssh-url>\n\n" +
|
|
- string user = null, password = null;
|
|
+ "/putty[:<putty-path>] -- Use PuTTY to connect", "SSH Handler Usage", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ private static void UserPassword(Uri uri, out string user, out string password)
|
|
|
|
+ {
|
|
if (uri.UserInfo.Length != 0)
|
|
if (uri.UserInfo.Length != 0)
|
|
{
|
|
{
|
|
string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
|
|
string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
|
|
-
|
|
|
|
user = userInfo[0];
|
|
user = userInfo[0];
|
|
|
|
+ password = userInfo.Length == 2 ? userInfo[1] : null;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ user = null;
|
|
|
|
+ password = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static bool FindPutty()
|
|
|
|
+ {
|
|
|
|
+ if (puttyPath != null)
|
|
|
|
+ return true;
|
|
|
|
|
|
- if (userInfo.Length == 2)
|
|
+ foreach (string key in new string[] { "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE" })
|
|
- password = userInfo[1];
|
|
+ {
|
|
|
|
+ string path = (string)Registry.GetValue(key + @"\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PuTTY_is1", "InstallLocation", null);
|
|
|
|
+ if (path == null)
|
|
|
|
+ continue;
|
|
|
|
+ puttyPath = Path.Combine(path, "putty.exe");
|
|
|
|
+ if (File.Exists(puttyPath))
|
|
|
|
+ return true;
|
|
|
|
+ else
|
|
|
|
+ puttyPath = null;
|
|
}
|
|
}
|
|
|
|
|
|
- StringBuilder args = new StringBuilder();
|
|
+ foreach (string path in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
|
|
|
|
+ {
|
|
|
|
+ puttyPath = Path.Combine(path, "putty.exe");
|
|
|
|
+ if (File.Exists(puttyPath))
|
|
|
|
+ return true;
|
|
|
|
+ else
|
|
|
|
+ puttyPath = null;
|
|
|
|
+ }
|
|
|
|
|
|
- if (password != null)
|
|
+ return false;
|
|
- args.Append("-pw ").Append(password).Append(' ');
|
|
+ }
|
|
|
|
|
|
- if (user != null)
|
|
+ private static void Putty(Uri uri)
|
|
- args.Append(user).Append('@');
|
|
+ {
|
|
|
|
+ if (!FindPutty())
|
|
|
|
+ throw new Exception("Could not find PuTTY executable.");
|
|
|
|
|
|
- args.Append(uri.Host);
|
|
+ string user, password;
|
|
|
|
+ UserPassword(uri, out user, out password);
|
|
|
|
|
|
|
|
+ StringBuilder args = new StringBuilder();
|
|
|
|
+ if (password != null)
|
|
|
|
+ args.AppendFormat("-pw {0} ", password);
|
|
if (uri.Port != -1)
|
|
if (uri.Port != -1)
|
|
- args.Append(':').Append(uri.Port);
|
|
+ args.AppendFormat("-P {0} ", uri.Port);
|
|
|
|
+ if (user != null)
|
|
|
|
+ args.AppendFormat("{0}@", user);
|
|
|
|
+ args.Append(uri.Host);
|
|
|
|
|
|
- Process.Start(@"C:\Program Files (x86)\PuTTY\putty.exe", args.ToString());
|
|
+ Debug.WriteLine("{0} {1}", puttyPath, args);
|
|
|
|
+ Process.Start(puttyPath, args.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|