mercredi 6 avril 2016

C# Test if proxies are working before using them

I want to check if the the proxies in my list are working before using them, is it possible?

It's easy for HTTP/HTTPS since you only have to use the webclient but for socks?

I tought this could work for all

public static bool SoketConnect(string addresse)
    {
        string[] proxy = addresse.Split(':');
        if (proxy.Count() == 2)
        {
            string host = proxy[0];
            int port = Convert.ToInt32(proxy[1]);

            var is_success = false;
            try
            {
                var connsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                connsock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 200);
                System.Threading.Thread.Sleep(500);
                var hip = IPAddress.Parse(host);
                var ipep = new IPEndPoint(hip, port);
                connsock.Connect(ipep);
                if (SocketConnected(connsock))
                {
                    try
                    {
                         //Connecting with socks proxy?
                    }
                    catch
                    {
                        is_success = false;
                    }
                }
                connsock.Close();
            }
            catch (Exception)
            {
                is_success = false;
            }
            return is_success;
        }
        else
            return false;
    }

but this method :

public static bool SocketConnected(Socket s)
    {
        return !(s.Poll(0, SelectMode.SelectRead) && s.Available == 0);
    }

return true even if the internet is disconnected.. ( not working )

tldr; How to check if my socks proxies are working ( Which comes back to how to use a socks proxy )

Aucun commentaire:

Enregistrer un commentaire