From 55b70e6bb2b2d0c41cbd579fb0cd8d36801b587f Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 14 May 2023 19:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=BF=87=E6=BB=A4auth?= =?UTF-8?q?=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/redis-acl.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/redis-acl.c b/src/redis-acl.c index c54d8f9..8ac33a8 100644 --- a/src/redis-acl.c +++ b/src/redis-acl.c @@ -3,32 +3,49 @@ #include "redismodule.h" static RedisModuleCommandFilter *filter; +static int time = 1; +static int MAX_TIME = 1000; void AuthFilter_CommandFilter(RedisModuleCommandFilter *filter) { - int log = 0; int pos = 0; RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "command filter"); while (pos < RedisModule_CommandFilterArgsCount(filter)) { const RedisModuleString *arg = RedisModule_CommandFilterArgGet(filter, pos); size_t 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++; } - RedisModuleUser *user = RedisModule_CreateModuleUser("default"); - if (user == NULL) { - RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "user is null"); - - } - + RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "filter finished"); } 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; } +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) { REDISMODULE_NOT_USED(argv); 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"); return REDISMODULE_ERR; } + RedisModule_Log(ctx, LOG_LEVEL_NOTICE, "init filter success"); if (RedisModule_CreateCommand(ctx, "acl.auth", AuthCommand_RedisCommand, "no-auth", 0,0,0) == REDISMODULE_ERR) { RedisModule_Log(ctx, LOG_LEVEL_WARNING, "init acl.auth failed"); 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!"); return REDISMODULE_OK;