示例#1
0
        private static EndPoint Request(Socket socket, SocksRequestCommand command, EndPoint dstAddr)
        {
            socket.Send(CreateRequest(command, dstAddr));

            byte[] response = new byte[262];

            if (socket.Receive(response) < 10)
            {
                throw new SocksClientException("The connection was reset by the remote peer.");
            }

            if (response[0] != SOCKS_VERSION)
            {
                throw new SocksClientException("Socks version 5 is not supported by the proxy server.");
            }

            SocksReplyCode replyCode = (SocksReplyCode)response[1];

            if (replyCode != SocksReplyCode.Succeeded)
            {
                throw new SocksClientException("Socks proxy server request failed: " + replyCode.ToString(), replyCode);
            }

            return(ParseEndpoint(response, 3));
        }
示例#2
0
        private SocksEndPoint Request(Socket socket, SocksRequestCommand command, SocksEndPoint dstAddr)
        {
            socket.Send(dstAddr.CreateRequest(command));

            byte[] response = new byte[262];

            if (socket.Receive(response) < 10)
            {
                throw new SocksClientException("Invalid response received from proxy server.");
            }

            if (response[0] != SOCKS_VERSION)
            {
                throw new SocksClientException("Socks version 5 is not supported by the proxy server.");
            }

            SocksReplyCode reply = (SocksReplyCode)response[1];

            if (reply != SocksReplyCode.Succeeded)
            {
                throw new SocksClientException("Socks proxy server request failed: " + reply.ToString());
            }

            return(new SocksEndPoint(response, 3));
        }
示例#3
0
        private static byte[] CreateRequest(SocksRequestCommand command, EndPoint dstAddr)
        {
            //get type, address bytes & port bytes
            SocksAddressType type;

            byte[] address;
            ushort port;

            switch (dstAddr.AddressFamily)
            {
            case AddressFamily.InterNetwork:
            {
                type = SocksAddressType.IPv4Address;

                IPEndPoint ep = dstAddr as IPEndPoint;
                address = ep.Address.GetAddressBytes();
                port    = Convert.ToUInt16(ep.Port);
            }
            break;

            case AddressFamily.InterNetworkV6:
            {
                type = SocksAddressType.IPv6Address;

                IPEndPoint ep = dstAddr as IPEndPoint;
                address = ep.Address.GetAddressBytes();
                port    = Convert.ToUInt16(ep.Port);
            }
            break;

            case AddressFamily.Unspecified:
            {
                type = SocksAddressType.DomainName;

                DomainEndPoint ep = dstAddr as DomainEndPoint;
                address = ep.GetAddressBytes();
                port    = Convert.ToUInt16(ep.Port);
            }
            break;

            default:
                throw new NotSupportedException("AddressFamily not supported.");
            }

            //create request
            byte[] request = new byte[address.Length + 6];

            request[0] = SOCKS_VERSION;
            request[1] = (byte)command;
            request[3] = (byte)type;

            Buffer.BlockCopy(address, 0, request, 4, address.Length);

            byte[] portBytes = BitConverter.GetBytes(port);
            Array.Reverse(portBytes);
            Buffer.BlockCopy(portBytes, 0, request, 4 + address.Length, 2);

            return(request);
        }
示例#4
0
        internal byte[] CreateRequest(SocksRequestCommand command)
        {
            byte[] request = new byte[_address.Length + 6];

            request[0] = SocksClient.SOCKS_VERSION;
            request[1] = (byte)command;
            request[3] = (byte)_type;

            Buffer.BlockCopy(_address, 0, request, 4, _address.Length);

            byte[] port = BitConverter.GetBytes(_port);
            Array.Reverse(port);
            Buffer.BlockCopy(port, 0, request, 4 + _address.Length, 2);

            return(request);
        }