changeset 6630:558db057adaa

Stream: geoip module.
author Vladimir Homutov <vl@nginx.com>
date Tue, 12 Jul 2016 17:34:43 +0300
parents 8ed51b02f655
children 80875b75d27e
files auto/modules auto/options src/stream/ngx_stream_geoip_module.c
diffstat 3 files changed, 151 insertions(+), 246 deletions(-) [+]
line wrap: on
line diff
--- a/auto/modules
+++ b/auto/modules
@@ -1024,6 +1024,16 @@ if [ $STREAM != NO ]; then
         . auto/module
     fi
 
+    if [ $STREAM_GEOIP != NO ]; then
+        ngx_module_name=ngx_stream_geoip_module
+        ngx_module_deps=
+        ngx_module_srcs=src/stream/ngx_stream_geoip_module.c
+        ngx_module_libs=GEOIP
+        ngx_module_link=$STREAM_GEOIP
+
+        . auto/module
+    fi
+
     if [ $STREAM_MAP = YES ]; then
         ngx_module_name=ngx_stream_map_module
         ngx_module_deps=
--- a/auto/options
+++ b/auto/options
@@ -117,6 +117,7 @@ STREAM=NO
 STREAM_SSL=NO
 STREAM_LIMIT_CONN=YES
 STREAM_ACCESS=YES
+STREAM_GEOIP=NO
 STREAM_MAP=YES
 STREAM_RETURN=YES
 STREAM_UPSTREAM_HASH=YES
@@ -293,6 +294,9 @@ use the \"--with-mail_ssl_module\" optio
         --with-stream)                   STREAM=YES                 ;;
         --with-stream=dynamic)           STREAM=DYNAMIC             ;;
         --with-stream_ssl_module)        STREAM_SSL=YES             ;;
+        --with-stream_geoip_module)      STREAM_GEOIP=YES           ;;
+        --with-stream_geoip_module=dynamic)
+                                         STREAM_GEOIP=DYNAMIC       ;;
         --without-stream_limit_conn_module)
                                          STREAM_LIMIT_CONN=NO       ;;
         --without-stream_access_module)  STREAM_ACCESS=NO           ;;
@@ -494,6 +498,8 @@ cat << END
   --with-stream                      enable TCP/UDP proxy module
   --with-stream=dynamic              enable dynamic TCP/UDP proxy module
   --with-stream_ssl_module           enable ngx_stream_ssl_module
+  --with-stream_geoip_module         enable ngx_stream_geoip_module
+  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
   --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
   --without-stream_access_module     disable ngx_stream_access_module
   --without-stream_map_module        disable ngx_stream_map_module
copy from src/http/modules/ngx_http_geoip_module.c
copy to src/stream/ngx_stream_geoip_module.c
--- a/src/http/modules/ngx_http_geoip_module.c
+++ b/src/stream/ngx_stream_geoip_module.c
@@ -7,7 +7,7 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_http.h>
+#include <ngx_stream.h>
 
 #include <GeoIP.h>
 #include <GeoIPCity.h>
@@ -22,27 +22,25 @@ typedef struct {
     GeoIP        *country;
     GeoIP        *org;
     GeoIP        *city;
-    ngx_array_t  *proxies;    /* array of ngx_cidr_t */
-    ngx_flag_t    proxy_recursive;
 #if (NGX_HAVE_GEOIP_V6)
     unsigned      country_v6:1;
     unsigned      org_v6:1;
     unsigned      city_v6:1;
 #endif
-} ngx_http_geoip_conf_t;
+} ngx_stream_geoip_conf_t;
 
 
 typedef struct {
     ngx_str_t    *name;
     uintptr_t     data;
-} ngx_http_geoip_var_t;
+} ngx_stream_geoip_var_t;
 
 
-typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *,
+typedef const char *(*ngx_stream_geoip_variable_handler_pt)(GeoIP *,
     u_long addr);
 
 
-ngx_http_geoip_variable_handler_pt ngx_http_geoip_country_functions[] = {
+ngx_stream_geoip_variable_handler_pt ngx_stream_geoip_country_functions[] = {
     GeoIP_country_code_by_ipnum,
     GeoIP_country_code3_by_ipnum,
     GeoIP_country_name_by_ipnum,
@@ -51,11 +49,13 @@ ngx_http_geoip_variable_handler_pt ngx_h
 
 #if (NGX_HAVE_GEOIP_V6)
 
-typedef const char *(*ngx_http_geoip_variable_handler_v6_pt)(GeoIP *,
+typedef const char *(*ngx_stream_geoip_variable_handler_v6_pt)(GeoIP *,
     geoipv6_t addr);
 
 
-ngx_http_geoip_variable_handler_v6_pt ngx_http_geoip_country_v6_functions[] = {
+ngx_stream_geoip_variable_handler_v6_pt
+    ngx_stream_geoip_country_v6_functions[] =
+{
     GeoIP_country_code_by_ipnum_v6,
     GeoIP_country_code3_by_ipnum_v6,
     GeoIP_country_name_by_ipnum_v6,
@@ -64,97 +64,75 @@ ngx_http_geoip_variable_handler_v6_pt ng
 #endif
 
 
-static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data);
-static ngx_int_t ngx_http_geoip_org_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data);
-static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data);
-static ngx_int_t ngx_http_geoip_region_name_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data);
-static ngx_int_t ngx_http_geoip_city_float_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data);
-static ngx_int_t ngx_http_geoip_city_int_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data);
-static GeoIPRecord *ngx_http_geoip_get_city_record(ngx_http_request_t *r);
+static ngx_int_t ngx_stream_geoip_country_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_geoip_org_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_geoip_city_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_geoip_region_name_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_geoip_city_float_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_geoip_city_int_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
+static GeoIPRecord *ngx_stream_geoip_get_city_record(ngx_stream_session_t *s);
 
-static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf);
-static void *ngx_http_geoip_create_conf(ngx_conf_t *cf);
-static char *ngx_http_geoip_init_conf(ngx_conf_t *cf, void *conf);
-static char *ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd,
-    void *conf);
-static char *ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd,
+static ngx_int_t ngx_stream_geoip_add_variables(ngx_conf_t *cf);
+static void *ngx_stream_geoip_create_conf(ngx_conf_t *cf);
+static char *ngx_stream_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
-static char *ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd,
-    void *conf);
-static char *ngx_http_geoip_proxy(ngx_conf_t *cf, ngx_command_t *cmd,
+static char *ngx_stream_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
-static ngx_int_t ngx_http_geoip_cidr_value(ngx_conf_t *cf, ngx_str_t *net,
-    ngx_cidr_t *cidr);
-static void ngx_http_geoip_cleanup(void *data);
+static char *ngx_stream_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
+static void ngx_stream_geoip_cleanup(void *data);
 
 
-static ngx_command_t  ngx_http_geoip_commands[] = {
+static ngx_command_t  ngx_stream_geoip_commands[] = {
 
     { ngx_string("geoip_country"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
-      ngx_http_geoip_country,
-      NGX_HTTP_MAIN_CONF_OFFSET,
+      NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE12,
+      ngx_stream_geoip_country,
+      NGX_STREAM_MAIN_CONF_OFFSET,
       0,
       NULL },
 
     { ngx_string("geoip_org"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
-      ngx_http_geoip_org,
-      NGX_HTTP_MAIN_CONF_OFFSET,
+      NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE12,
+      ngx_stream_geoip_org,
+      NGX_STREAM_MAIN_CONF_OFFSET,
       0,
       NULL },
 
     { ngx_string("geoip_city"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
-      ngx_http_geoip_city,
-      NGX_HTTP_MAIN_CONF_OFFSET,
+      NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE12,
+      ngx_stream_geoip_city,
+      NGX_STREAM_MAIN_CONF_OFFSET,
       0,
       NULL },
 
-    { ngx_string("geoip_proxy"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
-      ngx_http_geoip_proxy,
-      NGX_HTTP_MAIN_CONF_OFFSET,
-      0,
-      NULL },
-
-    { ngx_string("geoip_proxy_recursive"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
-      ngx_conf_set_flag_slot,
-      NGX_HTTP_MAIN_CONF_OFFSET,
-      offsetof(ngx_http_geoip_conf_t, proxy_recursive),
-      NULL },
-
       ngx_null_command
 };
 
 
-static ngx_http_module_t  ngx_http_geoip_module_ctx = {
-    ngx_http_geoip_add_variables,          /* preconfiguration */
+static ngx_stream_module_t  ngx_stream_geoip_module_ctx = {
+    ngx_stream_geoip_add_variables,        /* preconfiguration */
     NULL,                                  /* postconfiguration */
 
-    ngx_http_geoip_create_conf,            /* create main configuration */
-    ngx_http_geoip_init_conf,              /* init main configuration */
+    ngx_stream_geoip_create_conf,          /* create main configuration */
+    NULL,                                  /* init main configuration */
 
     NULL,                                  /* create server configuration */
-    NULL,                                  /* merge server configuration */
-
-    NULL,                                  /* create location configuration */
-    NULL                                   /* merge location configuration */
+    NULL                                   /* merge server configuration */
 };
 
 
-ngx_module_t  ngx_http_geoip_module = {
+ngx_module_t  ngx_stream_geoip_module = {
     NGX_MODULE_V1,
-    &ngx_http_geoip_module_ctx,            /* module context */
-    ngx_http_geoip_commands,               /* module directives */
-    NGX_HTTP_MODULE,                       /* module type */
+    &ngx_stream_geoip_module_ctx,          /* module context */
+    ngx_stream_geoip_commands,             /* module directives */
+    NGX_STREAM_MODULE,                     /* module type */
     NULL,                                  /* init master */
     NULL,                                  /* init module */
     NULL,                                  /* init process */
@@ -166,70 +144,70 @@ ngx_module_t  ngx_http_geoip_module = {
 };
 
 
-static ngx_http_variable_t  ngx_http_geoip_vars[] = {
+static ngx_stream_variable_t  ngx_stream_geoip_vars[] = {
 
     { ngx_string("geoip_country_code"), NULL,
-      ngx_http_geoip_country_variable,
+      ngx_stream_geoip_country_variable,
       NGX_GEOIP_COUNTRY_CODE, 0, 0 },
 
     { ngx_string("geoip_country_code3"), NULL,
-      ngx_http_geoip_country_variable,
+      ngx_stream_geoip_country_variable,
       NGX_GEOIP_COUNTRY_CODE3, 0, 0 },
 
     { ngx_string("geoip_country_name"), NULL,
-      ngx_http_geoip_country_variable,
+      ngx_stream_geoip_country_variable,
       NGX_GEOIP_COUNTRY_NAME, 0, 0 },
 
     { ngx_string("geoip_org"), NULL,
-      ngx_http_geoip_org_variable,
+      ngx_stream_geoip_org_variable,
       0, 0, 0 },
 
     { ngx_string("geoip_city_continent_code"), NULL,
-      ngx_http_geoip_city_variable,
+      ngx_stream_geoip_city_variable,
       offsetof(GeoIPRecord, continent_code), 0, 0 },
 
     { ngx_string("geoip_city_country_code"), NULL,
-      ngx_http_geoip_city_variable,
+      ngx_stream_geoip_city_variable,
       offsetof(GeoIPRecord, country_code), 0, 0 },
 
     { ngx_string("geoip_city_country_code3"), NULL,
-      ngx_http_geoip_city_variable,
+      ngx_stream_geoip_city_variable,
       offsetof(GeoIPRecord, country_code3), 0, 0 },
 
     { ngx_string("geoip_city_country_name"), NULL,
-      ngx_http_geoip_city_variable,
+      ngx_stream_geoip_city_variable,
       offsetof(GeoIPRecord, country_name), 0, 0 },
 
     { ngx_string("geoip_region"), NULL,
-      ngx_http_geoip_city_variable,
+      ngx_stream_geoip_city_variable,
       offsetof(GeoIPRecord, region), 0, 0 },
 
     { ngx_string("geoip_region_name"), NULL,
-      ngx_http_geoip_region_name_variable,
+      ngx_stream_geoip_region_name_variable,
       0, 0, 0 },
 
     { ngx_string("geoip_city"), NULL,
-      ngx_http_geoip_city_variable,
+      ngx_stream_geoip_city_variable,
       offsetof(GeoIPRecord, city), 0, 0 },
 
     { ngx_string("geoip_postal_code"), NULL,
-      ngx_http_geoip_city_variable,
+      ngx_stream_geoip_city_variable,
       offsetof(GeoIPRecord, postal_code), 0, 0 },
 
     { ngx_string("geoip_latitude"), NULL,
-      ngx_http_geoip_city_float_variable,
+      ngx_stream_geoip_city_float_variable,
       offsetof(GeoIPRecord, latitude), 0, 0 },
 
     { ngx_string("geoip_longitude"), NULL,
-      ngx_http_geoip_city_float_variable,
+      ngx_stream_geoip_city_float_variable,
       offsetof(GeoIPRecord, longitude), 0, 0 },
 
     { ngx_string("geoip_dma_code"), NULL,
-      ngx_http_geoip_city_int_variable,
+      ngx_stream_geoip_city_int_variable,
       offsetof(GeoIPRecord, dma_code), 0, 0 },
 
     { ngx_string("geoip_area_code"), NULL,
-      ngx_http_geoip_city_int_variable,
+      ngx_stream_geoip_city_int_variable,
       offsetof(GeoIPRecord, area_code), 0, 0 },
 
     { ngx_null_string, NULL, NULL, 0, 0, 0 }
@@ -237,22 +215,14 @@ static ngx_http_variable_t  ngx_http_geo
 
 
 static u_long
-ngx_http_geoip_addr(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf)
+ngx_stream_geoip_addr(ngx_stream_session_t *s, ngx_stream_geoip_conf_t *gcf)
 {
     ngx_addr_t           addr;
-    ngx_array_t         *xfwd;
     struct sockaddr_in  *sin;
 
-    addr.sockaddr = r->connection->sockaddr;
-    addr.socklen = r->connection->socklen;
-    /* addr.name = r->connection->addr_text; */
-
-    xfwd = &r->headers_in.x_forwarded_for;
-
-    if (xfwd->nelts > 0 && gcf->proxies != NULL) {
-        (void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL,
-                                           gcf->proxies, gcf->proxy_recursive);
-    }
+    addr.sockaddr = s->connection->sockaddr;
+    addr.socklen = s->connection->socklen;
+    /* addr.name = s->connection->addr_text; */
 
 #if (NGX_HAVE_INET6)
 
@@ -289,25 +259,17 @@ ngx_http_geoip_addr(ngx_http_request_t *
 #if (NGX_HAVE_GEOIP_V6)
 
 static geoipv6_t
-ngx_http_geoip_addr_v6(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf)
+ngx_stream_geoip_addr_v6(ngx_stream_session_t *s, ngx_stream_geoip_conf_t *gcf)
 {
     ngx_addr_t            addr;
-    ngx_array_t          *xfwd;
     in_addr_t             addr4;
     struct in6_addr       addr6;
     struct sockaddr_in   *sin;
     struct sockaddr_in6  *sin6;
 
-    addr.sockaddr = r->connection->sockaddr;
-    addr.socklen = r->connection->socklen;
-    /* addr.name = r->connection->addr_text; */
-
-    xfwd = &r->headers_in.x_forwarded_for;
-
-    if (xfwd->nelts > 0 && gcf->proxies != NULL) {
-        (void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL,
-                                           gcf->proxies, gcf->proxy_recursive);
-    }
+    addr.sockaddr = s->connection->sockaddr;
+    addr.socklen = s->connection->socklen;
+    /* addr.name = s->connection->addr_text; */
 
     switch (addr.sockaddr->sa_family) {
 
@@ -338,20 +300,20 @@ ngx_http_geoip_addr_v6(ngx_http_request_
 
 
 static ngx_int_t
-ngx_http_geoip_country_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data)
+ngx_stream_geoip_country_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
 {
-    ngx_http_geoip_variable_handler_pt     handler =
-        ngx_http_geoip_country_functions[data];
+    ngx_stream_geoip_variable_handler_pt     handler =
+        ngx_stream_geoip_country_functions[data];
 #if (NGX_HAVE_GEOIP_V6)
-    ngx_http_geoip_variable_handler_v6_pt  handler_v6 =
-        ngx_http_geoip_country_v6_functions[data];
+    ngx_stream_geoip_variable_handler_v6_pt  handler_v6 =
+        ngx_stream_geoip_country_v6_functions[data];
 #endif
 
-    const char             *val;
-    ngx_http_geoip_conf_t  *gcf;
+    const char               *val;
+    ngx_stream_geoip_conf_t  *gcf;
 
-    gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
+    gcf = ngx_stream_get_module_main_conf(s, ngx_stream_geoip_module);
 
     if (gcf->country == NULL) {
         goto not_found;
@@ -359,10 +321,10 @@ ngx_http_geoip_country_variable(ngx_http
 
 #if (NGX_HAVE_GEOIP_V6)
     val = gcf->country_v6
-              ? handler_v6(gcf->country, ngx_http_geoip_addr_v6(r, gcf))
-              : handler(gcf->country, ngx_http_geoip_addr(r, gcf));
+              ? handler_v6(gcf->country, ngx_stream_geoip_addr_v6(s, gcf))
+              : handler(gcf->country, ngx_stream_geoip_addr(s, gcf));
 #else
-    val = handler(gcf->country, ngx_http_geoip_addr(r, gcf));
+    val = handler(gcf->country, ngx_stream_geoip_addr(s, gcf));
 #endif
 
     if (val == NULL) {
@@ -386,14 +348,14 @@ not_found:
 
 
 static ngx_int_t
-ngx_http_geoip_org_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data)
+ngx_stream_geoip_org_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
 {
-    size_t                  len;
-    char                   *val;
-    ngx_http_geoip_conf_t  *gcf;
+    size_t                    len;
+    char                     *val;
+    ngx_stream_geoip_conf_t  *gcf;
 
-    gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
+    gcf = ngx_stream_get_module_main_conf(s, ngx_stream_geoip_module);
 
     if (gcf->org == NULL) {
         goto not_found;
@@ -402,11 +364,11 @@ ngx_http_geoip_org_variable(ngx_http_req
 #if (NGX_HAVE_GEOIP_V6)
     val = gcf->org_v6
               ? GeoIP_name_by_ipnum_v6(gcf->org,
-                                       ngx_http_geoip_addr_v6(r, gcf))
+                                       ngx_stream_geoip_addr_v6(s, gcf))
               : GeoIP_name_by_ipnum(gcf->org,
-                                    ngx_http_geoip_addr(r, gcf));
+                                    ngx_stream_geoip_addr(s, gcf));
 #else
-    val = GeoIP_name_by_ipnum(gcf->org, ngx_http_geoip_addr(r, gcf));
+    val = GeoIP_name_by_ipnum(gcf->org, ngx_stream_geoip_addr(s, gcf));
 #endif
 
     if (val == NULL) {
@@ -414,7 +376,7 @@ ngx_http_geoip_org_variable(ngx_http_req
     }
 
     len = ngx_strlen(val);
-    v->data = ngx_pnalloc(r->pool, len);
+    v->data = ngx_pnalloc(s->connection->pool, len);
     if (v->data == NULL) {
         ngx_free(val);
         return NGX_ERROR;
@@ -440,14 +402,14 @@ not_found:
 
 
 static ngx_int_t
-ngx_http_geoip_city_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data)
+ngx_stream_geoip_city_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
 {
     char         *val;
     size_t        len;
     GeoIPRecord  *gr;
 
-    gr = ngx_http_geoip_get_city_record(r);
+    gr = ngx_stream_geoip_get_city_record(s);
     if (gr == NULL) {
         goto not_found;
     }
@@ -458,7 +420,7 @@ ngx_http_geoip_city_variable(ngx_http_re
     }
 
     len = ngx_strlen(val);
-    v->data = ngx_pnalloc(r->pool, len);
+    v->data = ngx_pnalloc(s->connection->pool, len);
     if (v->data == NULL) {
         GeoIPRecord_delete(gr);
         return NGX_ERROR;
@@ -488,14 +450,14 @@ not_found:
 
 
 static ngx_int_t
-ngx_http_geoip_region_name_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data)
+ngx_stream_geoip_region_name_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
 {
     size_t        len;
     const char   *val;
     GeoIPRecord  *gr;
 
-    gr = ngx_http_geoip_get_city_record(r);
+    gr = ngx_stream_geoip_get_city_record(s);
     if (gr == NULL) {
         goto not_found;
     }
@@ -509,7 +471,7 @@ ngx_http_geoip_region_name_variable(ngx_
     }
 
     len = ngx_strlen(val);
-    v->data = ngx_pnalloc(r->pool, len);
+    v->data = ngx_pnalloc(s->connection->pool, len);
     if (v->data == NULL) {
         return NGX_ERROR;
     }
@@ -532,19 +494,19 @@ not_found:
 
 
 static ngx_int_t
-ngx_http_geoip_city_float_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data)
+ngx_stream_geoip_city_float_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
 {
     float         val;
     GeoIPRecord  *gr;
 
-    gr = ngx_http_geoip_get_city_record(r);
+    gr = ngx_stream_geoip_get_city_record(s);
     if (gr == NULL) {
         v->not_found = 1;
         return NGX_OK;
     }
 
-    v->data = ngx_pnalloc(r->pool, NGX_INT64_LEN + 5);
+    v->data = ngx_pnalloc(s->connection->pool, NGX_INT64_LEN + 5);
     if (v->data == NULL) {
         GeoIPRecord_delete(gr);
         return NGX_ERROR;
@@ -564,19 +526,19 @@ ngx_http_geoip_city_float_variable(ngx_h
 
 
 static ngx_int_t
-ngx_http_geoip_city_int_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data)
+ngx_stream_geoip_city_int_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
 {
     int           val;
     GeoIPRecord  *gr;
 
-    gr = ngx_http_geoip_get_city_record(r);
+    gr = ngx_stream_geoip_get_city_record(s);
     if (gr == NULL) {
         v->not_found = 1;
         return NGX_OK;
     }
 
-    v->data = ngx_pnalloc(r->pool, NGX_INT64_LEN);
+    v->data = ngx_pnalloc(s->connection->pool, NGX_INT64_LEN);
     if (v->data == NULL) {
         GeoIPRecord_delete(gr);
         return NGX_ERROR;
@@ -596,21 +558,21 @@ ngx_http_geoip_city_int_variable(ngx_htt
 
 
 static GeoIPRecord *
-ngx_http_geoip_get_city_record(ngx_http_request_t *r)
+ngx_stream_geoip_get_city_record(ngx_stream_session_t *s)
 {
-    ngx_http_geoip_conf_t  *gcf;
+    ngx_stream_geoip_conf_t  *gcf;
 
-    gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
+    gcf = ngx_stream_get_module_main_conf(s, ngx_stream_geoip_module);
 
     if (gcf->city) {
 #if (NGX_HAVE_GEOIP_V6)
         return gcf->city_v6
                    ? GeoIP_record_by_ipnum_v6(gcf->city,
-                                              ngx_http_geoip_addr_v6(r, gcf))
+                                              ngx_stream_geoip_addr_v6(s, gcf))
                    : GeoIP_record_by_ipnum(gcf->city,
-                                           ngx_http_geoip_addr(r, gcf));
+                                           ngx_stream_geoip_addr(s, gcf));
 #else
-        return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf));
+        return GeoIP_record_by_ipnum(gcf->city, ngx_stream_geoip_addr(s, gcf));
 #endif
     }
 
@@ -619,12 +581,12 @@ ngx_http_geoip_get_city_record(ngx_http_
 
 
 static ngx_int_t
-ngx_http_geoip_add_variables(ngx_conf_t *cf)
+ngx_stream_geoip_add_variables(ngx_conf_t *cf)
 {
-    ngx_http_variable_t  *var, *v;
+    ngx_stream_variable_t  *var, *v;
 
-    for (v = ngx_http_geoip_vars; v->name.len; v++) {
-        var = ngx_http_add_variable(cf, &v->name, v->flags);
+    for (v = ngx_stream_geoip_vars; v->name.len; v++) {
+        var = ngx_stream_add_variable(cf, &v->name, v->flags);
         if (var == NULL) {
             return NGX_ERROR;
         }
@@ -638,24 +600,22 @@ ngx_http_geoip_add_variables(ngx_conf_t 
 
 
 static void *
-ngx_http_geoip_create_conf(ngx_conf_t *cf)
+ngx_stream_geoip_create_conf(ngx_conf_t *cf)
 {
-    ngx_pool_cleanup_t     *cln;
-    ngx_http_geoip_conf_t  *conf;
+    ngx_pool_cleanup_t       *cln;
+    ngx_stream_geoip_conf_t  *conf;
 
-    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip_conf_t));
+    conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_geoip_conf_t));
     if (conf == NULL) {
         return NULL;
     }
 
-    conf->proxy_recursive = NGX_CONF_UNSET;
-
     cln = ngx_pool_cleanup_add(cf->pool, 0);
     if (cln == NULL) {
         return NULL;
     }
 
-    cln->handler = ngx_http_geoip_cleanup;
+    cln->handler = ngx_stream_geoip_cleanup;
     cln->data = conf;
 
     return conf;
@@ -663,20 +623,9 @@ ngx_http_geoip_create_conf(ngx_conf_t *c
 
 
 static char *
-ngx_http_geoip_init_conf(ngx_conf_t *cf, void *conf)
+ngx_stream_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_geoip_conf_t  *gcf = conf;
-
-    ngx_conf_init_value(gcf->proxy_recursive, 0);
-
-    return NGX_CONF_OK;
-}
-
-
-static char *
-ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
-    ngx_http_geoip_conf_t  *gcf = conf;
+    ngx_stream_geoip_conf_t  *gcf = conf;
 
     ngx_str_t  *value;
 
@@ -729,9 +678,9 @@ ngx_http_geoip_country(ngx_conf_t *cf, n
 
 
 static char *
-ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+ngx_stream_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_geoip_conf_t  *gcf = conf;
+    ngx_stream_geoip_conf_t  *gcf = conf;
 
     ngx_str_t  *value;
 
@@ -790,9 +739,9 @@ ngx_http_geoip_org(ngx_conf_t *cf, ngx_c
 
 
 static char *
-ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+ngx_stream_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_geoip_conf_t  *gcf = conf;
+    ngx_stream_geoip_conf_t  *gcf = conf;
 
     ngx_str_t  *value;
 
@@ -846,70 +795,10 @@ ngx_http_geoip_city(ngx_conf_t *cf, ngx_
 }
 
 
-static char *
-ngx_http_geoip_proxy(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+static void
+ngx_stream_geoip_cleanup(void *data)
 {
-    ngx_http_geoip_conf_t  *gcf = conf;
-
-    ngx_str_t   *value;
-    ngx_cidr_t  cidr, *c;
-
-    value = cf->args->elts;
-
-    if (ngx_http_geoip_cidr_value(cf, &value[1], &cidr) != NGX_OK) {
-        return NGX_CONF_ERROR;
-    }
-
-    if (gcf->proxies == NULL) {
-        gcf->proxies = ngx_array_create(cf->pool, 4, sizeof(ngx_cidr_t));
-        if (gcf->proxies == NULL) {
-            return NGX_CONF_ERROR;
-        }
-    }
-
-    c = ngx_array_push(gcf->proxies);
-    if (c == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    *c = cidr;
-
-    return NGX_CONF_OK;
-}
-
-static ngx_int_t
-ngx_http_geoip_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)
-{
-    ngx_int_t  rc;
-
-    if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
-        cidr->family = AF_INET;
-        cidr->u.in.addr = 0xffffffff;
-        cidr->u.in.mask = 0xffffffff;
-
-        return NGX_OK;
-    }
-
-    rc = ngx_ptocidr(net, cidr);
-
-    if (rc == NGX_ERROR) {
-        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid network \"%V\"", net);
-        return NGX_ERROR;
-    }
-
-    if (rc == NGX_DONE) {
-        ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
-                           "low address bits of %V are meaningless", net);
-    }
-
-    return NGX_OK;
-}
-
-
-static void
-ngx_http_geoip_cleanup(void *data)
-{
-    ngx_http_geoip_conf_t  *gcf = data;
+    ngx_stream_geoip_conf_t  *gcf = data;
 
     if (gcf->country) {
         GeoIP_delete(gcf->country);