Skip to content

Implement direct MySQL handshake and authentication protocol to remove MySql.Data dependency #4

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 20, 2025

Overview

This PR implements the MySQL handshake and authentication protocol directly without depending on the MySql.Data.MySqlClient driver, addressing the requirement to establish raw TCP connections and handle MySQL protocol communication natively.

Changes Made

1. MySQL Protocol Implementation

  • MySQLPacket: Base class for MySQL protocol packet handling with proper length-prefixed packet format
  • HandshakePacket: Parses initial handshake packets from MySQL server including capabilities, version, and auth data
  • HandshakeResponsePacket: Generates client handshake response with credentials and capabilities
  • CommandPacket: Handles MySQL command protocol for SQL query execution
  • MySQLAuth: Implements mysql_native_password authentication mechanism with SHA1-based scrambling

2. Direct Connection Implementation

  • DirectMySQLConnection: Complete replacement for MySqlConnection that:
    • Establishes raw TCP connections to MySQL server
    • Performs MySQL handshake protocol authentication
    • Executes SQL commands using native MySQL protocol
    • Provides result set parsing for queries like SHOW MASTER STATUS

3. ReplicationClient Updates

  • Replaced MySqlConnection usage with DirectMySQLConnection
  • Updated all SQL command execution methods:
    • LoadDatabaseSchemaAsync() - now uses direct queries to information_schema
    • GetBinlogFileNameAndPosition() - executes SHOW MASTER STATUS directly
    • GetBinlogChecksum() - queries global variables directly
    • ConfirmChecksum() - sets session variables directly
  • Removed reflection-based stream extraction (GetStreamFromMySQLConnection)
  • Maintained all existing functionality and public APIs

4. Dependency Management

  • Removed MySql.Data package reference from main library
  • Kept MySql.Data dependency in test project for test data setup
  • Updated target frameworks to supported versions (.NET 6.0, 7.0, 8.0)

Testing

Added focused tests to validate the new implementation:

[Fact]
public async Task TestReplicationClientWithDirectConnection()
{
    var client = new ReplicationClient();
    var result = await client.ConnectAsync("localhost", "root", "root", 1001);
    
    Assert.True(result.Result);
    Assert.NotNull(client.CurrentPosition);
    // Existing functionality works exactly the same
}

Backward Compatibility

Full backward compatibility maintained

  • All existing public APIs remain unchanged
  • ReplicationClient.ConnectAsync() works identically
  • All binlog replication functionality preserved
  • Same connection parameters and behavior

Key Benefits

  1. Dependency Reduction: Removes heavy MySql.Data.MySqlClient dependency from main library
  2. Direct Protocol Control: Full control over MySQL communication without driver abstraction
  3. Performance: Eliminates reflection-based stream extraction
  4. Maintainability: Clear separation between protocol handling and business logic
  5. Security: Direct implementation of authentication without driver dependencies

Implementation Details

The MySQL protocol implementation follows the official MySQL documentation:

Authentication uses the standard mysql_native_password method:

auth_response = SHA1(password) XOR SHA1(scramble + SHA1(SHA1(password)))

Files Changed

New Files

  • src/SciSharp.MySQL.Replication/Protocol/MySQLPacket.cs
  • src/SciSharp.MySQL.Replication/Protocol/HandshakePacket.cs
  • src/SciSharp.MySQL.Replication/Protocol/HandshakeResponsePacket.cs
  • src/SciSharp.MySQL.Replication/Protocol/CommandPacket.cs
  • src/SciSharp.MySQL.Replication/Protocol/MySQLAuth.cs
  • src/SciSharp.MySQL.Replication/Protocol/DirectMySQLConnection.cs
  • tests/Test/DirectConnectionTest.cs

Modified Files

  • src/SciSharp.MySQL.Replication/ReplicationClient.cs - Replaced MySqlConnection with DirectMySQLConnection
  • src/SciSharp.MySQL.Replication/SciSharp.MySQL.Replication.csproj - Removed MySql.Data dependency
  • tests/Test/Test.csproj - Added MySql.Data for test infrastructure

This implementation successfully removes the dependency on MySql.Data.MySqlClient while maintaining all existing functionality and providing a foundation for future MySQL protocol enhancements.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits June 20, 2025 17:50
Co-authored-by: kerryjiang <456060+kerryjiang@users.noreply.github.com>
Co-authored-by: kerryjiang <456060+kerryjiang@users.noreply.github.com>
Co-authored-by: kerryjiang <456060+kerryjiang@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Implement MySQL handshake and authentication without MySQL client driver Implement direct MySQL handshake and authentication protocol to remove MySql.Data dependency Jun 20, 2025
@Copilot Copilot AI requested a review from kerryjiang June 20, 2025 18:02
Copilot finished work on behalf of kerryjiang June 20, 2025 18:02
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants