Exemplo n.º 1
0
        ///<summary>Called when we have received some data from the client.</summary>
        ///<param name="ar">The result of the asynchronous operation.</param>
        private void OnStartSocksProtocol(IAsyncResult ar)
        {
            int Ret;

            try
            {
                Ret = ClientSocket.EndReceive(ar);
                if (Ret <= 0)
                {
                    Dispose();
                    return;
                }
                if (Buffer[0] == 4)
                {
                    //SOCKS4 Protocol
                    if (MustAuthenticate)
                    {
                        Dispose();
                        return;
                    }
                    else
                    {
                        Handler = new Socks4Handler(ClientSocket, OnEndSocksProtocol);
                    }
                }
                else if (Buffer[0] == 5)
                {
                    //SOCKS5 Protocol
                    if (MustAuthenticate && AuthList == null)
                    {
                        Dispose();
                        return;
                    }
                    Handler = new Socks5Handler(ClientSocket, OnEndSocksProtocol, AuthList);
                }
                else
                {
                    Dispose();
                    return;
                }
                Handler.StartNegotiating();
            }
            catch
            {
                Dispose();
            }
        }
Exemplo n.º 2
0
        public override async Task InitializeAsync()
        {
            List <HttpClientProxy> proxies = new List <HttpClientProxy>();

            if (JsonArray != null)
            {
                foreach (JObject obj in JsonArray)
                {
                    string proxyType = obj["type"]?.Value <string>() ?? string.Empty;

                    if (proxyType.Equals("http", StringComparison.OrdinalIgnoreCase))
                    {
                        string url = obj["url"]?.Value <string>();

                        if (string.IsNullOrWhiteSpace(url))
                        {
                            throw new Exception("Proxy URL must be specified and not empty.");
                        }

                        string username = obj["username"]?.Value <string>();
                        string password = obj["password"]?.Value <string>();

                        IWebProxy proxy = username != null
                                                                                  ? new WebProxy(url, false, new string[0], new NetworkCredential(username, password))
                                                                                  : new WebProxy(url);

                        proxies.Add(new HttpClientProxy(CreateNewClient(proxy), $"{username}@{url}"));
                    }
                    else if (proxyType.Equals("socks", StringComparison.OrdinalIgnoreCase))
                    {
                        string url = obj["url"]?.Value <string>();

                        if (string.IsNullOrWhiteSpace(url))
                        {
                            throw new Exception("Proxy URL must be specified and not empty.");
                        }

                        Uri uri = new Uri(url);

                        string username = obj["username"]?.Value <string>();
                        string password = obj["password"]?.Value <string>();


                        var handler = new Socks5Handler(uri, username, password);
                        handler.ResolveDnsLocally = ResolveDnsLocally;
                        proxies.Add(new HttpClientProxy(CreateNewClient(handler), $"{username}@{uri.Host}:{uri.Port}"));
                    }
                    else
                    {
                        if (proxyType == string.Empty)
                        {
                            throw new Exception("Proxy type must be specified.");
                        }

                        throw new Exception($"Unknown proxy type: {proxyType}");
                    }
                }
            }

            // add a direct connection client too
            proxies.Add(new HttpClientProxy(CreateNewClient((IWebProxy)null), "baseconnection/none"));

            var testTasks = proxies.Select(proxy => Task.Run(async() =>
            {
                bool success = true;

                for (int i = 0; i < 4; i++)
                {
                    success = true;

                    try
                    {
                        var result = await proxy.Client.GetAsync("https://a.4cdn.org/po/catalog.json");

                        if (!result.IsSuccessStatusCode)
                        {
                            success = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        Program.Log($"{proxy.Name} failed: {ex}", true);

                        success = false;
                    }

                    if (success)
                    {
                        break;
                    }
                }

                if (success)
                {
                    Program.Log($"Proxy '{proxy.Name}' tested successfully");
                    ProxyClients.Add(proxy);
                    Interlocked.Increment(ref _proxyCount);
                }
                else
                {
                    Program.Log($"Proxy '{proxy.Name}' failed test, will be ignored");
                }
            }));

            await Task.WhenAll(testTasks);
        }