为什么我的代理速度非常慢?

时间:2013-07-21 05:41:10

标签: c# .net sockets proxy tcpclient

少于< 130行。没有什么是复杂的。为什么这段代码太慢而我的代理无法使用?点击网页需要几秒钟,而且在点击2/3域后会变得更糟。

CPU也是通过屋顶让我感到困惑。此外,我无法在每秒下载350kb以上的大文件(我通过下载linux iso进行测试,我得到的一个> 350 w / o代理)

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);
                    }
                }
            }
        }
    }
}

0 个答案:

没有答案