pangu http通过ctemplate重写reject页面(403/404/451)。
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <event2/event.h>
|
||||
#include <event2/buffer.h>
|
||||
|
||||
#include <ctemplate/template.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
@@ -54,6 +55,7 @@ struct pangu_rt
|
||||
int log_level;
|
||||
int thread_num;
|
||||
int scan_table_id[__SCAN_TABLE_MAX];
|
||||
ctemplate::Template* tpl_403,*tpl_404,*tpl_451;
|
||||
char* reject_page;
|
||||
int page_size;
|
||||
};
|
||||
@@ -146,6 +148,15 @@ void pangu_http_init(struct tfe_proxy * proxy)
|
||||
goto error_out;
|
||||
}
|
||||
}
|
||||
|
||||
char page_path[256];
|
||||
MESA_load_profile_string_def(profile, "TEMPLATE", "PAGE_403", page_path,sizeof(page_path), "./template/HTTP403.html");
|
||||
g_pangu_rt->tpl_403 = ctemplate::Template::GetTemplate(page_path,ctemplate::DO_NOT_STRIP);
|
||||
MESA_load_profile_string_def(profile, "TEMPLATE", "PAGE_404", page_path,sizeof(page_path), "./template/HTTP404.html");
|
||||
g_pangu_rt->tpl_404 = ctemplate::Template::GetTemplate(page_path,ctemplate::DO_NOT_STRIP);
|
||||
MESA_load_profile_string_def(profile, "TEMPLATE", "PAGE_451", page_path,sizeof(page_path), "./template/HTTP451.html");
|
||||
g_pangu_rt->tpl_451 = ctemplate::Template::GetTemplate(page_path,ctemplate::DO_NOT_STRIP);
|
||||
|
||||
TFE_LOG_INFO(NULL, "Pangu HTTP init success.");
|
||||
return;
|
||||
|
||||
@@ -301,13 +312,36 @@ static enum pangu_action decide_ctrl_action(const struct Maat_rule_t* hit_rules,
|
||||
return prior_action;
|
||||
}
|
||||
//https://github.com/AndiDittrich/HttpErrorPages
|
||||
static void html_generate(const char* enforce_para, char** page_buff,size_t *page_size)
|
||||
static void html_generate(int cfg_id, int status_code,
|
||||
char** page_buff,size_t *page_size)
|
||||
{
|
||||
*page_buff=g_pangu_rt->reject_page;
|
||||
*page_size=g_pangu_rt->page_size;
|
||||
ctemplate::TemplateDictionary dict("pg_page_dict");
|
||||
dict.SetIntValue("cfg_id", cfg_id);
|
||||
std::string output;
|
||||
ctemplate::Template* tpl=NULL;
|
||||
switch(status_code)
|
||||
{
|
||||
case 403:
|
||||
tpl=g_pangu_rt->tpl_403;
|
||||
break;
|
||||
case 404:
|
||||
tpl=g_pangu_rt->tpl_404;
|
||||
break;
|
||||
case 451:
|
||||
tpl=g_pangu_rt->tpl_451;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
tpl->Expand(&output, &dict);
|
||||
//todo: do I need to delete dict?
|
||||
*page_size=output.length();
|
||||
*page_buff=ALLOC(char, *page_size);
|
||||
memcpy(*page_buff,output.c_str(), *page_size);
|
||||
}
|
||||
static void html_free(char** page_buff)
|
||||
{
|
||||
FREE(page_buff);
|
||||
return;
|
||||
}
|
||||
static int is_http_request(uint64_t events)
|
||||
@@ -668,13 +702,13 @@ static void http_reject(const struct tfe_http_session * session, uint64_t events
|
||||
if(ret!=1)
|
||||
{
|
||||
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Invalid reject rule %d paramter %s",
|
||||
ctx->enforce_rules->config_id, ctx->enforce_para);
|
||||
ctx->enforce_rules[0].config_id, ctx->enforce_para);
|
||||
goto error_out;
|
||||
}
|
||||
to_write_sess=tfe_http_session_allow_write(session);
|
||||
response=tfe_http_session_response_create(to_write_sess, resp_code);
|
||||
|
||||
html_generate(ctx->enforce_para, &page_buff, &page_size);
|
||||
html_generate(ctx->enforce_rules[0].config_id, resp_code, &page_buff, &page_size);
|
||||
_wrap_std_field_write(response, TFE_HTTP_CONT_TYPE, "text/html; charset=utf-8");
|
||||
snprintf(cont_len_str,sizeof(cont_len_str), "%lu", page_size);
|
||||
_wrap_std_field_write(response, TFE_HTTP_CONT_LENGTH, cont_len_str);
|
||||
@@ -691,12 +725,12 @@ static void http_redirect(const struct tfe_http_session * session, uint64_t even
|
||||
char* url=NULL;
|
||||
struct tfe_http_half* response=NULL;
|
||||
struct tfe_http_session* to_write=NULL;
|
||||
url=ALLOC(char, ctx->enforce_rules->serv_def_len);
|
||||
url=ALLOC(char, ctx->enforce_rules[0].serv_def_len);
|
||||
ret=sscanf(ctx->enforce_para,"code=%d%[^;];url=%*[^;];",&resp_code,url);
|
||||
if(ret!=2)
|
||||
{
|
||||
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Invalid redirect rule %d paramter %s",
|
||||
ctx->enforce_rules->config_id, ctx->enforce_para);
|
||||
ctx->enforce_rules[0].config_id, ctx->enforce_para);
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
@@ -827,7 +861,7 @@ void pangu_on_http_end(const struct tfe_stream * stream,
|
||||
|
||||
{
|
||||
struct pangu_http_ctx* ctx=*(struct pangu_http_ctx**)pme;
|
||||
struct pangu_log log_msg={.stream=stream, .http=session, .result=ctx->enforce_rules, .result_num=1};
|
||||
struct pangu_log log_msg={.stream=stream, .http=session, .result=ctx->enforce_rules, .result_num=ctx->n_enforce};
|
||||
if(ctx->action!=PG_ACTION_NONE)
|
||||
{
|
||||
pangu_log_send(g_pangu_rt->send_logger, &log_msg);
|
||||
|
||||
Reference in New Issue
Block a user