コード例 #1
0
ファイル: Socks4Handler.cs プロジェクト: hepper/ProxyKit
        protected override void ProcessRequest(NetworkStream stream)
        {
            try
            {
                var reader = new BinaryReader(stream);
                var type   = (CommandType)reader.ReadByte();
                if (type != CommandType.Connect && type != CommandType.Bind)
                {
                    Dispoce(false);
                    return;
                }
                switch (type)
                {
                case CommandType.Connect:
                {
                    int    port = ReaderUtils.ReadInt16BE(reader);
                    byte[] host = reader.ReadBytes(4);
                    Username     = ReadString(reader);
                    EndPoint     = new IPEndPoint(new IPAddress(host), port);
                    RemoteSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    RemoteSocket.Connect(EndPoint);
                    if (RemoteSocket.Connected)
                    {
                        SendRespoce(0x5a);
                        return;
                    }
                    SendRespoce(0x5b);
                    break;
                }

                case CommandType.Bind:
                {
                    // BUG: Potential bug
                    byte[] bytes = reader.ReadBytes(6);
                    int    port  = bytes[1] * 256 + bytes[0];
                    string host  = bytes[2] + "." + bytes[3] + "." + bytes[4] + "." + bytes[5];
                    BindEndPoint = new IPEndPoint(IPAddress.Parse(host), port);
                    AcceptSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    AcceptSocket.Bind(new IPEndPoint(0, 0));
                    AcceptSocket.Listen(10);
                    SendRespoce(0x5a);
                    AcceptSocket.BeginAccept(OnAccept, null);
                    break;
                }
                }
            }
            catch
            {
                SendRespoce(0x5b);
            }
        }