all repos — ngx-http-blocklistd @ master

ngx_http_blocklistd_module.c (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <blocklist.h>

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;
}