2014-01-31 00:00:34 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) Lee Valentine <lee@leev.net>
|
|
|
|
*
|
|
|
|
* Based on nginx's 'ngx_http_geoip_module.c' by Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
#include <maxminddb.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2016-06-17 19:52:58 +02:00
|
|
|
MMDB_s mmdb;
|
|
|
|
MMDB_lookup_result_s result;
|
2014-01-31 00:00:34 +01:00
|
|
|
#if (NGX_HAVE_INET6)
|
2016-06-17 19:52:58 +02:00
|
|
|
uint8_t address[16];
|
2014-01-31 00:00:34 +01:00
|
|
|
#else
|
2016-06-17 19:52:58 +02:00
|
|
|
unsigned long address;
|
2014-01-31 00:00:34 +01:00
|
|
|
#endif
|
2014-04-26 12:26:37 +02:00
|
|
|
} ngx_http_geoip2_db_t;
|
2014-01-31 00:00:34 +01:00
|
|
|
|
2014-04-26 12:26:37 +02:00
|
|
|
typedef struct {
|
2016-06-17 19:52:58 +02:00
|
|
|
ngx_array_t *databases;
|
|
|
|
ngx_array_t *proxies;
|
|
|
|
ngx_flag_t proxy_recursive;
|
2014-04-26 12:26:37 +02:00
|
|
|
} ngx_http_geoip2_conf_t;
|
2014-01-31 00:00:34 +01:00
|
|
|
|
|
|
|
typedef struct {
|
2016-06-17 19:52:58 +02:00
|
|
|
ngx_http_geoip2_db_t *database;
|
|
|
|
const char **lookup;
|
|
|
|
ngx_str_t default_value;
|
|
|
|
ngx_http_complex_value_t source;
|
2014-01-31 00:00:34 +01:00
|
|
|
} ngx_http_geoip2_ctx_t;
|
|
|
|
|
2018-06-26 02:14:54 +02:00
|
|
|
typedef struct {
|
|
|
|
ngx_http_geoip2_db_t *database;
|
|
|
|
ngx_str_t metavalue;
|
|
|
|
} ngx_http_geoip2_metadata_t;
|
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
|
|
|
|
static ngx_int_t ngx_http_geoip2_variable(ngx_http_request_t *r,
|
|
|
|
ngx_http_variable_value_t *v, uintptr_t data);
|
2018-06-26 02:14:54 +02:00
|
|
|
static ngx_int_t ngx_http_geoip2_metadata(ngx_http_request_t *r,
|
|
|
|
ngx_http_variable_value_t *v, uintptr_t data);
|
2014-01-31 00:00:34 +01:00
|
|
|
static void *ngx_http_geoip2_create_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_http_geoip2_init_conf(ngx_conf_t *cf, void *conf);
|
2014-04-26 12:26:37 +02:00
|
|
|
static char *ngx_http_geoip2(ngx_conf_t *cf, ngx_command_t *cmd,
|
2014-01-31 00:00:34 +01:00
|
|
|
void *conf);
|
2014-04-26 12:26:37 +02:00
|
|
|
static char *ngx_http_geoip2_add_variable(ngx_conf_t *cf, ngx_command_t *dummy,
|
2014-01-31 00:00:34 +01:00
|
|
|
void *conf);
|
2018-06-26 02:14:54 +02:00
|
|
|
static char *ngx_http_geoip2_add_variable_geodata(ngx_conf_t *cf,
|
|
|
|
ngx_http_geoip2_db_t *database);
|
|
|
|
static char *ngx_http_geoip2_add_variable_metadata(ngx_conf_t *cf,
|
|
|
|
ngx_http_geoip2_db_t *database);
|
2014-01-31 00:00:34 +01:00
|
|
|
static char *ngx_http_geoip2_proxy(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
|
|
|
static ngx_int_t ngx_http_geoip2_cidr_value(ngx_conf_t *cf, ngx_str_t *net,
|
|
|
|
ngx_cidr_t *cidr);
|
|
|
|
static void ngx_http_geoip2_cleanup(void *data);
|
|
|
|
|
|
|
|
|
2016-06-17 19:32:55 +02:00
|
|
|
#define FORMAT(fmt, ...) do { \
|
|
|
|
p = ngx_palloc(r->pool, NGX_OFF_T_LEN); \
|
|
|
|
if (p == NULL) { \
|
|
|
|
return NGX_ERROR; \
|
|
|
|
} \
|
|
|
|
v->len = ngx_sprintf(p, fmt, __VA_ARGS__) - p; \
|
|
|
|
v->data = p; \
|
|
|
|
} while (0)
|
2014-12-06 22:42:20 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
static ngx_command_t ngx_http_geoip2_commands[] = {
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-04-26 12:26:37 +02:00
|
|
|
{ ngx_string("geoip2"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
|
|
|
|
ngx_http_geoip2,
|
2014-01-31 00:00:34 +01:00
|
|
|
NGX_HTTP_MAIN_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
{ ngx_string("geoip2_proxy"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_http_geoip2_proxy,
|
|
|
|
NGX_HTTP_MAIN_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
{ ngx_string("geoip2_proxy_recursive"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
|
|
|
|
ngx_conf_set_flag_slot,
|
|
|
|
NGX_HTTP_MAIN_CONF_OFFSET,
|
|
|
|
offsetof(ngx_http_geoip2_conf_t, proxy_recursive),
|
|
|
|
NULL },
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_module_t ngx_http_geoip2_module_ctx = {
|
|
|
|
NULL, /* preconfiguration */
|
|
|
|
NULL, /* preconfiguration */
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
ngx_http_geoip2_create_conf, /* create main configuration */
|
|
|
|
ngx_http_geoip2_init_conf, /* init main configuration */
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
NULL, /* create location configuration */
|
|
|
|
NULL /* merge location configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_geoip2_module = {
|
|
|
|
NGX_MODULE_V1,
|
|
|
|
&ngx_http_geoip2_module_ctx, /* module context */
|
|
|
|
ngx_http_geoip2_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 ngx_int_t
|
|
|
|
ngx_http_geoip2_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
|
|
uintptr_t data)
|
|
|
|
{
|
|
|
|
ngx_http_geoip2_ctx_t *geoip2 = (ngx_http_geoip2_ctx_t *) data;
|
2014-04-26 12:26:37 +02:00
|
|
|
ngx_http_geoip2_db_t *database = geoip2->database;
|
2014-01-31 00:00:34 +01:00
|
|
|
int mmdb_error;
|
|
|
|
MMDB_entry_data_s entry_data;
|
|
|
|
ngx_http_geoip2_conf_t *gcf;
|
|
|
|
ngx_addr_t addr;
|
|
|
|
ngx_array_t *xfwd;
|
2014-04-23 14:22:02 +02:00
|
|
|
u_char *p;
|
2016-06-17 19:52:58 +02:00
|
|
|
ngx_str_t val;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
uint8_t address[16], *addressp = address;
|
|
|
|
#else
|
|
|
|
unsigned long address;
|
|
|
|
#endif
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2016-06-17 19:52:58 +02:00
|
|
|
if (geoip2->source.value.len > 0) {
|
|
|
|
if (ngx_http_complex_value(r, &geoip2->source, &val) != NGX_OK) {
|
|
|
|
goto not_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_parse_addr(r->pool, &addr, val.data, val.len) != NGX_OK) {
|
|
|
|
goto not_found;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip2_module);
|
|
|
|
addr.sockaddr = r->connection->sockaddr;
|
|
|
|
addr.socklen = r->connection->socklen;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2016-06-17 19:52:58 +02:00
|
|
|
xfwd = &r->headers_in.x_forwarded_for;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2016-06-17 19:52:58 +02:00
|
|
|
if (xfwd->nelts > 0 && gcf->proxies != NULL) {
|
|
|
|
(void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL,
|
|
|
|
gcf->proxies, gcf->proxy_recursive);
|
|
|
|
}
|
2014-01-31 00:00:34 +01:00
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
switch (addr.sockaddr->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
ngx_memset(addressp, 0, 12);
|
|
|
|
ngx_memcpy(addressp + 12, &((struct sockaddr_in *)
|
|
|
|
addr.sockaddr)->sin_addr.s_addr, 4);
|
|
|
|
break;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
case AF_INET6:
|
|
|
|
ngx_memcpy(addressp, &((struct sockaddr_in6 *)
|
|
|
|
addr.sockaddr)->sin6_addr.s6_addr, 16);
|
|
|
|
#else
|
|
|
|
address = ((struct sockaddr_in *)addr.sockaddr)->sin_addr.s_addr;
|
|
|
|
#endif
|
|
|
|
break;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
default:
|
|
|
|
goto not_found;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
#if (NGX_HAVE_INET6)
|
2014-04-26 12:26:37 +02:00
|
|
|
if (ngx_memcmp(&address, &database->address, sizeof(address))
|
|
|
|
!= 0) {
|
2014-01-31 00:00:34 +01:00
|
|
|
#else
|
2014-04-26 12:26:37 +02:00
|
|
|
if (address != database->address) {
|
2014-01-31 00:00:34 +01:00
|
|
|
#endif
|
2014-04-26 12:26:37 +02:00
|
|
|
memcpy(&database->address, &address, sizeof(address));
|
|
|
|
database->result = MMDB_lookup_sockaddr(&database->mmdb,
|
|
|
|
addr.sockaddr, &mmdb_error);
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
if (mmdb_error != MMDB_SUCCESS) {
|
|
|
|
goto not_found;
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-04-26 12:26:37 +02:00
|
|
|
if (!database->result.found_entry
|
|
|
|
|| MMDB_aget_value(&database->result.entry, &entry_data,
|
|
|
|
geoip2->lookup) != MMDB_SUCCESS) {
|
2014-01-31 00:00:34 +01:00
|
|
|
goto not_found;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
if (!entry_data.has_data) {
|
|
|
|
goto not_found;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
switch (entry_data.type) {
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_BOOLEAN:
|
|
|
|
FORMAT("%d", entry_data.boolean);
|
2016-06-17 19:32:55 +02:00
|
|
|
break;
|
2014-01-31 00:00:34 +01:00
|
|
|
case MMDB_DATA_TYPE_UTF8_STRING:
|
|
|
|
v->data = (u_char *) entry_data.utf8_string;
|
|
|
|
v->len = entry_data.data_size;
|
|
|
|
break;
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_BYTES:
|
|
|
|
v->data = (u_char *) entry_data.bytes;
|
|
|
|
v->len = entry_data.data_size;
|
2014-03-17 23:40:28 +01:00
|
|
|
break;
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_FLOAT:
|
|
|
|
FORMAT("%.5f", entry_data.float_value);
|
2016-06-17 19:32:55 +02:00
|
|
|
break;
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_DOUBLE:
|
|
|
|
FORMAT("%.5f", entry_data.double_value);
|
2016-06-17 19:32:55 +02:00
|
|
|
break;
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_UINT16:
|
2014-12-09 01:18:21 +01:00
|
|
|
FORMAT("%uD", entry_data.uint16);
|
2016-06-17 19:32:55 +02:00
|
|
|
break;
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_UINT32:
|
2014-12-09 01:18:21 +01:00
|
|
|
FORMAT("%uD", entry_data.uint32);
|
2016-06-17 19:32:55 +02:00
|
|
|
break;
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_INT32:
|
2014-12-09 01:18:21 +01:00
|
|
|
FORMAT("%D", entry_data.int32);
|
2016-06-17 19:32:55 +02:00
|
|
|
break;
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_UINT64:
|
2014-12-09 01:18:21 +01:00
|
|
|
FORMAT("%uL", entry_data.uint64);
|
2016-06-17 19:32:55 +02:00
|
|
|
break;
|
2014-12-06 22:42:20 +01:00
|
|
|
case MMDB_DATA_TYPE_UINT128: ;
|
|
|
|
#if MMDB_UINT128_IS_BYTE_ARRAY
|
|
|
|
uint8_t *val = (uint8_t *)entry_data.uint128;
|
|
|
|
FORMAT( "0x%02x%02x%02x%02x%02x%02x%02x%02x"
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x",
|
|
|
|
val[0], val[1], val[2], val[3],
|
|
|
|
val[4], val[5], val[6], val[7],
|
|
|
|
val[8], val[9], val[10], val[11],
|
|
|
|
val[12], val[13], val[14], val[15]);
|
|
|
|
#else
|
|
|
|
mmdb_uint128_t val = entry_data.uint128;
|
2014-12-09 01:18:21 +01:00
|
|
|
FORMAT("0x%016uxL%016uxL",
|
2014-12-06 22:42:20 +01:00
|
|
|
(uint64_t) (val >> 64), (uint64_t) val);
|
|
|
|
#endif
|
2016-06-17 19:32:55 +02:00
|
|
|
break;
|
2014-01-31 00:00:34 +01:00
|
|
|
default:
|
|
|
|
goto not_found;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
v->valid = 1;
|
|
|
|
v->no_cacheable = 0;
|
|
|
|
v->not_found = 0;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
return NGX_OK;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
not_found:
|
2014-03-19 20:58:41 +01:00
|
|
|
if (geoip2->default_value.len > 0) {
|
|
|
|
v->data = geoip2->default_value.data;
|
|
|
|
v->len = geoip2->default_value.len;
|
|
|
|
|
|
|
|
v->valid = 1;
|
|
|
|
v->no_cacheable = 0;
|
|
|
|
v->not_found = 0;
|
2014-03-19 22:47:03 +01:00
|
|
|
} else {
|
|
|
|
v->not_found = 1;
|
2014-03-19 20:58:41 +01:00
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-26 02:14:54 +02:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_geoip2_metadata(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
|
|
uintptr_t data)
|
|
|
|
{
|
|
|
|
ngx_http_geoip2_metadata_t *metadata = (ngx_http_geoip2_metadata_t *) data;
|
|
|
|
ngx_http_geoip2_db_t *database = metadata->database;
|
|
|
|
u_char *p;
|
|
|
|
|
|
|
|
if (ngx_strncmp(metadata->metavalue.data, "build_epoch", 11) == 0) {
|
|
|
|
FORMAT("%uL", database->mmdb.metadata.build_epoch);
|
|
|
|
} else {
|
|
|
|
v->not_found = 1;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->valid = 1;
|
|
|
|
v->no_cacheable = 0;
|
|
|
|
v->not_found = 0;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
static void *
|
|
|
|
ngx_http_geoip2_create_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_pool_cleanup_t *cln;
|
|
|
|
ngx_http_geoip2_conf_t *conf;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip2_conf_t));
|
|
|
|
if (conf == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
conf->proxy_recursive = NGX_CONF_UNSET;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
|
|
if (cln == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
cln->handler = ngx_http_geoip2_cleanup;
|
|
|
|
cln->data = conf;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2014-04-26 12:26:37 +02:00
|
|
|
ngx_http_geoip2(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
ngx_http_geoip2_conf_t *gcf = conf;
|
|
|
|
ngx_str_t *value;
|
|
|
|
int status, nelts, i;
|
|
|
|
ngx_http_geoip2_db_t *database;
|
|
|
|
char *rv;
|
|
|
|
ngx_conf_t save;
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
2018-05-02 12:32:31 +02:00
|
|
|
if (value[1].data && value[1].data[0] != '/') {
|
|
|
|
if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-26 12:26:37 +02:00
|
|
|
if (gcf->databases == NULL) {
|
|
|
|
gcf->databases = ngx_array_create(cf->pool, 2,
|
|
|
|
sizeof(ngx_http_geoip2_db_t));
|
|
|
|
if (gcf->databases == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nelts = (int) gcf->databases->nelts;
|
|
|
|
database = gcf->databases->elts;
|
|
|
|
|
|
|
|
for (i = 0; i < nelts; i++) {
|
|
|
|
if (ngx_strcmp(value[1].data, database[i].mmdb.filename) == 0) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"Duplicate GeoIP2 mmdb - %V", &value[1]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
database = ngx_array_push(gcf->databases);
|
|
|
|
if (database == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = MMDB_open((char *) value[1].data, MMDB_MODE_MMAP, &database->mmdb);
|
|
|
|
|
|
|
|
if (status != MMDB_SUCCESS) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"MMDB_open(\"%V\") failed - %s", &value[1],
|
|
|
|
MMDB_strerror(status));
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
ngx_memset(&database->address, 0, sizeof(database->address));
|
|
|
|
#else
|
|
|
|
database->address = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
save = *cf;
|
|
|
|
cf->handler = ngx_http_geoip2_add_variable;
|
|
|
|
cf->handler_conf = (void *) database;
|
|
|
|
|
|
|
|
rv = ngx_conf_parse(cf, NULL);
|
|
|
|
*cf = save;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geoip2_add_variable(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
2014-01-31 00:00:34 +01:00
|
|
|
{
|
2018-06-26 02:14:54 +02:00
|
|
|
ngx_http_geoip2_db_t *database;
|
|
|
|
ngx_str_t *value;
|
|
|
|
int nelts;
|
2014-03-19 20:58:41 +01:00
|
|
|
|
2018-06-26 02:14:54 +02:00
|
|
|
value = cf->args->elts;
|
|
|
|
|
|
|
|
if (value[0].data[0] != '$') {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid variable name \"%V\"", &value[0]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[0].len--;
|
|
|
|
value[0].data++;
|
|
|
|
|
|
|
|
nelts = (int) cf->args->nelts;
|
|
|
|
database = (ngx_http_geoip2_db_t *) conf;
|
|
|
|
|
|
|
|
if (nelts > 0 && value[1].len == 8 && ngx_strncmp(value[1].data, "metadata", 8) == 0) {
|
|
|
|
return ngx_http_geoip2_add_variable_metadata(cf, database);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ngx_http_geoip2_add_variable_geodata(cf, database);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geoip2_add_variable_metadata(ngx_conf_t *cf, ngx_http_geoip2_db_t *database)
|
|
|
|
{
|
|
|
|
ngx_http_geoip2_metadata_t *metadata;
|
|
|
|
ngx_str_t *value, name;
|
|
|
|
ngx_http_variable_t *var;
|
|
|
|
|
|
|
|
metadata = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip2_metadata_t));
|
|
|
|
if (metadata == NULL) {
|
2014-01-31 00:00:34 +01:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
value = cf->args->elts;
|
2014-04-26 12:26:37 +02:00
|
|
|
name = value[0];
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2018-06-26 02:14:54 +02:00
|
|
|
metadata->database = database;
|
|
|
|
metadata->metavalue = value[2];
|
|
|
|
|
|
|
|
var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
|
|
|
|
if (var == NULL) {
|
2014-01-31 00:00:34 +01:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2018-06-26 02:14:54 +02:00
|
|
|
var->get_handler = ngx_http_geoip2_metadata;
|
|
|
|
var->data = (uintptr_t) metadata;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geoip2_add_variable_geodata(ngx_conf_t *cf, ngx_http_geoip2_db_t *database)
|
|
|
|
{
|
|
|
|
ngx_http_geoip2_ctx_t *geoip2;
|
|
|
|
ngx_http_compile_complex_value_t ccv;
|
|
|
|
ngx_str_t *value, name, source;
|
|
|
|
ngx_http_variable_t *var;
|
|
|
|
int i, nelts, idx;
|
|
|
|
|
|
|
|
geoip2 = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip2_ctx_t));
|
|
|
|
if (geoip2 == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
geoip2->database = database;
|
|
|
|
ngx_str_null(&source);
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
name = value[0];
|
|
|
|
|
2014-04-26 12:26:37 +02:00
|
|
|
nelts = (int) cf->args->nelts;
|
|
|
|
idx = 1;
|
|
|
|
|
2016-06-17 19:52:58 +02:00
|
|
|
if (nelts > idx) {
|
|
|
|
for (i = idx; i < nelts; i++) {
|
|
|
|
if (ngx_strnstr(value[idx].data, "=", value[idx].len) == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value[idx].len > 8 && ngx_strncmp(value[idx].data, "default=", 8) == 0) {
|
|
|
|
if (geoip2->default_value.len > 0) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"default has already been declared for \"$%V\"", &name);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
geoip2->default_value.len = value[idx].len - 8;
|
|
|
|
geoip2->default_value.data = value[idx].data + 8;
|
|
|
|
} else if (value[idx].len > 7 && ngx_strncmp(value[idx].data, "source=", 7) == 0) {
|
|
|
|
if (source.len > 0) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"source has already been declared for \"$%V\"", &name);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
source.len = value[idx].len - 7;
|
|
|
|
source.data = value[idx].data + 7;
|
|
|
|
|
|
|
|
if (source.data[0] != '$') {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid source variable name \"%V\"", &source);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
|
|
ccv.cf = cf;
|
|
|
|
ccv.value = &source;
|
|
|
|
ccv.complex_value = &geoip2->source;
|
|
|
|
|
|
|
|
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"unable to compile \"%V\" for \"$%V\"", &source, &name);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid setting \"%V\" for \"$%V\"", &value[idx], &name);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
2014-03-19 20:58:41 +01:00
|
|
|
}
|
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
|
|
|
|
if (var == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-04-26 12:26:37 +02:00
|
|
|
geoip2->lookup = ngx_pcalloc(cf->pool, sizeof(const char *) *
|
|
|
|
(cf->args->nelts - (idx - 1)));
|
|
|
|
|
|
|
|
if (geoip2->lookup == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = idx; i < nelts; i++) {
|
|
|
|
geoip2->lookup[i - idx] = (char *) value[i].data;
|
2014-01-31 00:00:34 +01:00
|
|
|
}
|
2014-04-26 12:26:37 +02:00
|
|
|
geoip2->lookup[i - idx] = NULL;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
var->get_handler = ngx_http_geoip2_variable;
|
|
|
|
var->data = (uintptr_t) geoip2;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geoip2_init_conf(ngx_conf_t *cf, void *conf)
|
|
|
|
{
|
|
|
|
ngx_http_geoip2_conf_t *gcf = conf;
|
|
|
|
ngx_conf_init_value(gcf->proxy_recursive, 0);
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geoip2_proxy(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
ngx_http_geoip2_conf_t *gcf = conf;
|
|
|
|
ngx_str_t *value;
|
|
|
|
ngx_cidr_t cidr, *c;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
value = cf->args->elts;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
if (ngx_http_geoip2_cidr_value(cf, &value[1], &cidr) != NGX_OK) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
if (gcf->proxies == NULL) {
|
|
|
|
gcf->proxies = ngx_array_create(cf->pool, 4, sizeof(ngx_cidr_t));
|
|
|
|
if (gcf->proxies == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
c = ngx_array_push(gcf->proxies);
|
|
|
|
if (c == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
*c = cidr;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-26 12:26:37 +02:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_geoip2_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)
|
|
|
|
{
|
|
|
|
ngx_int_t rc;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
|
|
|
|
cidr->family = AF_INET;
|
|
|
|
cidr->u.in.addr = 0xffffffff;
|
|
|
|
cidr->u.in.mask = 0xffffffff;
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
rc = ngx_ptocidr(net, cidr);
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid network \"%V\"", net);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
if (rc == NGX_DONE) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"low address bits of %V are meaningless", net);
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-01-31 00:00:34 +01:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_http_geoip2_cleanup(void *data)
|
|
|
|
{
|
|
|
|
ngx_http_geoip2_conf_t *gcf = data;
|
2014-04-26 12:26:37 +02:00
|
|
|
ngx_uint_t i;
|
|
|
|
ngx_http_geoip2_db_t *database;
|
|
|
|
|
|
|
|
if (gcf->databases != NULL) {
|
|
|
|
database = gcf->databases->elts;
|
|
|
|
|
|
|
|
for (i = 0; i < gcf->databases->nelts; i++) {
|
|
|
|
MMDB_close(&database[i].mmdb);
|
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
|
2014-04-26 12:26:37 +02:00
|
|
|
ngx_array_destroy(gcf->databases);
|
2014-01-31 00:00:34 +01:00
|
|
|
}
|
2014-03-11 01:20:21 +01:00
|
|
|
}
|