1.修改原数据流动作定义,增加操作定义
2.重构原操作重定向、替换 3.新增接口http_block、http_hijack、http_insert 4.注册处理策略表接口policy_table_new_cb等 5.注册处理用户自定域json处理接口policy_param_new等 6.添加对添加对注入脚本的测试用例
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
@@ -30,7 +31,8 @@ enum replace_zone zone_name_to_id(const char * name)
|
||||
}
|
||||
return (enum replace_zone) i;
|
||||
}
|
||||
static char * strchr_esc(char * s, const char delim)
|
||||
static char *__attribute__((__unused__))
|
||||
strchr_esc(char * s, const char delim)
|
||||
{
|
||||
char * token;
|
||||
if (s == NULL)
|
||||
@@ -54,7 +56,8 @@ static char * strchr_esc(char * s, const char delim)
|
||||
return token;
|
||||
}
|
||||
}
|
||||
static char * strtok_r_esc(char * s, const char delim, char ** save_ptr)
|
||||
static char *__attribute__((__unused__))
|
||||
strtok_r_esc(char * s, const char delim, char ** save_ptr)
|
||||
{
|
||||
char * token;
|
||||
|
||||
@@ -75,8 +78,26 @@ static char * strtok_r_esc(char * s, const char delim, char ** save_ptr)
|
||||
return s;
|
||||
}
|
||||
|
||||
char *rt_read_file(const char* filename, size_t *input_sz)
|
||||
{
|
||||
FILE* fp=NULL;
|
||||
struct stat file_info;
|
||||
stat(filename, &file_info);
|
||||
*input_sz=file_info.st_size;
|
||||
|
||||
size_t format_replace_rule(const char * exec_para, struct replace_rule * replace, size_t n_replace)
|
||||
fp=fopen(filename,"r");
|
||||
if(fp==NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
char* data=(char*)malloc((*input_sz));
|
||||
fread(data,1,*input_sz,fp);
|
||||
fclose(fp);
|
||||
|
||||
return data;
|
||||
}
|
||||
size_t __attribute__((__unused__))
|
||||
format_replace_rule(const char * exec_para, struct replace_rule * replace, size_t n_replace)
|
||||
{
|
||||
char * tmp = ALLOC(char, strlen(exec_para) + 1);
|
||||
char * token = NULL, * sub_token = NULL, * saveptr = NULL, * saveptr2 = NULL;
|
||||
@@ -225,15 +246,81 @@ size_t execute_replace_rule(const char * in, size_t in_sz,
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char *stype, const char *type, char** out)
|
||||
{
|
||||
char *target=NULL;
|
||||
size_t outlen=0, target_size=0;
|
||||
char position[]="</head>";
|
||||
|
||||
/* "<script type=\"text/javascript\" class=\"RQ_SCRIPT\"></script>"*/
|
||||
int js_type_len = 58;
|
||||
/*"<style type=\"text/css\" class=\"RQ_SCRIPT\"></style>"*/
|
||||
int css_type_len = 49;
|
||||
|
||||
char* head_string=NULL;
|
||||
|
||||
if (0==strcasecmp(type, "css"))
|
||||
{
|
||||
target_size = in_sz+strlen(stype)+css_type_len;
|
||||
target = ALLOC(char, target_size);
|
||||
}
|
||||
|
||||
if (0==strcasecmp(type, "js"))
|
||||
{
|
||||
target_size = in_sz+strlen(stype)+js_type_len;
|
||||
target = ALLOC(char, target_size);
|
||||
}
|
||||
|
||||
if (insert_on != NULL && 0==strcasecmp(insert_on, "after-page-load"))
|
||||
{
|
||||
memcpy(position, "</body>", sizeof(position));
|
||||
}
|
||||
head_string=strstr(in, position);
|
||||
if (head_string != NULL)
|
||||
{
|
||||
strncat(target, in, MIN((unsigned int)(head_string-in), target_size));
|
||||
size_t style_len = 0; char *style_msg = NULL;
|
||||
if (0==strcasecmp(type, "js"))
|
||||
{
|
||||
style_len = strlen(stype)+js_type_len+1;
|
||||
style_msg = ALLOC(char, style_len);
|
||||
snprintf(style_msg, style_len, "<script type=\"text/javascript\" class=\"RQ_SCRIPT\">%s</script>", stype);
|
||||
}
|
||||
if (0==strcasecmp(type, "css"))
|
||||
{
|
||||
style_len = strlen(stype)+css_type_len+1;
|
||||
style_msg = ALLOC(char, style_len);
|
||||
snprintf(style_msg, style_len, "<style type=\"text/css\" class=\"RQ_SCRIPT\">%s</style>\n", stype);
|
||||
}
|
||||
strncat(target, style_msg, target_size);
|
||||
free(style_msg);
|
||||
style_msg = NULL;
|
||||
strncat(target, head_string, target_size);
|
||||
*out = target;
|
||||
outlen = strlen(target) + 1;
|
||||
}else
|
||||
{
|
||||
free(target);
|
||||
target = NULL;
|
||||
outlen = 0;
|
||||
}
|
||||
return outlen;
|
||||
}
|
||||
|
||||
size_t execute_insert_rule(char * in, size_t in_sz, const struct insert_rule * rules, char** out)
|
||||
{
|
||||
return insert_string(in, in_sz, rules->position, rules->stype, rules->type, out);
|
||||
}
|
||||
|
||||
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz)
|
||||
{
|
||||
char* exec_para=NULL;
|
||||
asprintf(&exec_para,"zone=http_resp_body;substitute=/%s/%s", find, replacement);
|
||||
size_t n_got_rule=0;
|
||||
size_t n_got_rule=1;
|
||||
struct replace_rule rules[16];
|
||||
n_got_rule=format_replace_rule(exec_para, rules, sizeof(rules)/sizeof(rules[0]));
|
||||
rules[0].zone = kZoneResponseBody;
|
||||
rules[0].find = tfe_strdup(find);
|
||||
rules[0].replace_with = tfe_strdup(replacement);
|
||||
*output_sz=execute_replace_rule(input, strlen(input), kZoneResponseBody, rules, n_got_rule, output);
|
||||
free(exec_para);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,11 @@ enum replace_zone
|
||||
kZoneResponseBody,
|
||||
kZoneMax
|
||||
};
|
||||
|
||||
/**
|
||||
rule type: replace
|
||||
rule type: insert(find: insert on, replace_with: insert content)
|
||||
*/
|
||||
struct replace_rule
|
||||
{
|
||||
enum replace_zone zone;
|
||||
@@ -16,11 +21,22 @@ struct replace_rule
|
||||
char * replace_with;
|
||||
};
|
||||
|
||||
struct insert_rule
|
||||
{
|
||||
char * stype;
|
||||
char * type;
|
||||
char * position;
|
||||
};
|
||||
|
||||
//@parm exec_para example input:
|
||||
//zone=http_req_body; substitute=/中華民國/中华人民共和国;zone=http_resp_header; substitute=/Content-Type:\btext\/html/Content-Type:\bvideo\/mp4
|
||||
//@return formated rule number.
|
||||
size_t format_replace_rule(const char * exec_para, struct replace_rule * replace, size_t n_replace);
|
||||
size_t execute_replace_rule(const char * in, size_t in_sz, enum replace_zone zone, const struct replace_rule * rules, size_t n_rule, char** out);
|
||||
size_t execute_insert_rule(char * in, size_t in_sz, const struct insert_rule * rules, char** out);
|
||||
size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char *stype, const char *type, char** out);
|
||||
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz);
|
||||
|
||||
enum replace_zone zone_name_to_id(const char * name);
|
||||
char *rt_read_file(const char* filename, size_t *input_sz);
|
||||
|
||||
|
||||
|
||||
@@ -113,6 +113,70 @@ TEST(PatternReplace, UTF8)
|
||||
return;
|
||||
}
|
||||
|
||||
TEST(PatternInsert, CSS)
|
||||
{
|
||||
const char* filename="./test_data/index_of__centos.html";
|
||||
const char* custom = "h1,h2{color: red;}ul.tabmain a{color: green;}";
|
||||
char *input=NULL, *output=NULL;
|
||||
size_t output_sz=0, input_sz = 0;
|
||||
|
||||
input = rt_read_file(filename, &input_sz);
|
||||
EXPECT_TRUE(input_sz>0);
|
||||
|
||||
output_sz = insert_string(input, input_sz, NULL, custom, "css", &output);
|
||||
|
||||
//printf("output = %s\n", output);
|
||||
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
EXPECT_TRUE(NULL!=strstr(output, custom));
|
||||
free(output);
|
||||
free(input);
|
||||
}
|
||||
|
||||
TEST(PatternInsert, after_body)
|
||||
{
|
||||
const char* filename="./test_data/index_of__centos.html";
|
||||
const char* custom = "var now=new Date();var year=now.getYear()+1900;var month=now.getMonth()+1;var date=now.getDate();var day=now.getDay();\
|
||||
var time=\"curtime\"+year+\"year\"+month+\"month\"+date+\"date\"+week;alert(time);";
|
||||
char *input=NULL, *output=NULL;
|
||||
size_t output_sz=0, input_sz = 0;
|
||||
|
||||
input = rt_read_file(filename, &input_sz);
|
||||
EXPECT_TRUE(input_sz>0);
|
||||
|
||||
output_sz = insert_string(input, input_sz, "after-page-load", custom, "js", &output);
|
||||
|
||||
//printf("%s\n", output);
|
||||
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
EXPECT_TRUE(NULL!=strstr(output, custom));
|
||||
|
||||
free(input);
|
||||
free(output);
|
||||
output = NULL;
|
||||
}
|
||||
|
||||
TEST(PatternInsert, before_body)
|
||||
{
|
||||
const char* filename="./test_data/index_of__centos.html";
|
||||
const char* custom = "var now=new Date();var year=now.getYear()+1900;var month=now.getMonth()+1;var date=now.getDate();var day=now.getDay();\
|
||||
var time=\"curtime\"+year+\"year\"+month+\"month\"+date+\"date\"+week;alert(time);";
|
||||
char *input=NULL, *output=NULL;
|
||||
size_t output_sz=0, input_sz = 0;
|
||||
|
||||
input = rt_read_file(filename, &input_sz);
|
||||
EXPECT_TRUE(input_sz>0);
|
||||
|
||||
output_sz = insert_string(input, input_sz, "before-page-load", custom, "js", &output);
|
||||
//printf("%s\n", output);
|
||||
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
EXPECT_TRUE(NULL!=strstr(output, custom));
|
||||
|
||||
free(input);
|
||||
free(output);
|
||||
output = NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
|
||||
85
plugin/business/pangu-http/test_data/index_of__centos.html
Normal file
85
plugin/business/pangu-http/test_data/index_of__centos.html
Normal file
@@ -0,0 +1,85 @@
|
||||
|
||||
<!-- saved from url=(0031)http://mirror.hoster.kz/centos/ -->
|
||||
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Index of /centos/</title>
|
||||
</head>
|
||||
<body bgcolor="white">
|
||||
<h1>Index of /centos/</h1><hr><pre><a href="http://mirror.hoster.kz/">../</a>
|
||||
<a href="http://mirror.hoster.kz/centos/2/">2/</a> 09-Sep-2009 05:18 -
|
||||
<a href="http://mirror.hoster.kz/centos/2.1/">2.1/</a> 09-Sep-2009 05:18 -
|
||||
<a href="http://mirror.hoster.kz/centos/3/">3/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/3.1/">3.1/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/3.3/">3.3/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/3.4/">3.4/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/3.5/">3.5/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/3.6/">3.6/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/3.7/">3.7/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/3.8/">3.8/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/3.9/">3.9/</a> 02-Mar-2011 23:44 -
|
||||
<a href="http://mirror.hoster.kz/centos/4/">4/</a> 03-Apr-2017 11:34 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.0/">4.0/</a> 18-Jul-2005 21:11 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.1/">4.1/</a> 21-Oct-2005 17:54 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.2/">4.2/</a> 04-Nov-2006 12:43 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.3/">4.3/</a> 10-Nov-2006 22:15 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.4/">4.4/</a> 07-Jul-2007 18:21 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.5/">4.5/</a> 10-Jan-2008 16:12 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.6/">4.6/</a> 31-Mar-2009 11:55 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.7/">4.7/</a> 08-Mar-2010 10:56 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.8/">4.8/</a> 13-Mar-2012 00:14 -
|
||||
<a href="http://mirror.hoster.kz/centos/4.9/">4.9/</a> 03-Apr-2017 11:34 -
|
||||
<a href="http://mirror.hoster.kz/centos/5/">5/</a> 03-Apr-2017 11:34 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.0/">5.0/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.1/">5.1/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.10/">5.10/</a> 03-Apr-2017 11:30 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.11/">5.11/</a> 03-Apr-2017 11:34 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.2/">5.2/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.3/">5.3/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.4/">5.4/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.5/">5.5/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.6/">5.6/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.7/">5.7/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.8/">5.8/</a> 16-Oct-2014 13:37 -
|
||||
<a href="http://mirror.hoster.kz/centos/5.9/">5.9/</a> 16-Oct-2014 13:38 -
|
||||
<a href="http://mirror.hoster.kz/centos/6/">6/</a> 02-Jul-2018 15:32 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.0/">6.0/</a> 16-Oct-2014 13:42 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.1/">6.1/</a> 16-Oct-2014 13:42 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.10/">6.10/</a> 02-Jul-2018 15:32 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.2/">6.2/</a> 16-Oct-2014 13:42 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.3/">6.3/</a> 16-Oct-2014 13:42 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.4/">6.4/</a> 16-Oct-2014 13:42 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.5/">6.5/</a> 05-Jan-2015 14:33 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.6/">6.6/</a> 12-Aug-2015 12:23 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.7/">6.7/</a> 01-Jun-2016 16:09 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.8/">6.8/</a> 12-Jun-2017 12:38 -
|
||||
<a href="http://mirror.hoster.kz/centos/6.9/">6.9/</a> 25-Jul-2018 15:42 -
|
||||
<a href="http://mirror.hoster.kz/centos/7/">7/</a> 02-Dec-2018 14:34 -
|
||||
<a href="http://mirror.hoster.kz/centos/7.0.1406/">7.0.1406/</a> 07-Apr-2015 14:24 -
|
||||
<a href="http://mirror.hoster.kz/centos/7.1.1503/">7.1.1503/</a> 08-Jan-2016 15:25 -
|
||||
<a href="http://mirror.hoster.kz/centos/7.2.1511/">7.2.1511/</a> 28-Jan-2017 14:29 -
|
||||
<a href="http://mirror.hoster.kz/centos/7.3.1611/">7.3.1611/</a> 25-Oct-2017 14:57 -
|
||||
<a href="http://mirror.hoster.kz/centos/7.4.1708/">7.4.1708/</a> 24-May-2018 13:25 -
|
||||
<a href="http://mirror.hoster.kz/centos/7.5.1804/">7.5.1804/</a> 04-Jan-2019 16:00 -
|
||||
<a href="http://mirror.hoster.kz/centos/7.6.1810/">7.6.1810/</a> 02-Dec-2018 14:34 -
|
||||
<a href="http://mirror.hoster.kz/centos/HEADER.images/">HEADER.images/</a> 07-Nov-2013 15:21 -
|
||||
<a href="http://mirror.hoster.kz/centos/build/">build/</a> 12-Jun-2005 12:56 -
|
||||
<a href="http://mirror.hoster.kz/centos/dostools/">dostools/</a> 04-Apr-2007 09:45 -
|
||||
<a href="http://mirror.hoster.kz/centos/graphics/">graphics/</a> 12-Jun-2014 11:59 -
|
||||
<a href="http://mirror.hoster.kz/centos/HEADER.html">HEADER.html</a> 29-Sep-2014 19:27 1234
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-3">RPM-GPG-KEY-CentOS-3</a> 15-Mar-2004 23:16 1795
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-4">RPM-GPG-KEY-CentOS-4</a> 26-Feb-2005 17:51 1795
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-5">RPM-GPG-KEY-CentOS-5</a> 19-Feb-2007 17:57 1504
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-6">RPM-GPG-KEY-CentOS-6</a> 10-Jul-2011 14:28 1706
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-7">RPM-GPG-KEY-CentOS-7</a> 04-Jul-2014 16:01 1690
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-Debug-6">RPM-GPG-KEY-CentOS-Debug-6</a> 10-Jul-2011 14:28 1730
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-Debug-7">RPM-GPG-KEY-CentOS-Debug-7</a> 09-Dec-2015 09:59 1004
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-Security-6">RPM-GPG-KEY-CentOS-Security-6</a> 10-Jul-2011 14:28 1730
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-Testing-6">RPM-GPG-KEY-CentOS-Testing-6</a> 10-Jul-2011 14:28 1734
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-CentOS-Testing-7">RPM-GPG-KEY-CentOS-Testing-7</a> 09-Dec-2015 09:59 1690
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-beta">RPM-GPG-KEY-beta</a> 19-Feb-2007 17:56 1512
|
||||
<a href="http://mirror.hoster.kz/centos/RPM-GPG-KEY-centos4">RPM-GPG-KEY-centos4</a> 26-Feb-2005 17:51 1795
|
||||
<a href="http://mirror.hoster.kz/centos/TIME">TIME</a> 17-May-2019 05:35 11
|
||||
<a href="http://mirror.hoster.kz/centos/dir_sizes">dir_sizes</a> 17-May-2019 05:10 963
|
||||
<a href="http://mirror.hoster.kz/centos/filelist.gz">filelist.gz</a> 17-May-2019 05:10 5612272
|
||||
<a href="http://mirror.hoster.kz/centos/timestamp.txt">timestamp.txt</a> 17-May-2019 05:35 29
|
||||
</pre><hr>
|
||||
|
||||
</body></html>
|
||||
Reference in New Issue
Block a user