Пример #1
0
        /// <summary>
        /// Tests the connectivity to ifconfig.me
        /// </summary>
        public static bool TestConnectivityToIfconfigMe(CheckProxyIsWorkingEventArgs e)
        {
            using (ProxyTestWebClient wc = new ProxyTestWebClient())
            {
                wc.Headers.Add("User-Agent", SettingsManager.Instance.settings.HTTPUserAgent);
                // TODO figure out why nat'd browsers cause REMOTE_ADDRESS in PHP to show
                // nat IP addresss...... I distinctly remember there being something like that...
                //wc.Headers.Add ("X-Forward-For", "192.168.0.100");
                //wc.Headers.Add ("X-Real-IP", "8.8.8.8");
                wc.Proxy = new WebProxy(e.httpp.url, e.httpp.port);

                using (XmlReader xr = XmlReader.Create(wc.OpenRead("http://ifconfig.me/all.xml")))
                {
                    var serializer = new XmlSerializer(typeof(Netcrave.ifconfig.me.schema.info));

                    Netcrave.ifconfig.me.schema.info result =
                        (Netcrave.ifconfig.me.schema.info)serializer.Deserialize(xr);

                    if (!string.IsNullOrEmpty(result.ip_addr))
                    {
                        //ProxyManager.instance.Log.WriteLine("found proxy, ifconfig.me ipaddr: " + result.ip_addr);
                        e.httpp.IfconfigMeInfo = result;
                        return(true);
                    }
                }
            }
            //ProxyManager.instance.Log.WriteLine("failed to connect to ifconfig.me");
            return(false);
        }
Пример #2
0
        /// <summary>
        /// Main worker thread, gets working http proxies, and rechecks them when finished, then starts over.
        /// </summary>
        /// <returns>
        /// The working proxy.
        /// </returns>
        private void GetWorkingHttpProxies()
        {
            bool recheckingMode = false;

begin:

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            TimeSpan ts          = stopWatch.Elapsed;
            string   elapsedTime = "";

            if (HTTPProxies.Count() == 0)
            {
                LoadHTTPProxiesFromRSSFeed();
            }

            ParallelOptions po = new ParallelOptions
            {
                MaxDegreeOfParallelism = SettingsManager.Instance.settings.MaxThreads
            };

            Parallel.ForEach(HTTPProxies, po, prx =>
            {
                Log.WriteLine("GetWorkingHttpProxy new thread");

                try
                {
                    CheckProxyIsWorkingEventArgs args = new CheckProxyIsWorkingEventArgs {
                        handled = false, httpp = prx
                    };
                    CheckingIfProxyIsWorking(this, args);

                    if (args.handled)
                    {
                        if (!recheckingMode)
                        {
                            Log.WriteLine("Proxy passed all tests: " + prx.url + ":" + prx.port.ToString());
                            AddProxyToUsableList(prx);
                        }

                        else
                        {
                            prx.enabled = true;
                        }
                    }

                    else
                    {
                        if (recheckingMode)
                        {
                            prx.enabled = false;
                        }
                    }
                }

                catch (Exception ex)
                {
                    Log.WriteLine("unknown error: " + ex.Message);
                }
            });

            stopWatch.Stop();
            ts = stopWatch.Elapsed;

            elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                        ts.Hours, ts.Minutes, ts.Seconds,
                                        ts.Milliseconds / 10);

            Log.WriteLine("finished parsing proxies: " + elapsedTime);



            // Re-check proxies, disable non functioning
            if (!recheckingMode)
            {
                recheckingMode = true;

                lock (syncRoot)
                {
                    HTTPProxies = CheckedProxies;
                    goto begin;
                }
            }

            // save, should probably make this an event
            this.SaveTestedProxiesToSquidCachePeerFormat();

            // rechecked, find more
            lock (syncRoot)
            {
                HTTPProxies = new List <HTTPProxy>();
            }

            recheckingMode = false;
            LoadHTTPProxiesFromRSSFeed();
            goto begin;
        }