diff --git a/inc/ngx_http_waf_module_config.h b/inc/ngx_http_waf_module_config.h index 9042020b..8b8300ea 100644 --- a/inc/ngx_http_waf_module_config.h +++ b/inc/ngx_http_waf_module_config.h @@ -119,6 +119,12 @@ ngx_int_t ngx_http_waf_rule_deatils_handler(ngx_http_request_t* r, ngx_http_vari ngx_int_t ngx_http_waf_spend_handler(ngx_http_request_t* r, ngx_http_variable_value_t* v, uintptr_t data); +/** + * @brief 初始化结构体 ngx_http_waf_main_conf_t +*/ +void* ngx_http_waf_create_main_conf(ngx_conf_t* cf); + + /** * @brief 初始化结构体 ngx_http_waf_loc_conf_t */ diff --git a/inc/ngx_http_waf_module_lru_cache.h b/inc/ngx_http_waf_module_lru_cache.h index a702fa5f..2130e693 100644 --- a/inc/ngx_http_waf_module_lru_cache.h +++ b/inc/ngx_http_waf_module_lru_cache.h @@ -32,4 +32,7 @@ void lru_cache_delete(lru_cache_t* lru, void* key, size_t key_len); void lru_cache_eliminate(lru_cache_t* lru, size_t count); +void lru_cache_destory(lru_cache_t* lru); + + #endif \ No newline at end of file diff --git a/inc/ngx_http_waf_module_type.h b/inc/ngx_http_waf_module_type.h index da9702ae..76695e82 100644 --- a/inc/ngx_http_waf_module_type.h +++ b/inc/ngx_http_waf_module_type.h @@ -279,6 +279,14 @@ typedef struct ngx_http_waf_ctx_s { } ngx_http_waf_ctx_t; +/** + * @struct ngx_http_waf_loc_conf_t +*/ +typedef struct ngx_http_waf_main_conf_s { + ngx_array_t *local_caches; /**< 已经启用的所有的缓存管理器数组 */ +} ngx_http_waf_main_conf_t; + + /** * @struct ngx_http_waf_loc_conf_t * @brief 每个 server 块的配置块 diff --git a/src/ngx_http_waf_module_check.c b/src/ngx_http_waf_module_check.c index 0921e57b..a67da4ad 100644 --- a/src/ngx_http_waf_module_check.c +++ b/src/ngx_http_waf_module_check.c @@ -799,6 +799,7 @@ ngx_int_t ngx_http_waf_regex_exec_arrray_sqli_xss(ngx_http_request_t* r, if (ngx_http_waf_check_flag(loc_conf->waf_mode, NGX_HTTP_WAF_MODE_EXTRA_CACHE) == NGX_HTTP_WAF_TRUE && loc_conf->waf_inspection_capacity != NGX_CONF_UNSET && cache != NULL) { + lru_cache_find_result_t tmp = lru_cache_find(cache, str->data, sizeof(u_char) * str->len); if (tmp.status == NGX_HTTP_WAF_KEY_EXISTS) { cache_hit = NGX_HTTP_WAF_SUCCESS; diff --git a/src/ngx_http_waf_module_config.c b/src/ngx_http_waf_module_config.c index 88242182..6b6c7352 100644 --- a/src/ngx_http_waf_module_config.c +++ b/src/ngx_http_waf_module_config.c @@ -4,6 +4,10 @@ extern ngx_module_t ngx_http_waf_module; extern FILE* ngx_http_waf_in; + +static void _cleanup_lru_cache(void* data); + + char* ngx_http_waf_conf(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) { if (ngx_conf_set_flag_slot(cf, cmd, conf) != NGX_CONF_OK) { return NGX_CONF_ERROR; @@ -419,6 +423,27 @@ char* ngx_http_waf_http_status_conf(ngx_conf_t* cf, ngx_command_t* cmd, void* co } +void* ngx_http_waf_create_main_conf(ngx_conf_t* cf) { + ngx_http_waf_main_conf_t* main_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_waf_main_conf_t)); + + if (main_conf == NULL) { + return NULL; + } + + main_conf->local_caches = ngx_array_create(cf->pool, 20, sizeof(lru_cache_t*)); + + if (main_conf->local_caches == NULL) { + return NULL; + } + + ngx_pool_cleanup_t* cln = ngx_pool_cleanup_add(cf->pool, 0); + cln->data = main_conf->local_caches; + cln->handler = _cleanup_lru_cache; + + return main_conf; +} + + void* ngx_http_waf_create_loc_conf(ngx_conf_t* cf) { return ngx_http_waf_init_conf(cf); } @@ -959,34 +984,52 @@ ngx_int_t ngx_http_waf_init_cc_shm(ngx_conf_t* cf, ngx_http_waf_loc_conf_t* conf ngx_int_t ngx_http_waf_init_lru_cache(ngx_conf_t* cf, ngx_http_waf_loc_conf_t* conf) { - conf->black_url_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t)); - conf->black_args_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t)); - conf->black_ua_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t)); - conf->black_referer_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t)); - conf->black_cookie_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t)); - conf->white_url_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t)); - conf->white_referer_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t)); + ngx_http_waf_main_conf_t* main_conf = ngx_http_conf_get_module_main_conf(cf, ngx_http_waf_module); + + conf->black_url_inspection_cache = NULL; + conf->black_args_inspection_cache = NULL; + conf->black_ua_inspection_cache = NULL; + conf->black_referer_inspection_cache = NULL; + conf->black_cookie_inspection_cache = NULL; + conf->white_url_inspection_cache = NULL; + conf->white_referer_inspection_cache = NULL; + + lru_cache_t** p = NULL; lru_cache_init(&conf->black_url_inspection_cache, - conf->waf_inspection_capacity, gernal_pool, cf->pool); + conf->waf_inspection_capacity, std, NULL); + p = ngx_array_push(main_conf->local_caches); + *p = conf->black_url_inspection_cache; lru_cache_init(&conf->black_args_inspection_cache, - conf->waf_inspection_capacity, gernal_pool, cf->pool); + conf->waf_inspection_capacity, std, NULL); + p = ngx_array_push(main_conf->local_caches); + *p = conf->black_args_inspection_cache; lru_cache_init(&conf->black_ua_inspection_cache, - conf->waf_inspection_capacity, gernal_pool, cf->pool); + conf->waf_inspection_capacity, std, NULL); + p = ngx_array_push(main_conf->local_caches); + *p = conf->black_ua_inspection_cache; lru_cache_init(&conf->black_referer_inspection_cache, - conf->waf_inspection_capacity, gernal_pool, cf->pool); + conf->waf_inspection_capacity, std, NULL); + p = ngx_array_push(main_conf->local_caches); + *p = conf->black_referer_inspection_cache; lru_cache_init(&conf->black_cookie_inspection_cache, - conf->waf_inspection_capacity, gernal_pool, cf->pool); + conf->waf_inspection_capacity, std, NULL); + p = ngx_array_push(main_conf->local_caches); + *p = conf->black_cookie_inspection_cache; lru_cache_init(&conf->white_url_inspection_cache, - conf->waf_inspection_capacity, gernal_pool, cf->pool); + conf->waf_inspection_capacity, std, NULL); + p = ngx_array_push(main_conf->local_caches); + *p = conf->white_url_inspection_cache; lru_cache_init(&conf->white_referer_inspection_cache, - conf->waf_inspection_capacity, gernal_pool, cf->pool); + conf->waf_inspection_capacity, std, NULL); + p = ngx_array_push(main_conf->local_caches); + *p = conf->white_referer_inspection_cache; return NGX_HTTP_WAF_SUCCESS; } @@ -1131,3 +1174,13 @@ ngx_int_t ngx_http_waf_free_memory(ngx_conf_t* cf, ngx_http_waf_loc_conf_t* conf return NGX_HTTP_WAF_SUCCESS; } + + +static void _cleanup_lru_cache(void* data) { + ngx_array_t* caches = (ngx_array_t*)data; + + for (ngx_uint_t i = 0; i < caches->nelts; i++) { + lru_cache_t* cache = ((lru_cache_t**)caches->elts)[i]; + lru_cache_destory(cache); + } +} diff --git a/src/ngx_http_waf_module_core.c b/src/ngx_http_waf_module_core.c index 6127bef2..bee8fcac 100644 --- a/src/ngx_http_waf_module_core.c +++ b/src/ngx_http_waf_module_core.c @@ -77,7 +77,7 @@ static ngx_command_t ngx_http_waf_commands[] = { static ngx_http_module_t ngx_http_waf_module_ctx = { NULL, ngx_http_waf_init_after_load_config, - NULL, + ngx_http_waf_create_main_conf, NULL, NULL, NULL, diff --git a/src/ngx_http_waf_module_lru_cache.c b/src/ngx_http_waf_module_lru_cache.c index 855bf3f2..9df7edc0 100644 --- a/src/ngx_http_waf_module_lru_cache.c +++ b/src/ngx_http_waf_module_lru_cache.c @@ -171,6 +171,11 @@ void lru_cache_eliminate(lru_cache_t* lru, size_t count) { } +void lru_cache_destory(lru_cache_t* lru) { + mem_pool_free(&lru->pool, lru); +} + + lru_cache_item_t* _lru_cache_hash_find(lru_cache_t* lru, void* key, size_t key_len) { lru_cache_item_t* ret; HASH_FIND(hh, lru->hash_head, key, key_len, ret); diff --git a/src/ngx_http_waf_module_mem_pool.c b/src/ngx_http_waf_module_mem_pool.c index 90c544bf..9693f0ee 100644 --- a/src/ngx_http_waf_module_mem_pool.c +++ b/src/ngx_http_waf_module_mem_pool.c @@ -19,7 +19,7 @@ ngx_int_t mem_pool_init(mem_pool_t* pool, mem_pool_type_e type, void* native_poo void* mem_pool_calloc(mem_pool_t* pool, ngx_uint_t byte_size) { void* addr; switch (pool->type) { - case std: addr = malloc(byte_size); ngx_memzero(addr, byte_size);break; + case std: addr = malloc(byte_size); ngx_memzero(addr, byte_size); break; case gernal_pool: addr = ngx_pcalloc(pool->native_pool.gernal_pool, byte_size); break; case slab_pool: addr = ngx_slab_calloc_locked(pool->native_pool.slab_pool, byte_size); break; default: addr = NULL; break;