1.修改hjack读取profile文件方式
2.命中query替换规则后,http2适配http修改 3.添加URI测试用例
This commit is contained in:
@@ -667,16 +667,10 @@ void ma_hijack_profile_table_new_cb(int table_id, const char* key, const char* t
|
||||
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;
|
||||
|
||||
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_id=profile_id;
|
||||
ply_profile->profile_msg=tfe_strdup(profile_path);
|
||||
ply_profile->profile_name=tfe_strdup(hijack_name);
|
||||
ply_profile->profile_type=tfe_strdup(formate);
|
||||
|
||||
@@ -1378,7 +1372,6 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
||||
rewrite_sz = execute_replace_rule(__http_body, __http_body_len, r_zone,
|
||||
rep_ctx->rule, rep_ctx->n_rule, &rewrite_buff);
|
||||
|
||||
|
||||
if (rewrite_sz >0 )
|
||||
{
|
||||
tfe_http_half_append_body(rep_ctx->replacing, rewrite_buff, rewrite_sz, 0);
|
||||
@@ -1564,22 +1557,35 @@ static void http_hijack(const struct tfe_http_session * session, enum tfe_http_e
|
||||
return;
|
||||
}
|
||||
|
||||
char * hijack_buff=NULL; size_t hijack_size=0;
|
||||
|
||||
hijack_buff = execute_read_file(hijack_profile->profile_msg, &hijack_size);
|
||||
if (NULL == hijack_buff){
|
||||
TFE_LOG_ERROR(g_pangu_rt->local_logger, "read hijack file faild, path = %s", hijack_profile->profile_msg);
|
||||
ctx->action = PG_ACTION_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
struct tfe_http_session * to_write_sess = NULL;
|
||||
char cont_len_str[16];
|
||||
|
||||
to_write_sess = tfe_http_session_allow_write(session);
|
||||
response = tfe_http_session_response_create(to_write_sess, 200);
|
||||
|
||||
int hijack_len = strlen(hijack_profile->profile_name)+strlen("filename=\"\"")+1;
|
||||
char *hijack_name = ALLOC(char, hijack_len);
|
||||
snprintf(hijack_name, hijack_len, "filename=\"%s\"", hijack_profile->profile_name);
|
||||
tfe_http_nonstd_field_write(response, "Content-Disposition", hijack_name);
|
||||
FREE(&hijack_name);
|
||||
if (0!=strcasecmp(hijack_profile->profile_name, "null"))
|
||||
{
|
||||
int hijack_file_len = strlen(hijack_profile->profile_name)+strlen("filename=\"\"")+1;
|
||||
char *hijack_file_name = ALLOC(char, hijack_file_len);
|
||||
snprintf(hijack_file_name, hijack_file_len, "filename=\"%s\"", hijack_profile->profile_name);
|
||||
tfe_http_nonstd_field_write(response, "Content-Disposition", hijack_file_name);
|
||||
FREE(&hijack_file_name);
|
||||
}
|
||||
|
||||
tfe_http_std_field_write(response, TFE_HTTP_CONT_TYPE, hijack_profile->profile_type);
|
||||
snprintf(cont_len_str, sizeof(cont_len_str), "%lu", hijack_profile->msg_len);
|
||||
snprintf(cont_len_str, sizeof(cont_len_str), "%lu", hijack_size);
|
||||
tfe_http_std_field_write(response, TFE_HTTP_CONT_LENGTH, cont_len_str);
|
||||
|
||||
tfe_http_half_append_body(response, hijack_profile->profile_msg, hijack_profile->msg_len, 0);
|
||||
tfe_http_half_append_body(response, hijack_buff, hijack_size, 0);
|
||||
tfe_http_half_append_body(response, NULL, 0, 0);
|
||||
tfe_http_session_response_set(to_write_sess, response);
|
||||
tfe_http_session_detach(session);
|
||||
|
||||
@@ -231,6 +231,7 @@ int pangu_send_log(struct pangu_logger* handle, const struct pangu_log* log_msg)
|
||||
break;
|
||||
}
|
||||
cJSON_AddNumberToObject(common_obj, "direction", 0); //0:域内->域外,1:域外->域内,描述的是CLIENT_IP信息
|
||||
cJSON_AddNumberToObject(common_obj, "Link_id", 0);
|
||||
cJSON_AddNumberToObject(common_obj, "stream_dir", 3); //1:c2s, 2:s2c, 3:double
|
||||
cJSON_AddStringToObject(common_obj, "cap_ip", handle->local_ip_str);
|
||||
cJSON_AddNumberToObject(common_obj, "entrance_id", handle->entry_id);
|
||||
|
||||
@@ -157,6 +157,43 @@ TEST(PatternReplace, CaseInsensitiveRussian)
|
||||
return;
|
||||
}
|
||||
|
||||
TEST(PatternReplace, QueryAdd)
|
||||
{
|
||||
const char * find = "(?<=\\?|^|&)q=([^&|^#]*)(?=&|$)";
|
||||
const char* replacement="q=find";
|
||||
const char* input="https://cn.bing.com/search?ei=pQnxXPS-LPSGr7wP3u6usAY&q=test&oq=test&gs_l=psy-ab.3..0i131i67j0l8j0i131.26791.27227..27885...0.0..0.235.683.0j3j1......0....1..gws-wiz.......0i71j0i67.klHdqBPS88k";
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
EXPECT_TRUE(NULL==strstr(output, find));
|
||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||
|
||||
printf("%s\n", output);
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
|
||||
TEST(PatternReplace, QueryDel)
|
||||
{
|
||||
const char * find = "(?<=\\?|^|&)sk=([^&|^#]*)(&|$)";
|
||||
const char* replacement="";
|
||||
const char* input="https://cn.bing.com/&search?q=find&qs=n&form=QBLH&sp=-1&pq=find&sk=";
|
||||
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
EXPECT_TRUE(NULL==strstr(output, find));
|
||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||
|
||||
printf("%s\n", output);
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
|
||||
TEST(PatternInsert, CSS)
|
||||
{
|
||||
const char* filename="./test_data/index_of__centos.html";
|
||||
|
||||
@@ -137,6 +137,32 @@ tfe_h2_header_add_field(struct tfe_h2_header *h2_header, const struct http_field
|
||||
TAILQ_INSERT_HEAD(&h2_header->h2_field_list, peer_h2_field, next);
|
||||
}
|
||||
|
||||
static nghttp2_nv*
|
||||
tfe_h2_header_modify_field(struct tfe_h2_header *header, nghttp2_nv *hdrs, const char *field_name, const char *filed_value)
|
||||
{
|
||||
int nvlen = 0;
|
||||
struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL;
|
||||
|
||||
TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field)
|
||||
{
|
||||
hdrs[nvlen].name = h2_field->nv.name;
|
||||
hdrs[nvlen].namelen = h2_field->nv.namelen;
|
||||
if (filed_value && (0==strcasecmp((const char*)h2_field->nv.name, field_name)))
|
||||
{
|
||||
hdrs[nvlen].value = (uint8_t *)filed_value;
|
||||
hdrs[nvlen].valuelen = strlen(filed_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
hdrs[nvlen].value = h2_field->nv.value;
|
||||
hdrs[nvlen].valuelen = h2_field->nv.valuelen;
|
||||
}
|
||||
hdrs[nvlen].flags = h2_field->nv.flags;
|
||||
nvlen++;
|
||||
}
|
||||
return hdrs;
|
||||
}
|
||||
|
||||
static inline void
|
||||
headers_init(struct tfe_h2_header *header)
|
||||
{
|
||||
@@ -169,7 +195,7 @@ method_to_str_idx(const char * method)
|
||||
}
|
||||
|
||||
static nghttp2_nv*
|
||||
nghttp2_nv_packet(struct tfe_h2_header *header, nghttp2_nv *hdrs)
|
||||
tfe_h2_header_convert_nv(struct tfe_h2_header *header, nghttp2_nv *hdrs)
|
||||
{
|
||||
int nvlen = 0;
|
||||
struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL;
|
||||
@@ -400,6 +426,7 @@ void delete_stream_half_data(struct tfe_h2_half_private **data,
|
||||
if ((*data)->url_storage)
|
||||
FREE(&((*data)->url_storage));
|
||||
delete_nv_packet_data(&((*data)->header));
|
||||
|
||||
if((*data)->event_cb_user_deleter != NULL)
|
||||
(*data)->event_cb_user_deleter((*data)->event_cb_user);
|
||||
free(*data);
|
||||
@@ -666,15 +693,20 @@ nghttp2_server_frame_submit_response(struct tfe_h2_stream *h2_stream_info,
|
||||
char str_sz_evbuf_body[TFE_STRING_MAX];
|
||||
snprintf(str_sz_evbuf_body, sizeof(str_sz_evbuf_body) - 1, "%lu", evbuffer_get_length(body->evbuf_body));
|
||||
|
||||
const static struct http_field_name __cont_encoding_length_name = {TFE_HTTP_CONT_LENGTH, NULL};
|
||||
tfe_http_field_write(&pangu_resp->half_public, &__cont_encoding_length_name, str_sz_evbuf_body);
|
||||
const static struct http_field_name encoding_field = {TFE_HTTP_CONT_LENGTH, NULL};
|
||||
tfe_http_field_write(&pangu_resp->half_public, &encoding_field, str_sz_evbuf_body);
|
||||
|
||||
nghttp2_data_provider data_prd;
|
||||
data_prd.source.ptr = (void *)body;
|
||||
data_prd.read_callback = upstream_read_callback;
|
||||
|
||||
nghttp2_nv hdrs[h2_header->nvlen];
|
||||
rv = nghttp2_submit_response(h2_stream_info->as_server, h2_session->ngh2_stream_id, nghttp2_nv_packet(h2_header, hdrs),
|
||||
/*Adapt Http uri Settings**/
|
||||
|
||||
tfe_http_field_write(&pangu_resp->half_public, &encoding_field, str_sz_evbuf_body);
|
||||
|
||||
|
||||
rv = nghttp2_submit_response(h2_stream_info->as_server, h2_session->ngh2_stream_id, tfe_h2_header_convert_nv(h2_header, hdrs),
|
||||
h2_header->nvlen, &data_prd);
|
||||
if (rv != 0){
|
||||
return ACTION_FORWARD_DATA;
|
||||
@@ -1006,7 +1038,7 @@ nghttp2_submit_end_header(struct tfe_h2_stream *h2_stream_info,
|
||||
|
||||
nghttp2_nv hdrs[headers.nvlen];
|
||||
stream_id = nghttp2_submit_headers(h2_stream_info->as_server, headers.flag,
|
||||
h2_session->ngh2_stream_id, NULL, nghttp2_nv_packet(&headers, hdrs),
|
||||
h2_session->ngh2_stream_id, NULL, tfe_h2_header_convert_nv(&headers, hdrs),
|
||||
headers.nvlen, h2_session);
|
||||
if (stream_id < 0){
|
||||
printf("Fatal headers error: %s\n", nghttp2_strerror(stream_id));
|
||||
@@ -1129,7 +1161,7 @@ upstream_create_req(struct tfe_h2_stream *h2_stream_info, nghttp2_session *as_se
|
||||
event->tf_stream = h2_stream_info->tf_stream;
|
||||
event->tfe_session = &h2_session->tfe_session;
|
||||
|
||||
half_set_callback(h2_session->req, event, NULL);
|
||||
half_set_callback(h2_session->req, event, free);
|
||||
|
||||
/* Call business plugin */
|
||||
half_private = h2_session->req;
|
||||
@@ -1173,7 +1205,7 @@ nghttp2_server_frame_submit_push_promise(struct tfe_h2_stream *h2_stream_info,
|
||||
|
||||
nghttp2_nv hdrs[headers->nvlen];
|
||||
stream_id = nghttp2_submit_push_promise(h2_stream_info->as_server, headers->flag,
|
||||
h2_session->ngh2_stream_id, nghttp2_nv_packet(headers, hdrs),
|
||||
h2_session->ngh2_stream_id, tfe_h2_header_convert_nv(headers, hdrs),
|
||||
headers->nvlen, peer_h2_stream);
|
||||
if (stream_id < 0){
|
||||
free(peer_h2_stream);
|
||||
@@ -1346,7 +1378,7 @@ nghttp2_server_frame_submit_header(struct tfe_h2_stream *h2_stream_info,
|
||||
}
|
||||
nghttp2_nv hdrs[headers->nvlen];
|
||||
xret = nghttp2_submit_headers(h2_stream_info->as_server, headers->flag,
|
||||
h2_session->ngh2_stream_id, NULL, nghttp2_nv_packet(headers, hdrs),
|
||||
h2_session->ngh2_stream_id, NULL, tfe_h2_header_convert_nv(headers, hdrs),
|
||||
headers->nvlen, h2_session);
|
||||
if (xret < 0){
|
||||
printf("Fatal headers error: %s\n", nghttp2_strerror(xret));
|
||||
@@ -1430,7 +1462,7 @@ fill_req_spec_from_handle(struct tfe_h2_half_private *half_private)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
char *urltmp = NULL;
|
||||
char *urltmp = half_private->url_storage;
|
||||
urltmp = (char *)malloc(urllen + 1);
|
||||
if(urltmp){
|
||||
sprintf(urltmp, "%s%s", (char *)req_spec->host, (char *)req_spec->uri);
|
||||
@@ -1509,7 +1541,7 @@ downstream_create_resp(struct tfe_h2_session *h2_session, nghttp2_session *as_cl
|
||||
event->tf_stream = tf_stream;
|
||||
event->tfe_session = &h2_session->tfe_session;
|
||||
|
||||
half_set_callback(h2_session->resp, event, NULL);
|
||||
half_set_callback(h2_session->resp, event, free);
|
||||
|
||||
h2_session->resp->frame_ctx = h2_session->frame_ctx;
|
||||
|
||||
@@ -1547,15 +1579,16 @@ nghttp2_client_frame_submit_header(struct tfe_h2_stream *h2_stream_info,
|
||||
nghttp2_session_set_next_stream_id(h2_stream_info->as_client, h2_session->ngh2_stream_id);
|
||||
|
||||
nghttp2_nv hdrs[headers->nvlen];
|
||||
|
||||
method = nghttp2_get_method(h2_session->req);
|
||||
if (method == (enum tfe_http_std_method)NGHTTP2_METHOD_POST ||
|
||||
method == (enum tfe_http_std_method)NGHTTP2_METHOD_PUT){
|
||||
stream_id = nghttp2_submit_headers(h2_stream_info->as_client, headers->flag,
|
||||
-1, NULL, nghttp2_nv_packet(headers, hdrs),
|
||||
-1, NULL, tfe_h2_header_modify_field(headers, hdrs, ":path", req->url_storage),
|
||||
headers->nvlen, h2_session);
|
||||
}else{
|
||||
stream_id = nghttp2_submit_request(h2_stream_info->as_client, NULL,
|
||||
nghttp2_nv_packet(headers, hdrs),
|
||||
tfe_h2_header_modify_field(headers, hdrs, ":path", req->url_storage),
|
||||
headers->nvlen, NULL, h2_session);
|
||||
}
|
||||
if (stream_id < 0){
|
||||
@@ -1865,6 +1898,7 @@ nghttp2_client_on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
|
||||
resp->body.flags = flags;
|
||||
}
|
||||
}
|
||||
if (uncompr_len) FREE(&uncompr);
|
||||
|
||||
stream_action = server_frame_submit_data(h2_stream_info, h2_session, CONN_DIR_UPSTREAM);
|
||||
if (stream_action == ACTION_DROP_DATA){
|
||||
@@ -1942,7 +1976,7 @@ create_upstream_data(nghttp2_session *session, int32_t stream_id,
|
||||
event->tf_stream = h2_stream_info->tf_stream;
|
||||
event->tfe_session = &h2_session->tfe_session;
|
||||
|
||||
half_set_callback(h2_session->resp, event, NULL);
|
||||
half_set_callback(h2_session->resp, event, free);
|
||||
|
||||
h2_session->resp->frame_ctx = h2_session->frame_ctx;
|
||||
|
||||
@@ -2101,7 +2135,7 @@ create_serv_stream_data(nghttp2_session *session, int32_t stream_id,
|
||||
event->tf_stream = h2_stream_info->tf_stream;
|
||||
event->tfe_session = &h2_session->tfe_session;
|
||||
|
||||
half_set_callback(h2_session->req, event, NULL);
|
||||
half_set_callback(h2_session->req, event, free);
|
||||
|
||||
/* Call business plugin */
|
||||
half_private = h2_session->req;
|
||||
@@ -2155,6 +2189,8 @@ nghttp2_server_on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
|
||||
}
|
||||
data = input;
|
||||
len = input_len;
|
||||
/*todo post data scan**/
|
||||
if (uncompr_len) FREE(&uncompr);
|
||||
|
||||
stream_action = server_frame_submit_data(h2_stream_info, h2_session, CONN_DIR_DOWNSTREAM);
|
||||
if (stream_action == ACTION_DROP_DATA){
|
||||
|
||||
Reference in New Issue
Block a user