developing a proxy script

176 views Asked by At

in developing an open source proxy script, a piece of code's are below: static void Main(string[] args) { string host = "www.google.com"; int proxyPort = 443;//443;

            byte[] buffer = new byte[2048];
            int bytes;

            // Connect socket
            TcpClient client = new TcpClient(host, proxyPort);
            NetworkStream stream = client.GetStream();


            byte[] tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT www.google.com:443 HTTP/1.1\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; rv:23.0) Gecko/20100101 Firefox/23.0\r\nProxy-Connection: keep-alive\r\nConnection: keep-alive\r\nHost: www.google.com\r\n\r\n", host));
            stream.Write(tunnelRequest, 0, tunnelRequest.Length);
            stream.Flush();

            SslStream sslStream = new SslStream(stream);
            sslStream.AuthenticateAsClient(host);
      } 

when i run the code, occur an error in this line: sslStream.AuthenticateAsClient(host); the explanation of error is: unable to read data from the transport connection. an existing connection was forcibly closed by the remote host. or this error: Autentication failed because the remote party has closed the transport stream. please help me thanks

1

There are 1 answers

1
Eugene Mayevski 'Callback On

You seem to mix two concepts. CONNECT verb is used to tell the proxy that an OPAQUE tunnel must be built. In this case the proxy just forwards the data received from one side to the other side. You don't need any SSL in this case.

If you want to act as a proxy, i.e. receive client requests, parse them, do other things, then connect to the server for the resource, then you don't need to handle CONNECT verb - you instead process GET, HEAD, POST etc requests.

Update: I have written a small article that describes two types of proxies.