Class: MysqlReplicator::Connections::Handshake

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_replicator/connections/handshake.rb

Class Method Summary collapse

Class Method Details

.debug_handshake_info(handshake_info) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mysql_replicator/connections/handshake.rb', line 117

def self.debug_handshake_info(handshake_info)
  MysqlReplicator::Logger.debug \
    "===== Start Handshake Info =====\n" \
    "Protocol version: #{handshake_info[:protocol_version]}\n" \
    "Server version: #{handshake_info[:server_version]}\n" \
    "Connection ID: #{handshake_info[:connection_id]}\n" \
    "Capability flags: 0x#{handshake_info[:capability_flags]&.to_s(16)&.upcase}\n" \
    "Character set: #{handshake_info[:charset]}\n" \
    "Status flags: #{handshake_info[:status_flags]}\n" \
    "Authentication plugin name: #{handshake_info[:auth_plugin_name]}\n" \
    "Authentication plugin data: #{handshake_info[:auth_plugin_data].unpack('C*').map { |b| format('%02X', b) }.join(' ')}\n" \
    '===== End Handshake Info ====='
end

.execute(connection) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/mysql_replicator/connections/handshake.rb', line 21

def self.execute(connection)
  handshake_response_packet = connection.read_packet
  handshake_info = parse_handshake_response_packet(handshake_response_packet)

  debug_handshake_info(handshake_info)
  handshake_info
end

.parse_handshake_response_packet(packet) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mysql_replicator/connections/handshake.rb', line 31

def self.parse_handshake_response_packet(packet)
  payload = packet[:payload]
  offset = 0

  # Protocol version (1 byte)
  protocol_version = MysqlReplicator::StringUtil.read_uint8(payload[offset])
  offset += 1

  # Server version is null-terminated string
  server_version_end = payload.index("\0", offset) || 0
  server_version = MysqlReplicator::StringUtil.read_str(payload[offset...server_version_end])
  offset = server_version_end + 1

  # ConnectionID is 4bytes and little endian
  connection_id = MysqlReplicator::StringUtil.read_uint32(payload[offset..(offset + 3)])
  offset += 4

  # Authentication plugin data (first 8 bytes)
  auth_plugin_data_part1 = MysqlReplicator::StringUtil.read_str(payload[offset..(offset + 7)])
  offset += 8

  # Reserved (1 byte, always 0x00)
  offset += 1

  # Server capability flags (lower 2 bytes)
  capability_flags_lower = MysqlReplicator::StringUtil.read_uint16(payload[offset..(offset + 1)])
  offset += 2

  # After MySQL 8.0, additional authentication plugin information is included
  if offset < payload.length
    # Character set (1 byte)
    charset = MysqlReplicator::StringUtil.read_uint8(payload[offset])
    offset += 1

    # Status flags (2 bytes)
    status_flags = MysqlReplicator::StringUtil.read_uint16(payload[offset..(offset + 1)])
    offset += 2

    # Server capability flags (upper 2 bytes)
    capability_flags_upper = MysqlReplicator::StringUtil.read_uint16(payload[offset..(offset + 1)])
    offset += 2

    # Feature flags
    capability_flags = capability_flags_lower | (capability_flags_upper << 16)

    # Authentication plugin data length (1 byte)
    auth_plugin_data_len = MysqlReplicator::StringUtil.read_uint8(payload[offset])
    offset += 1

    # Reserved (10 bytes)
    offset += 10

    # Authentication plugin data (part 2)
    remaining_auth_data_len = [auth_plugin_data_len - 8, 13].max
    auth_plugin_data_part2 = MysqlReplicator::StringUtil.read_str(payload[offset..(offset + remaining_auth_data_len - 1)])
    offset += remaining_auth_data_len

    # Authentication plugin name (null-terminated string)
    plugin_name_end = payload.index("\0", offset)
    auth_plugin_name = MysqlReplicator::StringUtil.read_str(payload[offset...plugin_name_end])
    auth_plugin_data = auth_plugin_data_part1 + MysqlReplicator::StringUtil.read_str(auth_plugin_data_part2[0..11])
    # Adjust 20 bytes
    if auth_plugin_data.length > 20
      auth_plugin_data = auth_plugin_data[0..19] || ''
    elsif auth_plugin_data.length < 20
      auth_plugin_data += "\x00" * (20 - auth_plugin_data.length)
    end
  else
    auth_plugin_name = 'mysql_native_password'
    auth_plugin_data = auth_plugin_data_part1
  end

  {
    protocol_version: protocol_version,
    server_version: server_version,
    connection_id: connection_id,
    capability_flags: capability_flags,
    charset: charset,
    status_flags: status_flags,
    auth_plugin_name: auth_plugin_name,
    auth_plugin_data: auth_plugin_data
  }
end