SessionOptions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text.RegularExpressions;
  5. using System.Globalization;
  6. namespace WinSCP
  7. {
  8. [Guid("F25C49A5-74A6-4E8F-AEB4-5B4E0DDF0EF9")]
  9. [ComVisible(true)]
  10. public enum Protocol
  11. {
  12. Sftp = 0,
  13. Scp = 1,
  14. Ftp = 2,
  15. }
  16. [Guid("D924FAB9-FCE7-47B8-9F23-5717698384D3")]
  17. [ComVisible(true)]
  18. public enum FtpMode
  19. {
  20. Passive = 0,
  21. Active = 1,
  22. }
  23. [Guid("F2FC81EB-4761-4A4E-A3EC-4AFDD474C18C")]
  24. [ComVisible(true)]
  25. public enum FtpSecure
  26. {
  27. None = 0,
  28. Implicit = 1,
  29. ExplicitSsl = 2,
  30. ExplicitTls = 3,
  31. }
  32. [Guid("2D4EF368-EE80-4C15-AE77-D12AEAF4B00A")]
  33. [ClassInterface(Constants.ClassInterface)]
  34. [ComVisible(true)]
  35. public sealed class SessionOptions
  36. {
  37. public SessionOptions()
  38. {
  39. Timeout = new TimeSpan(0, 0, 15);
  40. RawSettings = new Dictionary<string,string>();
  41. }
  42. public Protocol Protocol { get; set; }
  43. public string HostName { get; set; }
  44. public int PortNumber { get { return _portNumber; } set { SetPortNumber(value); } }
  45. public string UserName { get; set; }
  46. public string Password { get; set; }
  47. public TimeSpan Timeout { get { return _timeout; } set { SetTimeout(value); } }
  48. // SSH
  49. public string SshHostKeyFingerprint { get { return _sshHostKeyFingerprint; } set { SetSshHostKeyFingerprint(value); } }
  50. public bool GiveUpSecurityAndAcceptAnySshHostKey { get; set; }
  51. public string SshPrivateKeyPath { get; set; }
  52. // FTP
  53. public FtpMode FtpMode { get; set; }
  54. public FtpSecure FtpSecure { get; set; }
  55. public string SslHostCertificateFingerprint { get { return _sslHostCertificateFingerprint; } set { SetHostSslCertificateFingerprint(value); } }
  56. public bool GiveUpSecurityAndAcceptAnySslHostCertificate { get; set; }
  57. public void AddRawSettings(string setting, string value)
  58. {
  59. RawSettings.Add(setting, value);
  60. }
  61. internal Dictionary<string, string> RawSettings { get; private set; }
  62. internal bool IsSsh { get { return (Protocol == Protocol.Sftp) || (Protocol == Protocol.Scp); } }
  63. private void SetSshHostKeyFingerprint(string s)
  64. {
  65. if (s != null)
  66. {
  67. Match match = _sshHostKeyRegex.Match(s);
  68. if (!match.Success || (match.Length != s.Length))
  69. {
  70. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "SSH host key fingerprint \"{0}\" does not match pattern /{1}/", s, _sshHostKeyRegex));
  71. }
  72. }
  73. _sshHostKeyFingerprint = s;
  74. }
  75. private void SetHostSslCertificateFingerprint(string s)
  76. {
  77. if (s != null)
  78. {
  79. Match match = _sslCertificateRegex.Match(s);
  80. if (!match.Success || (match.Length != s.Length))
  81. {
  82. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "SSL host certificate fingerprint \"{0}\" does not match pattern /{1}/", s, _sslCertificateRegex));
  83. }
  84. }
  85. _sslHostCertificateFingerprint = s;
  86. }
  87. private void SetTimeout(TimeSpan value)
  88. {
  89. if (value <= TimeSpan.Zero)
  90. {
  91. throw new ArgumentException("Timeout has to be positive non-zero value");
  92. }
  93. _timeout = value;
  94. }
  95. private void SetPortNumber(int value)
  96. {
  97. if (value < 0)
  98. {
  99. throw new ArgumentException("Port number cannot be negative");
  100. }
  101. _portNumber = value;
  102. }
  103. private string _sshHostKeyFingerprint;
  104. private string _sslHostCertificateFingerprint;
  105. private TimeSpan _timeout;
  106. private int _portNumber;
  107. private const string _listPattern = @"{0}(;{0})*";
  108. private const string _sshHostKeyPattern = @"(ssh-rsa |ssh-dss )?\d+ ([0-9a-f]{2}:){15}[0-9a-f]{2}";
  109. private static readonly Regex _sshHostKeyRegex =
  110. new Regex(string.Format(CultureInfo.InvariantCulture, _listPattern, _sshHostKeyPattern));
  111. private const string _sslCertificatePattern = @"([0-9a-f]{2}:){19}[0-9a-f]{2}";
  112. private static readonly Regex _sslCertificateRegex =
  113. new Regex(string.Format(CultureInfo.InvariantCulture, _listPattern, _sslCertificatePattern));
  114. }
  115. }