Пример #1
0
 public override void HandleReceive(byte[] receiveBuffer, int offset, int count)
 {
     try
     {
         _RemoteSocksServerResponse = Socks4Response.Parse(receiveBuffer);
     }
     catch
     {
     }
     _WaitHandle.Set();
 }
Пример #2
0
        private void CheckResponse()
        {
            Socks4Response response = (Socks4Response)buffer[1];

            if (response == Socks4Response.RequestGranted)
            {
                _connected = true;
                InvokeRequestAccepted();
            }
            else
            {
                if (!_disposed)
                {
                    Dispose();
                }
            }

            InvokeStatus($"Response: {response.ToString()}.\r\nConnected: {_connected}");
        }
        public override void HandleReceive(byte[] receiveBuffer, int offset, int count)
        {
            try
            {
                Performance.UpdateBytesReceived(count);

                Socks4Response socksServerResponse = Socks4Response.Parse(receiveBuffer);
                if (!socksServerResponse.Success)
                {
                    HandleLocalDisconnect();
                    return;
                }

                _ClientConnection.SetObserver(new TransmissionRedirectConnectionObserver(_Client, true));
                _Client.SetObserver(new TransmissionRedirectConnectionObserver(_ClientConnection, false));

                _ClientConnection.BeginReceiving();
            }
            catch (Exception exp)
            {
                HandleLocalDisconnect();
            }
        }
Пример #4
0
        private static void TestRemoteServer(string remoteHost, int remotePort, string remoteSocksHost, int remoteSocksPort, ref int statuscode, ref string statusmessage)
        {
            Connection testConnection;

            try
            {
                testConnection = Connection.Connect(remoteSocksHost, remoteSocksPort);
            }
            catch (Exception exp)
            {
                statuscode    = 505;
                statusmessage = "Could not connect to the Socks Server: " + exp.Message;
                return;
            }

            try
            {
                testConnection.Send(Socks4Request.Build(Socks4Request.REQUEST_TYPE_CONNECT, remoteHost, remotePort, "").ToByteArray());
                testConnection.BeginReceivingSync();

                byte[] socksRawResponse = testConnection.GetNextSyncReceiveBuffer();
                testConnection.Close();

                Socks4Response socksResponse = Socks4Response.Parse(socksRawResponse);

                if (socksResponse == null || !socksResponse.Success)
                {
                    statuscode    = 504;
                    statusmessage = "Socks server reports that it failed to connect to the remote host";
                }
            }
            catch (Exception exp)
            {
                statuscode    = 504;
                statusmessage = "Failed while trying to connect to the remote host through socks: " + exp.Message;
            }
        }
Пример #5
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;
        }
Пример #6
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;
        }