Example #1
0
        protected override void Open()
        {
            bool isUnix = Settings.ConnectionProtocol == MySqlConnectionProtocol.Unix ||
                          Settings.ConnectionProtocol == MySqlConnectionProtocol.UnixSocket;

            _stream = MyNetworkStream.CreateStream(Settings.Server, Settings.ConnectTimeout, Settings.Keepalive, Settings.Port, isUnix);
            if (_stream == null)
            {
                throw new MySqlException(ResourcesX.UnableToConnect);
            }
            _reader  = new XPacketReaderWriter(_stream);
            _writer  = new XPacketReaderWriter(_stream);
            protocol = new XProtocol(_reader, _writer);

            Settings.CharacterSet = String.IsNullOrWhiteSpace(Settings.CharacterSet) ? "utf8mb4" : Settings.CharacterSet;

            var encoding = Encoding.GetEncoding(String.Compare(Settings.CharacterSet, "utf8mb4", true) == 0 ? "UTF-8" : Settings.CharacterSet);

            SetState(SessionState.Connecting, false);

            GetAndSetCapabilities();

            // Validates use of TLS.
            if (Settings.SslMode != MySqlSslMode.None)
            {
                if (serverSupportsTls)
                {
                    new Ssl(
                        Settings.Server,
                        Settings.SslMode,
                        Settings.CertificateFile,
                        Settings.CertificateStoreLocation,
                        Settings.CertificatePassword,
                        Settings.CertificateThumbprint,
                        Settings.SslCa,
                        Settings.SslCert,
                        Settings.SslKey)
                    .StartSSL(ref _stream, encoding, Settings.ToString());
                    _reader = new XPacketReaderWriter(_stream);
                    _writer = new XPacketReaderWriter(_stream);
                    protocol.SetXPackets(_reader, _writer);
                }
                else
                {
                    // Client requires SSL connections.
                    string message = String.Format(Resources.NoServerSSLSupport,
                                                   Settings.Server);
                    throw new MySqlException(message);
                }
            }

            Authenticate();

            SetState(SessionState.Open, false);
        }
Example #2
0
        protected override void Open()
        {
            if (Settings.ConnectionProtocol == MySqlConnectionProtocol.Tcp && Settings.IsSshEnabled())
            {
                _sshHandler = new Ssh(
                    Settings.SshHostName,
                    Settings.SshUserName,
                    Settings.SshPassword,
                    Settings.SshKeyFile,
                    Settings.SshPassphrase,
                    Settings.SshPort,
                    Settings.Server,
                    Settings.Port,
                    true);
                _sshHandler.StartClient();
            }

            bool isUnix = Settings.ConnectionProtocol == MySqlConnectionProtocol.Unix ||
                          Settings.ConnectionProtocol == MySqlConnectionProtocol.UnixSocket;

            _stream = MyNetworkStream.CreateStream(
                Settings.Server == "127.0.0.1" || Settings.Server == "::1"
            ? "localhost"
            : Settings.Server,
                Settings.ConnectTimeout,
                Settings.Keepalive,
                Settings.Port,
                isUnix);
            if (_stream == null)
            {
                throw new MySqlException(ResourcesX.UnableToConnect);
            }

            _reader  = new XPacketReaderWriter(_stream);
            _writer  = new XPacketReaderWriter(_stream);
            protocol = new XProtocol(_reader, _writer);

            Settings.CharacterSet = string.IsNullOrWhiteSpace(Settings.CharacterSet) ? "utf8mb4" : Settings.CharacterSet;

            var encoding = Encoding.GetEncoding(string.Compare(Settings.CharacterSet, "utf8mb4", true) == 0 ? "UTF-8" : Settings.CharacterSet);

            SetState(SessionState.Connecting, false);

            try
            {
                GetAndSetCapabilities();
            }
            catch (Exception)
            {
                if (Settings.ConnectionProtocol == MySqlConnectionProtocol.Tcp && Settings.IsSshEnabled())
                {
                    _sshHandler?.StopClient();
                }

                throw;
            }

            // Validates use of TLS.
            if (Settings.SslMode != MySqlSslMode.None)
            {
                if (serverSupportsTls)
                {
                    new Ssl(
                        Settings.Server,
                        Settings.SslMode,
                        Settings.CertificateFile,
                        Settings.CertificateStoreLocation,
                        Settings.CertificatePassword,
                        Settings.CertificateThumbprint,
                        Settings.SslCa,
                        Settings.SslCert,
                        Settings.SslKey,
                        Settings.TlsVersion)
                    .StartSSL(ref _stream, encoding, Settings.ToString());

                    if (_readerCompressionController != null && _readerCompressionController.IsCompressionEnabled)
                    {
                        _reader = new XPacketReaderWriter(_stream, _readerCompressionController);
                        _writer = new XPacketReaderWriter(_stream, _writerCompressionController);
                    }
                    else
                    {
                        _reader = new XPacketReaderWriter(_stream);
                        _writer = new XPacketReaderWriter(_stream);
                    }

                    protocol.SetXPackets(_reader, _writer);
                }
                else
                {
                    // Client requires SSL connections.
                    string message = String.Format(Resources.NoServerSSLSupport,
                                                   Settings.Server);
                    throw new MySqlException(message);
                }
            }
            else if (_readerCompressionController != null && _readerCompressionController.IsCompressionEnabled)
            {
                _reader = new XPacketReaderWriter(_stream, _readerCompressionController);
                _writer = new XPacketReaderWriter(_stream, _writerCompressionController);
                protocol.SetXPackets(_reader, _writer);
            }

            Authenticate();

            SetState(SessionState.Open, false);
        }
Example #3
0
        protected override void Open()
        {
            bool isUnix = Settings.ConnectionProtocol == MySqlConnectionProtocol.Unix ||
                          Settings.ConnectionProtocol == MySqlConnectionProtocol.UnixSocket;

            _stream = MyNetworkStream.CreateStream(Settings, isUnix);
            if (_stream == null)
            {
                throw new MySqlException(ResourcesX.UnableToConnect);
            }
            _reader  = new XPacketReaderWriter(_stream);
            _writer  = new XPacketReaderWriter(_stream);
            protocol = new XProtocol(_reader, _writer);

            Settings.CharacterSet = String.IsNullOrWhiteSpace(Settings.CharacterSet) ? "utf8mb4" : Settings.CharacterSet;

            var encoding = Encoding.GetEncoding(String.Compare(Settings.CharacterSet, "utf8mb4", true) == 0 ? "UTF-8" : Settings.CharacterSet);

            SetState(SessionState.Connecting, false);

            GetAndSetCapabilities();

            // validates TLS use
            if (Settings.SslMode != MySqlSslMode.None)
            {
                if (serverSupportsTls)
                {
                    new Ssl(Settings).StartSSL(ref _stream, encoding, Settings.ToString());
                    _reader = new XPacketReaderWriter(_stream);
                    _writer = new XPacketReaderWriter(_stream);
                    protocol.SetXPackets(_reader, _writer);
                }
                else
                {
                    // Client requires SSL connections.
                    string message = String.Format(Resources.NoServerSSLSupport,
                                                   Settings.Server);
                    throw new MySqlException(message);
                }
            }

            // Default authentication
            if (Settings.Auth == MySqlAuthenticationMode.Default)
            {
                if ((Settings.SslMode != MySqlSslMode.None && serverSupportsTls) || Settings.ConnectionProtocol == MySqlConnectionProtocol.Unix)
                {
                    Settings.Auth = MySqlAuthenticationMode.PLAIN;
                    AuthenticatePlain();
                }
                else
                {
                    Settings.Auth = MySqlAuthenticationMode.MYSQL41;
                    AuthenticateMySQL41();
                }
            }
            // User defined authentication
            else
            {
                switch (Settings.Auth)
                {
                case MySqlAuthenticationMode.PLAIN:
                    AuthenticatePlain();
                    break;

                case MySqlAuthenticationMode.MYSQL41:
                    AuthenticateMySQL41();
                    break;

                case MySqlAuthenticationMode.EXTERNAL:
                    AuthenticateExternal();
                    break;
                }
            }

            SetState(SessionState.Open, false);
        }