SshHandler.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 [/putty[:<putty-path>]] <ssh-url>\n\n" +
  73. string.Join("\n\n", handlers.SelectMany(handler => handler.Usages)), "SSH Handler Usage",
  74. MessageBoxButtons.OK, MessageBoxIcon.Information);
  75. return code;
  76. }
  77. private static bool MatchHandler(string arg)
  78. {
  79. foreach (Handler handler in handlers)
  80. switch (handler.DoMatch(arg))
  81. {
  82. case MatchOption.Set:
  83. Debug.WriteLine("Setting handler: {0}", handler, null);
  84. SshHandler.handler = handler;
  85. goto case MatchOption.Option;
  86. case MatchOption.Option:
  87. return true;
  88. }
  89. return false;
  90. }
  91. private static void SetUserPassword(Uri uri, out string user, out string password)
  92. {
  93. if (uri.UserInfo.Length != 0)
  94. {
  95. string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
  96. user = userInfo[0];
  97. password = userInfo.Length == 2 ? userInfo[1] : null;
  98. }
  99. else
  100. {
  101. user = null;
  102. password = null;
  103. }
  104. }
  105. private static Handler FindHandler()
  106. {
  107. foreach (Handler handler in handlers)
  108. if (handler.Find())
  109. return handler;
  110. throw new Exception("Could not find a suitable SSH application.");
  111. }
  112. }