SshHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SSH Handler
  2. //
  3. // Douglas Thrift
  4. //
  5. // SshHandler.cs
  6. /* Copyright 2013 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. public static int Main(string[] args)
  35. {
  36. Application.EnableVisualStyles();
  37. try
  38. {
  39. Regex usage = new Regex(@"^(?:/|--?)(?:h|help|usage|\?)$", RegexOptions.IgnoreCase);
  40. IList<string> uriParts = null;
  41. foreach (string arg in args)
  42. if (uriParts == null)
  43. {
  44. if (usage.IsMatch(arg))
  45. return Usage(0);
  46. if (!MatchHandler(arg))
  47. uriParts = new List<string>(new string[] { arg });
  48. }
  49. else
  50. uriParts.Add(arg);
  51. if (uriParts != null)
  52. {
  53. Uri uri = new Uri(string.Join(" ", uriParts), UriKind.Absolute);
  54. string user, password;
  55. SetUserPassword(uri, out user, out password);
  56. if (handler == null)
  57. handler = FindHandler();
  58. handler.Execute(uri, user, password);
  59. }
  60. else
  61. return Usage(1);
  62. }
  63. catch (Exception exception)
  64. {
  65. MessageBox.Show(exception.Message, "SSH Handler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  66. return 2;
  67. }
  68. return 0;
  69. }
  70. private static int Usage(int code)
  71. {
  72. MessageBox.Show("ssh-handler " +
  73. string.Join(" ", handlers.SelectMany(handler => handler.Options.Select(option => "[" + option + "]"))) +
  74. " <ssh-url>\n\n" +
  75. string.Join("\n\n", handlers.SelectMany(handler => handler.Options.Zip(handler.Usages, (option, usage) => option + " -- " + usage))),
  76. "SSH Handler Usage",
  77. MessageBoxButtons.OK, MessageBoxIcon.Information);
  78. return code;
  79. }
  80. private static bool MatchHandler(string arg)
  81. {
  82. foreach (Handler handler in handlers)
  83. switch (handler.DoMatch(arg))
  84. {
  85. case MatchOption.Set:
  86. Debug.WriteLine("Setting handler: {0}", handler, null);
  87. SshHandler.handler = handler;
  88. goto case MatchOption.Option;
  89. case MatchOption.Option:
  90. return true;
  91. }
  92. return false;
  93. }
  94. private static void SetUserPassword(Uri uri, out string user, out string password)
  95. {
  96. if (uri.UserInfo.Length != 0)
  97. {
  98. string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
  99. user = userInfo[0];
  100. password = userInfo.Length == 2 ? userInfo[1] : null;
  101. }
  102. else
  103. {
  104. user = null;
  105. password = null;
  106. }
  107. }
  108. private static Handler FindHandler()
  109. {
  110. foreach (Handler handler in handlers)
  111. if (handler.Find())
  112. return handler;
  113. throw new Exception("Could not find a suitable SSH application.");
  114. }
  115. }