コード例 #1
0
        /// <summary>
        /// Use a password to authenticate with a VNC Host. NOTE: This is only necessary if Connect() returns TRUE.
        /// </summary>
        /// <param name="password">The password to use.</param>
        /// <returns>Returns True if Authentication worked, otherwise False.</returns>
        public bool Authenticate(string password)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            // If new Security Types are supported in future, add the code here.  For now, only
            // VNC Authentication is supported.
            if (securityType == 2)
            {
                PerformVncAuthentication(password);
            }
            else
            {
                throw new NotSupportedException("Unable to Authenticate with Server. The Server uses an Authentication scheme unknown to the client.");
            }

            if (rfb.ReadSecurityResult() == 0)
            {
                return(true);
            }
            else
            {
                // Authentication failed, and if the server is using Protocol version 3.8, a
                // plain text message follows indicating why the error happend.  I'm not
                // currently using this message, but it is read here to clean out the stream.
                // In earlier versions of the protocol, the server will just drop the connection.
                if (rfb.ServerVersion == 3.8)
                {
                    rfb.ReadSecurityFailureReason();
                }
                rfb.Close();                    // TODO: Is this the right place for this???
                return(false);
            }
        }
コード例 #2
0
ファイル: VncClient.cs プロジェクト: KyulingLee/VncSharp
        /// <summary>
        /// Connect to a VNC Host and determine which type of Authentication it uses. If the host uses Password Authentication, a call to Authenticate() will be required.
        /// </summary>
        /// <param name="host">The IP Address or Host Name of the VNC Host.</param>
        /// <param name="display">The Display number (used on Unix hosts).</param>
        /// <param name="port">The Port number used by the Host, usually 5900.</param>
        /// <param name="viewOnly">True if mouse/keyboard events are to be ignored.</param>
        /// <returns>Returns True if the VNC Host requires a Password to be sent after Connect() is called, otherwise False.</returns>
        public bool Connect(string host, int display, int port, bool viewOnly)
        {
            if (host == null) throw new ArgumentNullException("host");

            // If a diplay number is specified (used to connect to Unix servers)
            // it must be 0 or greater.  This gets added to the default port number
            // in order to determine where the server will be listening for connections.
            if (display < 0) throw new ArgumentOutOfRangeException("display", display, "Display number must be non-negative.");
            port += display;

            rfb = new RfbProtocol();

            if (viewOnly) {
                inputPolicy = new VncViewInputPolicy(rfb);
            } else {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match
            try {
                rfb.Connect(host, port);
                rfb.ReadProtocolVersion();
                rfb.WriteProtocolVersion();

                // Figure out which type of authentication the server uses
                byte[] types = rfb.ReadSecurityTypes();

                // Based on what the server sends back in the way of supported Security Types, one of
                // two things will need to be done: either the server will reject the connection (i.e., type = 0),
                // or a list of supported types will be sent, of which we need to choose and use one.
                if (types.Length > 0) {
                    if (types[0] == 0) {
                        // The server is not able (or willing) to accept the connection.
                        // A message follows indicating why the connection was dropped.
                        throw new VncProtocolException("Connection Failed. The server rejected the connection for the following reason: " + rfb.ReadSecurityFailureReason());
                    } else {
                        securityType = GetSupportedSecurityType(types);
                        Debug.Assert(securityType > 0, "Unknown Security Type(s)", "The server sent one or more unknown Security Types.");

                        rfb.WriteSecurityType(securityType);

                        // Protocol 3.8 states that a SecurityResult is still sent when using NONE (see 6.2.1)
                        if (rfb.ServerVersion == 3.8f && securityType == 1) {
                            if (rfb.ReadSecurityResult() > 0) {
                                // For some reason, the server is not accepting the connection.  Get the
                                // reason and throw an exception
                                throw new VncProtocolException("Unable to Connecto to the Server. The Server rejected the connection for the following reason: " + rfb.ReadSecurityFailureReason());
                            }
                        }

                        return (securityType > 1) ? true : false;
                    }
                } else {
                    // Something is wrong, since we should have gotten at least 1 Security Type
                    throw new VncProtocolException("Protocol Error Connecting to Server. The Server didn't send any Security Types during the initial handshake.");
                }
            } catch (Exception e) {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }
コード例 #3
0
        /// <summary>
        ///     Connect to a VNC Host and determine which type of Authentication it uses. If the host uses Password Authentication,
        ///     a call to Authenticate() will be required.
        /// </summary>
        /// <param name="host">The IP Address or Host Name of the VNC Host.</param>
        /// <param name="display">The Display number (used on Unix hosts).</param>
        /// <param name="port">The Port number used by the Host, usually 5900.</param>
        /// <param name="viewOnly">True if mouse/keyboard events are to be ignored.</param>
        /// <returns>Returns True if the VNC Host requires a Password to be sent after Connect() is called, otherwise False.</returns>
        public bool Connect(string host, int display, int port, bool viewOnly)
        {
            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }

            // If a diplay number is specified (used to connect to Unix servers)
            // it must be 0 or greater.  This gets added to the default port number
            // in order to determine where the server will be listening for connections.
            if (display < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(display), display, "Display number must be non-negative.");
            }
            port += display;

            rfb = new RfbProtocol();

            viewOnlyMode = viewOnly;
            if (viewOnly)
            {
                inputPolicy = new VncViewInputPolicy(rfb);
            }
            else
            {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match
            try
            {
                rfb.Connect(host, port);
                rfb.ReadProtocolVersion();

                // Handle possible repeater connection
                if (rfb.ServerVersion == 0.0)
                {
                    rfb.WriteProxyAddress();
                    // Now we are connected to the real server; read the protocol version of the
                    // server
                    rfb.ReadProtocolVersion();
                    // Resume normal handshake and protocol
                }

                rfb.WriteProtocolVersion();

                // Figure out which type of authentication the server uses
                var types = rfb.ReadSecurityTypes();

                // Based on what the server sends back in the way of supported Security Types, one of
                // two things will need to be done: either the server will reject the connection (i.e., type = 0),
                // or a list of supported types will be sent, of which we need to choose and use one.
                if (types.Length <= 0)
                {
                    // Something is wrong, since we should have gotten at least 1 Security Type
                    throw new VncProtocolException(
                              "Protocol Error Connecting to Server. The Server didn't send any Security Types during the initial handshake.");
                }
                if (types[0] == 0)
                {
                    // The server is not able (or willing) to accept the connection.
                    // A message follows indicating why the connection was dropped.
                    throw new VncProtocolException(
                              "Connection Failed. The server rejected the connection for the following reason: " +
                              rfb.ReadSecurityFailureReason());
                }

                securityType = GetSupportedSecurityType(types);
                Debug.Assert(securityType > 0, "Unknown Security Type(s)",
                             "The server sent one or more unknown Security Types.");

                rfb.WriteSecurityType(securityType);

                // Protocol 3.8 states that a SecurityResult is still sent when using NONE (see 6.2.1)
                if (rfb.ServerVersion != 3.8f || securityType != 1)
                {
                    return(securityType > 1);
                }
                if (rfb.ReadSecurityResult() > 0)
                {
                    // For some reason, the server is not accepting the connection.  Get the
                    // reason and throw an exception
                    throw new VncProtocolException(
                              "Unable to Connecto to the Server. The Server rejected the connection for the following reason: " +
                              rfb.ReadSecurityFailureReason());
                }

                return(securityType > 1);
            }
            catch (Exception e)
            {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }