123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- // SSH Handler
- //
- // Douglas Thrift
- //
- // ssh-handler.cs
- /* Copyright 2013 Douglas Thrift
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using Microsoft.Win32;
- public class SshHandler
- {
- private enum Handler { Unspecified, Putty };
- private static Handler handler = Handler.Unspecified;
- private static string puttyPath = null;
- public static int Main(string[] args)
- {
- Application.EnableVisualStyles();
- try
- {
- Regex usage = new Regex(@"^(?:/|--?)(?:h|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))
- return Usage(0);
- 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
- return Usage(1);
- }
- catch (Exception exception)
- {
- MessageBox.Show(exception.Message, "SSH Handler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return 2;
- }
- return 0;
- }
- private static int Usage(int code)
- {
- MessageBox.Show("ssh-handler [/putty[:<putty-path>]] <ssh-url>\n\n" +
- "/putty[:<putty-path>] -- Use PuTTY to connect", "SSH Handler Usage", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return code;
- }
- private static void UserPassword(Uri uri, out string user, out string password)
- {
- if (uri.UserInfo.Length != 0)
- {
- string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
- user = userInfo[0];
- password = userInfo.Length == 2 ? userInfo[1] : null;
- }
- else
- {
- user = null;
- password = null;
- }
- }
- private static bool FindPutty()
- {
- if (puttyPath != null)
- goto found;
- foreach (RegistryHive hive in new RegistryHive[] { RegistryHive.CurrentUser, RegistryHive.LocalMachine })
- {
- IList<RegistryView> views = new List<RegistryView>(new RegistryView[] { RegistryView.Registry32 });
- if (Environment.Is64BitOperatingSystem)
- views.Insert(0, RegistryView.Registry64);
- foreach (RegistryView view in views)
- using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, view), key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PuTTY_is1"))
- if (key != null)
- {
- string path = (string)key.GetValue("InstallLocation");
- if (path == null)
- continue;
- puttyPath = Path.Combine(path, "putty.exe");
- if (File.Exists(puttyPath))
- {
- Debug.WriteLine("Found PuTTY in registry: {0}", puttyPath, null);
- goto found;
- }
- else
- puttyPath = null;
- }
- }
- foreach (string path in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
- {
- puttyPath = Path.Combine(path, "putty.exe");
- if (File.Exists(puttyPath))
- {
- Debug.WriteLine("Found PuTTY in path: {0}", puttyPath, null);
- goto found;
- }
- else
- puttyPath = null;
- }
- return false;
- found:
- puttyPath = puttyPath.Trim();
- return true;
- }
- private static void Putty(Uri uri)
- {
- if (!FindPutty())
- throw new Exception("Could not find PuTTY executable.");
- 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)
- args.AppendFormat("-P {0} ", uri.Port);
- if (user != null)
- args.AppendFormat("{0}@", user);
- args.Append(uri.Host);
- Debug.WriteLine("Running PuTTY command: {0} {1}", puttyPath, args);
- Process.Start(puttyPath, args.ToString());
- }
- }
|