ssh-handler.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SSH Handler
  2. //
  3. // Douglas Thrift
  4. //
  5. // ssh-handler.cs
  6. /* Copyright 2013 Douglas Thrift
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using System.Diagnostics;
  22. using System.IO;
  23. using System.Text;
  24. using System.Text.RegularExpressions;
  25. using System.Windows.Forms;
  26. using Microsoft.Win32;
  27. public class SshHandler
  28. {
  29. private enum Handler { Unspecified, Putty };
  30. private static Handler handler = Handler.Unspecified;
  31. private static string puttyPath = null;
  32. public static void Main(string[] args)
  33. {
  34. Application.EnableVisualStyles();
  35. try
  36. {
  37. Regex usage = new Regex(@"^(?:/|--?)(?:h|help|usage|\?)$", RegexOptions.IgnoreCase);
  38. Regex putty = new Regex(@"^(?:/|--?)putty(?:[:=](?<putty_path>.*))?$", RegexOptions.IgnoreCase);
  39. Uri uri = null;
  40. foreach (string arg in args)
  41. {
  42. if (usage.IsMatch(arg))
  43. {
  44. Usage();
  45. return;
  46. }
  47. Match match;
  48. if ((match = putty.Match(arg)).Success)
  49. {
  50. handler = Handler.Putty;
  51. Group group = match.Groups["putty_path"];
  52. if (group.Success)
  53. puttyPath = group.Value;
  54. }
  55. else
  56. uri = new Uri(arg, UriKind.Absolute);
  57. }
  58. if (uri != null)
  59. switch (handler)
  60. {
  61. case Handler.Unspecified:
  62. if (FindPutty())
  63. Putty(uri);
  64. else
  65. throw new Exception("Could not find a suitable SSH application.");
  66. break;
  67. case Handler.Putty:
  68. Putty(uri);
  69. break;
  70. }
  71. else
  72. Usage();
  73. }
  74. catch (Exception exception)
  75. {
  76. MessageBox.Show(exception.Message, "SSH Handler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  77. }
  78. }
  79. private static void Usage()
  80. {
  81. MessageBox.Show("ssh-handler [/putty[:<putty-path>]] <ssh-url>\n\n" +
  82. "/putty[:<putty-path>] -- Use PuTTY to connect", "SSH Handler Usage", MessageBoxButtons.OK, MessageBoxIcon.Information);
  83. }
  84. private static void UserPassword(Uri uri, out string user, out string password)
  85. {
  86. if (uri.UserInfo.Length != 0)
  87. {
  88. string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
  89. user = userInfo[0];
  90. password = userInfo.Length == 2 ? userInfo[1] : null;
  91. }
  92. else
  93. {
  94. user = null;
  95. password = null;
  96. }
  97. }
  98. private static bool FindPutty()
  99. {
  100. if (puttyPath != null)
  101. return true;
  102. foreach (string key in new string[] { "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE" })
  103. {
  104. string path = (string)Registry.GetValue(key + @"\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PuTTY_is1", "InstallLocation", null);
  105. if (path == null)
  106. continue;
  107. puttyPath = Path.Combine(path, "putty.exe");
  108. if (File.Exists(puttyPath))
  109. {
  110. Debug.WriteLine("Found PuTTY in registry: {0}", puttyPath, null);
  111. return true;
  112. }
  113. else
  114. puttyPath = null;
  115. }
  116. foreach (string path in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
  117. {
  118. puttyPath = Path.Combine(path, "putty.exe");
  119. if (File.Exists(puttyPath))
  120. {
  121. Debug.WriteLine("Found PuTTY in path: {0}", puttyPath, null);
  122. return true;
  123. }
  124. else
  125. puttyPath = null;
  126. }
  127. return false;
  128. }
  129. private static void Putty(Uri uri)
  130. {
  131. if (!FindPutty())
  132. throw new Exception("Could not find PuTTY executable.");
  133. string user, password;
  134. UserPassword(uri, out user, out password);
  135. StringBuilder args = new StringBuilder();
  136. if (password != null)
  137. args.AppendFormat("-pw {0} ", password);
  138. if (uri.Port != -1)
  139. args.AppendFormat("-P {0} ", uri.Port);
  140. if (user != null)
  141. args.AppendFormat("{0}@", user);
  142. args.Append(uri.Host);
  143. Debug.WriteLine("Running PuTTY command: {0} {1}", puttyPath, args);
  144. Process.Start(puttyPath, args.ToString());
  145. }
  146. }