commandfilter.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #define REDISMODULE_EXPERIMENTAL_API
  2. #include "redismodule.h"
  3. #include <string.h>
  4. static RedisModuleString *log_key_name;
  5. static const char log_command_name[] = "commandfilter.log";
  6. static const char ping_command_name[] = "commandfilter.ping";
  7. static const char retained_command_name[] = "commandfilter.retained";
  8. static const char unregister_command_name[] = "commandfilter.unregister";
  9. static int in_log_command = 0;
  10. static RedisModuleCommandFilter *filter;
  11. static RedisModuleString *retained;
  12. int CommandFilter_UnregisterCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  13. {
  14. (void) argc;
  15. (void) argv;
  16. RedisModule_ReplyWithLongLong(ctx,
  17. RedisModule_UnregisterCommandFilter(ctx, filter));
  18. return REDISMODULE_OK;
  19. }
  20. int CommandFilter_PingCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  21. {
  22. (void) argc;
  23. (void) argv;
  24. RedisModuleCallReply *reply = RedisModule_Call(ctx, "ping", "c", "@log");
  25. if (reply) {
  26. RedisModule_ReplyWithCallReply(ctx, reply);
  27. RedisModule_FreeCallReply(reply);
  28. } else {
  29. RedisModule_ReplyWithSimpleString(ctx, "Unknown command or invalid arguments");
  30. }
  31. return REDISMODULE_OK;
  32. }
  33. int CommandFilter_Retained(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  34. {
  35. (void) argc;
  36. (void) argv;
  37. if (retained) {
  38. RedisModule_ReplyWithString(ctx, retained);
  39. } else {
  40. RedisModule_ReplyWithNull(ctx);
  41. }
  42. return REDISMODULE_OK;
  43. }
  44. int CommandFilter_LogCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  45. {
  46. RedisModuleString *s = RedisModule_CreateString(ctx, "", 0);
  47. int i;
  48. for (i = 1; i < argc; i++) {
  49. size_t arglen;
  50. const char *arg = RedisModule_StringPtrLen(argv[i], &arglen);
  51. if (i > 1) RedisModule_StringAppendBuffer(ctx, s, " ", 1);
  52. RedisModule_StringAppendBuffer(ctx, s, arg, arglen);
  53. }
  54. RedisModuleKey *log = RedisModule_OpenKey(ctx, log_key_name, REDISMODULE_WRITE|REDISMODULE_READ);
  55. RedisModule_ListPush(log, REDISMODULE_LIST_HEAD, s);
  56. RedisModule_CloseKey(log);
  57. RedisModule_FreeString(ctx, s);
  58. in_log_command = 1;
  59. size_t cmdlen;
  60. const char *cmdname = RedisModule_StringPtrLen(argv[1], &cmdlen);
  61. RedisModuleCallReply *reply = RedisModule_Call(ctx, cmdname, "v", &argv[2], argc - 2);
  62. if (reply) {
  63. RedisModule_ReplyWithCallReply(ctx, reply);
  64. RedisModule_FreeCallReply(reply);
  65. } else {
  66. RedisModule_ReplyWithSimpleString(ctx, "Unknown command or invalid arguments");
  67. }
  68. in_log_command = 0;
  69. return REDISMODULE_OK;
  70. }
  71. void CommandFilter_CommandFilter(RedisModuleCommandFilterCtx *filter)
  72. {
  73. if (in_log_command) return; /* don't process our own RM_Call() from CommandFilter_LogCommand() */
  74. /* Fun manipulations:
  75. * - Remove @delme
  76. * - Replace @replaceme
  77. * - Append @insertbefore or @insertafter
  78. * - Prefix with Log command if @log encountered
  79. */
  80. int log = 0;
  81. int pos = 0;
  82. while (pos < RedisModule_CommandFilterArgsCount(filter)) {
  83. const RedisModuleString *arg = RedisModule_CommandFilterArgGet(filter, pos);
  84. size_t arg_len;
  85. const char *arg_str = RedisModule_StringPtrLen(arg, &arg_len);
  86. if (arg_len == 6 && !memcmp(arg_str, "@delme", 6)) {
  87. RedisModule_CommandFilterArgDelete(filter, pos);
  88. continue;
  89. }
  90. if (arg_len == 10 && !memcmp(arg_str, "@replaceme", 10)) {
  91. RedisModule_CommandFilterArgReplace(filter, pos,
  92. RedisModule_CreateString(NULL, "--replaced--", 12));
  93. } else if (arg_len == 13 && !memcmp(arg_str, "@insertbefore", 13)) {
  94. RedisModule_CommandFilterArgInsert(filter, pos,
  95. RedisModule_CreateString(NULL, "--inserted-before--", 19));
  96. pos++;
  97. } else if (arg_len == 12 && !memcmp(arg_str, "@insertafter", 12)) {
  98. RedisModule_CommandFilterArgInsert(filter, pos + 1,
  99. RedisModule_CreateString(NULL, "--inserted-after--", 18));
  100. pos++;
  101. } else if (arg_len == 7 && !memcmp(arg_str, "@retain", 7)) {
  102. if (retained) RedisModule_FreeString(NULL, retained);
  103. retained = RedisModule_CommandFilterArgGet(filter, pos + 1);
  104. RedisModule_RetainString(NULL, retained);
  105. pos++;
  106. } else if (arg_len == 4 && !memcmp(arg_str, "@log", 4)) {
  107. log = 1;
  108. }
  109. pos++;
  110. }
  111. if (log) RedisModule_CommandFilterArgInsert(filter, 0,
  112. RedisModule_CreateString(NULL, log_command_name, sizeof(log_command_name)-1));
  113. }
  114. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  115. if (RedisModule_Init(ctx,"commandfilter",1,REDISMODULE_APIVER_1)
  116. == REDISMODULE_ERR) return REDISMODULE_ERR;
  117. if (argc != 2) {
  118. RedisModule_Log(ctx, "warning", "Log key name not specified");
  119. return REDISMODULE_ERR;
  120. }
  121. long long noself = 0;
  122. log_key_name = RedisModule_CreateStringFromString(ctx, argv[0]);
  123. RedisModule_StringToLongLong(argv[1], &noself);
  124. retained = NULL;
  125. if (RedisModule_CreateCommand(ctx,log_command_name,
  126. CommandFilter_LogCommand,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  127. return REDISMODULE_ERR;
  128. if (RedisModule_CreateCommand(ctx,ping_command_name,
  129. CommandFilter_PingCommand,"deny-oom",1,1,1) == REDISMODULE_ERR)
  130. return REDISMODULE_ERR;
  131. if (RedisModule_CreateCommand(ctx,retained_command_name,
  132. CommandFilter_Retained,"readonly",1,1,1) == REDISMODULE_ERR)
  133. return REDISMODULE_ERR;
  134. if (RedisModule_CreateCommand(ctx,unregister_command_name,
  135. CommandFilter_UnregisterCommand,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  136. return REDISMODULE_ERR;
  137. if ((filter = RedisModule_RegisterCommandFilter(ctx, CommandFilter_CommandFilter,
  138. noself ? REDISMODULE_CMDFILTER_NOSELF : 0))
  139. == NULL) return REDISMODULE_ERR;
  140. return REDISMODULE_OK;
  141. }
  142. int RedisModule_OnUnload(RedisModuleCtx *ctx) {
  143. RedisModule_FreeString(ctx, log_key_name);
  144. if (retained) RedisModule_FreeString(NULL, retained);
  145. return REDISMODULE_OK;
  146. }