I am writing a proxy server and have no trouble with HTTP, but when it comes to HTTPS and SSL, I cannot get a tunnel working.
All I want to do is open a tunnel to the server so I don't have to get involved with certificates or use the
SslStream
.
The code uses a
TcpListener
to listen for requests on localhost port 444.
SSLtcplistener = new TcpListener(Local, 444);
When running the SSL request from the browser looks like:
CONNECT www.google.com:443 HTTP/1.0
User-Agent: Mozilla/4.0 (compatible; MSIE.........................
Host: www.google.com
Content-Length: 0
Proxy-Connection: Keep-Alive
Pragma: no-cache
Next I accept the connection.
Socket socket = SSLtcplistener.AcceptSocket();
and then extract the host string and port number from the SSL request and open a new socket to Google on port 433 like this:
IPEndPoint IPE = new IPEndPoint(Dns.Resolve(this.Connect).AddressList[0], this.Port);
this.Send = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.Send.Connect(IPE);
If the 'Send' socket is open, then I send this message back to the browsers:
HTTP/1.0 200 Connection Established\r\n
Proxy-agent: Netscape-Proxy/1.1\r\n\r\n
and close the browser connection expecting the browser to open another connection and to send encrypted data that I just need to relay back to the open send socket but the next request from the browsers is simply the same as the previous request and when I relay this to the Google server, I get nothing back.
I've looked at a few examples but they are in Java and use a different type of socket, so I'm a bit lost but the reason I want SSL to pass through the proxy is so that some sites can be banned.
Any help would be appreciated because I have spent a day getting nowhere.