2024-03-27 11:19:39 +00:00
|
|
|
#include "test_utils.h"
|
|
|
|
|
#include "maat_redis_monitor.h"
|
|
|
|
|
#include "maat_utils.h"
|
|
|
|
|
#include "maat_table.h"
|
2024-08-21 08:39:28 +00:00
|
|
|
#include "maat_core.h"
|
2024-03-27 11:19:39 +00:00
|
|
|
#include "maat_config_monitor.h"
|
|
|
|
|
#include "json2iris.h"
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2024-04-03 16:47:30 +08:00
|
|
|
int line_idx = 0;
|
|
|
|
|
long long absolute_expire_time = 0;
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
count_line_num_cb(const char *table_name, const char *line, void *u_para)
|
2024-03-27 11:19:39 +00:00
|
|
|
{
|
|
|
|
|
(*((unsigned int *)u_para))++;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 16:47:30 +08:00
|
|
|
static int
|
|
|
|
|
make_serial_rule(const char *table_name, const char *line, void *u_para)
|
2024-03-27 11:19:39 +00:00
|
|
|
{
|
|
|
|
|
struct serial_rule *s_rule=(struct serial_rule *)u_para;
|
|
|
|
|
redisContext *ctx = s_rule->ref_ctx;
|
|
|
|
|
char *buff = ALLOC(char, strlen(line) + 1);
|
|
|
|
|
|
|
|
|
|
memcpy(buff, line, strlen(line) + 1);
|
2024-04-03 16:47:30 +08:00
|
|
|
|
|
|
|
|
while (buff[strlen(line) - 1] == '\n' ||
|
|
|
|
|
buff[strlen(line) - 1] == '\t') {
|
2024-03-27 11:19:39 +00:00
|
|
|
buff[strlen(line) - 1] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *redis_rule_key = "TEST_RULE_KEY";
|
2024-04-03 16:47:30 +08:00
|
|
|
redisReply *reply =
|
|
|
|
|
maat_wrap_redis_command(ctx, NULL, "INCRBY %s %d", redis_rule_key, 1);
|
|
|
|
|
|
2024-03-27 11:19:39 +00:00
|
|
|
if (reply->type == REDIS_REPLY_NIL) {
|
|
|
|
|
printf("incrby redis_rule_key:%s failed.", redis_rule_key);
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
s_rule->rule_id = maat_read_redis_integer(reply);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
reply = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maat_set_serial_rule(s_rule + line_idx, MAAT_OP_ADD, s_rule->rule_id,
|
|
|
|
|
table_name, buff, absolute_expire_time);
|
|
|
|
|
(s_rule + line_idx)->ref_ctx = ctx;
|
|
|
|
|
line_idx++;
|
|
|
|
|
|
|
|
|
|
FREE(buff);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-04-03 16:47:30 +08:00
|
|
|
|
|
|
|
|
int write_json_to_iris(const char* json_fn, char *iris_path, size_t path_sz,
|
|
|
|
|
struct log_handle *logger)
|
2024-03-27 11:19:39 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
char *json_buff = NULL;
|
|
|
|
|
size_t json_buff_sz = 0;
|
|
|
|
|
|
|
|
|
|
int ret = load_file_to_memory(json_fn, (unsigned char **)&json_buff,
|
2024-04-03 16:47:30 +08:00
|
|
|
&json_buff_sz);
|
2024-03-27 11:19:39 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 16:47:30 +08:00
|
|
|
ret = json2iris(json_buff, json_fn, NULL, iris_path, path_sz, NULL, NULL, logger);
|
2024-03-27 11:19:39 +00:00
|
|
|
FREE(json_buff);
|
2024-04-03 16:47:30 +08:00
|
|
|
|
2024-03-27 11:19:39 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-04-03 16:47:30 +08:00
|
|
|
|
|
|
|
|
int write_iris_to_redis(const char *iris_path, char *redis_ip, int redis_port,
|
|
|
|
|
int redis_db, struct log_handle *logger)
|
2024-03-27 11:19:39 +00:00
|
|
|
{
|
|
|
|
|
redisContext *c = maat_connect_redis(redis_ip, redis_port, redis_db, logger);
|
|
|
|
|
if (NULL == c) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redisReply *reply = maat_wrap_redis_command(c, logger, "flushdb");
|
|
|
|
|
if (NULL == reply) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
reply = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t total_line_cnt = 0;
|
|
|
|
|
config_monitor_traverse(0, iris_path, NULL, count_line_num_cb,
|
|
|
|
|
NULL, &total_line_cnt, NULL, logger);
|
|
|
|
|
|
|
|
|
|
struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
|
|
|
|
|
s_rule->ref_ctx = c;
|
|
|
|
|
long long server_time = maat_redis_server_time_s(c);
|
|
|
|
|
if (server_time < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
absolute_expire_time = server_time + 300;
|
|
|
|
|
config_monitor_traverse(0, iris_path, NULL, make_serial_rule,
|
|
|
|
|
NULL, s_rule, NULL, logger);
|
|
|
|
|
s_rule->ref_ctx = NULL;
|
|
|
|
|
line_idx = 0;
|
|
|
|
|
absolute_expire_time = 0;
|
|
|
|
|
|
|
|
|
|
int success_cnt = 0;
|
|
|
|
|
do {
|
|
|
|
|
success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt,
|
|
|
|
|
server_time, logger);
|
|
|
|
|
} while (success_cnt < 0);
|
|
|
|
|
|
|
|
|
|
assert(success_cnt == (int)total_line_cnt);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < total_line_cnt; i++) {
|
|
|
|
|
maat_clear_rule_cache(s_rule + i);
|
|
|
|
|
}
|
|
|
|
|
FREE(s_rule);
|
|
|
|
|
redisFree(c);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-04-03 16:47:30 +08:00
|
|
|
|
|
|
|
|
int write_json_to_redis(const char *json_filename, char *redis_ip, int redis_port,
|
|
|
|
|
int redis_db, struct log_handle *logger)
|
2024-03-27 11:19:39 +00:00
|
|
|
{
|
|
|
|
|
char iris_path[512] = {0};
|
2024-04-03 16:47:30 +08:00
|
|
|
|
|
|
|
|
int ret = write_json_to_iris(json_filename, iris_path, sizeof(iris_path), logger);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = write_iris_to_redis(iris_path, redis_ip, redis_port, redis_db, logger);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-03-27 11:19:39 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 03:11:15 +00:00
|
|
|
int rule_table_set_line(struct maat *maat_inst, const char *table_name,
|
|
|
|
|
enum maat_operation op, long long rule_id,
|
2024-03-27 11:19:39 +00:00
|
|
|
const char *user_region, int clause_num,
|
|
|
|
|
int expire_after)
|
|
|
|
|
{
|
|
|
|
|
char table_line[1024 * 16] = {0};
|
|
|
|
|
sprintf(table_line, "%lld\t0\t0\t0\t0\t0\t%s\t%d\t%d\t0.0",
|
2024-08-22 03:11:15 +00:00
|
|
|
rule_id, user_region, clause_num, op);
|
2024-03-27 11:19:39 +00:00
|
|
|
|
|
|
|
|
struct maat_cmd_line line_rule;
|
2024-08-22 03:11:15 +00:00
|
|
|
line_rule.rule_id = rule_id;
|
2024-03-27 11:19:39 +00:00
|
|
|
line_rule.table_line = table_line;
|
|
|
|
|
line_rule.table_name = table_name;
|
|
|
|
|
line_rule.expire_after = expire_after;
|
|
|
|
|
|
|
|
|
|
return maat_cmd_set_line(maat_inst, &line_rule);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 16:47:30 +08:00
|
|
|
#define TO_GROUP2X_KEY(group_id, parent_id, clause_index) \
|
|
|
|
|
(((unsigned long)group_id<<32|parent_id) + clause_index)
|
2024-03-27 11:19:39 +00:00
|
|
|
|
2024-08-22 03:11:15 +00:00
|
|
|
int group2rule_table_set_line(struct maat *maat_inst, const char *table_name,
|
2024-03-27 11:19:39 +00:00
|
|
|
enum maat_operation op, long long group_id,
|
2024-08-22 03:11:15 +00:00
|
|
|
long long rule_id, int not_flag,
|
2024-08-22 06:42:37 +00:00
|
|
|
const char *attribute_name, int clause_index,
|
2024-03-27 11:19:39 +00:00
|
|
|
int expire_after)
|
|
|
|
|
{
|
|
|
|
|
char table_line[128] = {0};
|
|
|
|
|
sprintf(table_line, "%lld\t%lld\t%d\t%s\t%d\t%d",
|
2024-08-22 06:42:37 +00:00
|
|
|
group_id, rule_id, not_flag, attribute_name, clause_index, op);
|
2024-03-27 11:19:39 +00:00
|
|
|
|
|
|
|
|
struct maat_cmd_line line_rule;
|
2024-08-22 03:11:15 +00:00
|
|
|
line_rule.rule_id = TO_GROUP2X_KEY(group_id, rule_id, clause_index);
|
2024-03-27 11:19:39 +00:00
|
|
|
line_rule.table_line = table_line;
|
|
|
|
|
line_rule.table_name = table_name;
|
|
|
|
|
line_rule.expire_after = expire_after;
|
|
|
|
|
|
|
|
|
|
return maat_cmd_set_line(maat_inst, &line_rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int group2group_table_set_line(struct maat *maat_inst, const char *table_name,
|
|
|
|
|
enum maat_operation op, long long group_id,
|
|
|
|
|
long long sub_group_id, int expire_after)
|
|
|
|
|
{
|
|
|
|
|
char table_line[128] = {0};
|
|
|
|
|
sprintf(table_line, "%lld\t%lld\t%s\t%d", group_id, sub_group_id,
|
|
|
|
|
"null", op);
|
|
|
|
|
|
|
|
|
|
struct maat_cmd_line line_rule;
|
|
|
|
|
line_rule.rule_id = TO_GROUP2X_KEY(group_id, sub_group_id, 0);
|
|
|
|
|
line_rule.table_line = table_line;
|
|
|
|
|
line_rule.table_name = table_name;
|
|
|
|
|
line_rule.expire_after = expire_after;
|
|
|
|
|
|
|
|
|
|
return maat_cmd_set_line(maat_inst, &line_rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int expr_table_set_line(struct maat *maat_inst, const char *table_name,
|
|
|
|
|
enum maat_operation op, long long item_id,
|
|
|
|
|
long long group_id, const char *keywords,
|
2024-08-19 11:04:17 +00:00
|
|
|
const char *district, int expr_type, int expire_after)
|
2024-03-27 11:19:39 +00:00
|
|
|
{
|
|
|
|
|
char table_line[1024] = {0};
|
|
|
|
|
int table_id = maat_get_table_id(maat_inst, table_name);
|
|
|
|
|
if (table_id < 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 16:47:30 +08:00
|
|
|
enum table_type table_type =
|
|
|
|
|
table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
|
|
|
|
assert(table_type == TABLE_TYPE_EXPR ||
|
|
|
|
|
table_type == TABLE_TYPE_EXPR_PLUS);
|
2024-03-27 11:19:39 +00:00
|
|
|
|
|
|
|
|
if (table_type == TABLE_TYPE_EXPR_PLUS) {
|
2024-08-19 11:04:17 +00:00
|
|
|
sprintf(table_line, "%lld\t%lld\t%s\t%d\t%s\t%d",
|
|
|
|
|
item_id, group_id, district, expr_type, keywords, op);
|
2024-03-27 11:19:39 +00:00
|
|
|
} else {
|
2024-08-19 11:04:17 +00:00
|
|
|
sprintf(table_line, "%lld\t%lld\t%d\t%s\t%d",
|
|
|
|
|
item_id, group_id, expr_type, keywords, op);
|
2024-03-27 11:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct maat_cmd_line line_rule;
|
|
|
|
|
line_rule.rule_id = item_id;
|
|
|
|
|
line_rule.table_line = table_line;
|
|
|
|
|
line_rule.table_name = table_name;
|
|
|
|
|
line_rule.expire_after = expire_after;
|
|
|
|
|
|
|
|
|
|
return maat_cmd_set_line(maat_inst, &line_rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int interval_table_set_line(struct maat *maat_inst, const char *table_name,
|
|
|
|
|
enum maat_operation op, long long item_id, long long group_id,
|
2024-08-13 03:35:50 +00:00
|
|
|
const char *port_str, const char *district, int expire_after)
|
2024-03-27 11:19:39 +00:00
|
|
|
{
|
|
|
|
|
char table_line[1024] = {0};
|
|
|
|
|
int table_id = maat_get_table_id(maat_inst, table_name);
|
|
|
|
|
if (table_id < 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 16:47:30 +08:00
|
|
|
enum table_type table_type =
|
|
|
|
|
table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
|
|
|
|
assert(table_type == TABLE_TYPE_INTERVAL ||
|
|
|
|
|
table_type == TABLE_TYPE_INTERVAL_PLUS);
|
2024-03-27 11:19:39 +00:00
|
|
|
|
|
|
|
|
if (table_type == TABLE_TYPE_INTERVAL_PLUS) {
|
2024-08-13 03:35:50 +00:00
|
|
|
sprintf(table_line, "%lld\t%lld\t%s\t%s\t%d",
|
|
|
|
|
item_id, group_id, district, port_str, op);
|
2024-03-27 11:19:39 +00:00
|
|
|
} else {
|
2024-08-13 03:35:50 +00:00
|
|
|
sprintf(table_line, "%lld\t%lld\t%s\t%d",
|
|
|
|
|
item_id, group_id, port_str, op);
|
2024-03-27 11:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct maat_cmd_line line_rule;
|
|
|
|
|
line_rule.rule_id = item_id;
|
|
|
|
|
line_rule.table_line = table_line;
|
|
|
|
|
line_rule.table_name = table_name;
|
|
|
|
|
line_rule.expire_after = expire_after;
|
|
|
|
|
|
|
|
|
|
return maat_cmd_set_line(maat_inst, &line_rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ip_table_set_line(struct maat *maat_inst, const char *table_name,
|
|
|
|
|
enum maat_operation op, long long item_id,
|
2024-08-08 03:32:09 +00:00
|
|
|
long long group_id, const char *ip, int expire_after)
|
2024-03-27 11:19:39 +00:00
|
|
|
{
|
|
|
|
|
char table_line[1024] = {0};
|
|
|
|
|
int table_id = maat_get_table_id(maat_inst, table_name);
|
|
|
|
|
if (table_id < 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 03:32:09 +00:00
|
|
|
sprintf(table_line, "%lld\t%lld\t%s\t0-65535\t%d",
|
|
|
|
|
item_id, group_id, ip, op);
|
2024-03-27 11:19:39 +00:00
|
|
|
struct maat_cmd_line line_rule;
|
|
|
|
|
|
|
|
|
|
line_rule.rule_id = item_id;
|
|
|
|
|
line_rule.table_line = table_line;
|
|
|
|
|
line_rule.table_name = table_name;
|
|
|
|
|
line_rule.expire_after = expire_after;
|
|
|
|
|
|
|
|
|
|
return maat_cmd_set_line(maat_inst, &line_rule);
|
|
|
|
|
}
|
|
|
|
|
int flag_table_set_line(struct maat *maat_inst, const char *table_name,
|
|
|
|
|
enum maat_operation op, long long item_id,
|
|
|
|
|
long long group_id, long long flag,
|
|
|
|
|
long long flag_mask, int expire_after)
|
|
|
|
|
{
|
|
|
|
|
char table_line[1024] = {0};
|
|
|
|
|
int table_id = maat_get_table_id(maat_inst, table_name);
|
|
|
|
|
if (table_id < 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sprintf(table_line, "%lld\t%lld\t%lld\t%lld\t%d",
|
|
|
|
|
item_id, group_id, flag, flag_mask, op);
|
|
|
|
|
struct maat_cmd_line line_rule;
|
|
|
|
|
|
|
|
|
|
line_rule.rule_id = item_id;
|
|
|
|
|
line_rule.table_line = table_line;
|
|
|
|
|
line_rule.table_name = table_name;
|
|
|
|
|
line_rule.expire_after = expire_after;
|
|
|
|
|
|
|
|
|
|
return maat_cmd_set_line(maat_inst, &line_rule);
|
2024-04-03 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void random_keyword_generate(char *keyword_buf, size_t sz)
|
|
|
|
|
{
|
|
|
|
|
#define MIN_KEYWORD_LEN 4
|
|
|
|
|
size_t i = 0, len = 0;
|
|
|
|
|
len = random() % (sz - 1 - MIN_KEYWORD_LEN) + MIN_KEYWORD_LEN;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
keyword_buf[i] = 'a' + random() % ('z' - 'a');
|
|
|
|
|
}
|
|
|
|
|
keyword_buf[i] = '\0';
|
|
|
|
|
}
|