SessionOptions.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. ExplicitTls = 3,
  30. ExplicitSsl = 2,
  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. public int TimeoutInMilliseconds { get { return GetTimeoutInMilliseconds(); } set { SetTimeoutInMilliseconds(value); } }
  49. // SSH
  50. public string SshHostKeyFingerprint { get { return _sshHostKeyFingerprint; } set { SetSshHostKeyFingerprint(value); } }
  51. public bool GiveUpSecurityAndAcceptAnySshHostKey { get; set; }
  52. public string SshPrivateKeyPath { get; set; }
  53. // FTP
  54. public FtpMode FtpMode { get; set; }
  55. public FtpSecure FtpSecure { get; set; }
  56. public string TlsHostCertificateFingerprint { get { return _tlsHostCertificateFingerprint; } set { SetHostTlsCertificateFingerprint(value); } }
  57. [Obsolete("Use TlsHostCertificateFingerprint")]
  58. public string SslHostCertificateFingerprint { get { return TlsHostCertificateFingerprint; } set { TlsHostCertificateFingerprint = value; } }
  59. public bool GiveUpSecurityAndAcceptAnyTlsHostCertificate { get; set; }
  60. [Obsolete("Use GiveUpSecurityAndAcceptAnyTlsHostCertificate")]
  61. public bool GiveUpSecurityAndAcceptAnySslHostCertificate { get { return GiveUpSecurityAndAcceptAnyTlsHostCertificate; } set { GiveUpSecurityAndAcceptAnyTlsHostCertificate = value; } }
  62. public void AddRawSettings(string setting, string value)
  63. {
  64. RawSettings.Add(setting, value);
  65. }
  66. internal Dictionary<string, string> RawSettings { get; private set; }
  67. internal bool IsSsh { get { return (Protocol == Protocol.Sftp) || (Protocol == Protocol.Scp); } }
  68. private void SetSshHostKeyFingerprint(string s)
  69. {
  70. if (s != null)
  71. {
  72. Match match = _sshHostKeyRegex.Match(s);
  73. if (!match.Success || (match.Length != s.Length))
  74. {
  75. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "SSH host key fingerprint \"{0}\" does not match pattern /{1}/", s, _sshHostKeyRegex));
  76. }
  77. }
  78. _sshHostKeyFingerprint = s;
  79. }
  80. private void SetHostTlsCertificateFingerprint(string s)
  81. {
  82. if (s != null)
  83. {
  84. Match match = _tlsCertificateRegex.Match(s);
  85. if (!match.Success || (match.Length != s.Length))
  86. {
  87. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "TLS host certificate fingerprint \"{0}\" does not match pattern /{1}/", s, _tlsCertificateRegex));
  88. }
  89. }
  90. _tlsHostCertificateFingerprint = s;
  91. }
  92. private void SetTimeout(TimeSpan value)
  93. {
  94. if (value <= TimeSpan.Zero)
  95. {
  96. throw new ArgumentException("Timeout has to be positive non-zero value");
  97. }
  98. _timeout = value;
  99. }
  100. private void SetPortNumber(int value)
  101. {
  102. if (value < 0)
  103. {
  104. throw new ArgumentException("Port number cannot be negative");
  105. }
  106. _portNumber = value;
  107. }
  108. private int GetTimeoutInMilliseconds()
  109. {
  110. if ((Timeout.TotalMilliseconds > int.MaxValue) || (Timeout.TotalMilliseconds < int.MinValue))
  111. {
  112. throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, "Cannot convert {0} to integer", Timeout));
  113. }
  114. return (int)Timeout.TotalMilliseconds;
  115. }
  116. private void SetTimeoutInMilliseconds(int value)
  117. {
  118. Timeout = TimeSpan.FromMilliseconds(value);
  119. }
  120. private string _sshHostKeyFingerprint;
  121. private string _tlsHostCertificateFingerprint;
  122. private TimeSpan _timeout;
  123. private int _portNumber;
  124. private const string _listPattern = @"{0}(;{0})*";
  125. private const string _sshHostKeyPattern = @"(ssh-rsa |ssh-dss )?\d+ ([0-9a-f]{2}:){15}[0-9a-f]{2}";
  126. private static readonly Regex _sshHostKeyRegex =
  127. new Regex(string.Format(CultureInfo.InvariantCulture, _listPattern, _sshHostKeyPattern));
  128. private const string _tlsCertificatePattern = @"([0-9a-f]{2}:){19}[0-9a-f]{2}";
  129. private static readonly Regex _tlsCertificateRegex =
  130. new Regex(string.Format(CultureInfo.InvariantCulture, _listPattern, _tlsCertificatePattern));
  131. }
  132. }