-
Notifications
You must be signed in to change notification settings - Fork 9
/
debug.lua
100 lines (91 loc) · 2.93 KB
/
debug.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
-- MySQL Proxy script for development/debugging purposes
--
-- Written by Patrick Allaert <patrickallaert@php.net>
-- Copyright © 2009-2011 Libereco Technologies
--
-- 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, either version 3 of the License, or
-- (at your option) any later version.
--
-- 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, see <http://www.gnu.org/licenses/>.
use_sql_no_cache = 0
function nocache(query)
local word = string.upper(string.sub(query,1,6))
if ( word == "SELECT" and string.find(query, "SQL_NO_CACHE") == nil ) then
query = "SELECT SQL_NO_CACHE" .. query:sub(7)
end
return query
end
transaction_counter = 0
function read_query( packet )
if packet:byte() ~= proxy.COM_QUERY then
return
end
local query = packet:sub(2)
if ( use_sql_no_cache == 1 ) then
query = nocache(query)
end
proxy.queries:append(1, string.char(proxy.COM_QUERY) .. query, {resultset_is_needed = true})
return proxy.PROXY_SEND_QUERY
end
function read_query_result (inj)
local res = assert(inj.resultset)
local error_status = ""
local color = ""
if res.flags.no_good_index_used then
error_status = error_status .. "No good index used!"
color = "\27[33m"
end
if res.flags.no_index_used then
error_status = error_status .. "No index used!"
color = "\27[1;33m"
end
local row_count = 0
if res.affected_rows then
row_count = res.affected_rows
else
local num_cols = string.byte(res.raw, 1)
if num_cols > 0 and num_cols < 255 then
for row in inj.resultset.rows do
row_count = row_count + 1
end
end
end
if res.query_status == proxy.MYSQLD_PACKET_ERR then
error_status = string.format("%q", res.raw:sub(10))
color = "\27[1;31m"
end
local query = string.gsub(string.sub(inj.query, 2), "%s+", " ")
local word = string.upper(string.sub(query,1,6))
if word == "UPDATE" or word == "DELETE" or word == "INSERT" then
color = "\27[35m"
elseif word == "COMMIT" then
transaction_counter = transaction_counter - 1
end
local i = 0
while i < transaction_counter do
io.write(" ")
i = i +1
end
if string.upper(string.sub(query,1,5)) == "BEGIN" then
transaction_counter = transaction_counter + 1
end
print(
string.format(
"%s%s\t%s\t\27[0m%s\27[0m\t%s%fms\27[0m",
color,
query,
error_status,
row_count == 0 and "\27[7;31m<NONE>" or row_count,
inj.response_time > 1e5 and "\27[1;31m" or "\27[32m",
inj.response_time / 1e3
)
)
end