Why is my proxy unusably slow?

社会主义新天地 提交于 2019-12-12 01:14:55

问题


Its less <130 lines. Nothing about it is complex. Why is this code so slow that my proxy is unusable? It takes seconds to hit a webpage and it gets worse after hitting 2/3 domains.

The CPU is also through the roof which confuses me. Also I can't get above 350kb a second downloading a large file (i tested by downloading linux iso, one that i get >350 w/o the proxy)

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Threading;

namespace ProxyBox
{
    class Program
    {
        static ManualResetEvent tcpClientConnected =new ManualResetEvent(false);
        static void Main(string[] args)
        {
            var s2 = new TcpListener(9998);
            s2.Start();
            Task.Run(() =>
            {
                while (true)
                {
                    tcpClientConnected.Reset();
                    s2.BeginAcceptTcpClient(Blah, s2);
                    tcpClientConnected.WaitOne();
                }
            });
            while (true)
                System.Threading.Thread.Sleep(10000000);
        }

        static void Blah(IAsyncResult ar)
        {
            Console.WriteLine("Connection");
            TcpListener listener = (TcpListener)ar.AsyncState;
            using (var socketin = listener.EndAcceptTcpClient(ar))
            {
                tcpClientConnected.Set();
                var ns1 = socketin.GetStream();
                var r1 = new BinaryReader(ns1);
                var w1 = new BinaryWriter(ns1);

                {
                    if (!(r1.ReadByte() == 5 && r1.ReadByte() == 1))
                        return;
                    var c = r1.ReadByte();
                    for (int i = 0; i < c; ++i)
                        r1.ReadByte();
                    w1.Write((byte)5);
                    w1.Write((byte)0);
                }
                {
                    if (!(r1.ReadByte() == 5 && r1.ReadByte() == 1))
                        return;
                    if (r1.ReadByte() != 0)
                        return;
                }
                byte[] ipAddr = null;
                string hostname = null;
                var type=r1.ReadByte();
                switch (type)
                {
                    case 1:
                        ipAddr = r1.ReadBytes(4);
                        break;
                    case 3:
                        hostname = Encoding.ASCII.GetString(r1.ReadBytes(r1.ReadByte()));
                        break;
                    case 4:
                        throw new Exception();
                }
                var nhport=r1.ReadInt16();
                var port = IPAddress.NetworkToHostOrder(nhport);

                var socketout = new TcpClient();
                if (hostname != null)
                    socketout.Connect(hostname, port);
                else
                    socketout.Connect(new IPAddress(ipAddr), port);

                w1.Write((byte)5);
                w1.Write((byte)0);
                w1.Write((byte)0);
                w1.Write(type);
                switch (type)
                {
                    case 1:
                        w1.Write(ipAddr);
                        break;
                    case 2:
                        w1.Write((byte)hostname.Length);
                        w1.Write(Encoding.ASCII.GetBytes(hostname), 0, hostname.Length);
                        break;
                }
                w1.Write(nhport);

                var buf = new byte[4096];
                var ns2 = socketout.GetStream();
                var r2 = new BinaryReader(ns2);
                var w2 = new BinaryWriter(ns2);
                while (true)
                {
                    var a = true;
                    while (ns1.DataAvailable)
                    {
                        var len = r1.Read(buf, 0, buf.Length);
                        w2.Write(buf, 0, len);
                        a = false;
                    }
                    while (ns2.DataAvailable)
                    {
                        var len = r2.Read(buf, 0, buf.Length);
                        w1.Write(buf, 0, len);
                        a = false;
                    }
                    if (a)
                    {
                        if (socketout.Connected == false || socketin.Connected == false)
                            return;                        
                        System.Threading.Thread.Sleep(0);
                    }
                }
            }
        }
    }
}

来源:https://stackoverflow.com/questions/17769451/why-is-my-proxy-unusably-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!