支持过滤auth命令

This commit is contained in:
LingZhaoHui 2023-05-14 19:24:54 +08:00
parent 51a76f3e12
commit 55b70e6bb2
Signed by: zeekling
GPG Key ID: D96E4E75267CA2CC

View File

@ -3,32 +3,49 @@
#include "redismodule.h" #include "redismodule.h"
static RedisModuleCommandFilter *filter; static RedisModuleCommandFilter *filter;
static int time = 1;
static int MAX_TIME = 1000;
void AuthFilter_CommandFilter(RedisModuleCommandFilter *filter) { void AuthFilter_CommandFilter(RedisModuleCommandFilter *filter) {
int log = 0;
int pos = 0; int pos = 0;
RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "command filter"); RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "command filter");
while (pos < RedisModule_CommandFilterArgsCount(filter)) { while (pos < RedisModule_CommandFilterArgsCount(filter)) {
const RedisModuleString *arg = RedisModule_CommandFilterArgGet(filter, pos); const RedisModuleString *arg = RedisModule_CommandFilterArgGet(filter, pos);
size_t arg_len; size_t arg_len;
const char *arg_str = RedisModule_StringPtrLen(arg, &arg_len); const char *arg_str = RedisModule_StringPtrLen(arg, &arg_len);
RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "str=%s,len=%d", arg_str, arg_len); RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "str=%s,len=%ld", arg_str, arg_len);
if (strcmp(arg_str, "auth") == 0) {
RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "command is auth");
RedisModule_CommandFilterArgReplace(filter, pos, RedisModule_CreateString(NULL, "acl.auth", 9));
}
// 解密 // 解密
pos++; pos++;
} }
RedisModuleUser *user = RedisModule_CreateModuleUser("default"); RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "filter finished");
if (user == NULL) {
RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "user is null");
}
} }
int AuthCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { int AuthCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
REDISMODULE_NOT_USED(ctx);
REDISMODULE_NOT_USED(argc);
REDISMODULE_NOT_USED(argv);
RedisModule_Log(ctx, LOG_LEVEL_NOTICE, "acl.auth begin");
RedisModule_ReplyWithCString(ctx, "ok");
return REDISMODULE_OK; return REDISMODULE_OK;
} }
void cronLoopCallBack(RedisModuleCtx *ctx, RedisModuleEvent *e, uint64_t sub, void *data) {
REDISMODULE_NOT_USED(e);
RedisModuleCronLoop *ei = data;
REDISMODULE_NOT_USED(ei);
REDISMODULE_NOT_USED(sub);
if (time < MAX_TIME) {
time++;
return;
}
RedisModule_Log(ctx, LOG_LEVEL_NOTICE, "cron event");
time = 0;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
REDISMODULE_NOT_USED(argv); REDISMODULE_NOT_USED(argv);
REDISMODULE_NOT_USED(argc); REDISMODULE_NOT_USED(argc);
@ -42,12 +59,15 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
RedisModule_Log(ctx, LOG_LEVEL_WARNING, "init filter failed"); RedisModule_Log(ctx, LOG_LEVEL_WARNING, "init filter failed");
return REDISMODULE_ERR; return REDISMODULE_ERR;
} }
RedisModule_Log(ctx, LOG_LEVEL_NOTICE, "init filter success");
if (RedisModule_CreateCommand(ctx, "acl.auth", AuthCommand_RedisCommand, if (RedisModule_CreateCommand(ctx, "acl.auth", AuthCommand_RedisCommand,
"no-auth", 0,0,0) == REDISMODULE_ERR) { "no-auth", 0,0,0) == REDISMODULE_ERR) {
RedisModule_Log(ctx, LOG_LEVEL_WARNING, "init acl.auth failed"); RedisModule_Log(ctx, LOG_LEVEL_WARNING, "init acl.auth failed");
return REDISMODULE_ERR; return REDISMODULE_ERR;
} }
RedisModule_Log(ctx, LOG_LEVEL_NOTICE, "init command success");
RedisModule_SubscribeToServerEvent(ctx, RedisModuleEvent_CronLoop, cronLoopCallBack);
RedisModule_Log(ctx, LOG_LEVEL_NOTICE, "init redis-auth success!"); RedisModule_Log(ctx, LOG_LEVEL_NOTICE, "init redis-auth success!");
return REDISMODULE_OK; return REDISMODULE_OK;