PuttyHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // PuTTY Handler
  2. //
  3. // Douglas Thrift
  4. //
  5. // PuttyHandler.cs
  6. /* Copyright 2014 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.Collections.Generic;
  22. using System.Diagnostics;
  23. using System.IO;
  24. using System.Text;
  25. using System.Text.RegularExpressions;
  26. using Microsoft.Win32;
  27. public class PuttyHandler : AbstractHandler, Handler
  28. {
  29. private Regex regex = new Regex(@"^(?:/|--?)putty(?:[:=](?<putty_path>.*))?$", RegexOptions.IgnoreCase);
  30. private string path = null;
  31. public IList<string> Options
  32. {
  33. get
  34. {
  35. return new string[] { "/putty[:<putty-path>]" };
  36. }
  37. }
  38. public IList<string> Usages
  39. {
  40. get
  41. {
  42. return new string[] { "Use PuTTY to connect" };
  43. }
  44. }
  45. public MatchOption DoMatch(string arg)
  46. {
  47. Match match;
  48. if ((match = regex.Match(arg)).Success)
  49. return SetValue(match, "putty_path", ref path);
  50. else
  51. return MatchOption.None;
  52. }
  53. public bool Find()
  54. {
  55. if (path != null)
  56. goto Found;
  57. foreach (var hive in new RegistryHive[] { RegistryHive.CurrentUser, RegistryHive.LocalMachine })
  58. {
  59. var views = new List<RegistryView>(new RegistryView[] { RegistryView.Registry32 });
  60. if (Environment.Is64BitOperatingSystem)
  61. views.Insert(0, RegistryView.Registry64);
  62. foreach (RegistryView view in views)
  63. using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, view), key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PuTTY_is1"))
  64. if (key != null)
  65. {
  66. string location = (string)key.GetValue("InstallLocation");
  67. if (location == null)
  68. continue;
  69. path = Path.Combine(location, "putty.exe");
  70. if (File.Exists(path))
  71. {
  72. Debug.WriteLine("Found PuTTY in registry: {0}", path, null);
  73. goto Found;
  74. }
  75. else
  76. path = null;
  77. }
  78. }
  79. if ((path = FindInPath("putty.exe")) != null)
  80. {
  81. Debug.WriteLine("Found PuTTY in path: {0}", path, null);
  82. goto Found;
  83. }
  84. return false;
  85. Found:
  86. path = path.Trim();
  87. return true;
  88. }
  89. public void Execute(Uri uri, string user, string password)
  90. {
  91. if (!Find())
  92. throw new Exception("Could not find PuTTY executable.");
  93. StringBuilder args = new StringBuilder();
  94. if (password != null)
  95. args.AppendFormat("-pw \"{0}\" ", password);
  96. if (uri.Port != -1)
  97. args.AppendFormat("-P {0} ", uri.Port);
  98. if (user != null)
  99. args.AppendFormat("{0}@", user);
  100. args.Append(uri.Host);
  101. Debug.WriteLine("Running PuTTY command: {0} {1}", path, args);
  102. Process.Start(path, args.ToString());
  103. }
  104. }