Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_geoip_module.c @ 8531:41f4bd4c51f1
Disabled control characters and space in header names.
Control characters (0x00-0x1f, 0x7f), space, and colon were never allowed in
header names. The only somewhat valid use is header continuation which nginx
never supported and which is explicitly obsolete by RFC 7230.
Previously, such headers were considered invalid and were ignored by default
(as per ignore_invalid_headers directive). With this change, such headers
are unconditionally rejected.
It is expected to make nginx more resilient to various attacks, in particular,
with ignore_invalid_headers switched off (which is inherently unsecure, though
nevertheless sometimes used in the wild).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 28 Jun 2021 18:01:18 +0300 |
parents | 2a288909abc6 |
children | ef6a3a99a81a |
rev | line source |
---|---|
2985 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
2985 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 #include <GeoIP.h> | |
13 #include <GeoIPCity.h> | |
14 | |
15 | |
5015 | 16 #define NGX_GEOIP_COUNTRY_CODE 0 |
17 #define NGX_GEOIP_COUNTRY_CODE3 1 | |
18 #define NGX_GEOIP_COUNTRY_NAME 2 | |
19 | |
20 | |
2985 | 21 typedef struct { |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
22 GeoIP *country; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
23 GeoIP *org; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
24 GeoIP *city; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
25 ngx_array_t *proxies; /* array of ngx_cidr_t */ |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
26 ngx_flag_t proxy_recursive; |
5015 | 27 #if (NGX_HAVE_GEOIP_V6) |
28 unsigned country_v6:1; | |
29 unsigned org_v6:1; | |
30 unsigned city_v6:1; | |
31 #endif | |
2985 | 32 } ngx_http_geoip_conf_t; |
33 | |
34 | |
35 typedef struct { | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
36 ngx_str_t *name; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
37 uintptr_t data; |
2985 | 38 } ngx_http_geoip_var_t; |
39 | |
40 | |
5015 | 41 typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, |
42 u_long addr); | |
43 | |
44 | |
45 ngx_http_geoip_variable_handler_pt ngx_http_geoip_country_functions[] = { | |
46 GeoIP_country_code_by_ipnum, | |
47 GeoIP_country_code3_by_ipnum, | |
48 GeoIP_country_name_by_ipnum, | |
49 }; | |
50 | |
51 | |
52 #if (NGX_HAVE_GEOIP_V6) | |
2985 | 53 |
5015 | 54 typedef const char *(*ngx_http_geoip_variable_handler_v6_pt)(GeoIP *, |
55 geoipv6_t addr); | |
56 | |
57 | |
58 ngx_http_geoip_variable_handler_v6_pt ngx_http_geoip_country_v6_functions[] = { | |
59 GeoIP_country_code_by_ipnum_v6, | |
60 GeoIP_country_code3_by_ipnum_v6, | |
61 GeoIP_country_name_by_ipnum_v6, | |
62 }; | |
63 | |
64 #endif | |
65 | |
66 | |
2985 | 67 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r, |
68 ngx_http_variable_value_t *v, uintptr_t data); | |
3915 | 69 static ngx_int_t ngx_http_geoip_org_variable(ngx_http_request_t *r, |
70 ngx_http_variable_value_t *v, uintptr_t data); | |
2985 | 71 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r, |
72 ngx_http_variable_value_t *v, uintptr_t data); | |
3737 | 73 static ngx_int_t ngx_http_geoip_region_name_variable(ngx_http_request_t *r, |
74 ngx_http_variable_value_t *v, uintptr_t data); | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
75 static ngx_int_t ngx_http_geoip_city_float_variable(ngx_http_request_t *r, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
76 ngx_http_variable_value_t *v, uintptr_t data); |
3736
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
77 static ngx_int_t ngx_http_geoip_city_int_variable(ngx_http_request_t *r, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
78 ngx_http_variable_value_t *v, uintptr_t data); |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
79 static GeoIPRecord *ngx_http_geoip_get_city_record(ngx_http_request_t *r); |
2985 | 80 |
81 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf); | |
82 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf); | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
83 static char *ngx_http_geoip_init_conf(ngx_conf_t *cf, void *conf); |
2985 | 84 static char *ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, |
85 void *conf); | |
3915 | 86 static char *ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd, |
87 void *conf); | |
2985 | 88 static char *ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, |
89 void *conf); | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
90 static char *ngx_http_geoip_proxy(ngx_conf_t *cf, ngx_command_t *cmd, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
91 void *conf); |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
92 static ngx_int_t ngx_http_geoip_cidr_value(ngx_conf_t *cf, ngx_str_t *net, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
93 ngx_cidr_t *cidr); |
2985 | 94 static void ngx_http_geoip_cleanup(void *data); |
95 | |
96 | |
97 static ngx_command_t ngx_http_geoip_commands[] = { | |
98 | |
99 { ngx_string("geoip_country"), | |
3859
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
100 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12, |
2985 | 101 ngx_http_geoip_country, |
102 NGX_HTTP_MAIN_CONF_OFFSET, | |
103 0, | |
104 NULL }, | |
105 | |
3915 | 106 { ngx_string("geoip_org"), |
107 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12, | |
108 ngx_http_geoip_org, | |
109 NGX_HTTP_MAIN_CONF_OFFSET, | |
110 0, | |
111 NULL }, | |
112 | |
2985 | 113 { ngx_string("geoip_city"), |
3859
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
114 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12, |
2985 | 115 ngx_http_geoip_city, |
116 NGX_HTTP_MAIN_CONF_OFFSET, | |
117 0, | |
118 NULL }, | |
119 | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
120 { ngx_string("geoip_proxy"), |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
121 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
122 ngx_http_geoip_proxy, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
123 NGX_HTTP_MAIN_CONF_OFFSET, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
124 0, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
125 NULL }, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
126 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
127 { ngx_string("geoip_proxy_recursive"), |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
128 NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
129 ngx_conf_set_flag_slot, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
130 NGX_HTTP_MAIN_CONF_OFFSET, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
131 offsetof(ngx_http_geoip_conf_t, proxy_recursive), |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
132 NULL }, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
133 |
2985 | 134 ngx_null_command |
135 }; | |
136 | |
137 | |
138 static ngx_http_module_t ngx_http_geoip_module_ctx = { | |
139 ngx_http_geoip_add_variables, /* preconfiguration */ | |
140 NULL, /* postconfiguration */ | |
141 | |
142 ngx_http_geoip_create_conf, /* create main configuration */ | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
143 ngx_http_geoip_init_conf, /* init main configuration */ |
2985 | 144 |
145 NULL, /* create server configuration */ | |
146 NULL, /* merge server configuration */ | |
147 | |
148 NULL, /* create location configuration */ | |
149 NULL /* merge location configuration */ | |
150 }; | |
151 | |
152 | |
153 ngx_module_t ngx_http_geoip_module = { | |
154 NGX_MODULE_V1, | |
155 &ngx_http_geoip_module_ctx, /* module context */ | |
156 ngx_http_geoip_commands, /* module directives */ | |
157 NGX_HTTP_MODULE, /* module type */ | |
158 NULL, /* init master */ | |
159 NULL, /* init module */ | |
160 NULL, /* init process */ | |
161 NULL, /* init thread */ | |
162 NULL, /* exit thread */ | |
163 NULL, /* exit process */ | |
164 NULL, /* exit master */ | |
165 NGX_MODULE_V1_PADDING | |
166 }; | |
167 | |
168 | |
169 static ngx_http_variable_t ngx_http_geoip_vars[] = { | |
170 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
171 { ngx_string("geoip_country_code"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
172 ngx_http_geoip_country_variable, |
5015 | 173 NGX_GEOIP_COUNTRY_CODE, 0, 0 }, |
2985 | 174 |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
175 { ngx_string("geoip_country_code3"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
176 ngx_http_geoip_country_variable, |
5015 | 177 NGX_GEOIP_COUNTRY_CODE3, 0, 0 }, |
2985 | 178 |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
179 { ngx_string("geoip_country_name"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
180 ngx_http_geoip_country_variable, |
5015 | 181 NGX_GEOIP_COUNTRY_NAME, 0, 0 }, |
2985 | 182 |
3915 | 183 { ngx_string("geoip_org"), NULL, |
184 ngx_http_geoip_org_variable, | |
5015 | 185 0, 0, 0 }, |
3915 | 186 |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
187 { ngx_string("geoip_city_continent_code"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
188 ngx_http_geoip_city_variable, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
189 offsetof(GeoIPRecord, continent_code), 0, 0 }, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
190 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
191 { ngx_string("geoip_city_country_code"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
192 ngx_http_geoip_city_variable, |
2985 | 193 offsetof(GeoIPRecord, country_code), 0, 0 }, |
194 | |
195 { ngx_string("geoip_city_country_code3"), NULL, | |
196 ngx_http_geoip_city_variable, | |
197 offsetof(GeoIPRecord, country_code3), 0, 0 }, | |
198 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
199 { ngx_string("geoip_city_country_name"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
200 ngx_http_geoip_city_variable, |
2985 | 201 offsetof(GeoIPRecord, country_name), 0, 0 }, |
202 | |
203 { ngx_string("geoip_region"), NULL, | |
204 ngx_http_geoip_city_variable, | |
205 offsetof(GeoIPRecord, region), 0, 0 }, | |
206 | |
3737 | 207 { ngx_string("geoip_region_name"), NULL, |
208 ngx_http_geoip_region_name_variable, | |
209 0, 0, 0 }, | |
210 | |
2985 | 211 { ngx_string("geoip_city"), NULL, |
212 ngx_http_geoip_city_variable, | |
213 offsetof(GeoIPRecord, city), 0, 0 }, | |
214 | |
215 { ngx_string("geoip_postal_code"), NULL, | |
216 ngx_http_geoip_city_variable, | |
217 offsetof(GeoIPRecord, postal_code), 0, 0 }, | |
218 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
219 { ngx_string("geoip_latitude"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
220 ngx_http_geoip_city_float_variable, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
221 offsetof(GeoIPRecord, latitude), 0, 0 }, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
222 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
223 { ngx_string("geoip_longitude"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
224 ngx_http_geoip_city_float_variable, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
225 offsetof(GeoIPRecord, longitude), 0, 0 }, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
226 |
3736
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
227 { ngx_string("geoip_dma_code"), NULL, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
228 ngx_http_geoip_city_int_variable, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
229 offsetof(GeoIPRecord, dma_code), 0, 0 }, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
230 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
231 { ngx_string("geoip_area_code"), NULL, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
232 ngx_http_geoip_city_int_variable, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
233 offsetof(GeoIPRecord, area_code), 0, 0 }, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
234 |
7077
2a288909abc6
Variables: macros for null variables.
Ruslan Ermilov <ru@nginx.com>
parents:
5773
diff
changeset
|
235 ngx_http_null_variable |
2985 | 236 }; |
237 | |
238 | |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
239 static u_long |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
240 ngx_http_geoip_addr(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf) |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
241 { |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
242 ngx_addr_t addr; |
5084
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
243 ngx_array_t *xfwd; |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
244 struct sockaddr_in *sin; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
245 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
246 addr.sockaddr = r->connection->sockaddr; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
247 addr.socklen = r->connection->socklen; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
248 /* addr.name = r->connection->addr_text; */ |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
249 |
5084
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
250 xfwd = &r->headers_in.x_forwarded_for; |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
251 |
5084
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
252 if (xfwd->nelts > 0 && gcf->proxies != NULL) { |
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
253 (void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL, |
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
254 gcf->proxies, gcf->proxy_recursive); |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
255 } |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
256 |
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
257 #if (NGX_HAVE_INET6) |
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
258 |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
259 if (addr.sockaddr->sa_family == AF_INET6) { |
4828
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
260 u_char *p; |
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
261 in_addr_t inaddr; |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
262 struct in6_addr *inaddr6; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
263 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
264 inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr; |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
265 |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
266 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { |
4828
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
267 p = inaddr6->s6_addr; |
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
268 |
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
269 inaddr = p[12] << 24; |
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
270 inaddr += p[13] << 16; |
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
271 inaddr += p[14] << 8; |
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
272 inaddr += p[15]; |
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
273 |
f57154322e0e
Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents:
4649
diff
changeset
|
274 return inaddr; |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
275 } |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
276 } |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
277 |
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
278 #endif |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
279 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
280 if (addr.sockaddr->sa_family != AF_INET) { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
281 return INADDR_NONE; |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
282 } |
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
283 |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
284 sin = (struct sockaddr_in *) addr.sockaddr; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
285 return ntohl(sin->sin_addr.s_addr); |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
286 } |
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
287 |
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
288 |
5015 | 289 #if (NGX_HAVE_GEOIP_V6) |
290 | |
291 static geoipv6_t | |
292 ngx_http_geoip_addr_v6(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf) | |
293 { | |
294 ngx_addr_t addr; | |
5084
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
295 ngx_array_t *xfwd; |
5015 | 296 in_addr_t addr4; |
297 struct in6_addr addr6; | |
298 struct sockaddr_in *sin; | |
299 struct sockaddr_in6 *sin6; | |
300 | |
301 addr.sockaddr = r->connection->sockaddr; | |
302 addr.socklen = r->connection->socklen; | |
303 /* addr.name = r->connection->addr_text; */ | |
304 | |
5084
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
305 xfwd = &r->headers_in.x_forwarded_for; |
5015 | 306 |
5084
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
307 if (xfwd->nelts > 0 && gcf->proxies != NULL) { |
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
308 (void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL, |
f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
Ruslan Ermilov <ru@nginx.com>
parents:
5029
diff
changeset
|
309 gcf->proxies, gcf->proxy_recursive); |
5015 | 310 } |
311 | |
312 switch (addr.sockaddr->sa_family) { | |
313 | |
314 case AF_INET: | |
315 /* Produce IPv4-mapped IPv6 address. */ | |
316 sin = (struct sockaddr_in *) addr.sockaddr; | |
317 addr4 = ntohl(sin->sin_addr.s_addr); | |
318 | |
319 ngx_memzero(&addr6, sizeof(struct in6_addr)); | |
320 addr6.s6_addr[10] = 0xff; | |
321 addr6.s6_addr[11] = 0xff; | |
322 addr6.s6_addr[12] = addr4 >> 24; | |
323 addr6.s6_addr[13] = addr4 >> 16; | |
324 addr6.s6_addr[14] = addr4 >> 8; | |
325 addr6.s6_addr[15] = addr4; | |
326 return addr6; | |
327 | |
328 case AF_INET6: | |
329 sin6 = (struct sockaddr_in6 *) addr.sockaddr; | |
330 return sin6->sin6_addr; | |
331 | |
332 default: | |
333 return in6addr_any; | |
334 } | |
335 } | |
336 | |
337 #endif | |
338 | |
339 | |
2985 | 340 static ngx_int_t |
341 ngx_http_geoip_country_variable(ngx_http_request_t *r, | |
342 ngx_http_variable_value_t *v, uintptr_t data) | |
343 { | |
5015 | 344 ngx_http_geoip_variable_handler_pt handler = |
345 ngx_http_geoip_country_functions[data]; | |
346 #if (NGX_HAVE_GEOIP_V6) | |
347 ngx_http_geoip_variable_handler_v6_pt handler_v6 = | |
348 ngx_http_geoip_country_v6_functions[data]; | |
349 #endif | |
2985 | 350 |
351 const char *val; | |
352 ngx_http_geoip_conf_t *gcf; | |
353 | |
354 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); | |
355 | |
356 if (gcf->country == NULL) { | |
357 goto not_found; | |
358 } | |
359 | |
5015 | 360 #if (NGX_HAVE_GEOIP_V6) |
361 val = gcf->country_v6 | |
362 ? handler_v6(gcf->country, ngx_http_geoip_addr_v6(r, gcf)) | |
363 : handler(gcf->country, ngx_http_geoip_addr(r, gcf)); | |
364 #else | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
365 val = handler(gcf->country, ngx_http_geoip_addr(r, gcf)); |
5015 | 366 #endif |
2985 | 367 |
368 if (val == NULL) { | |
369 goto not_found; | |
370 } | |
371 | |
372 v->len = ngx_strlen(val); | |
373 v->valid = 1; | |
374 v->no_cacheable = 0; | |
375 v->not_found = 0; | |
376 v->data = (u_char *) val; | |
377 | |
378 return NGX_OK; | |
379 | |
380 not_found: | |
381 | |
382 v->not_found = 1; | |
383 | |
384 return NGX_OK; | |
385 } | |
386 | |
387 | |
388 static ngx_int_t | |
3915 | 389 ngx_http_geoip_org_variable(ngx_http_request_t *r, |
390 ngx_http_variable_value_t *v, uintptr_t data) | |
391 { | |
4647
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
392 size_t len; |
4649
95d93f7e6fa2
geoip: got rid of ugly casts when calling ngx_free().
Ruslan Ermilov <ru@nginx.com>
parents:
4648
diff
changeset
|
393 char *val; |
3915 | 394 ngx_http_geoip_conf_t *gcf; |
395 | |
396 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); | |
397 | |
398 if (gcf->org == NULL) { | |
399 goto not_found; | |
400 } | |
401 | |
5015 | 402 #if (NGX_HAVE_GEOIP_V6) |
403 val = gcf->org_v6 | |
404 ? GeoIP_name_by_ipnum_v6(gcf->org, | |
405 ngx_http_geoip_addr_v6(r, gcf)) | |
406 : GeoIP_name_by_ipnum(gcf->org, | |
407 ngx_http_geoip_addr(r, gcf)); | |
408 #else | |
409 val = GeoIP_name_by_ipnum(gcf->org, ngx_http_geoip_addr(r, gcf)); | |
410 #endif | |
3915 | 411 |
412 if (val == NULL) { | |
413 goto not_found; | |
414 } | |
415 | |
4647
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
416 len = ngx_strlen(val); |
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
417 v->data = ngx_pnalloc(r->pool, len); |
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
418 if (v->data == NULL) { |
4649
95d93f7e6fa2
geoip: got rid of ugly casts when calling ngx_free().
Ruslan Ermilov <ru@nginx.com>
parents:
4648
diff
changeset
|
419 ngx_free(val); |
4647
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
420 return NGX_ERROR; |
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
421 } |
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
422 |
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
423 ngx_memcpy(v->data, val, len); |
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
424 |
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
425 v->len = len; |
3915 | 426 v->valid = 1; |
427 v->no_cacheable = 0; | |
428 v->not_found = 0; | |
4647
a321eadcb16c
Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents:
4627
diff
changeset
|
429 |
4649
95d93f7e6fa2
geoip: got rid of ugly casts when calling ngx_free().
Ruslan Ermilov <ru@nginx.com>
parents:
4648
diff
changeset
|
430 ngx_free(val); |
3915 | 431 |
432 return NGX_OK; | |
433 | |
434 not_found: | |
435 | |
436 v->not_found = 1; | |
437 | |
438 return NGX_OK; | |
439 } | |
440 | |
441 | |
442 static ngx_int_t | |
2985 | 443 ngx_http_geoip_city_variable(ngx_http_request_t *r, |
444 ngx_http_variable_value_t *v, uintptr_t data) | |
445 { | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
446 char *val; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
447 size_t len; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
448 GeoIPRecord *gr; |
2985 | 449 |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
450 gr = ngx_http_geoip_get_city_record(r); |
2985 | 451 if (gr == NULL) { |
452 goto not_found; | |
453 } | |
454 | |
455 val = *(char **) ((char *) gr + data); | |
456 if (val == NULL) { | |
3031
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
457 goto no_value; |
2985 | 458 } |
459 | |
3031
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
460 len = ngx_strlen(val); |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
461 v->data = ngx_pnalloc(r->pool, len); |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
462 if (v->data == NULL) { |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
463 GeoIPRecord_delete(gr); |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
464 return NGX_ERROR; |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
465 } |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
466 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
467 ngx_memcpy(v->data, val, len); |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
468 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
469 v->len = len; |
2985 | 470 v->valid = 1; |
471 v->no_cacheable = 0; | |
472 v->not_found = 0; | |
3031
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
473 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
474 GeoIPRecord_delete(gr); |
2985 | 475 |
476 return NGX_OK; | |
477 | |
3031
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
478 no_value: |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
479 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
480 GeoIPRecord_delete(gr); |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
481 |
2985 | 482 not_found: |
483 | |
484 v->not_found = 1; | |
485 | |
486 return NGX_OK; | |
487 } | |
488 | |
489 | |
490 static ngx_int_t | |
3737 | 491 ngx_http_geoip_region_name_variable(ngx_http_request_t *r, |
492 ngx_http_variable_value_t *v, uintptr_t data) | |
493 { | |
494 size_t len; | |
495 const char *val; | |
496 GeoIPRecord *gr; | |
497 | |
498 gr = ngx_http_geoip_get_city_record(r); | |
499 if (gr == NULL) { | |
500 goto not_found; | |
501 } | |
502 | |
503 val = GeoIP_region_name_by_code(gr->country_code, gr->region); | |
3742
01691af60f94
we can free GeoIPRecord just after GeoIP_region_name_by_code(),
Igor Sysoev <igor@sysoev.ru>
parents:
3741
diff
changeset
|
504 |
01691af60f94
we can free GeoIPRecord just after GeoIP_region_name_by_code(),
Igor Sysoev <igor@sysoev.ru>
parents:
3741
diff
changeset
|
505 GeoIPRecord_delete(gr); |
01691af60f94
we can free GeoIPRecord just after GeoIP_region_name_by_code(),
Igor Sysoev <igor@sysoev.ru>
parents:
3741
diff
changeset
|
506 |
3741
0f9b2d285bfc
fix segfault, the bug has been introduced in r3738
Igor Sysoev <igor@sysoev.ru>
parents:
3737
diff
changeset
|
507 if (val == NULL) { |
3742
01691af60f94
we can free GeoIPRecord just after GeoIP_region_name_by_code(),
Igor Sysoev <igor@sysoev.ru>
parents:
3741
diff
changeset
|
508 goto not_found; |
3741
0f9b2d285bfc
fix segfault, the bug has been introduced in r3738
Igor Sysoev <igor@sysoev.ru>
parents:
3737
diff
changeset
|
509 } |
3737 | 510 |
511 len = ngx_strlen(val); | |
512 v->data = ngx_pnalloc(r->pool, len); | |
513 if (v->data == NULL) { | |
514 return NGX_ERROR; | |
515 } | |
516 | |
517 ngx_memcpy(v->data, val, len); | |
518 | |
519 v->len = len; | |
520 v->valid = 1; | |
521 v->no_cacheable = 0; | |
522 v->not_found = 0; | |
523 | |
524 return NGX_OK; | |
525 | |
526 not_found: | |
527 | |
528 v->not_found = 1; | |
529 | |
530 return NGX_OK; | |
531 } | |
532 | |
533 | |
534 static ngx_int_t | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
535 ngx_http_geoip_city_float_variable(ngx_http_request_t *r, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
536 ngx_http_variable_value_t *v, uintptr_t data) |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
537 { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
538 float val; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
539 GeoIPRecord *gr; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
540 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
541 gr = ngx_http_geoip_get_city_record(r); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
542 if (gr == NULL) { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
543 v->not_found = 1; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
544 return NGX_OK; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
545 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
546 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
547 v->data = ngx_pnalloc(r->pool, NGX_INT64_LEN + 5); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
548 if (v->data == NULL) { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
549 GeoIPRecord_delete(gr); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
550 return NGX_ERROR; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
551 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
552 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
553 val = *(float *) ((char *) gr + data); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
554 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
555 v->len = ngx_sprintf(v->data, "%.4f", val) - v->data; |
5773
494c2c2a0247
GeoIP: not all variable fields were initialized.
Yichun Zhang <agentzh@gmail.com>
parents:
5758
diff
changeset
|
556 v->valid = 1; |
494c2c2a0247
GeoIP: not all variable fields were initialized.
Yichun Zhang <agentzh@gmail.com>
parents:
5758
diff
changeset
|
557 v->no_cacheable = 0; |
494c2c2a0247
GeoIP: not all variable fields were initialized.
Yichun Zhang <agentzh@gmail.com>
parents:
5758
diff
changeset
|
558 v->not_found = 0; |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
559 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
560 GeoIPRecord_delete(gr); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
561 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
562 return NGX_OK; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
563 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
564 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
565 |
3736
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
566 static ngx_int_t |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
567 ngx_http_geoip_city_int_variable(ngx_http_request_t *r, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
568 ngx_http_variable_value_t *v, uintptr_t data) |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
569 { |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
570 int val; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
571 GeoIPRecord *gr; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
572 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
573 gr = ngx_http_geoip_get_city_record(r); |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
574 if (gr == NULL) { |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
575 v->not_found = 1; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
576 return NGX_OK; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
577 } |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
578 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
579 v->data = ngx_pnalloc(r->pool, NGX_INT64_LEN); |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
580 if (v->data == NULL) { |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
581 GeoIPRecord_delete(gr); |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
582 return NGX_ERROR; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
583 } |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
584 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
585 val = *(int *) ((char *) gr + data); |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
586 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
587 v->len = ngx_sprintf(v->data, "%d", val) - v->data; |
5773
494c2c2a0247
GeoIP: not all variable fields were initialized.
Yichun Zhang <agentzh@gmail.com>
parents:
5758
diff
changeset
|
588 v->valid = 1; |
494c2c2a0247
GeoIP: not all variable fields were initialized.
Yichun Zhang <agentzh@gmail.com>
parents:
5758
diff
changeset
|
589 v->no_cacheable = 0; |
494c2c2a0247
GeoIP: not all variable fields were initialized.
Yichun Zhang <agentzh@gmail.com>
parents:
5758
diff
changeset
|
590 v->not_found = 0; |
3736
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
591 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
592 GeoIPRecord_delete(gr); |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
593 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
594 return NGX_OK; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
595 } |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
596 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
597 |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
598 static GeoIPRecord * |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
599 ngx_http_geoip_get_city_record(ngx_http_request_t *r) |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
600 { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
601 ngx_http_geoip_conf_t *gcf; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
602 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
603 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
604 |
3919
0dceaa117e0d
support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents:
3915
diff
changeset
|
605 if (gcf->city) { |
5015 | 606 #if (NGX_HAVE_GEOIP_V6) |
607 return gcf->city_v6 | |
608 ? GeoIP_record_by_ipnum_v6(gcf->city, | |
609 ngx_http_geoip_addr_v6(r, gcf)) | |
610 : GeoIP_record_by_ipnum(gcf->city, | |
611 ngx_http_geoip_addr(r, gcf)); | |
612 #else | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
613 return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf)); |
5015 | 614 #endif |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
615 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
616 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
617 return NULL; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
618 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
619 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
620 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
621 static ngx_int_t |
2985 | 622 ngx_http_geoip_add_variables(ngx_conf_t *cf) |
623 { | |
624 ngx_http_variable_t *var, *v; | |
625 | |
626 for (v = ngx_http_geoip_vars; v->name.len; v++) { | |
627 var = ngx_http_add_variable(cf, &v->name, v->flags); | |
628 if (var == NULL) { | |
629 return NGX_ERROR; | |
630 } | |
631 | |
632 var->get_handler = v->get_handler; | |
633 var->data = v->data; | |
634 } | |
635 | |
636 return NGX_OK; | |
637 } | |
638 | |
639 | |
640 static void * | |
641 ngx_http_geoip_create_conf(ngx_conf_t *cf) | |
642 { | |
643 ngx_pool_cleanup_t *cln; | |
644 ngx_http_geoip_conf_t *conf; | |
645 | |
646 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip_conf_t)); | |
647 if (conf == NULL) { | |
648 return NULL; | |
649 } | |
650 | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
651 conf->proxy_recursive = NGX_CONF_UNSET; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
652 |
2985 | 653 cln = ngx_pool_cleanup_add(cf->pool, 0); |
654 if (cln == NULL) { | |
655 return NULL; | |
656 } | |
657 | |
658 cln->handler = ngx_http_geoip_cleanup; | |
659 cln->data = conf; | |
660 | |
661 return conf; | |
662 } | |
663 | |
664 | |
665 static char * | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
666 ngx_http_geoip_init_conf(ngx_conf_t *cf, void *conf) |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
667 { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
668 ngx_http_geoip_conf_t *gcf = conf; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
669 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
670 ngx_conf_init_value(gcf->proxy_recursive, 0); |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
671 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
672 return NGX_CONF_OK; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
673 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
674 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
675 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
676 static char * |
2985 | 677 ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
678 { | |
679 ngx_http_geoip_conf_t *gcf = conf; | |
680 | |
681 ngx_str_t *value; | |
682 | |
683 if (gcf->country) { | |
684 return "is duplicate"; | |
685 } | |
686 | |
687 value = cf->args->elts; | |
688 | |
689 gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE); | |
690 | |
691 if (gcf->country == NULL) { | |
692 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
3914 | 693 "GeoIP_open(\"%V\") failed", &value[1]); |
2985 | 694 |
695 return NGX_CONF_ERROR; | |
696 } | |
697 | |
3859
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
698 if (cf->args->nelts == 3) { |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
699 if (ngx_strcmp(value[2].data, "utf8") == 0) { |
5758
f3df4e420ae7
Style: remove whitespace between function name and parentheses.
Piotr Sikora <piotr@cloudflare.com>
parents:
5084
diff
changeset
|
700 GeoIP_set_charset(gcf->country, GEOIP_CHARSET_UTF8); |
3859
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
701 |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
702 } else { |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
703 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
704 "invalid parameter \"%V\"", &value[2]); |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
705 return NGX_CONF_ERROR; |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
706 } |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
707 } |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
708 |
2985 | 709 switch (gcf->country->databaseType) { |
710 | |
711 case GEOIP_COUNTRY_EDITION: | |
712 | |
713 return NGX_CONF_OK; | |
714 | |
5015 | 715 #if (NGX_HAVE_GEOIP_V6) |
716 case GEOIP_COUNTRY_EDITION_V6: | |
717 | |
718 gcf->country_v6 = 1; | |
719 return NGX_CONF_OK; | |
720 #endif | |
721 | |
2985 | 722 default: |
723 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
724 "invalid GeoIP database \"%V\" type:%d", | |
725 &value[1], gcf->country->databaseType); | |
726 return NGX_CONF_ERROR; | |
727 } | |
728 } | |
729 | |
730 | |
731 static char * | |
3915 | 732 ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
733 { | |
734 ngx_http_geoip_conf_t *gcf = conf; | |
735 | |
736 ngx_str_t *value; | |
737 | |
738 if (gcf->org) { | |
739 return "is duplicate"; | |
740 } | |
741 | |
742 value = cf->args->elts; | |
743 | |
744 gcf->org = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE); | |
745 | |
746 if (gcf->org == NULL) { | |
747 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
748 "GeoIP_open(\"%V\") failed", &value[1]); | |
749 | |
750 return NGX_CONF_ERROR; | |
751 } | |
752 | |
753 if (cf->args->nelts == 3) { | |
754 if (ngx_strcmp(value[2].data, "utf8") == 0) { | |
5758
f3df4e420ae7
Style: remove whitespace between function name and parentheses.
Piotr Sikora <piotr@cloudflare.com>
parents:
5084
diff
changeset
|
755 GeoIP_set_charset(gcf->org, GEOIP_CHARSET_UTF8); |
3915 | 756 |
757 } else { | |
758 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
759 "invalid parameter \"%V\"", &value[2]); | |
760 return NGX_CONF_ERROR; | |
761 } | |
762 } | |
763 | |
764 switch (gcf->org->databaseType) { | |
765 | |
766 case GEOIP_ISP_EDITION: | |
767 case GEOIP_ORG_EDITION: | |
768 case GEOIP_DOMAIN_EDITION: | |
769 case GEOIP_ASNUM_EDITION: | |
770 | |
771 return NGX_CONF_OK; | |
772 | |
5015 | 773 #if (NGX_HAVE_GEOIP_V6) |
774 case GEOIP_ISP_EDITION_V6: | |
775 case GEOIP_ORG_EDITION_V6: | |
776 case GEOIP_DOMAIN_EDITION_V6: | |
777 case GEOIP_ASNUM_EDITION_V6: | |
778 | |
779 gcf->org_v6 = 1; | |
780 return NGX_CONF_OK; | |
781 #endif | |
782 | |
3915 | 783 default: |
784 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
785 "invalid GeoIP database \"%V\" type:%d", | |
786 &value[1], gcf->org->databaseType); | |
787 return NGX_CONF_ERROR; | |
788 } | |
789 } | |
790 | |
791 | |
792 static char * | |
2985 | 793 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
794 { | |
795 ngx_http_geoip_conf_t *gcf = conf; | |
796 | |
797 ngx_str_t *value; | |
798 | |
799 if (gcf->city) { | |
800 return "is duplicate"; | |
801 } | |
802 | |
803 value = cf->args->elts; | |
804 | |
805 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE); | |
806 | |
807 if (gcf->city == NULL) { | |
808 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
3914 | 809 "GeoIP_open(\"%V\") failed", &value[1]); |
2985 | 810 |
811 return NGX_CONF_ERROR; | |
812 } | |
813 | |
3859
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
814 if (cf->args->nelts == 3) { |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
815 if (ngx_strcmp(value[2].data, "utf8") == 0) { |
5758
f3df4e420ae7
Style: remove whitespace between function name and parentheses.
Piotr Sikora <piotr@cloudflare.com>
parents:
5084
diff
changeset
|
816 GeoIP_set_charset(gcf->city, GEOIP_CHARSET_UTF8); |
3859
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
817 |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
818 } else { |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
819 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
820 "invalid parameter \"%V\"", &value[2]); |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
821 return NGX_CONF_ERROR; |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
822 } |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
823 } |
30d4d6187316
utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents:
3742
diff
changeset
|
824 |
2985 | 825 switch (gcf->city->databaseType) { |
826 | |
827 case GEOIP_CITY_EDITION_REV0: | |
828 case GEOIP_CITY_EDITION_REV1: | |
829 | |
830 return NGX_CONF_OK; | |
831 | |
5015 | 832 #if (NGX_HAVE_GEOIP_V6) |
833 case GEOIP_CITY_EDITION_REV0_V6: | |
834 case GEOIP_CITY_EDITION_REV1_V6: | |
835 | |
836 gcf->city_v6 = 1; | |
837 return NGX_CONF_OK; | |
838 #endif | |
839 | |
2985 | 840 default: |
841 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
842 "invalid GeoIP City database \"%V\" type:%d", | |
843 &value[1], gcf->city->databaseType); | |
844 return NGX_CONF_ERROR; | |
845 } | |
846 } | |
847 | |
848 | |
4627
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
849 static char * |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
850 ngx_http_geoip_proxy(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
851 { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
852 ngx_http_geoip_conf_t *gcf = conf; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
853 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
854 ngx_str_t *value; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
855 ngx_cidr_t cidr, *c; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
856 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
857 value = cf->args->elts; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
858 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
859 if (ngx_http_geoip_cidr_value(cf, &value[1], &cidr) != NGX_OK) { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
860 return NGX_CONF_ERROR; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
861 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
862 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
863 if (gcf->proxies == NULL) { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
864 gcf->proxies = ngx_array_create(cf->pool, 4, sizeof(ngx_cidr_t)); |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
865 if (gcf->proxies == NULL) { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
866 return NGX_CONF_ERROR; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
867 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
868 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
869 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
870 c = ngx_array_push(gcf->proxies); |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
871 if (c == NULL) { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
872 return NGX_CONF_ERROR; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
873 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
874 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
875 *c = cidr; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
876 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
877 return NGX_CONF_OK; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
878 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
879 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
880 static ngx_int_t |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
881 ngx_http_geoip_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr) |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
882 { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
883 ngx_int_t rc; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
884 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
885 if (ngx_strcmp(net->data, "255.255.255.255") == 0) { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
886 cidr->family = AF_INET; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
887 cidr->u.in.addr = 0xffffffff; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
888 cidr->u.in.mask = 0xffffffff; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
889 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
890 return NGX_OK; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
891 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
892 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
893 rc = ngx_ptocidr(net, cidr); |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
894 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
895 if (rc == NGX_ERROR) { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
896 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid network \"%V\"", net); |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
897 return NGX_ERROR; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
898 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
899 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
900 if (rc == NGX_DONE) { |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
901 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
902 "low address bits of %V are meaningless", net); |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
903 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
904 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
905 return NGX_OK; |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
906 } |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
907 |
3152e4c371d7
geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
908 |
2985 | 909 static void |
910 ngx_http_geoip_cleanup(void *data) | |
911 { | |
912 ngx_http_geoip_conf_t *gcf = data; | |
913 | |
914 if (gcf->country) { | |
915 GeoIP_delete(gcf->country); | |
916 } | |
917 | |
3915 | 918 if (gcf->org) { |
919 GeoIP_delete(gcf->org); | |
920 } | |
921 | |
2985 | 922 if (gcf->city) { |
923 GeoIP_delete(gcf->city); | |
924 } | |
925 } |