SshHandler.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SSH Handler
  2. //
  3. // Douglas Thrift
  4. //
  5. // SshHandler.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.Linq;
  24. using System.Text.RegularExpressions;
  25. using System.Windows.Forms;
  26. public class SshHandler
  27. {
  28. private static IList<Handler> handlers = new Handler[]
  29. {
  30. new PuttyHandler(),
  31. new OpensshHandler(),
  32. };
  33. private static Handler handler = null;
  34. [STAThread]
  35. public static int Main(string[] args)
  36. {
  37. Application.EnableVisualStyles();
  38. try
  39. {
  40. Regex usage = new Regex(@"^(?:/|--?)(?:h|help|usage|\?)$", RegexOptions.IgnoreCase);
  41. Regex settings = new Regex(@"^(?:/|--?)settings(?:[:=](?<settings_type>.*))?$", RegexOptions.IgnoreCase);
  42. IList<string> uriParts = null;
  43. foreach (string arg in args)
  44. if (uriParts == null)
  45. {
  46. if (usage.IsMatch(arg))
  47. return Usage(0);
  48. Match match;
  49. if ((match = settings.Match(arg)).Success)
  50. return Settings(match.Groups["settings_type"].Value);
  51. if (!MatchHandler(arg))
  52. uriParts = new List<string>(new string[] { arg });
  53. }
  54. else
  55. uriParts.Add(arg);
  56. if (uriParts != null)
  57. {
  58. Uri uri = new Uri(string.Join(" ", uriParts), UriKind.Absolute);
  59. string user, password;
  60. SetUserPassword(uri, out user, out password);
  61. if (handler == null)
  62. handler = FindHandler();
  63. handler.Execute(uri, user, password);
  64. }
  65. else
  66. return Usage(1);
  67. }
  68. catch (Exception exception)
  69. {
  70. MessageBox.Show(exception.Message, "SSH Handler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  71. return 2;
  72. }
  73. return 0;
  74. }
  75. private static int Usage(int code)
  76. {
  77. MessageBox.Show("ssh-handler [/settings] " +
  78. string.Join(" ", handlers.SelectMany(handler => handler.Options.Select(option => "[" + option + "]"))) +
  79. " <ssh-url>\n\n" +
  80. "/settings[:(user|global)] -- Show settings dialog\n\n" +
  81. string.Join("\n\n", handlers.SelectMany(handler => handler.Options.Zip(handler.Usages, (option, usage) => option + " -- " + usage))),
  82. "SSH Handler Usage",
  83. MessageBoxButtons.OK, MessageBoxIcon.Information);
  84. return code;
  85. }
  86. private static int Settings(string type)
  87. {
  88. var settings = new SettingsDialog(handlers, type);
  89. var result = settings.ShowDialog();
  90. Debug.WriteLine("Settings result: {0}", result, null);
  91. return 0;
  92. }
  93. private static bool MatchHandler(string arg)
  94. {
  95. foreach (Handler handler in handlers)
  96. switch (handler.DoMatch(arg))
  97. {
  98. case MatchOption.Set:
  99. Debug.WriteLine("Setting handler: {0}", handler, null);
  100. SshHandler.handler = handler;
  101. goto case MatchOption.Option;
  102. case MatchOption.Option:
  103. return true;
  104. }
  105. return false;
  106. }
  107. private static void SetUserPassword(Uri uri, out string user, out string password)
  108. {
  109. if (uri.UserInfo.Length != 0)
  110. {
  111. string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
  112. user = userInfo[0];
  113. password = userInfo.Length == 2 ? userInfo[1] : null;
  114. }
  115. else
  116. {
  117. user = null;
  118. password = null;
  119. }
  120. }
  121. private static Handler FindHandler()
  122. {
  123. foreach (Handler handler in handlers)
  124. if (handler.Find())
  125. return handler;
  126. throw new Exception("Could not find a suitable SSH application.");
  127. }
  128. }