PuttyHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 IList<Setting> Settings
  46. {
  47. get
  48. {
  49. return new Setting[] { new Setting("/putty", "PuTTY", SettingType.OptionalPath, true) };
  50. }
  51. }
  52. public MatchOption DoMatch(string arg)
  53. {
  54. Match match;
  55. if ((match = regex.Match(arg)).Success)
  56. return SetValue(match, "putty_path", ref path);
  57. else
  58. return MatchOption.None;
  59. }
  60. public bool Find()
  61. {
  62. if (path != null)
  63. goto Found;
  64. foreach (var hive in new RegistryHive[] { RegistryHive.CurrentUser, RegistryHive.LocalMachine })
  65. {
  66. var views = new List<RegistryView>(new RegistryView[] { RegistryView.Registry32 });
  67. if (Environment.Is64BitOperatingSystem)
  68. views.Insert(0, RegistryView.Registry64);
  69. foreach (RegistryView view in views)
  70. using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, view), key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PuTTY_is1"))
  71. if (key != null)
  72. {
  73. string location = (string)key.GetValue("InstallLocation");
  74. if (location == null)
  75. continue;
  76. path = Path.Combine(location, "putty.exe");
  77. if (File.Exists(path))
  78. {
  79. Debug.WriteLine("Found PuTTY in registry: {0}", path, null);
  80. goto Found;
  81. }
  82. else
  83. path = null;
  84. }
  85. }
  86. if ((path = FindInPath("putty.exe")) != null)
  87. {
  88. Debug.WriteLine("Found PuTTY in path: {0}", path, null);
  89. goto Found;
  90. }
  91. return false;
  92. Found:
  93. path = path.Trim();
  94. return true;
  95. }
  96. public void Execute(Uri uri, string user, string password)
  97. {
  98. if (!Find())
  99. throw new Exception("Could not find PuTTY executable.");
  100. StringBuilder args = new StringBuilder();
  101. if (password != null)
  102. args.AppendFormat("-pw \"{0}\" ", password);
  103. if (uri.Port != -1)
  104. args.AppendFormat("-P {0} ", uri.Port);
  105. if (user != null)
  106. args.AppendFormat("{0}@", user);
  107. args.Append(uri.Host);
  108. Debug.WriteLine("Running PuTTY command: {0} {1}", path, args);
  109. Process.Start(path, args.ToString());
  110. }
  111. }