forked from Qihoo360/Atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.lua
302 lines (267 loc) · 9.17 KB
/
admin.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
--[[ $%BEGINLICENSE%$
Copyright (c) 2008, 2009, 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%$ --]]
function set_error(errmsg)
proxy.response = {
type = proxy.MYSQLD_PACKET_ERR,
errmsg = errmsg or "error"
}
end
function read_query(packet)
if packet:byte() ~= proxy.COM_QUERY then
set_error("[admin] we only handle text-based queries (COM_QUERY)")
return proxy.PROXY_SEND_RESULT
end
local query = packet:sub(2)
local rows = { }
local fields = { }
if string.find(query:lower(), "^select%s+*%s+from%s+backends$") then
fields = {
{ name = "backend_ndx",
type = proxy.MYSQL_TYPE_LONG },
{ name = "address",
type = proxy.MYSQL_TYPE_STRING },
{ name = "state",
type = proxy.MYSQL_TYPE_STRING },
{ name = "type",
type = proxy.MYSQL_TYPE_STRING },
}
for i = 1, #proxy.global.backends do
local states = {
"unknown",
"up",
"down",
"offline"
}
local types = {
"unknown",
"rw",
"ro"
}
local b = proxy.global.backends[i]
rows[#rows + 1] = {
i,
b.dst.name, -- configured backend address
states[b.state + 1], -- the C-id is pushed down starting at 0
types[b.type + 1], -- the C-id is pushed down starting at 0
}
end
elseif string.find(query:lower(), "^set%s+%a+%s+%d+$") then
local state,id = string.match(query:lower(), "^set%s+(%a+)%s+(%d+)$")
if proxy.global.backends[id] == nil then
set_error("backend id is not exsit")
return proxy.PROXY_SEND_RESULT
end
if state == "offline" then
proxy.global.backends[id].state = 3
elseif state == "online" then
proxy.global.backends[id].state = 0
else
set_error("invalid operation")
return proxy.PROXY_SEND_RESULT
end
fields = {
{ name = "backend_ndx",
type = proxy.MYSQL_TYPE_LONG },
{ name = "address",
type = proxy.MYSQL_TYPE_STRING },
{ name = "state",
type = proxy.MYSQL_TYPE_STRING },
{ name = "type",
type = proxy.MYSQL_TYPE_STRING },
}
local states = {
"unknown",
"up",
"down",
"offline"
}
local types = {
"unknown",
"rw",
"ro"
}
local b = proxy.global.backends[id]
rows[#rows + 1] = {
id,
b.dst.name, -- configured backend address
states[b.state + 1], -- the C-id is pushed down starting at 0
types[b.type + 1], -- the C-id is pushed down starting at 0
}
elseif string.find(query:lower(), "^add%s+master%s+%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?:%d%d?%d?%d?%d?$") then
local newserver = string.match(query:lower(), "^add%s+master%s+(.+)$")
proxy.global.backends.addmaster = newserver
if proxy.global.config.rwsplit then proxy.global.config.rwsplit.max_weight = -1 end
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
elseif string.find(query:lower(), "^add%s+slave%s+%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?:%d%d?%d?%d?%d?$") then
local newserver = string.match(query:lower(), "^add%s+slave%s+(.+)$")
proxy.global.backends.addslave = newserver
if proxy.global.config.rwsplit then proxy.global.config.rwsplit.max_weight = -1 end
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
elseif string.find(query:lower(), "^remove%s+backend%s+%d+$") then
local newserver = tonumber(string.match(query:lower(), "^remove%s+backend%s+(%d+)$"))
if newserver <= 0 or newserver > #proxy.global.backends then
set_error("invalid backend_id")
return proxy.PROXY_SEND_RESULT
else
proxy.global.backends.removebackend = newserver - 1
if proxy.global.config.rwsplit then proxy.global.config.rwsplit.max_weight = -1 end
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
end
elseif string.find(query:lower(), "^select%s+*%s+from%s+clients$") then
fields = {
{ name = "client",
type = proxy.MYSQL_TYPE_STRING },
}
for i = 1, #proxy.global.clients do
rows[#rows + 1] = {
proxy.global.clients[i]
}
end
elseif string.find(query:lower(), "^add%s+client%s+(.+)$") then
local client = string.match(query:lower(), "^add%s+client%s+(.+)$")
if proxy.global.clients(client) == 1 then
set_error("this client is exist")
return proxy.PROXY_SEND_RESULT
end
proxy.global.backends.addclient = client
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
elseif string.find(query:lower(), "^remove%s+client%s+(.+)$") then
local client = string.match(query:lower(), "^remove%s+client%s+(.+)$")
if proxy.global.clients(client) == 0 then
set_error("this client is NOT exist")
return proxy.PROXY_SEND_RESULT
end
proxy.global.backends.removeclient = client
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
elseif string.find(query:lower(), "^select%s+*%s+from%s+pwds$") then
fields = {
{ name = "username",
type = proxy.MYSQL_TYPE_STRING },
{ name = "password",
type = proxy.MYSQL_TYPE_STRING },
}
for i = 1, #proxy.global.pwds do
local user_pwd = proxy.global.pwds[i]
local pos = string.find(user_pwd, ":")
rows[#rows + 1] = {
string.sub(user_pwd, 1, pos-1),
string.sub(user_pwd, pos+1)
}
end
elseif string.find(query, "^[aA][dD][dD]%s+[pP][wW][dD]%s+(.+):(.+)$") then
local user, pwd = string.match(query, "^[aA][dD][dD]%s+[pP][wW][dD]%s+(.+):(.+)$")
local ret = proxy.global.backends(user, pwd, 1)
if ret == 1 then
set_error("this user is exist")
return proxy.PROXY_SEND_RESULT
end
if ret == 2 then
set_error("failed to encrypt")
return proxy.PROXY_SEND_RESULT
end
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
elseif string.find(query, "^[aA][dD][dD]%s+[eE][nN][pP][wW][dD]%s+(.+):(.+)$") then
local user, pwd = string.match(query, "^[aA][dD][dD]%s+[eE][nN][pP][wW][dD]%s+(.+):(.+)$")
local ret = proxy.global.backends(user, pwd, 2)
if ret == 1 then
set_error("this user is exist")
return proxy.PROXY_SEND_RESULT
end
if ret == 2 then
set_error("failed to decrypt")
return proxy.PROXY_SEND_RESULT
end
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
elseif string.find(query, "^[rR][eE][mM][oO][vV][eE]%s+[pP][wW][dD]%s+(.+)$") then
local user = string.match(query, "^[rR][eE][mM][oO][vV][eE]%s+[pP][wW][dD]%s+(.+)$")
local ret = proxy.global.backends(user, nil, 3)
if ret == 1 then
set_error("this user is NOT exist")
return proxy.PROXY_SEND_RESULT
end
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
elseif string.find(query:lower(), "^save%s+config+$") then
proxy.global.backends.saveconfig = 0
fields = {
{ name = "status",
type = proxy.MYSQL_TYPE_STRING },
}
elseif string.find(query:lower(), "^select%s+version+$") then
fields = {
{ name = "version",
type = proxy.MYSQL_TYPE_STRING },
}
rows[#rows + 1] = { "2.2.2" }
elseif string.find(query:lower(), "^select%s+*%s+from%s+help$") then
fields = {
{ name = "command",
type = proxy.MYSQL_TYPE_STRING },
{ name = "description",
type = proxy.MYSQL_TYPE_STRING },
}
rows[#rows + 1] = { "SELECT * FROM help", "shows this help" }
rows[#rows + 1] = { "SELECT * FROM backends", "lists the backends and their state" }
rows[#rows + 1] = { "SET OFFLINE $backend_id", "offline backend server, $backend_id is backend_ndx's id" }
rows[#rows + 1] = { "SET ONLINE $backend_id", "online backend server, ..." }
rows[#rows + 1] = { "ADD MASTER $backend", "example: \"add master 127.0.0.1:3306\", ..." }
rows[#rows + 1] = { "ADD SLAVE $backend", "example: \"add slave 127.0.0.1:3306\", ..." }
rows[#rows + 1] = { "REMOVE BACKEND $backend_id", "example: \"remove backend 1\", ..." }
rows[#rows + 1] = { "SELECT * FROM clients", "lists the clients" }
rows[#rows + 1] = { "ADD CLIENT $client", "example: \"add client 192.168.1.2\", ..." }
rows[#rows + 1] = { "REMOVE CLIENT $client", "example: \"remove client 192.168.1.2\", ..." }
rows[#rows + 1] = { "SELECT * FROM pwds", "lists the pwds" }
rows[#rows + 1] = { "ADD PWD $pwd", "example: \"add pwd user:raw_password\", ..." }
rows[#rows + 1] = { "ADD ENPWD $pwd", "example: \"add enpwd user:encrypted_password\", ..." }
rows[#rows + 1] = { "REMOVE PWD $pwd", "example: \"remove pwd user\", ..." }
rows[#rows + 1] = { "SAVE CONFIG", "save the backends to config file" }
rows[#rows + 1] = { "SELECT VERSION", "display the version of Atlas" }
else
set_error("use 'SELECT * FROM help' to see the supported commands")
return proxy.PROXY_SEND_RESULT
end
proxy.response = {
type = proxy.MYSQLD_PACKET_OK,
resultset = {
fields = fields,
rows = rows
}
}
return proxy.PROXY_SEND_RESULT
end