Exemplo n.º 1
0
        public static TcpClient ConnectTcpClientTo(IPAddress proxyAddress, short proxyPort, IPAddress destAddress, short destPort, string asciiUserId, int timeout)
        {
            // create the socket
            var socket = new TcpClient(AddressFamily.InterNetwork);
            socket.ReceiveTimeout = timeout;
            socket.SendTimeout = timeout;

            // connect to the proxy
            socket.Connect(new IPEndPoint(proxyAddress, proxyPort));

            // create the request
            var requestPacket = new Socks4Request(1, destPort, destAddress, asciiUserId);

            // Get the request
            var requestData = requestPacket.GetBytes();

            // Send the request
            socket.GetStream().Write(requestData, 0, requestData.Length);

            // Get the response from teh socket
            var response = new Socks4Response(socket);

            if (response.Version != 0)
            {
                // Unexpected version (must be 0)
                throw new Socks4Exception(string.Format("Invalid version. Expected 0, got {0}.", response.Version));
            }

            if (response.Command < (byte)Socks4ResponseCommand.MinValue || response.Command > (byte)Socks4ResponseCommand.MaxValue)
            {
                // Invalid response command
                throw new Socks4Exception(string.Format("Invalid reply command. Got {0}.", response.Command));
            }

            if (response.Command != (byte)Socks4ResponseCommand.Granted)
            {
                // Request failed
                throw new Socks4Exception((Socks4ResponseCommand)response.Command);
            }

            // Connection made! Return the socket.
            return socket;
        }
Exemplo n.º 2
0
        public static System.Net.Sockets.Socket ConnectSocketTo(IPAddress proxyAddress, short proxyPort, IPAddress destAddress, short destPort, string asciiUserId)
        {
            // create the socket
            var socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // connect to the proxy
            socket.Connect(new IPEndPoint(proxyAddress, proxyPort));

            // create the request
            var requestPacket = new Socks4Request(1, destPort, destAddress, asciiUserId);

            // Send the request
            socket.Send(requestPacket.GetBytes());

            // Get the response from teh socket
            var response = new Socks4Response(socket);

            if (response.Version != 0)
            {
                // Unexpected version (must be 0)
                throw new Socks4Exception(string.Format("Invalid version. Expected 0, got {0}.", response.Version));
            }

            if (response.Command < (byte)Socks4ResponseCommand.MinValue || response.Command > (byte)Socks4ResponseCommand.MaxValue)
            {
                // Invalid response command
                throw new Socks4Exception(string.Format("Invalid reply command. Got {0}.", response.Command));
            }

            if (response.Command != (byte)Socks4ResponseCommand.Granted)
            {
                // Request failed
                throw new Socks4Exception((Socks4ResponseCommand)response.Command);
            }

            // Connection made! Return the socket.
            return socket;
        }