SshHandler.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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$", 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. if (settings.IsMatch(arg))
  49. return Settings();
  50. if (!MatchHandler(arg))
  51. uriParts = new List<string>(new string[] { arg });
  52. }
  53. else
  54. uriParts.Add(arg);
  55. if (uriParts != null)
  56. {
  57. Uri uri = new Uri(string.Join(" ", uriParts), UriKind.Absolute);
  58. string user, password;
  59. SetUserPassword(uri, out user, out password);
  60. if (handler == null)
  61. handler = FindHandler();
  62. handler.Execute(uri, user, password);
  63. }
  64. else
  65. return Usage(1);
  66. }
  67. catch (Exception exception)
  68. {
  69. MessageBox.Show(exception.Message, "SSH Handler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  70. return 2;
  71. }
  72. return 0;
  73. }
  74. private static int Usage(int code)
  75. {
  76. MessageBox.Show("ssh-handler [/settings] " +
  77. string.Join(" ", handlers.SelectMany(handler => handler.Options.Select(option => "[" + option + "]"))) +
  78. " <ssh-url>\n\n" +
  79. "/settings -- Show settings dialog\n\n" +
  80. string.Join("\n\n", handlers.SelectMany(handler => handler.Options.Zip(handler.Usages, (option, usage) => option + " -- " + usage))),
  81. "SSH Handler Usage",
  82. MessageBoxButtons.OK, MessageBoxIcon.Information);
  83. return code;
  84. }
  85. private static int Settings()
  86. {
  87. new SshHandlerSettings().ShowDialog();
  88. return 0;
  89. }
  90. private static bool MatchHandler(string arg)
  91. {
  92. foreach (Handler handler in handlers)
  93. switch (handler.DoMatch(arg))
  94. {
  95. case MatchOption.Set:
  96. Debug.WriteLine("Setting handler: {0}", handler, null);
  97. SshHandler.handler = handler;
  98. goto case MatchOption.Option;
  99. case MatchOption.Option:
  100. return true;
  101. }
  102. return false;
  103. }
  104. private static void SetUserPassword(Uri uri, out string user, out string password)
  105. {
  106. if (uri.UserInfo.Length != 0)
  107. {
  108. string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
  109. user = userInfo[0];
  110. password = userInfo.Length == 2 ? userInfo[1] : null;
  111. }
  112. else
  113. {
  114. user = null;
  115. password = null;
  116. }
  117. }
  118. private static Handler FindHandler()
  119. {
  120. foreach (Handler handler in handlers)
  121. if (handler.Find())
  122. return handler;
  123. throw new Exception("Could not find a suitable SSH application.");
  124. }
  125. }