From 5f366986ba51ab1612c4e79f9729d4ed935782d5 Mon Sep 17 00:00:00 2001 From: fengweihao Date: Wed, 21 Aug 2019 19:47:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9insert=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E6=96=B9=E5=BC=8F=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E8=84=9A=E6=9C=AC=E4=BD=8D=E7=BD=AE=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/business/pangu-http/src/pangu_http.cpp | 81 +++++++++++++++---- .../pangu-http/src/pattern_replace.cpp | 16 ++-- 2 files changed, 73 insertions(+), 24 deletions(-) diff --git a/plugin/business/pangu-http/src/pangu_http.cpp b/plugin/business/pangu-http/src/pangu_http.cpp index 5dda782..b20321f 100644 --- a/plugin/business/pangu-http/src/pangu_http.cpp +++ b/plugin/business/pangu-http/src/pangu_http.cpp @@ -98,6 +98,7 @@ struct manipulate_profile char *profile_name; char *profile_msg; char *profile_type; + char *profile_position; ctemplate::Template * tpl; 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); return; } + struct manipulate_profile* ply_profile=ALLOC(struct manipulate_profile, 1); 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; } +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) { 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_msg); FREE(&ply_obj->profile_name); + if (ply_obj->profile_position) + FREE(&ply_obj->profile_position); FREE(&ply_obj); *ad=NULL; 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 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); 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, - ma_profile_table_new_cb, - ma_profile_table_free_cb, - ma_profile_table_dup_cb); - if(ret<0) - { - goto error_out; - } + goto error_out; + } + + ret = maat_table_ex_init(POLICY_PROFILE_TABLE_INSERT, + ma_insert_profile_table_new_cb, + ma_profile_table_free_cb, + ma_profile_table_dup_cb); + if(ret<0) + { + goto error_out; } 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_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")) { options = 1; @@ -1652,7 +1702,7 @@ static void http_hijack(const struct tfe_http_session * session, enum tfe_http_e 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; @@ -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->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); insert_profile = NULL; 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); 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) { 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; } - + const char * str_url = session->req->req_spec.url; int str_url_length = (int) (strlen(session->req->req_spec.url)); diff --git a/plugin/business/pangu-http/src/pattern_replace.cpp b/plugin/business/pangu-http/src/pattern_replace.cpp index 9e54bae..87d0de0 100644 --- a/plugin/business/pangu-http/src/pattern_replace.cpp +++ b/plugin/business/pangu-http/src/pattern_replace.cpp @@ -212,17 +212,17 @@ size_t replace_string(const char * in, size_t in_sz, const struct replace_rule * not_enough_mem_retry: out_buffer = (PCRE2_UCHAR*)malloc(sizeof(PCRE2_UCHAR)*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, - 0, 0, //pcre2_match_data *match_data, pcre2_match_context - replacement, strlen(zone->replace_with), + 0, 0, //pcre2_match_data *match_data, pcre2_match_context + replacement, strlen(zone->replace_with), out_buffer, &outlen); if(outlen>outbuff_size) { outbuff_size=outlen; free(out_buffer); out_buffer=NULL; - goto not_enough_mem_retry; + goto not_enough_mem_retry; } 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_sz = output_size; - + interator = new_out; interator_sz = output_size; - + new_out=NULL; output_size=0; } @@ -308,7 +308,7 @@ static char *find_insert_position(char * in) insert_from=head_in; } 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) { @@ -328,7 +328,7 @@ size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char 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, ""); }