修改insert读取位置方式
修改插入脚本位置判断
This commit is contained in:
@@ -98,6 +98,7 @@ struct manipulate_profile
|
|||||||
char *profile_name;
|
char *profile_name;
|
||||||
char *profile_msg;
|
char *profile_msg;
|
||||||
char *profile_type;
|
char *profile_type;
|
||||||
|
char *profile_position;
|
||||||
ctemplate::Template * tpl;
|
ctemplate::Template * tpl;
|
||||||
pthread_mutex_t lock;
|
pthread_mutex_t lock;
|
||||||
};
|
};
|
||||||
@@ -640,6 +641,7 @@ void ma_profile_table_new_cb(int table_id, const char* key, const char* table_li
|
|||||||
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Policy table parse config failed: %s", table_line);
|
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Policy table parse config failed: %s", table_line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct manipulate_profile* ply_profile=ALLOC(struct manipulate_profile, 1);
|
struct manipulate_profile* ply_profile=ALLOC(struct manipulate_profile, 1);
|
||||||
memset(ply_profile, 0, sizeof(struct manipulate_profile));
|
memset(ply_profile, 0, sizeof(struct manipulate_profile));
|
||||||
|
|
||||||
@@ -667,6 +669,47 @@ void ma_profile_table_new_cb(int table_id, const char* key, const char* table_li
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ma_insert_profile_table_new_cb(int table_id, const char* key, const char* table_line, MAAT_PLUGIN_EX_DATA* ad, long argl, void* argp)
|
||||||
|
{
|
||||||
|
int ret=0, profile_id=0, is_valid=0;
|
||||||
|
char profile_name[128]={0}, formate[128]={0};
|
||||||
|
char profile_path[TFE_PATH_MAX]={0},profile_position[TFE_PATH_MAX]={0};
|
||||||
|
|
||||||
|
ret=sscanf(table_line, "%d\t%s\t%s\t%s\t%s\t%d", &profile_id, profile_name, formate, profile_path, profile_position, &is_valid);
|
||||||
|
if(ret!=6)
|
||||||
|
{
|
||||||
|
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Policy table parse config failed: %s", table_line);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct manipulate_profile* ply_profile=ALLOC(struct manipulate_profile, 1);
|
||||||
|
memset(ply_profile, 0, sizeof(struct manipulate_profile));
|
||||||
|
|
||||||
|
ply_profile->profile_id=profile_id;
|
||||||
|
ply_profile->ref_cnt=1;
|
||||||
|
pthread_mutex_init(&(ply_profile->lock), NULL);
|
||||||
|
|
||||||
|
if(strcasecmp(formate, "template") == 0)
|
||||||
|
{
|
||||||
|
ply_profile->tpl = ctemplate::Template::GetTemplate(profile_path, ctemplate::DO_NOT_STRIP);
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
ply_profile->profile_msg = execute_read_file(profile_path, &ply_profile->msg_len);
|
||||||
|
if (ply_profile->profile_msg == NULL)
|
||||||
|
{
|
||||||
|
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Read file failed %d:%s:%s", profile_id, profile_name, profile_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ply_profile->profile_name=tfe_strdup(profile_name);
|
||||||
|
ply_profile->profile_type=tfe_strdup(formate);
|
||||||
|
ply_profile->profile_position=tfe_strdup(profile_position);
|
||||||
|
|
||||||
|
TFE_LOG_INFO(g_pangu_rt->local_logger, "Policy table add success %d", profile_id);
|
||||||
|
|
||||||
|
*ad = ply_profile;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ma_hijack_profile_table_new_cb(int table_id, const char* key, const char* table_line, MAAT_PLUGIN_EX_DATA* ad, long argl, void* argp)
|
void ma_hijack_profile_table_new_cb(int table_id, const char* key, const char* table_line, MAAT_PLUGIN_EX_DATA* ad, long argl, void* argp)
|
||||||
{
|
{
|
||||||
int ret=0, profile_id=0, is_valid=0;
|
int ret=0, profile_id=0, is_valid=0;
|
||||||
@@ -714,6 +757,8 @@ void ma_profile_table_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl,
|
|||||||
FREE(&ply_obj->profile_type);
|
FREE(&ply_obj->profile_type);
|
||||||
FREE(&ply_obj->profile_msg);
|
FREE(&ply_obj->profile_msg);
|
||||||
FREE(&ply_obj->profile_name);
|
FREE(&ply_obj->profile_name);
|
||||||
|
if (ply_obj->profile_position)
|
||||||
|
FREE(&ply_obj->profile_position);
|
||||||
FREE(&ply_obj);
|
FREE(&ply_obj);
|
||||||
*ad=NULL;
|
*ad=NULL;
|
||||||
return;
|
return;
|
||||||
@@ -778,7 +823,6 @@ int maat_table_ex_init(int profile_idx,
|
|||||||
int pangu_policy_init(const char* profile_path, const char* static_section, const char* dynamic_section)
|
int pangu_policy_init(const char* profile_path, const char* static_section, const char* dynamic_section)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int profile_table_idx = 0;
|
|
||||||
|
|
||||||
g_pangu_rt->maat = create_maat_feather("static", profile_path, static_section, g_pangu_rt->thread_num, g_pangu_rt->local_logger);
|
g_pangu_rt->maat = create_maat_feather("static", profile_path, static_section, g_pangu_rt->thread_num, g_pangu_rt->local_logger);
|
||||||
if (!g_pangu_rt->maat)
|
if (!g_pangu_rt->maat)
|
||||||
@@ -834,16 +878,22 @@ int pangu_policy_init(const char* profile_path, const char* static_section, cons
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (profile_table_idx = 0; profile_table_idx <=POLICY_PROFILE_TABLE_INSERT; profile_table_idx++)
|
ret = maat_table_ex_init(POLICY_PROFLIE_TABLE_REJECT,
|
||||||
|
ma_profile_table_new_cb,
|
||||||
|
ma_profile_table_free_cb,
|
||||||
|
ma_profile_table_dup_cb);
|
||||||
|
if(ret<0)
|
||||||
{
|
{
|
||||||
ret = maat_table_ex_init(profile_table_idx,
|
goto error_out;
|
||||||
ma_profile_table_new_cb,
|
}
|
||||||
ma_profile_table_free_cb,
|
|
||||||
ma_profile_table_dup_cb);
|
ret = maat_table_ex_init(POLICY_PROFILE_TABLE_INSERT,
|
||||||
if(ret<0)
|
ma_insert_profile_table_new_cb,
|
||||||
{
|
ma_profile_table_free_cb,
|
||||||
goto error_out;
|
ma_profile_table_dup_cb);
|
||||||
}
|
if(ret<0)
|
||||||
|
{
|
||||||
|
goto error_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = maat_table_ex_init(POLICY_PROFILE_TABLE_HIJACK,
|
ret = maat_table_ex_init(POLICY_PROFILE_TABLE_HIJACK,
|
||||||
@@ -1413,7 +1463,7 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
|||||||
rewrite_buff = NULL;
|
rewrite_buff = NULL;
|
||||||
rewrite_sz = 0;
|
rewrite_sz = 0;
|
||||||
|
|
||||||
if (tfe_http_in_response(events) && in_resp_spec->content_type != NULL
|
if (tfe_http_in_response(events) && in_resp_spec->content_type != NULL
|
||||||
&& strcasestr(in_resp_spec->content_type, "utf-8"))
|
&& strcasestr(in_resp_spec->content_type, "utf-8"))
|
||||||
{
|
{
|
||||||
options = 1;
|
options = 1;
|
||||||
@@ -1652,7 +1702,7 @@ static void http_hijack(const struct tfe_http_session * session, enum tfe_http_e
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int format_insert_rule(int profile_id, const char* insert_position, struct insert_rule *rule)
|
static int format_insert_rule(int profile_id, struct insert_rule *rule)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
@@ -1664,9 +1714,8 @@ static int format_insert_rule(int profile_id, const char* insert_position, struc
|
|||||||
}
|
}
|
||||||
rule->script = tfe_strdup(insert_profile->profile_msg);
|
rule->script = tfe_strdup(insert_profile->profile_msg);
|
||||||
rule->type = tfe_strdup(insert_profile->profile_type);
|
rule->type = tfe_strdup(insert_profile->profile_type);
|
||||||
|
rule->position = tfe_strdup(insert_profile->profile_position);
|
||||||
|
|
||||||
if (insert_position)
|
|
||||||
rule->position=tfe_strdup(insert_position);
|
|
||||||
ma_profile_table_free(insert_profile);
|
ma_profile_table_free(insert_profile);
|
||||||
insert_profile = NULL;
|
insert_profile = NULL;
|
||||||
return ret;
|
return ret;
|
||||||
@@ -1698,7 +1747,7 @@ static void http_insert(const struct tfe_stream * stream, const struct tfe_http_
|
|||||||
{
|
{
|
||||||
ctx->ins_ctx = ins_ctx = ALLOC(struct insert_ctx, 1);
|
ctx->ins_ctx = ins_ctx = ALLOC(struct insert_ctx, 1);
|
||||||
ins_ctx->rule = ALLOC(struct insert_rule, 1);
|
ins_ctx->rule = ALLOC(struct insert_rule, 1);
|
||||||
int ret=format_insert_rule(param->profile_id, param->position, ins_ctx->rule);
|
int ret=format_insert_rule(param->profile_id, ins_ctx->rule);
|
||||||
if (ret<0)
|
if (ret<0)
|
||||||
{
|
{
|
||||||
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Failed to get policy table, profile_id = %d", param->profile_id);
|
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Failed to get policy table, profile_id = %d", param->profile_id);
|
||||||
@@ -1865,7 +1914,7 @@ enum pangu_action http_scan(const struct tfe_http_session * session, enum tfe_ht
|
|||||||
{
|
{
|
||||||
hit_cnt += scan_ret;
|
hit_cnt += scan_ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * str_url = session->req->req_spec.url;
|
const char * str_url = session->req->req_spec.url;
|
||||||
int str_url_length = (int) (strlen(session->req->req_spec.url));
|
int str_url_length = (int) (strlen(session->req->req_spec.url));
|
||||||
|
|
||||||
|
|||||||
@@ -212,17 +212,17 @@ size_t replace_string(const char * in, size_t in_sz, const struct replace_rule *
|
|||||||
not_enough_mem_retry:
|
not_enough_mem_retry:
|
||||||
out_buffer = (PCRE2_UCHAR*)malloc(sizeof(PCRE2_UCHAR)*outbuff_size);
|
out_buffer = (PCRE2_UCHAR*)malloc(sizeof(PCRE2_UCHAR)*outbuff_size);
|
||||||
outlen = outbuff_size;
|
outlen = outbuff_size;
|
||||||
int rc = pcre2_substitute(re, subject, in_sz, 0,
|
int rc = pcre2_substitute(re, subject, in_sz, 0,
|
||||||
PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH,
|
PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH,
|
||||||
0, 0, //pcre2_match_data *match_data, pcre2_match_context
|
0, 0, //pcre2_match_data *match_data, pcre2_match_context
|
||||||
replacement, strlen(zone->replace_with),
|
replacement, strlen(zone->replace_with),
|
||||||
out_buffer, &outlen);
|
out_buffer, &outlen);
|
||||||
if(outlen>outbuff_size)
|
if(outlen>outbuff_size)
|
||||||
{
|
{
|
||||||
outbuff_size=outlen;
|
outbuff_size=outlen;
|
||||||
free(out_buffer);
|
free(out_buffer);
|
||||||
out_buffer=NULL;
|
out_buffer=NULL;
|
||||||
goto not_enough_mem_retry;
|
goto not_enough_mem_retry;
|
||||||
}
|
}
|
||||||
if(rc<=0)
|
if(rc<=0)
|
||||||
{
|
{
|
||||||
@@ -266,10 +266,10 @@ size_t execute_replace_rule(const char * in, size_t in_sz,
|
|||||||
}
|
}
|
||||||
pre_out = new_out;
|
pre_out = new_out;
|
||||||
pre_out_sz = output_size;
|
pre_out_sz = output_size;
|
||||||
|
|
||||||
interator = new_out;
|
interator = new_out;
|
||||||
interator_sz = output_size;
|
interator_sz = output_size;
|
||||||
|
|
||||||
new_out=NULL;
|
new_out=NULL;
|
||||||
output_size=0;
|
output_size=0;
|
||||||
}
|
}
|
||||||
@@ -308,7 +308,7 @@ static char *find_insert_position(char * in)
|
|||||||
insert_from=head_in;
|
insert_from=head_in;
|
||||||
}
|
}
|
||||||
return insert_from;
|
return insert_from;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char *script, const char *type, char** out)
|
size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char *script, const char *type, char** out)
|
||||||
{
|
{
|
||||||
@@ -328,7 +328,7 @@ size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (insert_on != NULL && 0==strcasecmp(insert_on, "after-page-load"))
|
if (insert_on != NULL && 0==strcasecmp(insert_on, "after_page_load"))
|
||||||
{
|
{
|
||||||
insert_from=strstr(in, "</body>");
|
insert_from=strstr(in, "</body>");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user