Created
July 11, 2012 03:41
-
-
Save abedra/3087852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ngx_config.h> | |
#include <ngx_core.h> | |
#include <ngx_http.h> | |
typedef struct { | |
ngx_str_t abusers_filename; | |
} ngx_http_repsheet_loc_conf_t; | |
static char *ngx_http_repsheet(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
static void *ngx_http_repsheet_create_loc_conf(ngx_conf_t *cf); | |
static char *ngx_http_repsheet_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child); | |
static ngx_int_t ngx_http_repsheet_init(ngx_conf_t *cf); | |
int is_abuser(ngx_str_t *ip_addr); | |
static ngx_command_t ngx_http_repsheet_commands[] = { | |
{ ngx_string("process_repsheet"), | |
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
ngx_http_repsheet, | |
NGX_HTTP_LOC_CONF_OFFSET, | |
0, | |
NULL }, | |
ngx_null_command | |
}; | |
static ngx_http_module_t ngx_http_repsheet_module_ctx = { | |
NULL, /* preconfiguration */ | |
ngx_http_repsheet_init, /* postconfiguration */ | |
NULL, /* create main configuration */ | |
NULL, /* init main configuration */ | |
NULL, /* create server configuration */ | |
NULL, /* merge server configuration */ | |
ngx_http_repsheet_create_loc_conf, /* create location configuration */ | |
ngx_http_repsheet_merge_loc_conf /* merge location configuration */ | |
}; | |
ngx_module_t ngx_http_repsheet_module = { | |
NGX_MODULE_V1, | |
&ngx_http_repsheet_module_ctx, /* module context */ | |
ngx_http_repsheet_commands, /* module directives */ | |
NGX_HTTP_MODULE, /* module type */ | |
NULL, /* init master */ | |
NULL, /* init module */ | |
NULL, /* init process */ | |
NULL, /* init thread */ | |
NULL, /* exit thread */ | |
NULL, /* exit process */ | |
NULL, /* exit master */ | |
NGX_MODULE_V1_PADDING | |
}; | |
static char * | |
ngx_http_repsheet(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { | |
ngx_http_repsheet_loc_conf_t *rlcf = conf; | |
ngx_str_t *value; | |
value = cf->args->elts; | |
rlcf->abusers_filename = value[1]; | |
return NGX_CONF_OK; | |
} | |
static void * | |
ngx_http_repsheet_create_loc_conf(ngx_conf_t *cf) { | |
ngx_http_repsheet_loc_conf_t *conf; | |
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_repsheet_loc_conf_t)); | |
if (conf == NULL) { | |
return NULL; | |
} | |
return conf; | |
} | |
static char* | |
ngx_http_repsheet_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) { | |
ngx_http_repsheet_loc_conf_t *prev = parent; | |
ngx_http_repsheet_loc_conf_t *conf = child; | |
if (conf->abusers_filename.len == 0) { | |
conf->abusers_filename = prev->abusers_filename; | |
} | |
return NGX_CONF_OK; | |
} | |
static ngx_int_t | |
ngx_http_repsheet_handler(ngx_http_request_t *r) { | |
ngx_table_elt_t *h; | |
ngx_http_repsheet_loc_conf_t *rlcf; | |
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_repsheet_module); | |
if (rlcf->abusers_filename.len == 0) { | |
ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "Handler got no filename"); | |
} | |
if (is_abuser(&r->connection->addr_text)) { | |
h = ngx_list_push(&r->headers_out.headers); | |
if (h == NULL) { | |
return NGX_ERROR; | |
} | |
h->hash = 1; | |
h->key.len = sizeof ("X-Repsheet") - 1; | |
h->key.data = (u_char *) "X-Repsheet"; | |
h->value.len = sizeof("true") - 1; | |
h->value.data = (u_char *) "true"; | |
} | |
return NGX_DECLINED; | |
} | |
static ngx_int_t | |
ngx_http_repsheet_init(ngx_conf_t *cf) | |
{ | |
ngx_http_handler_pt *h; | |
ngx_http_core_main_conf_t *cmcf; | |
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers); | |
if (h == NULL) { | |
return NGX_ERROR; | |
} | |
*h = ngx_http_repsheet_handler; | |
return NGX_OK; | |
} | |
int is_abuser(ngx_str_t *ip_addr) { | |
const char filename[] = "/Users/abedra/Desktop/abusers.txt"; | |
FILE *fp = fopen(filename, "r"); | |
if (fp != NULL) { | |
char addr[16], svc[256]; | |
char c; | |
int a_pos = 0, s_pos = 0; | |
while ((c = getc(fp)) != EOF) { | |
bzero(&addr, sizeof(addr)); | |
bzero(&svc, sizeof(svc)); | |
a_pos = 0; | |
s_pos = 0; | |
addr[a_pos++] = c; | |
while ((addr[a_pos++] = getc(fp)) != ':'); | |
addr[a_pos-1] = '\0'; | |
while ((svc[s_pos++] = getc(fp)) != '\n'); | |
svc[s_pos-1] = '\0'; | |
if (ngx_strcmp((u_char *)addr, ip_addr->data) == 0) { | |
fclose(fp); | |
return 1; | |
} | |
} | |
fclose(fp); | |
return 0; | |
} else { | |
perror(filename); | |
} | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment