浏览代码

Add ClientIP to client info to allow select client IP that should be used for connection (#132)

oleg 8 年之前
父节点
当前提交
78679a3c30
共有 2 个文件被更改,包括 71 次插入1 次删除
  1. 70 0
      src/Renci.SshNet/ConnectionInfo.cs
  2. 1 1
      src/Renci.SshNet/Session.cs

+ 70 - 0
src/Renci.SshNet/ConnectionInfo.cs

@@ -9,6 +9,7 @@ using Renci.SshNet.Common;
 using Renci.SshNet.Messages.Authentication;
 using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
 using Renci.SshNet.Security.Cryptography.Ciphers;
+using System.Net;
 
 namespace Renci.SshNet
 {
@@ -205,6 +206,14 @@ namespace Renci.SshNet
         /// </summary>
         public string ClientVersion { get; internal set; }
 
+        /// <summary>
+        /// Gets the client ip to use for connection when client has multiple IP addresses availble.
+        /// </summary>
+        /// <value>
+        /// The client ip to use for connection.
+        /// </value>
+        public IPAddress ClientIP { get; internal set; }
+
         /// <summary>
         /// Gets the current client compression algorithm.
         /// </summary>
@@ -263,6 +272,66 @@ namespace Renci.SshNet
         /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
         /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
         public ConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods)
+            : this(host, DefaultPort, username, null, ProxyTypes.None, null, 0, null, null, authenticationMethods)
+        {
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
+        /// </summary>
+        /// <param name="host">The host.</param>
+        /// <param name="username">The username.</param>
+        /// <param name="clientIP">The client IP to be used.</param>
+        /// <param name="authenticationMethods">The authentication methods.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="host" /> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException"><paramref name="host" /> is a zero-length string.</exception>
+        /// <exception cref="ArgumentException"><paramref name="host" /> is a zero-length string.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="host" /> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException"><paramref name="host" /> is a zero-length string.</exception>
+        public ConnectionInfo(string host, string username, IPAddress clientIP, params AuthenticationMethod[] authenticationMethods)
+            : this(host, DefaultPort, username, clientIP, ProxyTypes.None, null, 0, null, null, authenticationMethods)
+        {
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
+        /// </summary>
+        /// <param name="host">The host.</param>
+        /// <param name="port">The port.</param>
+        /// <param name="username">The username.</param>
+        /// <param name="clientIP">The client IP to be used.</param>
+        /// <param name="authenticationMethods">The authentication methods.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="host" /> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="host" /> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
+        public ConnectionInfo(string host, int port, string username, IPAddress clientIP, params AuthenticationMethod[] authenticationMethods)
+            : this(host, port, username, clientIP, ProxyTypes.None, null, 0, null, null, authenticationMethods)
+        {
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
+        /// </summary>
+        /// <param name="host">Connection host.</param>
+        /// <param name="port">Connection port.</param>
+        /// <param name="username">Connection username.</param>
+        /// <param name="clientIP">The client IP to be used.</param>
+        /// <param name="proxyType">Type of the proxy.</param>
+        /// <param name="proxyHost">The proxy host.</param>
+        /// <param name="proxyPort">The proxy port.</param>
+        /// <param name="proxyUsername">The proxy username.</param>
+        /// <param name="proxyPassword">The proxy password.</param>
+        /// <param name="authenticationMethods">The authentication methods.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyHost" /> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyPort" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
+        public ConnectionInfo(string host, int port, string username, IPAddress clientIP, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods)
         {
             if (host == null)
                 throw new ArgumentNullException("host");
@@ -386,6 +455,7 @@ namespace Renci.SshNet
             Host = host;
             Port = port;
             Username = username;
+            ClientIP = clientIP;
 
             ProxyType = proxyType;
             ProxyHost = proxyHost;

+ 1 - 1
src/Renci.SshNet/Session.cs

@@ -1600,7 +1600,7 @@ namespace Renci.SshNet
         /// <exception cref="SocketException">An error occurred trying to establish the connection.</exception>
         private void SocketConnect(string host, int port)
         {
-            var ipAddress = DnsAbstraction.GetHostAddresses(host)[0];
+            var ipAddress = ConnectionInfo.ClientIP ?? DnsAbstraction.GetHostAddresses(host)[0];
             var ep = new IPEndPoint(ipAddress, port);
 
             DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));