@@ -155,6 +155,7 @@ int ACCEPT_Insert_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, in
155
155
close (fd );
156
156
157
157
RedisModule_StringSet (key , argv [1 ]);
158
+
158
159
size_t newlen = RedisModule_ValueLength (key );
159
160
RedisModule_CloseKey (key );
160
161
RedisModule_ReplyWithLongLong (ctx , newlen );
@@ -186,6 +187,82 @@ int ACCEPT_Delete_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, in
186
187
return REDISMODULE_OK ;
187
188
}
188
189
190
+ int TTL_ACCEPT_Insert_RedisCommand (RedisModuleCtx * ctx , RedisModuleString * * argv , int argc )
191
+ {
192
+ if (argc != 3 )
193
+ return RedisModule_WrongArity (ctx );
194
+ RedisModuleKey * key = RedisModule_OpenKey (ctx , argv [1 ],
195
+ REDISMODULE_READ | REDISMODULE_WRITE );
196
+ long long count ;
197
+ if ((RedisModule_StringToLongLong (argv [2 ],& count ) != REDISMODULE_OK ) ||
198
+ (count < 0 )) {
199
+ return RedisModule_ReplyWithError (ctx ,"ERR invalid count" );
200
+ }
201
+ pid_t pid ;
202
+ int fd ;
203
+ char tmp_buf [4096 ];
204
+
205
+ static char check_command [256 ], insert_command [256 ];
206
+ sprintf (check_command , "iptables -C INPUT -s %s -j ACCEPT" ,
207
+ RedisModule_StringPtrLen (argv [1 ], NULL ));
208
+ sprintf (insert_command , "iptables -I INPUT -s %s -j ACCEPT" ,
209
+ RedisModule_StringPtrLen (argv [1 ], NULL ));
210
+ printf ("%s || %s\n" , RedisModule_StringPtrLen (argv [0 ], NULL ),
211
+ RedisModule_StringPtrLen (argv [1 ], NULL ));
212
+ fd = execute_popen (& pid , check_command );
213
+ redis_waitpid (pid );
214
+ if (0 < read (fd , tmp_buf , sizeof (tmp_buf ) - 1 )) {
215
+ close (fd );
216
+ execute_popen (& pid , insert_command );
217
+ redis_waitpid (pid );
218
+ }
219
+ close (fd );
220
+ RedisModule_StringSet (key , argv [1 ]);
221
+ RedisModule_SetExpire (key , count * 1000 );
222
+ size_t newlen = RedisModule_ValueLength (key );
223
+ RedisModule_CloseKey (key );
224
+ RedisModule_ReplyWithLongLong (ctx , newlen );
225
+ return REDISMODULE_OK ;
226
+ }
227
+
228
+ int TTL_DROP_Insert_RedisCommand (RedisModuleCtx * ctx , RedisModuleString * * argv , int argc )
229
+ {
230
+ if (argc != 3 )
231
+ return RedisModule_WrongArity (ctx );
232
+ RedisModuleKey * key = RedisModule_OpenKey (ctx , argv [1 ],
233
+ REDISMODULE_READ | REDISMODULE_WRITE );
234
+ long long count ;
235
+ if ((RedisModule_StringToLongLong (argv [2 ],& count ) != REDISMODULE_OK ) ||
236
+ (count < 0 )) {
237
+ return RedisModule_ReplyWithError (ctx ,"ERR invalid count" );
238
+ }
239
+ pid_t pid ;
240
+ int fd ;
241
+ char tmp_buf [4096 ];
242
+
243
+ static char check_command [256 ], insert_command [256 ];
244
+ sprintf (check_command , "iptables -C INPUT -s %s -j DROP" ,
245
+ RedisModule_StringPtrLen (argv [1 ], NULL ));
246
+ sprintf (insert_command , "iptables -I INPUT -s %s -j DROP" ,
247
+ RedisModule_StringPtrLen (argv [1 ], NULL ));
248
+ printf ("%s || %s\n" , RedisModule_StringPtrLen (argv [0 ], NULL ),
249
+ RedisModule_StringPtrLen (argv [1 ], NULL ));
250
+ fd = execute_popen (& pid , check_command );
251
+ redis_waitpid (pid );
252
+ if (0 < read (fd , tmp_buf , sizeof (tmp_buf ) - 1 )) {
253
+ close (fd );
254
+ execute_popen (& pid , insert_command );
255
+ redis_waitpid (pid );
256
+ }
257
+ close (fd );
258
+ RedisModule_StringSet (key , argv [1 ]);
259
+ RedisModule_SetExpire (key , count * 1000 );
260
+ size_t newlen = RedisModule_ValueLength (key );
261
+ RedisModule_CloseKey (key );
262
+ RedisModule_ReplyWithLongLong (ctx , newlen );
263
+ return REDISMODULE_OK ;
264
+ }
265
+
189
266
/* This function must be present on each Redis module. It is used in order to
190
267
* register the commands into the Redis server. */
191
268
int RedisModule_OnLoad (RedisModuleCtx * ctx , RedisModuleString * * argv , int argc ) {
@@ -210,7 +287,12 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
210
287
if (RedisModule_CreateCommand (ctx , "accept.delete" ,
211
288
ACCEPT_Delete_RedisCommand , "write deny-oom" , 1 , 1 , 1 ) == REDISMODULE_ERR )
212
289
return REDISMODULE_ERR ;
213
-
290
+ if (RedisModule_CreateCommand (ctx , "ttl.accept.insert" ,
291
+ TTL_ACCEPT_Insert_RedisCommand , "write deny-oom" , 1 , 1 , 1 ) == REDISMODULE_ERR )
292
+ return REDISMODULE_ERR ;
293
+ if (RedisModule_CreateCommand (ctx , "ttl.drop.insert" ,
294
+ TTL_DROP_Insert_RedisCommand , "write deny-oom" , 1 , 1 , 1 ) == REDISMODULE_ERR )
295
+ return REDISMODULE_ERR ;
214
296
215
297
return REDISMODULE_OK ;
216
298
}
0 commit comments