public void Connect(string host, int port, FtpIpVersion ipVersions) { IAsyncResult asyncResult = null; IPAddress[] hostAddresses = Dns.GetHostAddresses(host); if (ipVersions == 0) { throw new ArgumentException("The ipVersions parameter must contain at least 1 flag."); } for (int i = 0; i < hostAddresses.Length; i++) { if (ipVersions == FtpIpVersion.ANY) { goto Label_0044; } AddressFamily addressFamily = hostAddresses[i].AddressFamily; if (addressFamily != AddressFamily.InterNetwork) { if (addressFamily == AddressFamily.InterNetworkV6) { goto Label_003E; } goto Label_0044; } if ((ipVersions & FtpIpVersion.IPv4) == FtpIpVersion.IPv4) { goto Label_0044; } continue; Label_003E: if ((ipVersions & FtpIpVersion.IPv6) != FtpIpVersion.IPv6) { continue; } Label_0044: this.m_socket = new System.Net.Sockets.Socket(hostAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp); asyncResult = this.m_socket.BeginConnect(hostAddresses[i], port, null, null); if (!asyncResult.AsyncWaitHandle.WaitOne(this.m_connectTimeout, true)) { this.Close(); if ((i + 1) == hostAddresses.Length) { throw new TimeoutException("Timed out trying to connect!"); } } else { this.m_socket.EndConnect(asyncResult); break; } } if ((this.m_socket == null) || !this.m_socket.Connected) { this.Close(); throw new IOException("Failed to connect to host."); } this.m_netStream = new System.Net.Sockets.NetworkStream(this.m_socket); this.m_lastActivity = DateTime.Now; }
/// <summary> /// Connect to the FTP server. Overridden in proxy classes. /// </summary> protected virtual Task ConnectAsync(FtpSocketStream stream, string host, int port, FtpIpVersion ipVersions, CancellationToken token) { return(stream.ConnectAsync(host, port, ipVersions, token)); }
/// <summary> /// Connect to the FTP server. Overridden in proxy classes. /// </summary> protected virtual void Connect(FtpSocketStream stream, string host, int port, FtpIpVersion ipVersions) { stream.Connect(host, port, ipVersions); }
/// <summary> /// Connects to the server using an existing <see cref="FtpSocketStream"/> /// </summary> /// <param name="stream">The existing socket stream</param> /// <param name="host">Host name</param> /// <param name="port">Port number</param> /// <param name="ipVersions">IP version to use</param> protected override void Connect(FtpSocketStream stream, string host, int port, FtpIpVersion ipVersions) { base.Connect(stream); var writer = new StreamWriter(stream); writer.WriteLine("CONNECT {0}:{1} HTTP/1.1", host, port); writer.WriteLine("Host: {0}:{1}", host, port); if (Proxy.Credentials != null) { var credentialsHash = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Proxy.Credentials.UserName + ":" + Proxy.Credentials.Password)); writer.WriteLine("Proxy-Authorization: Basic " + credentialsHash); } writer.WriteLine("User-Agent: custom-ftp-client"); writer.WriteLine(); writer.Flush(); ProxyHandshake(stream); }
/// <summary> /// Connects to the server using an existing <see cref="FtpSocketStream"/> /// </summary> /// <param name="stream">The existing socket stream</param> /// <param name="host">Host name</param> /// <param name="port">Port number</param> /// <param name="ipVersions">IP version to use</param> /// <param name="token">IP version to use</param> protected override async Task ConnectAsync(FtpSocketStream stream, string host, int port, FtpIpVersion ipVersions, CancellationToken token) { await base.ConnectAsync(stream, token); var writer = new StreamWriter(stream); await writer.WriteLineAsync(string.Format("CONNECT {0}:{1} HTTP/1.1", host, port)); await writer.WriteLineAsync(string.Format("Host: {0}:{1}", host, port)); if (Proxy.Credentials != null) { var credentialsHash = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Proxy.Credentials.UserName + ":" + Proxy.Credentials.Password)); await writer.WriteLineAsync("Proxy-Authorization: Basic " + credentialsHash); } await writer.WriteLineAsync("User-Agent: custom-ftp-client"); await writer.WriteLineAsync(); await writer.FlushAsync(); await ProxyHandshakeAsync(stream, token); }
/// <summary> /// Connect to the specified host /// </summary> /// <param name="host">The host to connect to</param> /// <param name="port">The port to connect to</param> /// <param name="ipVersions">Internet Protocol versions to support durring the connection phase</param> public void Connect(string host, int port, FtpIpVersion ipVersions) { IAsyncResult ar = null; IPAddress[] addresses = Dns.GetHostAddresses(host); if (ipVersions == 0) { throw new ArgumentException("The ipVersions parameter must contain at least 1 flag."); } for (int i = 0; i < addresses.Length; i++) { #if DEBUG FtpTrace.WriteLine("{0}: {1}", addresses[i].AddressFamily.ToString(), addresses[i].ToString()); #endif // we don't need to do this check unless // a particular version of IP has been // omitted so we won't. if (ipVersions != FtpIpVersion.ANY) { switch (addresses[i].AddressFamily) { case AddressFamily.InterNetwork: if ((ipVersions & FtpIpVersion.IPv4) != FtpIpVersion.IPv4) { #if DEBUG FtpTrace.WriteLine("SKIPPED!"); #endif continue; } break; case AddressFamily.InterNetworkV6: if ((ipVersions & FtpIpVersion.IPv6) != FtpIpVersion.IPv6) { #if DEBUG FtpTrace.WriteLine("SKIPPED!"); #endif continue; } break; } } m_socket = new Socket(addresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp); ar = m_socket.BeginConnect(addresses[i], port, null, null); if (addresses[i].ToString() != "::1") // 21Nov2014 Added by Lemu { if (!ar.AsyncWaitHandle.WaitOne(m_connectTimeout, true)) { Close(); // check to see if we're out of addresses // and if we are throw a TimeoutException if (i + 1 == addresses.Length) { throw new TimeoutException("Timed out trying to connect!"); } } else { m_socket.EndConnect(ar); // we got a connection, break out // of the loop. break; } } } // make sure that we actually connected to // one of the addresses returned from GetHostAddresses() if (m_socket == null || !m_socket.Connected) { Close(); throw new IOException("Failed to connect to host."); } m_netStream = new NetworkStream(m_socket); m_lastActivity = DateTime.Now; }
/// <summary> /// Connect to the specified host /// </summary> /// <param name="host">The host to connect to</param> /// <param name="port">The port to connect to</param> /// <param name="ipVersions">Internet Protocol versions to support during the connection phase</param> public void Connect(string host, int port, FtpIpVersion ipVersions) { #if CORE IPAddress[] addresses = Dns.GetHostAddressesAsync(host).Result; #else IAsyncResult ar = null; IPAddress[] addresses = Dns.GetHostAddresses(host); #endif if (ipVersions == 0) { throw new ArgumentException("The ipVersions parameter must contain at least 1 flag."); } for (int i = 0; i < addresses.Length; i++) { // we don't need to do this check unless // a particular version of IP has been // omitted so we won't. if (ipVersions != FtpIpVersion.ANY) { switch (addresses[i].AddressFamily) { case AddressFamily.InterNetwork: if ((ipVersions & FtpIpVersion.IPv4) != FtpIpVersion.IPv4) { #if DEBUG FtpTrace.WriteStatus(FtpTraceLevel.Verbose, "Skipped IPV4 address : " + addresses[i].ToString()); #endif continue; } break; case AddressFamily.InterNetworkV6: if ((ipVersions & FtpIpVersion.IPv6) != FtpIpVersion.IPv6) { #if DEBUG FtpTrace.WriteStatus(FtpTraceLevel.Verbose, "Skipped IPV6 address : " + addresses[i].ToString()); #endif continue; } break; } } if (FtpTrace.LogIP) { FtpTrace.WriteStatus(FtpTraceLevel.Info, "Connecting to " + addresses[i].ToString() + ":" + port); } else { FtpTrace.WriteStatus(FtpTraceLevel.Info, "Connecting to ***:" + port); } m_socket = new Socket(addresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp); #if CORE m_socket.ConnectAsync(addresses[i], port).Wait(); #else ar = m_socket.BeginConnect(addresses[i], port, null, null); if (!ar.AsyncWaitHandle.WaitOne(m_connectTimeout, true)) { Close(); // check to see if we're out of addresses, and throw a TimeoutException if ((i + 1) == addresses.Length) { throw new TimeoutException("Timed out trying to connect!"); } } else { m_socket.EndConnect(ar); // we got a connection, break out // of the loop. break; } #endif } // make sure that we actually connected to // one of the addresses returned from GetHostAddresses() if (m_socket == null || !m_socket.Connected) { Close(); throw new IOException("Failed to connect to host."); } m_netStream = new NetworkStream(m_socket); m_lastActivity = DateTime.Now; }
/// <summary> Redefine connect for FtpClient : authentication on the Proxy </summary> /// <param name="stream">The socket stream.</param> protected override void Connect(FtpSocketStream stream, string host, int port, FtpIpVersion ipVersions) { stream.Connect(Proxy.Host, Proxy.Port, InternetProtocolVersions); }
/// <summary> /// Connect to the specified host /// </summary> /// <param name="host">The host to connect to</param> /// <param name="port">The port to connect to</param> /// <param name="ipVersions">Internet Protocol versions to support during the connection phase</param> public async Task ConnectAsync(string host, int port, FtpIpVersion ipVersions) { IPAddress[] addresses = await Dns.GetHostAddressesAsync(host); if (ipVersions == 0) { throw new ArgumentException("The ipVersions parameter must contain at least 1 flag."); } for (int i = 0; i < addresses.Length; i++) { // we don't need to do this check unless // a particular version of IP has been // omitted so we won't. if (ipVersions != FtpIpVersion.ANY) { switch (addresses[i].AddressFamily) { case AddressFamily.InterNetwork: if ((ipVersions & FtpIpVersion.IPv4) != FtpIpVersion.IPv4) { #if DEBUG this.Client.LogStatus(FtpTraceLevel.Verbose, "Skipped IPV4 address : " + addresses[i].ToString()); #endif continue; } break; case AddressFamily.InterNetworkV6: if ((ipVersions & FtpIpVersion.IPv6) != FtpIpVersion.IPv6) { #if DEBUG this.Client.LogStatus(FtpTraceLevel.Verbose, "Skipped IPV6 address : " + addresses[i].ToString()); #endif continue; } break; } } if (FtpTrace.LogIP) { this.Client.LogStatus(FtpTraceLevel.Info, "Connecting to " + addresses[i].ToString() + ":" + port); } else { this.Client.LogStatus(FtpTraceLevel.Info, "Connecting to ***:" + port); } m_socket = new Socket(addresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp); #if CORE await m_socket.ConnectAsync(addresses[i], port); break; #else var connectResult = m_socket.BeginConnect(addresses[i], port, null, null); await Task.Factory.FromAsync(connectResult, m_socket.EndConnect); break; #endif } // make sure that we actually connected to // one of the addresses returned from GetHostAddresses() if (m_socket == null || !m_socket.Connected) { Close(); throw new IOException("Failed to connect to host."); } m_netStream = new NetworkStream(m_socket); m_netStream.ReadTimeout = m_readTimeout; m_lastActivity = DateTime.Now; }
/// <summary> /// Connect to the specified host /// </summary> /// <param name="host">The host to connect to</param> /// <param name="port">The port to connect to</param> /// <param name="ipVersions">Internet Protocol versions to support durring the connection phase</param> public void Connect(string host, int port, FtpIpVersion ipVersions) { IAsyncResult ar = null; IPAddress[] addresses = Dns.GetHostAddresses(host); if (ipVersions == 0) throw new ArgumentException("The ipVersions parameter must contain at least 1 flag."); for (int i = 0; i < addresses.Length; i++) { #if DEBUG FtpTrace.WriteLine("{0}: {1}", addresses[i].AddressFamily.ToString(), addresses[i].ToString()); #endif // we don't need to do this check unless // a particular version of IP has been // omitted so we won't. if (ipVersions != FtpIpVersion.ANY) { switch (addresses[i].AddressFamily) { case AddressFamily.InterNetwork: if ((ipVersions & FtpIpVersion.IPv4) != FtpIpVersion.IPv4) { #if DEBUG FtpTrace.WriteLine("SKIPPED!"); #endif continue; } break; case AddressFamily.InterNetworkV6: if ((ipVersions & FtpIpVersion.IPv6) != FtpIpVersion.IPv6) { #if DEBUG FtpTrace.WriteLine("SKIPPED!"); #endif continue; } break; } } m_socket = new Socket(addresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp); ar = m_socket.BeginConnect(addresses[i], port, null, null); if (!ar.AsyncWaitHandle.WaitOne(m_connectTimeout, true)) { Close(); // check to see if we're out of addresses // and if we are throw a TimeoutException if (i + 1 == addresses.Length) throw new TimeoutException("Timed out trying to connect!"); } else { m_socket.EndConnect(ar); // we got a connection, break out // of the loop. break; } } // make sure that we actually connected to // one of the addresses returned from GetHostAddresses() if (m_socket == null || !m_socket.Connected) { Close(); throw new IOException("Failed to connect to host."); } m_netStream = new NetworkStream(m_socket); m_lastActivity = DateTime.Now; }
protected override void Connect(FtpSocketStream stream, string host, int port, FtpIpVersion ipVersions) { base.Connect(stream); var proxy = new SocksProxy(Host, port, stream); proxy.Negotiate(); proxy.Authenticate(); proxy.Connect(); }