在为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()) if packet:sub() == "SELECT 1" then
proxy.queries:append(, 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(, packet, { resultset_is_needed = true } )
return proxy.PROXY_SEND_QUERY
elseif cmd_type == proxy.COM_STMT_EXECUTE then
proxy.queries:append(, packet, { resultset_is_needed = true } )
return proxy.PROXY_SEND_QUERY
elseif cmd_type == proxy.COM_STMT_CLOSE then
proxy.queries:append(, packet, { resultset_is_needed = true } )
return proxy.PROXY_SEND_QUERY
end
end function read_query_result(inj)
if inj.id == 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() == 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 == 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 == 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,对其不了解可以查看下面的连接。

戳我

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

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

    第一步(环境准备工作): 工具: ●LuaForWindows_v5.1.4-46.exe傻瓜式安装. 作用:此工具可以在windows环境下编译运行Lua脚本程序.安装完成后会有两个图标:Lua和S ...

  2. 在redis中使用lua脚本

    在实际工作过程中,可以使用lua脚本来解决一些需要保证原子性的问题,而且lua脚本可以缓存在redis服务器上,势必会增加性能. 不过lua也会有很多限制,在使用的时候要注意. 在Redis中执行Lu ...

  3. redis中使用lua脚本

    lua脚本 Lua是一个高效的轻量级脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能 使用脚本的好处 1.减少网络开销,在Lua脚 ...

  4. 新姿势!Redis中调用Lua脚本以实现原子性操作

    背景:有一服务提供者Leader,有多个消息订阅者Workers.Leader是一个排队程序,维护了一个用户队列,当某个资源空闲下来并被分配至队列中的用户时,Leader会向订阅者推送消息(消息带有唯 ...

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

    [COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-1 ...

  6. 怎样在Cocos2d-x中使用Lua脚本

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013321328/article/details/25699545 笔者使用的是Cocos2d-x ...

  7. 服务降级 托底预案 Nginx中使用Lua脚本检测CPU使用率,当达到阀值时开启限流,让用户排队

    https://mp.weixin.qq.com/s/FZAcQQAKomGEe95kln1HCQ 在京东我们是如何做服务降级的 https://mp.weixin.qq.com/s/FZAcQQAK ...

  8. Redis中的原子操作(2)-redis中使用Lua脚本保证命令原子性

    Redis 如何应对并发访问 使用 Lua 脚本 Redis 中如何使用 Lua 脚本 EVAL EVALSHA SCRIPT 命令 SCRIPT LOAD SCRIPT EXISTS SCRIPT ...

  9. 在redis一致性hash(shard)中使用lua脚本的坑

    redis 2.8之前的版本,为了实现支持巨量数据缓存或者持久化,一般需要通过redis sharding模式来实现redis集群,普遍大家使用的是twitter开源的Twemproxy. twemp ...

随机推荐

  1. Raphael实现商品来源去向图

    数据可视化 是很多大数据分析的一项重要工作,甚至有专门的团队做这项工作.Web上的各种图形(饼状图,柱状图等)一直被flash所垄断,随着HTML5的发展,SVG和Canvas也逐渐走上舞台.这不,产 ...

  2. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  3. JAVASE笔记回顾

    第一部分,JAVA基础和面向对象 part01 入门与开发环境搭建 1: 计算机基础知识(了解)(1)计算机(2)计算机硬件(3)计算机软件系统软件:windows,linux,mac应用软件:QQ, ...

  4. sqlite-jdbc jar包下载过程笔记

    在网络上找一些开源的jar包和对应的源码时,官网上往往是最为安全,版本最新.但大部分的网站都为英文网站,有时候定位下载地址稍有困难,下面是网上找sqlite-jdbc jar包过程,记录一下,以供参考 ...

  5. python3使用套接字遇到TypeError: 'str' does not support the buffer interface如何解决

    这是我查看的博客 http://blog.csdn.net/chuanchuan608/article/details/17915959 直接引用里面的关键语句: When you use clien ...

  6. HDU 3333 Turing Tree --树状数组+离线处理

    题意:统计一段序列[L,R]的和,重复元素只算一次. 解法:容易看出在线做很难处理重复的情况,干脆全部讲查询读进来,然后将查询根据右端点排个序,然后离散化数据以后就可以操作了. 每次读入一个数,如果这 ...

  7. URAL 1416 Confidential --最小生成树与次小生成树

    题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...

  8. MySQL的重装问题解决方法

    最近在工作上遇到了在Windows环境中将MySQL重装的问题,今天记录一下我的解决过程. 首先因为某些原因,我不得不把mysql卸载,然后重装,不论我用控制面板的卸载删除程序方式还是安全卫士的卸载, ...

  9. java9-5 修饰符

    1. 修饰符: 权限修饰符:private,默认的,protected,public 状态修饰符:static,final 抽象修饰符:abstract 类: 权限修饰符:默认修饰符,public 状 ...

  10. 开坑,Unix环境高级编程,转行之路又得缓缓了

    不要问我基础,我用了近6年的Linux系统,最早的版本可以追溯到Ubuntu 8.04,常用的命令 VIM基本上是没压力,遇到问题google 配置环境变量 网络环境也不在话下, C语法基本熟练,过去 ...