示例#1
0
        public void TestSocks4CreateConnection(string proxyHost, int proxyPort, string destHost, int destPort)
        {
            Socks4ProxyClient p = new Socks4ProxyClient();

            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();
        }
示例#2
0
        public static ProxyWithType CheckProxy(string IP, int port, int timeout)
        {
            string connectionTestIPAddress  = ConfigurationManager.AppSettings["ConnectionTestIP"];
            string connectionTestWebAddress = ConfigurationManager.AppSettings["ConnectionTestWebAddress"];
            string connectionTestPort       = ConfigurationManager.AppSettings["ConnectionTestPort"];

            ProxyWithType proxyWithType = new ProxyWithType();

            proxyWithType.IP        = IP;
            proxyWithType.Port      = port.ToString();
            proxyWithType.ProxyType = Model.ProxyType.Dead;

            TaskFactory taskFactory = new TaskFactory();
            List <Task> tasks       = new List <Task>();

            tasks.Add(taskFactory.StartNew(delegate
            {
                try
                {
                    Socks4ProxyClient proxyClient4 = new Socks4ProxyClient(IP, port);
                    proxyClient4.CreateConnection(connectionTestIPAddress, int.Parse(connectionTestPort));
                    proxyWithType.ProxyType = Model.ProxyType.Socks4;
                }
                catch (Exception)
                {
                    //If proxy timeouts don't do anything
                }
            }));
            tasks.Add(taskFactory.StartNew(delegate
            {
                try
                {
                    Socks5ProxyClient proxyClient5 = new Socks5ProxyClient(IP, port);
                    proxyClient5.CreateConnection(connectionTestIPAddress, int.Parse(connectionTestPort));
                    proxyWithType.ProxyType = Model.ProxyType.Socks5;
                }
                catch (Exception)
                {
                    //If proxy timeouts don't do anything
                }
            }));
            tasks.Add(taskFactory.StartNew(delegate
            {
                try
                {
                    WebClient wc = new WebClient();
                    wc.Proxy     = new WebProxy(IP, port);
                    wc.DownloadString(connectionTestWebAddress);
                    proxyWithType.ProxyType = Model.ProxyType.HTTP;
                }
                catch (Exception)
                {
                    //If proxy timeouts don't do anything
                }
            }));
            if (timeout > 0)
            {
                Task.WaitAll(tasks.ToArray(), timeout);
            }
            else
            {
                Task.WaitAll(tasks.ToArray());
            }

            return(proxyWithType);
        }
示例#3
0
        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);
        }