private Socket SocksProxyConnect(Socket socket, IPEndPoint target) { try { var proxyClient = new Socks4aProxyClient(new TcpClient { Client = socket }); var tcpClient = proxyClient.CreateConnection(target.Address.ToString(), target.Port); return(tcpClient.Client); } catch (ProxyException) { throw new ProxyRouteException(new ProxyServer(ProxyProtocol.HttpConnect, target.Address, target.Port)); } }
public void TestSocks4aCreateConnection(string proxyHost, int proxyPort, string destHost, int destPort) { Socks4aProxyClient p = new Socks4aProxyClient(); p.ProxyHost = proxyHost; p.ProxyPort = proxyPort; TcpClient c = p.CreateConnection(destHost, destPort); byte[] sendBuf = System.Text.ASCIIEncoding.ASCII.GetBytes("GET / HTTP/1.1\n"); c.GetStream().Write(sendBuf, 0, sendBuf.Length); byte[] recvBuf = new byte[1024]; c.GetStream().Read(recvBuf, 0, recvBuf.Length); Console.Out.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(recvBuf)); c.Close(); }
protected System.Net.Sockets.TcpClient ProxyHandler(string address, int port) { IProxyClient proxy; switch (ProxyType) { case ProxyType.Socks5: proxy = new Socks5ProxyClient(ProxyHost, ProxyPort); break; case ProxyType.Socks4: proxy = new Socks4ProxyClient(ProxyHost, ProxyPort); break; case ProxyType.Socks4a: proxy = new Socks4aProxyClient(ProxyHost, ProxyPort); break; default: proxy = null; break; } return(proxy?.CreateConnection(address, port)); }
public ConnectionBase CreateConnection(string uri) { Socket socket = null; ConnectionBase connection = null; try { if (this.ConnectionType == ConnectionType.Tcp) { #if !DEBUG { Regex regex = new Regex(@"(.*?):(.*):(\d*)"); var match = regex.Match(uri); Uri url = new Uri(string.Format("{0}://{1}:{2}", match.Groups[1], match.Groups[2], match.Groups[3])); if (url.HostNameType == UriHostNameType.IPv4) { var myIpAddress = IPAddress.Parse(url.Host); if (IPAddress.Any.ToString() == myIpAddress.ToString() || IPAddress.Loopback.ToString() == myIpAddress.ToString() || IPAddress.Broadcast.ToString() == myIpAddress.ToString()) { return(null); } if (Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("10.0.0.0").GetAddressBytes()) >= 0 && Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("10.255.255.255").GetAddressBytes()) <= 0) { return(null); } if (Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("172.16.0.0").GetAddressBytes()) >= 0 && Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("172.31.255.255").GetAddressBytes()) <= 0) { return(null); } if (Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("127.0.0.0").GetAddressBytes()) >= 0 && Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("127.255.255.255").GetAddressBytes()) <= 0) { return(null); } if (Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("192.168.0.0").GetAddressBytes()) >= 0 && Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("192.168.255.255").GetAddressBytes()) <= 0) { return(null); } } else if (url.HostNameType == UriHostNameType.IPv6) { var myIpAddress = IPAddress.Parse(url.Host); if (IPAddress.IPv6Any.ToString() == myIpAddress.ToString() || IPAddress.IPv6Loopback.ToString() == myIpAddress.ToString() || IPAddress.IPv6None.ToString() == myIpAddress.ToString()) { return(null); } if (myIpAddress.ToString().ToLower().StartsWith("fe80:")) { return(null); } } } #endif connection = new TcpConnection(SessionManager.Connect(SessionManager.GetIpEndPoint(uri), new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager); } else if (this.ConnectionType == ConnectionType.Socks4Proxy) { Regex regex = new Regex(@"(.*?):(.*):(\d*)"); var match = regex.Match(uri); if (!match.Success) { return(null); } socket = SessionManager.Connect(SessionManager.GetIpEndPoint(this.ProxyUri), new TimeSpan(0, 0, 10)); var proxy = new Socks4ProxyClient(socket, match.Groups[2].Value, int.Parse(match.Groups[3].Value)); connection = new TcpConnection(proxy.CreateConnection(new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager); } else if (this.ConnectionType == ConnectionType.Socks4aProxy) { Regex regex = new Regex(@"(.*?):(.*):(\d*)"); var match = regex.Match(uri); if (!match.Success) { return(null); } socket = SessionManager.Connect(SessionManager.GetIpEndPoint(this.ProxyUri), new TimeSpan(0, 0, 10)); var proxy = new Socks4aProxyClient(socket, match.Groups[2].Value, int.Parse(match.Groups[3].Value)); connection = new TcpConnection(proxy.CreateConnection(new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager); } else if (this.ConnectionType == ConnectionType.Socks5Proxy) { Regex regex = new Regex(@"(.*?):(.*):(\d*)"); var match = regex.Match(uri); if (!match.Success) { return(null); } socket = SessionManager.Connect(SessionManager.GetIpEndPoint(this.ProxyUri), new TimeSpan(0, 0, 10)); var proxy = new Socks5ProxyClient(socket, match.Groups[2].Value, int.Parse(match.Groups[3].Value)); connection = new TcpConnection(proxy.CreateConnection(new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager); } else if (this.ConnectionType == ConnectionType.HttpProxy) { Regex regex = new Regex(@"(.*?):(.*):(\d*)"); var match = regex.Match(uri); if (!match.Success) { return(null); } socket = SessionManager.Connect(SessionManager.GetIpEndPoint(this.ProxyUri), new TimeSpan(0, 0, 10)); var proxy = new HttpProxyClient(socket, match.Groups[2].Value, int.Parse(match.Groups[3].Value)); connection = new TcpConnection(proxy.CreateConnection(new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager); } var secureConnection = new SecureClientConnection(connection, null, _bufferManager); secureConnection.Connect(new TimeSpan(0, 0, 20)); return(new CompressConnection(secureConnection, SessionManager.MaxReceiveCount, _bufferManager)); } catch (Exception) { if (socket != null) { socket.Close(); } if (connection != null) { connection.Dispose(); } } return(null); }