Exemplo n.º 1
0
        private WebProxy GetProxy()
        {
            var pool = new ProxyPool();

            pool.GetProxysFromAPIs();
            WebProxy available          = null;
            CancellationTokenSource src = new CancellationTokenSource();
            var token = src.Token;

            new Thread(new ThreadStart(() =>
            {
                try
                {
                    pool.proxys.AsParallel().WithDegreeOfParallelism(64).WithCancellation(token).ForAll(proxy =>
                    {
                        if (available != null)
                        {
                            return;
                        }
                        try
                        {
                            var client = new HttpClient(new HttpClientHandler
                            {
                                Proxy = new WebProxy(proxy.ToString())
                            });

                            var result = client.GetAsync("http://production-game-api.sekai.colorfulpalette.org/api/system").Result;

                            if (result.StatusCode == HttpStatusCode.UnsupportedMediaType)
                            {
                                available = new WebProxy(proxy.ToString());
                                src.Cancel();
                            }
                            Console.WriteLine(result.StatusCode);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    });
                }
                catch { }
            })).Start();

            token.WaitHandle.WaitOne();

            Console.WriteLine(available.Address.ToString());
            return(available);
        }
Exemplo n.º 2
0
        public SekaiClient(EnvironmentInfo info, bool useProxy = false)
        {
            var headertype = typeof(HttpClient).Assembly.GetType("System.Net.Http.Headers.HttpHeaderType");

            environment = info;

            if (useProxy)
            {
                var pool = new ProxyPool();
                pool.GetProxysFromAPIs();
                foreach (var proxy in pool.proxys)
                {
                    try
                    {
                        client = new HttpClient(new HttpClientHandler
                        {
                            Proxy = new WebProxy(proxy.ToString())
                        });
                        client.Timeout = new TimeSpan(0, 0, 5);
                        typeof(HttpHeaders).GetField("_allowedHeaderTypes", BindingFlags.NonPublic | BindingFlags.Instance)
                        .SetValue(client.DefaultRequestHeaders, Enum.Parse(headertype, "All"));

                        SetupHeaders();
                        Register().Wait();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }

                throw new Exception("No proxy");
            }
            else
            {
                client = new HttpClient();
                typeof(HttpHeaders).GetField("_allowedHeaderTypes", BindingFlags.NonPublic | BindingFlags.Instance)
                .SetValue(client.DefaultRequestHeaders, Enum.Parse(headertype, "All"));
                SetupHeaders();
            }

            //client.DefaultRequestHeaders.TryAddWithoutValidation("Connection", "Keep-Alive");
        }