diff --git a/src/redis-acl.c b/src/redis-acl.c index 1697629..c54d8f9 100644 --- a/src/redis-acl.c +++ b/src/redis-acl.c @@ -7,53 +7,49 @@ static RedisModuleCommandFilter *filter; void AuthFilter_CommandFilter(RedisModuleCommandFilter *filter) { int log = 0; int pos = 0; - RedisModule_Log(NULL, LOG_LEVL_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_LEVL_NOTICE, "str=%s,len=%d", arg_str, arg_len); - // 解密 - pos++; - } - RedisModuleUser *user = RedisModule_CreateModuleUser("default"); - if (user == NULL) { - RedisModule_Log(NULL, LOG_LEVL_NOTICE, "user is null"); + 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); + // 解密 + pos++; + } + RedisModuleUser *user = RedisModule_CreateModuleUser("default"); + if (user == NULL) { + RedisModule_Log(NULL, LOG_LEVEL_NOTICE, "user is null"); - } + } } int AuthCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { - return REDISMODULE_OK; -} - -void create_users(RedisModuleCtx *ctx) { - RedisModuleUser *user = RedisModule_CreateModuleUser("default"); - RedisModule_SetModuleUserACL(user, "allcommands"); - RedisModule_SetModuleUserACL(user, "allkeys"); - RedisModule_SetModuleUserACL(user, "on"); - RedisModule_Log(ctx, LOG_LEVL_NOTICE, "init module user success!"); - RedisModule_FreeModuleUser(user); + return REDISMODULE_OK; } int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { - REDISMODULE_NOT_USED(argv); + REDISMODULE_NOT_USED(argv); REDISMODULE_NOT_USED(argc); - if (RedisModule_Init(ctx, "redis-auth", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) { - RedisModule_Log(ctx, LOG_LEVL_NOTICE, "init redis-auth failed"); - return REDISMODULE_ERR; - } - - create_users(ctx); + if (RedisModule_Init(ctx, "redis-auth", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) { + RedisModule_Log(ctx, LOG_LEVEL_NOTICE, "init redis-auth failed"); + return REDISMODULE_ERR; + } filter = RedisModule_RegisterCommandFilter(ctx, AuthFilter_CommandFilter, 0); - if (filter == NULL) { - return REDISMODULE_ERR; - } + if (filter == NULL) { + RedisModule_Log(ctx, LOG_LEVEL_WARNING, "init filter failed"); + return REDISMODULE_ERR; + } - RedisModule_Log(ctx, LOG_LEVL_NOTICE, "init redis-auth success!"); - return REDISMODULE_OK; + 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 redis-auth success!"); + return REDISMODULE_OK; } diff --git a/src/redis-acl.h b/src/redis-acl.h index 641518a..eb28d51 100644 --- a/src/redis-acl.h +++ b/src/redis-acl.h @@ -5,7 +5,8 @@ /* Error status return values. */ #define REDISMODULE_OK 0 #define REDISMODULE_ERR 1 -#define LOG_LEVL_NOTICE "notice" +#define LOG_LEVEL_NOTICE "notice" +#define LOG_LEVEL_WARNING "warning" /* * * Redis Auth command