#include #include #include #include static struct blocklist *bl_state = NULL; static ngx_int_t ngx_http_blocklistd_handler(ngx_http_request_t *r); static char *ngx_http_blocklistd( ngx_conf_t *cf, ngx_command_t *cmd, void *conf ); static ngx_int_t ngx_http_blocklistd_init_process(ngx_cycle_t *cycle); static void ngx_http_blocklistd_exit_process(ngx_cycle_t *cycle); static ngx_command_t ngx_http_blocklistd_commands[] = { { ngx_string("blocklistd"), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_blocklistd, 0, 0, NULL }, ngx_null_command }; static ngx_http_module_t ngx_http_blocklistd_module_ctx = { NULL, /* preconfiguration */ NULL, /* postconfiguration */ NULL, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ NULL, /* create location configuration */ NULL /* merge location configuration */ }; ngx_module_t ngx_http_blocklistd_module = { NGX_MODULE_V1, &ngx_http_blocklistd_module_ctx, /* module context */ ngx_http_blocklistd_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ ngx_http_blocklistd_init_process, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ ngx_http_blocklistd_exit_process, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING }; static ngx_int_t ngx_http_blocklistd_init_process(ngx_cycle_t *cycle) { bl_state = blocklist_open(); if (bl_state == NULL) { ngx_log_error( NGX_LOG_ERR, cycle->log, 0, "[blocklistd] init failed to open socket connection" ); } else { ngx_log_error( NGX_LOG_DEBUG, cycle->log, 0, "[blocklistd] socket to blocklistd establised" ); } return NGX_OK; } static void ngx_http_blocklistd_exit_process(ngx_cycle_t *cycle) { if (bl_state != NULL) { blocklist_close(bl_state); bl_state = NULL; ngx_log_error( NGX_LOG_DEBUG, cycle->log, 0, "[blocklistd] socket to blocklistd stopped" ); } } static ngx_int_t ngx_http_blocklistd_handler(ngx_http_request_t *r) { if (bl_state == NULL) { ngx_log_error( NGX_LOG_DEBUG, r->connection->log, 0, "[blocklistd] socket connection lost? Trying to reconnect" ); bl_state = blocklist_open(); } if (bl_state == NULL) { ngx_log_error( NGX_LOG_ERR, r->connection->log, 0, "[blocklistd] no socket connection available" ); } else { blocklist_r(bl_state, 1, r->connection->fd, "nginx"); ngx_log_error( NGX_LOG_DEBUG, r->connection->log, 0, "[blocklistd] offense sent" ); } return NGX_HTTP_CLOSE; } static char *ngx_http_blocklistd( ngx_conf_t *cf, ngx_command_t *cmd, void *conf ){ ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf->handler = ngx_http_blocklistd_handler; return NGX_CONF_OK; }