日期:2014-05-16  浏览次数:20961 次

mysql-proxy read_query方法 实现执行特殊SQL后认证连接
在mysql-proxy中可以通过设置会话变量的方式,实现 在得到数据库连接后再执行指定的SQL语句进行二次认证的功能。如果没有二次认证,则拦截该数据库连接的一切请求并返回错误。

local authed = false

function read_query( packet )
        if (authed == false) then
           if packet:byte() == proxy.COM_QUERY then
                if(packet:sub(2):lower() == "select 2013 from dual") then
                    authed = true                   
                else                  
                    proxy.response.type = proxy.MYSQLD_PACKET_ERR
                    proxy.response.errmsg = "unauthed connection !!! "
                    return proxy.PROXY_SEND_RESULT
                end
            else
                proxy.response.type = proxy.MYSQLD_PACKET_ERR
                proxy.response.errmsg = "unauthed connection !!!"
                return proxy.PROXY_SEND_RESULT
           end
        end
end