Skip to content

Commit dcaf3a1

Browse files
author
zhuchao-hit
committed
select * from clients/pwds
1 parent c7edff3 commit dcaf3a1

File tree

6 files changed

+130
-17
lines changed

6 files changed

+130
-17
lines changed

lib/admin.lua

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function read_query(packet)
3737
local fields = { }
3838

3939
if string.find(query:lower(), "^select%s+*%s+from%s+backends$") then
40-
fields = {
40+
fields = {
4141
{ name = "backend_ndx",
4242
type = proxy.MYSQL_TYPE_LONG },
4343
{ name = "address",
@@ -147,22 +147,47 @@ function read_query(packet)
147147
type = proxy.MYSQL_TYPE_STRING },
148148
}
149149
end
150-
elseif string.find(query:lower(), "^add%s+client%s+%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?$") then
150+
elseif string.find(query:lower(), "^select%s+*%s+from%s+clients$") then
151+
fields = {
152+
{ name = "client",
153+
type = proxy.MYSQL_TYPE_STRING },
154+
}
155+
for i = 1, #proxy.global.clients do
156+
rows[#rows + 1] = {
157+
proxy.global.clients[i]
158+
}
159+
end
160+
elseif string.find(query:lower(), "^add%s+client%s+(.+)$") then
151161
local client = string.match(query:lower(), "^add%s+client%s+(.+)$")
152162
proxy.global.backends.addclient = client
153163

154164
fields = {
155165
{ name = "status",
156166
type = proxy.MYSQL_TYPE_STRING },
157167
}
158-
elseif string.find(query:lower(), "^remove%s+client%s+%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?$") then
168+
elseif string.find(query:lower(), "^remove%s+client%s+(.+)$") then
159169
local client = string.match(query:lower(), "^remove%s+client%s+(.+)$")
160170
proxy.global.backends.removeclient = client
161171

162172
fields = {
163173
{ name = "status",
164174
type = proxy.MYSQL_TYPE_STRING },
165175
}
176+
elseif string.find(query:lower(), "^select%s+*%s+from%s+pwds$") then
177+
fields = {
178+
{ name = "username",
179+
type = proxy.MYSQL_TYPE_STRING },
180+
{ name = "password",
181+
type = proxy.MYSQL_TYPE_STRING },
182+
}
183+
for i = 1, #proxy.global.pwds do
184+
local user_pwd = proxy.global.pwds[i]
185+
local pos = string.find(user_pwd, ":")
186+
rows[#rows + 1] = {
187+
string.sub(user_pwd, 1, pos-1),
188+
string.sub(user_pwd, pos+1)
189+
}
190+
end
166191
elseif string.find(query:lower(), "^add%s+pwd%s+(.+):(.+)$") then
167192
local pwd = string.match(query:lower(), "^add%s+pwd%s+(.+)$")
168193
proxy.global.backends.addpwd = pwd

plugins/proxy/proxy-plugin.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,7 @@ int network_mysqld_proxy_plugin_apply_config(chassis *chas, chassis_plugin_confi
25142514
}
25152515

25162516
for (i = 0; config->client_ips && config->client_ips[i]; i++) {
2517+
g_hash_table_add(chas->backends->raw_ips, g_strdup(config->client_ips[i]));
25172518
guint* sum = g_new0(guint, 1);
25182519
char* token;
25192520
while ((token = strsep(&config->client_ips[i], ".")) != NULL) {
@@ -2596,7 +2597,7 @@ int network_mysqld_proxy_plugin_apply_config(chassis *chas, chassis_plugin_confi
25962597
GString* hashed_password = g_string_new(NULL);
25972598
network_mysqld_proto_password_hash(hashed_password, raw_pwd, strlen(raw_pwd));
25982599
g_hash_table_insert(config->pwd_table[config->pwd_table_index], g_strdup(user), hashed_password);
2599-
g_hash_table_insert(chas->backends->raw_pwds, g_strdup(user), raw_pwd);
2600+
g_hash_table_insert(chas->backends->raw_pwds, g_strdup(user), g_strdup_printf("%s:%s", user, raw_pwd));
26002601
} else {
26012602
g_critical("password decrypt failed");
26022603
return -1;

src/network-backend-lua.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,38 @@ static int proxy_backends_get(lua_State *L) {
137137
return 1;
138138
}
139139

140+
static int proxy_clients_get(lua_State *L) {
141+
GHashTable *raw_ips = *(GHashTable **)luaL_checkself(L);
142+
int index = luaL_checkinteger(L, 2); /** lua is indexes from 1, C from 0 */
143+
144+
GHashTableIter iter;
145+
g_hash_table_iter_init(&iter, raw_ips);
146+
gchar *ip = NULL;
147+
int i;
148+
for (i = 0; i < index; ++i) {
149+
g_hash_table_iter_next(&iter, &ip, NULL);
150+
}
151+
152+
lua_pushlstring(L, ip, strlen(ip));
153+
return 1;
154+
}
155+
156+
static int proxy_pwds_get(lua_State *L) {
157+
GHashTable *raw_pwds = *(GHashTable **)luaL_checkself(L);
158+
int index = luaL_checkinteger(L, 2); /** lua is indexes from 1, C from 0 */
159+
160+
GHashTableIter iter;
161+
g_hash_table_iter_init(&iter, raw_pwds);
162+
gchar *user_pwd = NULL;
163+
int i;
164+
for (i = 0; i < index; ++i) {
165+
g_hash_table_iter_next(&iter, NULL, &user_pwd);
166+
}
167+
168+
lua_pushlstring(L, user_pwd, strlen(user_pwd));
169+
return 1;
170+
}
171+
140172
/**
141173
* set proxy.global.backends.addslave
142174
*
@@ -185,9 +217,19 @@ static int proxy_backends_set(lua_State *L) {
185217

186218
static int proxy_backends_len(lua_State *L) {
187219
network_backends_t *bs = *(network_backends_t **)luaL_checkself(L);
188-
189220
lua_pushinteger(L, network_backends_count(bs));
221+
return 1;
222+
}
223+
224+
static int proxy_clients_len(lua_State *L) {
225+
GHashTable *raw_ips = *(GHashTable **)luaL_checkself(L);
226+
lua_pushinteger(L, g_hash_table_size(raw_ips));
227+
return 1;
228+
}
190229

230+
static int proxy_pwds_len(lua_State *L) {
231+
GHashTable *raw_pwds = *(GHashTable **)luaL_checkself(L);
232+
lua_pushinteger(L, g_hash_table_size(raw_pwds));
191233
return 1;
192234
}
193235

@@ -201,3 +243,23 @@ int network_backends_lua_getmetatable(lua_State *L) {
201243

202244
return proxy_getmetatable(L, methods);
203245
}
246+
247+
int network_clients_lua_getmetatable(lua_State *L) {
248+
static const struct luaL_reg methods[] = {
249+
{ "__index", proxy_clients_get },
250+
{ "__len", proxy_clients_len },
251+
{ NULL, NULL },
252+
};
253+
254+
return proxy_getmetatable(L, methods);
255+
}
256+
257+
int network_pwds_lua_getmetatable(lua_State *L) {
258+
static const struct luaL_reg methods[] = {
259+
{ "__index", proxy_pwds_get },
260+
{ "__len", proxy_pwds_len },
261+
{ NULL, NULL },
262+
};
263+
264+
return proxy_getmetatable(L, methods);
265+
}

src/network-backend.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ network_backends_t *network_backends_new(guint event_thread_count, gchar *defaul
7373
bs->global_wrr = g_wrr_poll_new();
7474
bs->event_thread_count = event_thread_count;
7575
bs->default_file = g_strdup(default_file);
76+
bs->raw_ips = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
7677
bs->raw_pwds = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
7778

7879
return bs;
@@ -113,6 +114,9 @@ void network_backends_free(network_backends_t *bs) {
113114
g_wrr_poll_free(bs->global_wrr);
114115
g_free(bs->default_file);
115116

117+
g_hash_table_remove_all(bs->raw_ips);
118+
g_hash_table_destroy(bs->raw_ips);
119+
116120
g_hash_table_remove_all(bs->raw_pwds);
117121
g_hash_table_destroy(bs->raw_pwds);
118122

@@ -142,6 +146,8 @@ void copy_pwd(gchar *key, GString *value, GHashTable *table) {
142146
}
143147

144148
int network_backends_addclient(network_backends_t *bs, gchar *address) {
149+
g_hash_table_add(bs->raw_ips, g_strdup(address));
150+
145151
guint* sum = g_new0(guint, 1);
146152
char* token;
147153
while ((token = strsep(&address, ".")) != NULL) {
@@ -186,12 +192,14 @@ int network_backends_addpwd(network_backends_t *bs, gchar *address) {
186192
g_hash_table_insert(new_table, g_strdup(user), hashed_password);
187193
g_atomic_int_set(bs->pwd_table_index, 1-index);
188194

189-
g_hash_table_insert(bs->raw_pwds, g_strdup(user), g_strdup(pwd));
195+
g_hash_table_insert(bs->raw_pwds, g_strdup(user), g_strdup_printf("%s:%s", user, pwd));
190196

191197
return 0;
192198
}
193199

194200
int network_backends_removeclient(network_backends_t *bs, gchar *address) {
201+
g_hash_table_remove(bs->raw_ips, address);
202+
195203
guint sum;
196204
char* token;
197205
while ((token = strsep(&address, ".")) != NULL) {
@@ -294,11 +302,9 @@ int network_backends_save(network_backends_t *bs) {
294302
for (i = 0; i < len; ++i) {
295303
network_backend_t *backend = g_ptr_array_index(backends, i);
296304
if (backend->type == BACKEND_TYPE_RW) {
297-
g_string_append_c(master, ',');
298-
g_string_append(master, backend->addr->name->str);
305+
g_string_append_printf(master, ",%s", backend->addr->name->str);
299306
} else if (backend->type == BACKEND_TYPE_RO) {
300-
g_string_append_c(slave, ',');
301-
g_string_append(slave, backend->addr->name->str);
307+
g_string_append_printf(slave, ",%s", backend->addr->name->str);
302308
}
303309
}
304310
g_mutex_unlock(bs->backends_mutex);
@@ -317,9 +323,15 @@ int network_backends_save(network_backends_t *bs) {
317323
g_string_free(master, TRUE);
318324
g_string_free(slave, TRUE);
319325

326+
GHashTableIter iter;
327+
320328
GString *client_ips = g_string_new(NULL);
321-
GHashTable *ip_table = bs->ip_table[*(bs->ip_table_index)];
322-
g_hash_table_foreach(ip_table, append_key, client_ips);
329+
330+
g_hash_table_iter_init(&iter, bs->raw_ips);
331+
gchar *client_ip = NULL;
332+
while (g_hash_table_iter_next(&iter, &client_ip, NULL)) {
333+
g_string_append_printf(client_ips, ",%s", client_ip);
334+
}
323335

324336
if (client_ips->len != 0) {
325337
g_key_file_set_value(keyfile, "mysql-proxy", "client-ips", client_ips->str+1);
@@ -329,12 +341,12 @@ int network_backends_save(network_backends_t *bs) {
329341

330342
g_string_free(client_ips, TRUE);
331343

332-
GHashTableIter iter;
333-
g_hash_table_iter_init(&iter, bs->raw_pwds);
334-
gchar *user = NULL, *pwd = NULL;
335-
336344
GString *pwds = g_string_new(NULL);
337-
while (g_hash_table_iter_next(&iter, &user, &pwd)) {
345+
346+
g_hash_table_iter_init(&iter, bs->raw_pwds);
347+
gchar *user = NULL, *user_pwd = NULL;
348+
while (g_hash_table_iter_next(&iter, &user, &user_pwd)) {
349+
char *pwd = strchr(user_pwd, ':') + 1;
338350
gchar *encrypt_pwd = encrypt(pwd);
339351
g_string_append_printf(pwds, ",%s:%s", user, encrypt_pwd);
340352
g_free(encrypt_pwd);

src/network-backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ typedef struct {
7777
gchar *default_file;
7878
GHashTable **ip_table;
7979
gint *ip_table_index;
80+
GHashTable *raw_ips;
8081
GHashTable **pwd_table;
8182
gint *pwd_table_index;
8283
GHashTable *raw_pwds;

src/network-mysqld-lua.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@ void network_mysqld_lua_setup_global(lua_State *L , chassis *chas) {
226226

227227
lua_setfield(L, -2, "backends");
228228

229+
GHashTable **raw_ips_p = lua_newuserdata(L, sizeof(GHashTable *));
230+
*raw_ips_p = chas->backends->raw_ips;
231+
network_clients_lua_getmetatable(L);
232+
lua_setmetatable(L, -2);
233+
lua_setfield(L, -2, "clients");
234+
235+
GHashTable **raw_pwds_p = lua_newuserdata(L, sizeof(GHashTable *));
236+
*raw_pwds_p = chas->backends->raw_pwds;
237+
network_pwds_lua_getmetatable(L);
238+
lua_setmetatable(L, -2);
239+
lua_setfield(L, -2, "pwds");
240+
229241
lua_pop(L, 2); /* _G.proxy.global and _G.proxy */
230242

231243
g_assert(lua_gettop(L) == stack_top);

0 commit comments

Comments
 (0)