PuttyHandler.cs 3.6 KB

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