#pragma once #include #include #include #include #include #include #include #include "IServer.h" #if !defined(ARCADIA_BUILD) # include #endif #if USE_SSL # include #endif #include namespace CurrentMetrics { extern const Metric MySQLConnection; } namespace DB { /// Handler for MySQL wire protocol connections. Allows to connect to ClickHouse using MySQL client. class MySQLHandler : public Poco::Net::TCPServerConnection { public: MySQLHandler(IServer & server_, const Poco::Net::StreamSocket & socket_, bool ssl_enabled, size_t connection_id_); void run() final; protected: CurrentMetrics::Increment metric_increment{CurrentMetrics::MySQLConnection}; /// Enables SSL, if client requested. void finishHandshake(MySQLProtocol::ConnectionPhase::HandshakeResponse &); void comQuery(ReadBuffer & payload); void comFieldList(ReadBuffer & payload); void comPing(); void comInitDB(ReadBuffer & payload); void authenticate(const String & user_name, const String & auth_plugin_name, const String & auth_response); virtual void authPluginSSL(); virtual void finishHandshakeSSL(size_t packet_size, char * buf, size_t pos, std::function read_bytes, MySQLProtocol::ConnectionPhase::HandshakeResponse & packet); IServer & server; Poco::Logger * log; UInt64 connection_id = 0; uint32_t server_capabilities = 0; uint32_t client_capabilities = 0; size_t max_packet_size = 0; uint8_t sequence_id = 0; MySQLProtocol::PacketEndpointPtr packet_endpoint; std::unique_ptr session; using ReplacementFn = std::function; using Replacements = std::unordered_map; Replacements replacements; std::unique_ptr auth_plugin; std::shared_ptr in; std::shared_ptr out; bool secure_connection = false; }; #if USE_SSL class MySQLHandlerSSL : public MySQLHandler { public: MySQLHandlerSSL(IServer & server_, const Poco::Net::StreamSocket & socket_, bool ssl_enabled, size_t connection_id_, RSA & public_key_, RSA & private_key_); private: void authPluginSSL() override; void finishHandshakeSSL( size_t packet_size, char * buf, size_t pos, std::function read_bytes, MySQLProtocol::ConnectionPhase::HandshakeResponse & packet) override; RSA & public_key; RSA & private_key; std::shared_ptr ss; }; #endif }