0

I am trying to implement HTTP+HTTPS+POP3+IMAP proxy server. As of now i have implemented HTTP and HTTPS proxy. But while trying to use HTTPS proxy, i Have the following exception: an existing connection was forcibly closed by remote host

Here's my code that handles socket from TCP listener:

 static void PerformConnection(object o)
    {
        try
        {


            Socket receiver = o as Socket;
            if (receiver != null)
            {
                NetworkStream ns = new NetworkStream(socket: receiver);
                byte[] buffer = new byte[450000];
                ns.Read(buffer, 0, Convert.ToInt32(450000));
                string data = Encoding.UTF8.GetString(buffer);
                string line = data.Replace("\r\n", "\n").Split(new string[] {"\n"}, StringSplitOptions.None)[0];
                if (line != "" && line != null)
                {
                    Uri uri = new Uri("http://someforbiddenhost.org");

                    uri = new Uri(line.Split(new string[] {" "}, StringSplitOptions.None)[1]);
                    if (uri.Host.Contains("shelesta.org"))
                    {
                        string Html = "<html><body><h1>It works!</h1></body></html>";
                        string response = "HTTP/1.1 200 OK\nContent-type: text/html\nContent-Length:" + Html.Length.ToString() + "\n\n" + Html;
                        receiver.Send(Encoding.UTF8.GetBytes(response));
                    }
                    else
                    {
                        if (line.StartsWith("CONNECT"))
                        {

                            string host = uri.ToString().Substring(0, uri.ToString().IndexOf(':'));
                            TcpClient clDestination = new TcpClient();
                            clDestination.Connect(host, 443);
                            SslStream sslStream = new SslStream(clDestination.GetStream());


                            sslStream.AuthenticateAsClient(host);  

                            byte[] request = Encoding.UTF8.GetBytes(String.Format("GET https://{0}:443/  HTTP/1.1\r\nHost: {0}\r\n\r\n", host));
                            sslStream.Write(request, 0, request.Length);
                            sslStream.Flush();
                            int bytes = 10;
                            receiver.ReceiveTimeout = 6000000;
                            do
                            {
                                bytes = sslStream.Read(buffer, 0, buffer.Length);
                                Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
                                receiver.Send(buffer,buffer.Length,SocketFlags.Partial);// HERE EXCEPTION RAISES

                            } while (bytes != 0); 



                        }
                        else
                        {
                            IPHostEntry rh = Dns.GetHostEntry(uri.Host);
                            Socket webserver = new Socket(rh.AddressList[0].AddressFamily, SocketType.Stream,
                                                          ProtocolType.IP);
                            webserver.Connect(new IPEndPoint(rh.AddressList[0], 80));
                            byte[] databytes = Encoding.ASCII.GetBytes(data);
                            webserver.Send(databytes, databytes.Length, SocketFlags.None);
                            byte[] ReceiverBuffer = new byte[2000048];
                            webserver.Receive(ReceiverBuffer);
                            receiver.Send(ReceiverBuffer); 
                        }
                    }
                }
            }
        }
        catch
        {

        }
    }

And sorry about code formatting, I can't see SO WYSIWYG editor, just message: Stack Overflow requires external JavaScript from another domain, which is blocked or failed to load.

seeker
  • 3,255
  • 7
  • 36
  • 68
  • Is your problem similar? http://stackoverflow.com/questions/3389401/c-sharp-an-existing-connection-was-forcibly-closed-by-the-remote-host-socket-pr – user35443 Dec 16 '12 at 09:06
  • Try to narrow it down to particular host/ip and specific lines of your code. Right now it sounds like "oh, I made some mess here, please clean it up". – Nikolai Fetissov Dec 16 '12 at 16:29
  • 'line != "" && line != null' is back to front. Check your code for other similar errors. – user207421 Dec 17 '12 at 21:11

0 Answers0