Exemplo n.º 1
0
        public bool IsProxyValidCandidate(
            ProxyDatum proxy,
            AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType,
            TimeSpan maximumConnectionTime,
            bool checkMaximumConnectionTime
            )
        {
            var isTested = IsProxyTested(proxy, addressFamily, socketType, protocolType);

            if (isTested)
            {
                var testElement = proxy.Tests[addressFamily][socketType][protocolType];

                if (checkMaximumConnectionTime)
                {
                    var maximumConnectionTimeInSeconds = maximumConnectionTime.TotalSeconds;

                    return(testElement
                           .Any(w => w.Value >= 0 && w.Value <= maximumConnectionTimeInSeconds));
                }

                return(testElement
                       .Any(w => w.Value >= 0));
            }

            return(false);
        }
Exemplo n.º 2
0
        public ProxyDatum[] GetRandomChainedProxy(IList <ProxyDatum> proxies, int length)
        {
            var proxiesCount = proxies.Count;

            if (proxiesCount == 0)
            {
                return(null);
            }

            if (length > proxies.Count)
            {
                length = proxies.Count;
            }

            ProxyDatum[] chain = new ProxyDatum[length];

            var randomIndexes = uniqueRandom.GenerateRandom(length, 0, proxies.Count);

            for (int idx = 0; idx < length; ++idx)
            {
                chain[idx] = proxies[randomIndexes[idx]];
            }

            return(chain);
        }
Exemplo n.º 3
0
        public static double GetTestedProxyResponseTime(this ProxyDatum proxy, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            var selected = proxy.Tests[addressFamily][socketType][protocolType]
                           .Where(w => w.Value >= 0)
                           .OrderBy(w => w.Value)
                           .FirstOrDefault();

            return(selected.Value);
        }
Exemplo n.º 4
0
        public static ProxyTypes GetTestedProxyType(this ProxyDatum proxy, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            var selected = proxy.Tests[addressFamily][socketType][protocolType]
                           .Where(w => w.Value >= 0 && w.Value <= ProxyTester.MaximumConnectionTime.TotalSeconds)
                           .OrderBy(w => w.Value)
                           .FirstOrDefault();

            return(selected.Key);
        }
Exemplo n.º 5
0
        void DoWorkInternal(object actionState)
        {
            ProxyDatum proxy = actionState as ProxyDatum;

            //var addressFamily = AddressFamily.InterNetwork;
            var socketType   = SocketType.Stream;
            var protocolType = ProtocolType.Tcp;

            bool isProxyInvalid = true;

            lock (State)
            {
                ++State.Testing;
            }

            foreach (var proxyTypeToTest in proxyTypesToTest)
            {
                bool isProxySocketValid = ProbeProxy(proxy, socketType, protocolType, proxyTypeToTest, MaximumConnectionTime);

                SpinProxiesApiWrapper.WriteTempContent(spinProxiesData, SpinProxiesProxyClass.Socks5);

                isProxyInvalid = (isProxyInvalid && !isProxySocketValid);

                if (isProxySocketValid)
                {
                    lock (State)
                    {
                        ++State.Available;
                    }

                    lock (availableProxiesList)
                    {
                        availableProxiesList.Add(proxy);
                    }

                    break;
                }
            }

            lock (State)
            {
                ++State.Tested;
            }

            if (isProxyInvalid)
            {
                lock (State)
                {
                    ++State.Failed;
                }
            }
        }
Exemplo n.º 6
0
 public bool IsProxyValidCandidate(
     ProxyDatum proxy,
     AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType,
     TimeSpan maximumConnectionTime
     )
 {
     return(IsProxyValidCandidate(
                proxy,
                addressFamily, socketType, protocolType,
                maximumConnectionTime,
                true
                ));
 }
Exemplo n.º 7
0
        public ChainedProxySocket AddChainedProxy(ProxyDatum proxy, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, string proxyUsername, string proxyPassword)
        {
            // Create a new ChainedProxySocket
            var chainedProxySocket = new ChainedProxySocket(this, addressFamily, socketType, protocolType, proxyUsername, proxyPassword);

            chainedProxySocket.ProxyDatum = proxy;

            // Set the proxy settings
            chainedProxySocket.ProxyEndPoint = new IPEndPoint(IPAddress.Parse(proxy.Ip), proxy.Port);
            chainedProxySocket.ProxyType     = proxy.GetTestedProxyType(addressFamily, socketType, protocolType);

            return(chainedProxySocket);
        }
Exemplo n.º 8
0
 public bool IsProxyTested(ProxyDatum proxy, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
 {
     lock (proxy)
     {
         return
             (!(
                  proxy.Tests == null ||
                  !proxy.Tests.ContainsKey(addressFamily) ||
                  proxy.Tests[addressFamily] == null ||
                  !proxy.Tests[addressFamily].ContainsKey(socketType) ||
                  proxy.Tests[addressFamily][socketType] == null ||
                  !proxy.Tests[addressFamily][socketType].ContainsKey(protocolType) ||
                  proxy.Tests[addressFamily][socketType][protocolType] == null ||
                  !(proxy.Tests[addressFamily][socketType][protocolType].Count != 0)
                  ));
     }
 }
Exemplo n.º 9
0
        internal static void SetTestValidity(
            ProxyDatum proxy,
            AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, ProxyTypes proxyType,
            TimeSpan connectionTime
            )
        {
            lock (proxy)
            {
                if (null == proxy.Tests)
                {
                    proxy.Tests = new Dictionary <AddressFamily, Dictionary <SocketType, Dictionary <ProtocolType, Dictionary <ProxyTypes, double> > > >();
                }

                if (!proxy.Tests.ContainsKey(addressFamily))
                {
                    proxy.Tests[addressFamily] = new Dictionary <SocketType, Dictionary <ProtocolType, Dictionary <ProxyTypes, double> > >();
                }

                if (!proxy.Tests[addressFamily].ContainsKey(socketType))
                {
                    proxy.Tests[addressFamily][socketType] = new Dictionary <ProtocolType, Dictionary <ProxyTypes, double> >();
                }

                if (!proxy.Tests[addressFamily][socketType].ContainsKey(protocolType))
                {
                    proxy.Tests[addressFamily][socketType][protocolType] = new Dictionary <ProxyTypes, double>();
                }

                if (!proxy.Tests[addressFamily][socketType][protocolType].ContainsKey(proxyType))
                {
                    proxy.Tests[addressFamily][socketType][protocolType].Add(proxyType, connectionTime.TotalSeconds);
                }
                else
                {
                    proxy.Tests[addressFamily][socketType][protocolType][proxyType] = connectionTime.TotalSeconds;
                }
            }
        }
Exemplo n.º 10
0
        private bool ProbeProxySocketImplementation(
            ProxyDatum proxy,
            SocketType socketType, ProtocolType protocolType, ProxyTypes proxyType,
            out AddressFamily addressFamily,
            out string errorMsg, out TimeSpan connectionSeconds
            )
        {
            Stopwatch stopWatch = new Stopwatch();

            addressFamily     = AddressFamily.Unspecified;
            errorMsg          = "";
            connectionSeconds = InvalidProxyConnectionTime;

            try
            {
                //stopWatch.Start();

                byte[] recvBuf = new byte[128];

                // TODO: Get IP from DNS
                IPEndPoint RHost = new IPEndPoint(IPAddress.Parse("104.20.2.47"), 80);
                addressFamily = RHost.AddressFamily;

                using (ProxySocket proxySocket = new ProxySocket(RHost.AddressFamily, socketType, protocolType))
                {
                    proxySocket.ProxyEndPoint = new IPEndPoint(IPAddress.Parse(proxy.Ip), proxy.Port);
                    proxySocket.ProxyType     = proxyType;

                    proxySocket.NoDelay = true;

                    stopWatch.Start();

                    try
                    {
                        proxySocket.Connect(RHost);
                    }
                    catch (SocketException) { return(false); }

                    proxySocket.Blocking = false;

                    try
                    {
                        proxySocket.Send(probeProxyHeaderBuffer, SocketFlags.None);

                        // TODO : Review

                        /*
                         * if (!proxySocket.Poll((int)connectionSeconds.TotalMilliseconds * 1000, SelectMode.SelectRead))
                         * {
                         *      return false;
                         * }
                         * //*/
                        /*
                         * if (proxySocket.Blocking)
                         * {
                         *      proxySocket.ReceiveTimeout = 10000;
                         *      proxySocket.Receive(recvBuf, recvBuf.Length, SocketFlags.None);
                         * }
                         * //*/
                    }
                    catch (SocketException) { return(false); }
                }

                return(true);
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
                return(false);
            }
            finally
            {
                stopWatch.Stop();
                connectionSeconds = stopWatch.Elapsed;
            }
        }
Exemplo n.º 11
0
        public bool ProbeProxy(
            ProxyDatum proxy,
            SocketType socketType, ProtocolType protocolType, ProxyTypes proxyType,
            TimeSpan maximumConnectionTime
            )
        {
            var           success       = false;
            AddressFamily addressFamily = AddressFamily.Unspecified;

            var parentThread = Thread.CurrentThread;

            var task = Task.Factory.StartNew((object parentThreadName) =>
            {
                var currentThread  = Thread.CurrentThread;
                currentThread.Name = $"{parentThreadName} -> TestProxySocket-{currentThread.ManagedThreadId}";

                /*
                 * Logger.Log.Debug(new
                 * {
                 *      Message = "Testing proxy",
                 *      proxy.Ip,
                 *      proxy.Port,
                 *      SocketType = socketType,
                 *      ProtocolType = protocolType,
                 *      ProxyType = proxyType
                 * });
                 */

                success = ProbeProxySocketImplementation(proxy, socketType, protocolType, proxyType, out addressFamily, out string errorMsg, out TimeSpan connectionTime);

                Logger.Log.Info(new
                {
                    Message = "Proxy tested",
                    Success = success,
                    proxy.Ip,
                    proxy.Port,
                    AddressFamily  = addressFamily,
                    SocketType     = socketType,
                    ProtocolType   = protocolType,
                    ProxyType      = proxyType,
                    ConnectionTime = connectionTime,
                    ErrorMessage   = errorMsg
                });

                var testedConnectionTime = InvalidProxyConnectionTime;

                if (success)
                {
                    //testedConnectionTime = (connectionTime <= maximumConnectionTime ? connectionTime : -connectionTime);
                    testedConnectionTime = connectionTime;
                }

                SetTestValidity(proxy, addressFamily, socketType, protocolType, proxyType, testedConnectionTime);
            }, parentThread.Name);

            // TODO : Use as multiplier 1.25
            if (!task.Wait(TimeSpan.FromSeconds(maximumConnectionTime.TotalSeconds * 2)))
            {
                SetTestValidity(proxy, addressFamily, socketType, protocolType, proxyType, InvalidProxyConnectionTime);

                return(false);
            }

            //WriteMasterContent(masterProxyListFilePath, storedData);

            return(success);
        }
Exemplo n.º 12
0
 public ChainedProxySocket AddChainedProxy(ProxyDatum proxy, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, string proxyUsername)
 {
     return(AddChainedProxy(proxy, addressFamily, socketType, protocolType, proxyUsername, String.Empty));
 }