#include <ngx_core.h>
#include <ngx_string.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mhash.h>
#include <openssl/md5.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
static void * ngx_http_ip_checker_create_loc_conf(ngx_conf_t*);
static char * ngx_http_ip_checker_merge_loc_conf (ngx_conf_t*, void*, void*);
static ngx_int_t ngx_http_ip_checker_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_ip_checker_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
typedef struct {
ngx_flag_t enable;
} ngx_http_ip_checker_loc_conf_t;
static ngx_command_t ngx_http_ip_checker_commands[] = {
{
ngx_string("ip_checker"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_ip_checker_loc_conf_t, enable),
NULL
},
};
static ngx_http_module_t ngx_http_ip_checker_module_ctx = {
ngx_http_ip_checker_add_variables,
NULL,
NULL,
NULL,
NULL,
NULL,
ngx_http_ip_checker_create_loc_conf,
ngx_http_ip_checker_merge_loc_conf
};
ngx_module_t ngx_http_ip_checker_module = {
NGX_MODULE_V1,
&ngx_http_ip_checker_module_ctx,
ngx_http_ip_checker_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_str_t ngx_http_ip_checker = ngx_string("reverseismatch");
static void * ngx_http_ip_checker_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_ip_checker_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ip_checker_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET;
return conf;
}
static char * ngx_http_ip_checker_merge_loc_conf (ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_ip_checker_loc_conf_t *prev = parent;
ngx_http_ip_checker_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->enable, prev->enable, 0);
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_ip_checker_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) {
ngx_http_ip_checker_loc_conf_t *gcc;
int value = 0;
gcc = ngx_http_get_module_loc_conf(r, ngx_http_ip_checker_module);
if (gcc->enable != 1)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ip_checker: module not enabled");
}
v->not_found= 0;
v->valid = 1;
v->no_cacheable = 0;
v->data = ngx_pcalloc(r->pool, sizeof(char) * 3);
if (v->data == NULL){
return NGX_ERROR;
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ip_checker: %s", r->connection->addr_text.data);
return NGX_OK;
}
static ngx_int_t
ngx_http_ip_checker_add_variables(ngx_conf_t *cf)
{
ngx_http_variable_t *var;
var = ngx_http_add_variable(cf, &ngx_http_ip_checker, NGX_HTTP_VAR_NOHASH);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = ngx_http_ip_checker_variable;
return NGX_OK;
}