Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 17th, 2012  |  syntax: C  |  size: 3.06 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <ngx_core.h>
  2. #include <ngx_string.h>
  3. #include <ngx_config.h>
  4. #include <ngx_core.h>
  5. #include <ngx_http.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <mhash.h>
  10. #include <openssl/md5.h>
  11. #include <ctype.h>
  12. #include <sys/socket.h>
  13. #include <arpa/inet.h>
  14. #include <netdb.h>
  15.  
  16. static void * ngx_http_ip_checker_create_loc_conf(ngx_conf_t*);
  17. static char * ngx_http_ip_checker_merge_loc_conf (ngx_conf_t*, void*, void*);
  18. static ngx_int_t ngx_http_ip_checker_add_variables(ngx_conf_t *cf);
  19. static ngx_int_t ngx_http_ip_checker_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
  20.  
  21.  
  22. typedef struct {
  23.   ngx_flag_t enable;
  24. } ngx_http_ip_checker_loc_conf_t;
  25.  
  26. static ngx_command_t ngx_http_ip_checker_commands[] = {
  27.   {
  28.     ngx_string("ip_checker"),
  29.     NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  30.     ngx_conf_set_flag_slot,
  31.     NGX_HTTP_LOC_CONF_OFFSET,
  32.     offsetof(ngx_http_ip_checker_loc_conf_t, enable),
  33.     NULL
  34.   },
  35. };
  36.  
  37. static ngx_http_module_t ngx_http_ip_checker_module_ctx = {
  38.   ngx_http_ip_checker_add_variables,
  39.   NULL,
  40.   NULL,
  41.   NULL,
  42.   NULL,
  43.   NULL,
  44.   ngx_http_ip_checker_create_loc_conf,
  45.   ngx_http_ip_checker_merge_loc_conf
  46. };
  47.  
  48. ngx_module_t ngx_http_ip_checker_module = {
  49.   NGX_MODULE_V1,
  50.   &ngx_http_ip_checker_module_ctx,
  51.   ngx_http_ip_checker_commands,
  52.   NGX_HTTP_MODULE,
  53.   NULL,
  54.   NULL,
  55.   NULL,
  56.   NULL,
  57.   NULL,
  58.   NULL,
  59.   NULL,
  60.   NGX_MODULE_V1_PADDING
  61. };
  62.  
  63. static ngx_str_t  ngx_http_ip_checker = ngx_string("reverseismatch");
  64.  
  65.  
  66. static void * ngx_http_ip_checker_create_loc_conf(ngx_conf_t *cf)
  67. {
  68.   ngx_http_ip_checker_loc_conf_t *conf;
  69.  
  70.   conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ip_checker_loc_conf_t));
  71.   if (conf == NULL) {
  72.     return NGX_CONF_ERROR;
  73.   }
  74.   conf->enable = NGX_CONF_UNSET;
  75.   return conf;
  76. }
  77.  
  78. static char * ngx_http_ip_checker_merge_loc_conf (ngx_conf_t *cf, void *parent, void *child)
  79. {
  80.   ngx_http_ip_checker_loc_conf_t *prev = parent;
  81.   ngx_http_ip_checker_loc_conf_t *conf = child;
  82.  
  83.   ngx_conf_merge_value(conf->enable, prev->enable, 0);
  84.   return NGX_CONF_OK;
  85. }
  86.  
  87. static ngx_int_t ngx_http_ip_checker_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) {
  88.   ngx_http_ip_checker_loc_conf_t *gcc;
  89.   int value = 0;
  90.   gcc = ngx_http_get_module_loc_conf(r, ngx_http_ip_checker_module);
  91.   if (gcc->enable != 1)
  92.   {
  93.       ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ip_checker: module not enabled");
  94.   }
  95.   v->not_found= 0;
  96.   v->valid = 1;
  97.   v->no_cacheable = 0;
  98.   v->data = ngx_pcalloc(r->pool, sizeof(char) * 3);
  99.   if (v->data == NULL){
  100.      return NGX_ERROR;
  101.   }
  102.  
  103.   ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ip_checker: %s", r->connection->addr_text.data);
  104.  
  105.   return NGX_OK;
  106. }
  107.  
  108. static ngx_int_t
  109. ngx_http_ip_checker_add_variables(ngx_conf_t *cf)
  110. {
  111.     ngx_http_variable_t  *var;
  112.  
  113.     var = ngx_http_add_variable(cf, &ngx_http_ip_checker, NGX_HTTP_VAR_NOHASH);
  114.     if (var == NULL) {
  115.         return NGX_ERROR;
  116.     }
  117.     var->get_handler = ngx_http_ip_checker_variable;
  118.     return NGX_OK;
  119. }