Mysql-proxy中的lua脚本编程(一)

在为mysql-proxy编写lua脚步的时候,需要知道一下几个入口函数,通过这几个入口函数我们可以控制mysql-proxy的一些行为。

  • connect_server()          当代理服务器接受到客户端连接请求时(tcp中的握手)会调用该函数
  • read_handshake()        当mysql服务器返回握手相应时会被调用
  • read_auth()             当客户端发送认证信息(username,password,port,database)时会被调用
  • read_auth_result(aut)  当mysql返回认证结果时会被调用
  • read_query(packet)      当客户端提交一个sql语句时会被调用
  • read_query_result(inj) 当mysql返回查询结果时会被调用

1.connect_server使用


function read_handshake( )
    local con = proxy.connection

    print("<-- let‘s send him some information about us")
    print("    server-addr   : " .. con.server.dst.name)
    print("    client-addr   : " .. con.client.src.name)

-- lets deny clients from !127.0.0.1
   if con.client.src.address ~= "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

获取代理的链接对象,这个对象是全局的,可以在函数中直接拿来使用。从连接对象中我们可以拿到客户端名称和服务器名称,通过也能获得客户端的ip地址,上面的代码就是禁止非本机ip登录mysql。

2、read_auth使用



读取用户的认证信息包括用户名、密码、所要连接的数据库。其中的proxy.MYSQLD_PACKET_ERR是mysql-proxy中自带的常量。

function read_auth( )
    local con = proxy.connection

    print("--> there, look, the client is responding to the server auth packet")
    print("    username      : " .. con.client.username)
    print("    password      : " .. string.format("%q", con.client.scrambled_password))
    print("    default_db    : " .. con.client.default_db)

    if con.client.username == "evil" then
        proxy.response.type = proxy.MYSQLD_PACKET_ERR
        proxy.response.errmsg = "evil logins are not allowed"

        return proxy.PROXY_SEND_RESULT
    end
end

3.read_auth_result使用



通过该方法我们可以获得mysql数据库的认证结果,认证结果由auth对象持有,我们可以访问其packet属性(字符串类型),可以查看返回结果。字符串的第一个字符是对结果的标识。

function read_auth_result( auth )
    local state = auth.packet:byte()  //获取第一个字符并将其转为整型

    if state == proxy.MYSQLD_PACKET_OK then
        print("<-- auth ok");
    elseif state == proxy.MYSQLD_PACKET_ERR then
        print("<-- auth failed");
    else
        print("<-- auth ... don‘t know: " .. string.format("%q", auth.packet));
    end
end

4.read_query的使用



packet中就存放着客户请求的SQL语句,类型为字符串类型。起始第一个字符同上,为标识符。这里判断是不是一个查询语句,是的话就从第二个字符开始输出查询语句。

function read_query( packet )
    print("--> someone sent us a query")
    if packet:byte() == proxy.COM_QUERY then
        print("    query: " .. packet:sub(2))

        if packet:sub(2) == "SELECT 1" then
            proxy.queries:append(1, packet)
        end
    end

end

5、read_query_result使用



其实我们该函数和read_query函数时是希望对SQL语句进行处理,但是由于时间有限,不能对mysql-proxy提供的sql处理继续研究,这里先就先贴出来。

function read_query_result( inj )
    print("<-- ... ok, this only gets called when read_query() told us")

    proxy.response = {
        type = proxy.MYSQLD_PACKET_RAW,
        packets = {
            "\255" ..
              "\255\004" .. -- errno
              "#" ..
              "12S23" ..
              "raw, raw, raw"
        }
    }

    return proxy.PROXY_SEND_RESULT
end

--[[ $%BEGINLICENSE%$
 Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
 published by the Free Software Foundation; version 2 of the
 License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 02110-1301  USA

 $%ENDLICENSE%$ --]]

local proto = require("mysql.proto")

local prep_stmts = { }

function read_query( packet )
    local cmd_type = packet:byte()
    if cmd_type == proxy.COM_STMT_PREPARE then
        proxy.queries:append(1, packet, { resultset_is_needed = true } )
        return proxy.PROXY_SEND_QUERY
    elseif cmd_type == proxy.COM_STMT_EXECUTE then
        proxy.queries:append(2, packet, { resultset_is_needed = true } )
        return proxy.PROXY_SEND_QUERY
    elseif cmd_type == proxy.COM_STMT_CLOSE then
        proxy.queries:append(3, packet, { resultset_is_needed = true } )
        return proxy.PROXY_SEND_QUERY
    end
end

function read_query_result(inj)
    if inj.id == 1 then
        -- print the query we sent
        local stmt_prepare = assert(proto.from_stmt_prepare_packet(inj.query))
        print(("> PREPARE: %s"):format(stmt_prepare.stmt_text))

        -- and the stmt-id we got for it
        if inj.resultset.raw:byte() == 0 then
            local stmt_prepare_ok = assert(proto.from_stmt_prepare_ok_packet(inj.resultset.raw))
            print(("< PREPARE: stmt-id = %d (resultset-cols = %d, params = %d)"):format(
                stmt_prepare_ok.stmt_id,
                stmt_prepare_ok.num_columns,
                stmt_prepare_ok.num_params))

            prep_stmts[stmt_prepare_ok.stmt_id] = {
                num_columns = stmt_prepare_ok.num_columns,
                num_params = stmt_prepare_ok.num_params,
            }
        end
    elseif inj.id == 2 then
        local stmt_id = assert(proto.stmt_id_from_stmt_execute_packet(inj.query))
        local stmt_execute = assert(proto.from_stmt_execute_packet(inj.query, prep_stmts[stmt_id].num_params))
        print(("> EXECUTE: stmt-id = %d"):format(stmt_execute.stmt_id))
        if stmt_execute.new_params_bound then
            for ndx, v in ipairs(stmt_execute.params) do
                print((" [%d] %s (type = %d)"):format(ndx, tostring(v.value), v.type))
            end
        end
    elseif inj.id == 3 then
        local stmt_close = assert(proto.from_stmt_close_packet(inj.query))
        print(("> CLOSE: stmt-id = %d"):format(stmt_close.stmt_id))

        prep_stmts[stmt_close.stmt_id] = nil -- cleanup
    end
end

这里使用了MySQL新的接口stmt,对其不了解可以查看下面的连接。

戳我

时间: 2024-08-29 00:47:41

Mysql-proxy中的lua脚本编程(一)的相关文章

在Unity3d中解析Lua脚本的方法

由于近期项目中提出了热更新的需求,因此本周末在Lua的陪伴下度过.对Lua与Unity3d的搭配使用,仅仅达到了一个初窥门径的程度,记录一二于此.水平有限,欢迎批评指正. 网络上关于Lua脚本和Unity3d的配合使用的资料不多,例子工程大多相同.大概了解到针对性的插件有uLua.UniLua.KopiLua三种.试用了前两种,抛开效率与安全性不说,感觉uLua试用起来比较简单,本文只介绍uLua的使用步骤. uLua的原理是在Unity3d中解析字符串形式的Lua脚本,让Lua与C#相互传递参

C++中嵌入Lua脚本环境搭建

第一步(环境准备工作): 工具: ●LuaForWindows_v5.1.4-46.exe傻瓜式安装. 作用:此工具可以在windows环境下编译运行Lua脚本程序.安装完成后会有两个图标:Lua和SciTE.Lua是命令行,SciTE是图形运行环境,两个都可以编译运行,看个人喜好. ●VS2012大家都会,此处省略若干字... 第二步(在VS2012下新建并运行C++中嵌入Lua脚本程序): ●打开VS2012,新建一个控制台的C++空项目 ●配置Lua的安装路径和引用相关Lua库 1.右击新

【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!

[COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-16 10:08 30803人阅读 评论(18) 收藏 举报 游戏脚本luaanimationpython 本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/681.htm

在redis中使用lua脚本

在实际工作过程中,可以使用lua脚本来解决一些需要保证原子性的问题,而且lua脚本可以缓存在redis服务器上,势必会增加性能. 不过lua也会有很多限制,在使用的时候要注意. 在Redis中执行Lua脚本有两种方法:eval和evalsha eval EVAL script numkeys key [key ...] arg [arg ...] 其中: <1> script:你的lua脚本 <2> numkeys:  key的个数 <3> key:redis中各种数据

Java中使用Lua脚本语言(转)

Lua是一个实用的脚本语言,相对于Python来说,比较小巧,但它功能并不逊色,特别是在游戏开发中非常实用(WoW采用的就是Lua作为脚本的).Lua在C\C++的实现我就不多说了,网上随便一搜,到处都是这方面的介绍,我想说的是如何在Java下使用Lua以提高编程效率.增强你的程序可扩展性. 首先,要在Java上使用Lua脚本,必须有关于Lua脚本解释器以及Java程序可以访问这些脚本的相关API,即相关类库.我使用的是一个叫做LuaJava的开源项目,可以在: http://www.keple

在Unity中使用Lua脚本

前言:为什么要用Lua首先要说,所有编程语言里面,我最喜欢的还是C#,VisualStudio+C#,只能说太舒服了.所以说,为什么非要在unity里面用Lua呢?可能主要是闲的蛋疼.....另外还有一些次要原因:方便做功能的热更新:Lua语言的深度和广度都不大,易学易用,可以降低项目成本. C#与Lua互相调用的方案坦白来将,我并没有对现在C#与Lua互相调用的所有库进行一个仔细的调研,大概搜了一下,找到这样几个:slua:https://github.com/pangweiwei/sluaN

在Unity中使用Lua脚本:语言层和游戏逻辑粘合层处理

前言:为什么要用Lua 首先要说,所有编程语言里面,我最喜欢的还是C#,VisualStudio+C#,只能说太舒服了.所以说,为什么非要在Unity里面用Lua呢?可能主要是闲的蛋疼.....另外还有一些次要原因: 方便做功能的热更新: Lua语言的深度和广度都不大,易学易用,可以降低项目成本. C#与Lua互相调用的方案 坦白来将,我并没有对现在C#与Lua互相调用的所有库进行一个仔细的调研,大概搜了一下,找到这样几个: slua:https://github.com/pangweiwei/

Lua脚本编程:Lua语言入门

Lua是一门简单而强大的语言,其本身强大的扩展性使得这门语言在游戏设计等领域发挥着重要的作用.博主曾在Unity3D中使用过这门语言,并且针对Lua和Unity.C++等方面的内容进行了学习和讨论.最近因为在[游戏脚本高级编程]这本书中详细介绍了Lua脚本的相关内容,因此在这里记录下博主的读书心得,方便以后在需要的时候查阅. Lua系统构成 Lua系统由Lua链接库.Luac编译器.Lua解释器三部分构成. * Lua链接库主要由lua.lib和lua.h这两个文件组成.Lua链接库主要负责对自

Linux中的shell脚本编程——函数

概述: 本章节将总结while,for循环语句的特殊用法,在不同的场景当中,更能发挥其功能和价值.除此之外,还会介绍一种特殊的循环语句select语句,实现菜单的作用.最重要的是讲解shell脚本编程中函数的用法.这些内容都要熟练掌握. 一.循环语句的特殊用法: 1.while循环的特殊用法(遍历文件的每一行): □语法:while read line; do 循环体 done < /PATH/FROM/SOMEFILE □意义:依次读取/PATH/FROM/SOMEFILE文件中的每一行,且将