[C#] Socket connect using a http proxy server

Discussion in 'Mixed Languages' started by FreeStyler, Dec 18, 2012.

  1. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,563
    3,848
    120
    Anyone knows if it is possible to create a socket connection in C# using a (public) http proxy server?
     
  2. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,329
    1,378
    90
    Not sure what you mean exactly. Connect to a proxy server using c# to download?

    Check HttpWebRequest.Proxy
     
  3. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,563
    3,848
    120
    #3 FreeStyler, Dec 18, 2012
    Last edited: Dec 18, 2012
    (OP)
    Nope, not exactly

    I am working on a asp.net page that checks if certain ports are open/listening
    But coming from my own IP it reports some ports open that should be closed to the outside (blocked by firewall, ip filter)
    So i was thinking if i could connect through a proxy it shouldn't report blocked ports being open for proxy ip
     
  4. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,329
    1,378
    90
    #4 BobSheep, Dec 18, 2012
    Last edited: Dec 18, 2012
    Is the code executed server or client side?

    It's server. I Checked. Any firewall exceptions from the server to your IP?
     
  5. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,563
    3,848
    120
    #5 FreeStyler, Dec 18, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Server, eg:

    Code:
        public static bool ScanPort(string host, int port, int type)
        {
            bool open = false;
            try
            {
                IPAddress ip = (IPAddress)Dns.GetHostAddresses(host)[0];
                Socket sock;
                if (type == 2)
                {
                    sock = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Udp);
                }
                else
                {
                    sock = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                }
                IAsyncResult result = sock.BeginConnect(ip, port, null, null);
                bool success = result.AsyncWaitHandle.WaitOne(3000, true);
                if (!success)
                {// Port is unused and could not establish connection 
                    open = false;
                }
                else
                {// Port is open
                    open = true;
                }
                sock.Close();
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                if (ex.ErrorCode == 10061)
                {// Port is unused and could not establish connection 
                    open = false;
                }
                else
                {
                    open = false;
                }
            }
            return open;
        }
     
  6. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,329
    1,378
    90
    Well. First of all UDP is connectionless so you can't check that.
    Are the ports which appear open UDP?

    Also Open is not the same as Listening. If the port is open but there is no socket waiting for connections on that port it will not appear open.
     
  7. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,563
    3,848
    120
    #7 FreeStyler, Dec 18, 2012
    Last edited: Dec 18, 2012
    (OP)
    ok ditched the UDP part...

    Example: port 110 / 995 should be closed from outside, but appears open/listening (because my ip is allowed)
     
  8. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,329
    1,378
    90
    #8 BobSheep, Dec 18, 2012
    Last edited: Dec 18, 2012
    pm. the ip you're checking. I'll check from here. Soz. you gave the host. I'll check and report back.

    Open. I'll check with a scanner from work.

    I'd guess there is a rule from the server to your host which is allowed.
     
  9. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,563
    3,848
    120
    Yep...therefor i thought to go through proxy
     
  10. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,329
    1,378
    90
    I understand now. In that case the answer is no. Proxies only support HTTP protocols and there are special http headers written to tell the proxy the endpoint http address. A socket proxy does not exist. What you need is a port forwarder but that means it must be configured to accept connections and make them to the endpoint.