1

Im writing a HTTP proxy in c# sockets. Proxy have to make changes in HTML files. Everything works fine until I replace text larger or smaller than originall word was. For example, if I replace "abcde" to "abcde" proxy works perfectly, but if I replace "abcde" to "ab" I got socket exception "An established connection was aborted by the software in your host machine" in socket.send method. I have disabled firewall and windows defender.

Accepting browser socket:

static void Main(string[] args)
    {
        TcpListener tcpl = new TcpListener(IPAddress.Parse("127.0.0.1"), 1024);

        tcpl.Start();

        while (true)
        {
            Socket sock = tcpl.AcceptSocket();
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

            RequestHandler rh = new RequestHandler(sock);

            rh.Handle();
        }
    }

Handle request:

public void Handle()
{
    string header = GetHeader(browserSocket, 1);
    string host = AnalyzeHeader(header);

    if (string.IsNullOrEmpty(header) || string.IsNullOrEmpty(host))
    {
        browserSocket.Close();
        return;
    }
    Socket destServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPAddress[] addresslist = Dns.GetHostAddresses(host);
    IPEndPoint remoteEP = new IPEndPoint (addresslist[0], 80);
    destServerSocket.Connect(remoteEP);

    SendRequest(destServerSocket, header);

    header = GetHeader(destServerSocket, 0);

    SendRequest(browserSocket, header);       
    int receivedBytes = 1;
    const int BUFSZ = 1024;
    byte[] buffer = new byte[BUFSZ];

    while (receivedBytes > 0)
    {
        if (destServerSocket.Poll(100000, SelectMode.SelectRead))
        {
            receivedBytes = destServerSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);

            if (header.Contains("Content-Type: text/html"))
            {
                string charset = GetCharset(header);
                charset = string.IsNullOrEmpty(charset) ? "UTF-8" : charset;
                var inputEncoding = Encoding.GetEncoding(charset);
                var text = inputEncoding.GetString(buffer);
                if (text.Contains("abcde"))
                {
                    text = text.Replace("abcde", "ab");
                    buffer = inputEncoding.GetBytes(text);
                    receivedBytes = buffer.Length;
                }

            }

            this.browserSocket.Send(buffer, receivedBytes, SocketFlags.None); //Exception 10053
        }
        else
        {
            receivedBytes = 0;
        }

    }

    this.browserSocket.Shutdown(SocketShutdown.Both);
    this.browserSocket.Close();
}

I have tried to add Content-Length to response header, but error still occur. What do you think the proble is? I've come up to nothing after days of research.

Makler
  • 51
  • 4
  • You have to create a new variable. You cannot change receiveBytes which is the receive buffer. – jdweng Jun 03 '17 at 10:02
  • I've created new variable but error unfortunately still occurs – Makler Jun 03 '17 at 15:29
  • Your send buffer and receive buffer are the same. This may be causing the issue. Try using two buffers. I don't think the send method is blocking so you start to receive data while sending. – jdweng Jun 03 '17 at 17:42
  • I have run my program on PC with windows 7 and everything works fine, but on laptop with windows 10 error still occur. – Makler Jun 03 '17 at 19:16
  • Same error : "An established connection was aborted by the software in your host machine"? Do you have all updates in PC? Same version of Net? Are you compiling on Win10 or just porting executable to PC? – jdweng Jun 04 '17 at 08:51
  • Yes, that error. On PC where everything works fine i don't have all updates, windows updates are disabled. Version .Net is the same.In previous test the server and client was on the same machine but I have made another test. I compiled program on laptop where error always occured but client (mozilla browser) was on my PC. Error didin't occur. It looks like, that it doesn't matter where the program is compiling, but where the client is. It is possible that windows 10 has some built in security mechanism to prevent changing content of html request? – Makler Jun 04 '17 at 10:44
  • You are missing a header : https://stackoverflow.com/questions/11841540/setting-the-user-agent-header-for-a-webclient-request – jdweng Jun 04 '17 at 10:57
  • I'm sending header to the browser in SendRequest(browserSocket, header); method. All header i got from destination server in: header = GetHeader(destServerSocket, 0); method. – Makler Jun 04 '17 at 14:09
  • Waht about : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) – jdweng Jun 04 '17 at 15:36
  • I downloaded User Agent Overrider for Mozilla and insert "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0" like u said, but nothing changed :( – Makler Jun 04 '17 at 19:03
  • I have a hunch but not sure. I suspect the newer server is defaults to http 1.1 while older machines are using http 1.0. This has nothing to do with size of data changing so also suspect that the size has nothing really to do with the error, just a coincidence.What I always recommend in these cases is to use a sniffer like wireshark or fiddler and compare good results with bad results.You can download these sniffers for free from web. The error messages in Net are often misleading. With sniffers you only need to look at html packets.Looks at headers and status. Make sure stats is 200 done. – jdweng Jun 04 '17 at 21:26
  • Also make sure you delete cookies from the IE settings. If you change code also make sure you go back and keep on deleting cookies. Code remembers old cookies and you can fix the code yet the cookies remember old code so it still doesn't work when it should. – jdweng Jun 04 '17 at 21:30
  • I couldn't find out what's wrong and i have to left it at this moment. Maybe i will try again later and download some sniffer like U said. Many thanks for your help. – Makler Jun 07 '17 at 13:56

0 Answers0