开发者

How to make a Fully Async proxy (http 1.0 compiant not 1.1)

开发者 https://www.devze.com 2023-02-16 12:48 出处:网络
For a school project I am making a multi-client proxy that has to be compliant with http 1.0 and not 1.1(so that makes it easier). The teacher told me that it is better to make it fully async. So I ma

For a school project I am making a multi-client proxy that has to be compliant with http 1.0 and not 1.1(so that makes it easier). The teacher told me that it is better to make it fully async. So I made a fully async proxy, there is only one problem. It only works when I put a threadsleep in it, but this is not making it faster, but it's the only way to let it work. Please help me find a solution and maybe someone knows why it needs the threadsleep to let it work?

The teacher sees this problem every year and the only found solution is the threadsleep, so the teacher has not found a real solution.

First the simple code for the form. The form has a start button and a textbox to view the request and a textbox to view the responds. After the form follows the code for the proxy. By the way, in internet explorer you can switch to a http 1.0 mode, so that's the best way to test, also you need to make the browser listen to a proxyserver(listed in de code).

using System;
using System.Windows.Forms;
using System.Threading;

namespace Proxy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void startProxy_Click(object sender, EventArgs e)
        {
            var proxy = new Proxy(requestView, respondsView);
            var thread = new Thread(new ThreadStart(proxy.StartProxy));
            thread.IsBackground = true;
            thread.Start();
            startProxy.Enabled = false;
        }
    }
}

And now the proxy where the problem exists...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Proxy
{
    class Proxy
    {
        private TextBox requestView;
        private TextBox respondsView;
        private delegate void UpdateLogCallback(string strMessage, TextBox txtView);
        public const int PROXY_PORT = 5008;
        public const int WEB_PROXY_PORT = 80;
        public const int BACKLOG = 20;
        public const int TIMEOUT = 4000;

        public Proxy(TextBox _requestView, TextBox _respondsView)
        {
            requestView = _requestView;
            respondsView = _respondsView;
        }

        public void StartProxy()
        {
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Unspecified);
            clientSocket.Bind(new IPEndPoint(IPAddress.Any, PROXY_PORT));
            clientSocket.Listen(BACKLOG);
            clientSocket.BeginAccept(HandleConnection, clientSocket);
        }


        private void HandleConnection(IAsyncResult iar)
        {
            Socket clientSocket = iar.AsyncState as Socket;
            Socket client = clientSocket.EndAccept(iar);
            clientSocket.BeginAccept(HandleConnection, clientSocket);
            SocketData data = new SocketData() { SocketToClient = client };
            client.BeginReceive(data.buffer, 0, SocketData.BUFFER_SIZE, SocketFlags.None, OnDataArrived, data);
        }

        private void OnDataArrived(IAsyncResult iar)
        {
            SocketData socketdata = iar.AsyncState as SocketData;
            int bytesreceived = 0;
            UpdateLogCallback uLC = new UpdateLogCallback(ReceiveMessages);            
            socketdata.SocketToClient.ReceiveTimeout = TIMEOUT;
            try
            {
                bytesreceived = socketdata.SocketToClient.EndReceive(iar);
                if (bytesreceived == SocketData.BUFFER_SIZE)
                {
                    socketdata.sb.Append(ASCIIEncoding.ASCII.GetString(socketdata.buffer, 0, bytesreceived));
                    socketdata.SocketToClient.BeginReceive(socketdata.buffer, 0, SocketData.BUFFER_SIZE, SocketFlags.None, OnDataArrived, socketdata);
                }
                else
                {
                    socketdata.sb.Append(ASCIIEncoding.ASCII.GetString(socketdata.buffer, 0, bytesreceived));
                    string strContent = socketdata.sb.ToString();
                    string[] testing = strContent.Split(' ');
                    if (testing[0] == "CONNECT")
                    {
                        //this is to prevent weird request to microsoft servers(???) also prevents ssl request...                       
                    }
                    else
                    {
                        IPEndPoint ip = new IPEndPoint(Dns.GetHostEntry(GetHostnameFromRequest(strContent)).AddressList[0], WEB_PROXY_PORT);
                        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Unspecified);
                        server.Connect(ip);
                        requestView.Invoke(new UpdateLogCallback(uLC), new object[] { strContent, requestView });
                        server.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(strContent));
                        socketdata.SocketToServer = server;                        
                        server.BeginReceive(socketdata.buffer2, 0, SocketData.BUFFER_SIZE, SocketFlags.None, OnWebsiteDataArrived, socketdata);
                    }
                }
            }
            catch
            {
                socketdata.SocketToClient.Close();
            }
        }

        private void OnWebsiteDataArrived(IAsyncResult iar)
        {
            SocketData socketdata = iar.AsyncState as SocketData;
            int bytesreceived = 0;
            UpdateLogCallback uLC = new UpdateLogCallback(ReceiveMessages);
            socketdata.SocketToServer.ReceiveTimeout = TIMEOUT;

            try
            {
                bytesreceived = socketdata.SocketToServer.EndReceive(iar);
                Thread.Sleep(10);
                if (bytesreceived == SocketData.BUFFER_SIZE)
                {
                    socketdata.sb2.Append(ASCIIEncoding.ASCII.GetString(socketdata.buffer2, 0, bytesreceived));
                    socketdata.SocketToClient.S开发者_StackOverflow社区end(socketdata.buffer2, 0, SocketData.BUFFER_SIZE, SocketFlags.None);
                    socketdata.SocketToServer.BeginReceive(socketdata.buffer2, 0, SocketData.BUFFER_SIZE, SocketFlags.None, OnWebsiteDataArrived, socketdata);
                }
                else
                {
                    socketdata.sb2.Append(ASCIIEncoding.ASCII.GetString(socketdata.buffer2, 0, bytesreceived));
                    respondsView.Invoke(new UpdateLogCallback(uLC), new object[] { socketdata.sb2.ToString(), respondsView });
                    socketdata.SocketToClient.Send(socketdata.buffer2, 0, bytesreceived, SocketFlags.None);
                    socketdata.SocketToClient.Close();
                    socketdata.SocketToServer.Close();
                }
            }
            catch
            {
                socketdata.SocketToClient.Close();
            }
        }

        private static string GetHostnameFromRequest(string strContent)
        {
            string[] host = strContent.Split(new string[] { "\r\n", ": " }, StringSplitOptions.RemoveEmptyEntries);
            int check = Array.IndexOf(host, "Host");
            return host[check + 1];
        }

        public void ReceiveMessages(string receiveMessages, TextBox txtView)
        {
            if (txtView.InvokeRequired)
            {
                UpdateLogCallback uLC = new UpdateLogCallback(ReceiveMessages);
                txtView.Invoke(new UpdateLogCallback(uLC), new object[] { receiveMessages, txtView });
            }
            else
            {
                txtView.AppendText(receiveMessages);
            }
        }

        public class SocketData
        {
            public SocketData()
            {
                this.packetlenght = 0;
            }
            public Socket SocketToClient { get; set; }
            public Socket SocketToServer { get; set; }
            public StringBuilder sb = new StringBuilder();
            public StringBuilder sb2 = new StringBuilder();
            public const int BUFFER_SIZE = 128;
            public byte[] buffer = new byte[BUFFER_SIZE];
            public byte[] buffer2 = new byte[BUFFER_SIZE];
            public int packetlenght { get; set; }
        }
    }
}


From Socket.EndReceive:

The EndReceive method will block until data is available. If you are using a connectionless protocol, EndReceive will read the first enqueued datagram available in the incoming network buffer. If you are using a connection-oriented protocol, the EndReceive method will read as much data as is available up to the number of bytes you specified in the size parameter of the BeginReceive method.

I read that to imply that (if you're on a slow network), it may return a size < SocketData.BUFFER_SIZE at any time, not just when you've received the end of the message. So the delay probably adds enough time that the only time it returns < SocketData.BUFFER_SIZE is once the message is complete.


  1. You should go through your naming since it makes it hard to follow your code. As an example: clientSocket is not a very good name for a listening socket.

  2. Your exception handling is not good. At least log the exceptions.

  3. And you need to check if the number of received bytes is zero. It indicates that the remote socket has closed the connection.

  4. Your proxy thread will die directly since BeginAccept is not a blocking operation.

  5. I do not understand your if in OnDataArrived. Why the check to see if the number of bytes is the same as the buffer size? TCP do not guarantee anything when it comes to received data. A partially filled buffer do not mean that the message is complete. Continue to build the buffer until the number of body bytes is the same as the specified Content-Length.

  6. The same goes for OnWebsiteDataArrived. You are trying to use TCP in a way that it's not intended for. It's not message oriented. Keep building the buffer as suggested in #5.

0

精彩评论

暂无评论...
验证码 换一张
取 消