握手信息是在初始连接建立后(通过 connect_server())由服务器发送到客户端的。握手信息中包含关于 MySQL 版本、用于处理连接信息的线程 ID 、客户端和服务器 IP 地址等细节信息。这些信息通过 proxy.connection 结构提供。
proxy.connection.server.mysqld_version: MySQL 服务器版本
proxy.connection.server.thread_id: 线程 ID
proxy.connection.server.scramble_buffer: 存放 scramble password 的 buffer
proxy.connection.server.dst.name: 服务器的 IP 地址
proxy.connection.client.src.name: 客户端的 IP 地址
例如,你可以打印出握手数据信息,并通过 IP 地址拒绝一些客户端的连接,具体如下:
function read_handshake( auth ) print("<-- let's send him some information about us") print(" mysqld-version: " .. proxy.connection.server.mysqld_version) print(" thread-id : " .. proxy.connection.server.thread_id) print(" scramble-buf : " .. string.format("%q", proxy.connection.server.scramble_buffer)) print(" server-addr : " .. proxy.connection.server.dst.name) print(" client-addr : " .. proxy.connection.client.dst.name) if not proxy.connection.client.dst.name:match("^127.0.0.1:") then proxy.response.type = proxy.MYSQLD_PACKET_ERR proxy.response.errmsg = "only local connects are allowed" print("we don't like this client"); return proxy.PROXY_SEND_RESULT end end
注意到,你必须通过 proxy.PROXY_SEND_RESULT 才能返回错误包信息给客户端。