ngx_http_geoip2_module.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * Copyright (C) Lee Valentine <lee@leev.net>
  3. *
  4. * Based on nginx's 'ngx_http_geoip_module.c' by Igor Sysoev
  5. */
  6. #include <ngx_config.h>
  7. #include <ngx_core.h>
  8. #include <ngx_http.h>
  9. #include <maxminddb.h>
  10. typedef struct {
  11. MMDB_s mmdb;
  12. MMDB_lookup_result_s result;
  13. time_t last_check;
  14. time_t last_change;
  15. time_t check_interval;
  16. #if (NGX_HAVE_INET6)
  17. uint8_t address[16];
  18. #else
  19. unsigned long address;
  20. #endif
  21. ngx_queue_t queue;
  22. } ngx_http_geoip2_db_t;
  23. typedef struct {
  24. ngx_queue_t databases;
  25. ngx_array_t *proxies;
  26. ngx_flag_t proxy_recursive;
  27. } ngx_http_geoip2_conf_t;
  28. typedef struct {
  29. ngx_http_geoip2_db_t *database;
  30. const char **lookup;
  31. ngx_str_t default_value;
  32. ngx_http_complex_value_t source;
  33. } ngx_http_geoip2_ctx_t;
  34. typedef struct {
  35. ngx_http_geoip2_db_t *database;
  36. ngx_str_t metavalue;
  37. } ngx_http_geoip2_metadata_t;
  38. static ngx_int_t ngx_http_geoip2_variable(ngx_http_request_t *r,
  39. ngx_http_variable_value_t *v, uintptr_t data);
  40. static ngx_int_t ngx_http_geoip2_metadata(ngx_http_request_t *r,
  41. ngx_http_variable_value_t *v, uintptr_t data);
  42. static void *ngx_http_geoip2_create_conf(ngx_conf_t *cf);
  43. static char *ngx_http_geoip2_init_conf(ngx_conf_t *cf, void *conf);
  44. static char *ngx_http_geoip2(ngx_conf_t *cf, ngx_command_t *cmd,
  45. void *conf);
  46. static char *ngx_http_geoip2_parse_config(ngx_conf_t *cf, ngx_command_t *dummy,
  47. void *conf);
  48. static char *ngx_http_geoip2_add_variable(ngx_conf_t *cf, ngx_command_t *dummy,
  49. void *conf);
  50. static char *ngx_http_geoip2_add_variable_geodata(ngx_conf_t *cf,
  51. ngx_http_geoip2_db_t *database);
  52. static char *ngx_http_geoip2_add_variable_metadata(ngx_conf_t *cf,
  53. ngx_http_geoip2_db_t *database);
  54. static char *ngx_http_geoip2_proxy(ngx_conf_t *cf, ngx_command_t *cmd,
  55. void *conf);
  56. static ngx_int_t ngx_http_geoip2_cidr_value(ngx_conf_t *cf, ngx_str_t *net,
  57. ngx_cidr_t *cidr);
  58. static void ngx_http_geoip2_cleanup(void *data);
  59. static ngx_int_t ngx_http_geoip2_init(ngx_conf_t *cf);
  60. #define FORMAT(fmt, ...) do { \
  61. p = ngx_palloc(r->pool, NGX_OFF_T_LEN); \
  62. if (p == NULL) { \
  63. return NGX_ERROR; \
  64. } \
  65. v->len = ngx_sprintf(p, fmt, __VA_ARGS__) - p; \
  66. v->data = p; \
  67. } while (0)
  68. static ngx_command_t ngx_http_geoip2_commands[] = {
  69. { ngx_string("geoip2"),
  70. NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
  71. ngx_http_geoip2,
  72. NGX_HTTP_MAIN_CONF_OFFSET,
  73. 0,
  74. NULL },
  75. { ngx_string("geoip2_proxy"),
  76. NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
  77. ngx_http_geoip2_proxy,
  78. NGX_HTTP_MAIN_CONF_OFFSET,
  79. 0,
  80. NULL },
  81. { ngx_string("geoip2_proxy_recursive"),
  82. NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
  83. ngx_conf_set_flag_slot,
  84. NGX_HTTP_MAIN_CONF_OFFSET,
  85. offsetof(ngx_http_geoip2_conf_t, proxy_recursive),
  86. NULL },
  87. ngx_null_command
  88. };
  89. static ngx_http_module_t ngx_http_geoip2_module_ctx = {
  90. NULL, /* preconfiguration */
  91. ngx_http_geoip2_init, /* postconfiguration */
  92. ngx_http_geoip2_create_conf, /* create main configuration */
  93. ngx_http_geoip2_init_conf, /* init main configuration */
  94. NULL, /* create server configuration */
  95. NULL, /* merge server configuration */
  96. NULL, /* create location configuration */
  97. NULL /* merge location configuration */
  98. };
  99. ngx_module_t ngx_http_geoip2_module = {
  100. NGX_MODULE_V1,
  101. &ngx_http_geoip2_module_ctx, /* module context */
  102. ngx_http_geoip2_commands, /* module directives */
  103. NGX_HTTP_MODULE, /* module type */
  104. NULL, /* init master */
  105. NULL, /* init module */
  106. NULL, /* init process */
  107. NULL, /* init thread */
  108. NULL, /* exit thread */
  109. NULL, /* exit process */
  110. NULL, /* exit master */
  111. NGX_MODULE_V1_PADDING
  112. };
  113. static ngx_int_t
  114. ngx_http_geoip2_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
  115. uintptr_t data)
  116. {
  117. ngx_http_geoip2_ctx_t *geoip2 = (ngx_http_geoip2_ctx_t *) data;
  118. ngx_http_geoip2_db_t *database = geoip2->database;
  119. int mmdb_error;
  120. MMDB_entry_data_s entry_data;
  121. ngx_http_geoip2_conf_t *gcf;
  122. ngx_addr_t addr;
  123. #if defined(nginx_version) && nginx_version >= 1023000
  124. ngx_table_elt_t *xfwd;
  125. #else
  126. ngx_array_t *xfwd;
  127. #endif
  128. u_char *p;
  129. ngx_str_t val;
  130. #if (NGX_HAVE_INET6)
  131. uint8_t address[16], *addressp = address;
  132. #else
  133. unsigned long address;
  134. #endif
  135. if (geoip2->source.value.len > 0) {
  136. if (ngx_http_complex_value(r, &geoip2->source, &val) != NGX_OK) {
  137. goto not_found;
  138. }
  139. if (ngx_parse_addr(r->pool, &addr, val.data, val.len) != NGX_OK) {
  140. goto not_found;
  141. }
  142. } else {
  143. gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip2_module);
  144. addr.sockaddr = r->connection->sockaddr;
  145. addr.socklen = r->connection->socklen;
  146. #if defined(nginx_version) && nginx_version >= 1023000
  147. xfwd = r->headers_in.x_forwarded_for;
  148. if (xfwd != NULL && gcf->proxies != NULL) {
  149. #else
  150. xfwd = &r->headers_in.x_forwarded_for;
  151. if (xfwd->nelts > 0 && gcf->proxies != NULL) {
  152. #endif
  153. (void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL,
  154. gcf->proxies, gcf->proxy_recursive);
  155. }
  156. }
  157. switch (addr.sockaddr->sa_family) {
  158. case AF_INET:
  159. #if (NGX_HAVE_INET6)
  160. ngx_memset(addressp, 0, 12);
  161. ngx_memcpy(addressp + 12, &((struct sockaddr_in *)
  162. addr.sockaddr)->sin_addr.s_addr, 4);
  163. break;
  164. case AF_INET6:
  165. ngx_memcpy(addressp, &((struct sockaddr_in6 *)
  166. addr.sockaddr)->sin6_addr.s6_addr, 16);
  167. #else
  168. address = ((struct sockaddr_in *)addr.sockaddr)->sin_addr.s_addr;
  169. #endif
  170. break;
  171. default:
  172. goto not_found;
  173. }
  174. #if (NGX_HAVE_INET6)
  175. if (ngx_memcmp(&address, &database->address, sizeof(address))
  176. != 0) {
  177. #else
  178. if (address != database->address) {
  179. #endif
  180. memcpy(&database->address, &address, sizeof(address));
  181. database->result = MMDB_lookup_sockaddr(&database->mmdb,
  182. addr.sockaddr, &mmdb_error);
  183. if (mmdb_error != MMDB_SUCCESS) {
  184. goto not_found;
  185. }
  186. }
  187. if (!database->result.found_entry
  188. || MMDB_aget_value(&database->result.entry, &entry_data,
  189. geoip2->lookup) != MMDB_SUCCESS) {
  190. goto not_found;
  191. }
  192. if (!entry_data.has_data) {
  193. goto not_found;
  194. }
  195. switch (entry_data.type) {
  196. case MMDB_DATA_TYPE_BOOLEAN:
  197. FORMAT("%d", entry_data.boolean);
  198. break;
  199. case MMDB_DATA_TYPE_UTF8_STRING:
  200. v->len = entry_data.data_size;
  201. v->data = ngx_pnalloc(r->pool, v->len);
  202. if (v->data == NULL) {
  203. return NGX_ERROR;
  204. }
  205. ngx_memcpy(v->data, (u_char *) entry_data.utf8_string, v->len);
  206. break;
  207. case MMDB_DATA_TYPE_BYTES:
  208. v->len = entry_data.data_size;
  209. v->data = ngx_pnalloc(r->pool, v->len);
  210. if (v->data == NULL) {
  211. return NGX_ERROR;
  212. }
  213. ngx_memcpy(v->data, (u_char *) entry_data.bytes, v->len);
  214. break;
  215. case MMDB_DATA_TYPE_FLOAT:
  216. FORMAT("%.5f", entry_data.float_value);
  217. break;
  218. case MMDB_DATA_TYPE_DOUBLE:
  219. FORMAT("%.5f", entry_data.double_value);
  220. break;
  221. case MMDB_DATA_TYPE_UINT16:
  222. FORMAT("%uD", entry_data.uint16);
  223. break;
  224. case MMDB_DATA_TYPE_UINT32:
  225. FORMAT("%uD", entry_data.uint32);
  226. break;
  227. case MMDB_DATA_TYPE_INT32:
  228. FORMAT("%D", entry_data.int32);
  229. break;
  230. case MMDB_DATA_TYPE_UINT64:
  231. FORMAT("%uL", entry_data.uint64);
  232. break;
  233. case MMDB_DATA_TYPE_UINT128: ;
  234. #if MMDB_UINT128_IS_BYTE_ARRAY
  235. uint8_t *val = (uint8_t *)entry_data.uint128;
  236. FORMAT( "0x%02x%02x%02x%02x%02x%02x%02x%02x"
  237. "%02x%02x%02x%02x%02x%02x%02x%02x",
  238. val[0], val[1], val[2], val[3],
  239. val[4], val[5], val[6], val[7],
  240. val[8], val[9], val[10], val[11],
  241. val[12], val[13], val[14], val[15]);
  242. #else
  243. mmdb_uint128_t val = entry_data.uint128;
  244. FORMAT("0x%016uxL%016uxL",
  245. (uint64_t) (val >> 64), (uint64_t) val);
  246. #endif
  247. break;
  248. default:
  249. goto not_found;
  250. }
  251. v->valid = 1;
  252. v->no_cacheable = 0;
  253. v->not_found = 0;
  254. return NGX_OK;
  255. not_found:
  256. if (geoip2->default_value.len > 0) {
  257. v->data = geoip2->default_value.data;
  258. v->len = geoip2->default_value.len;
  259. v->valid = 1;
  260. v->no_cacheable = 0;
  261. v->not_found = 0;
  262. } else {
  263. v->not_found = 1;
  264. }
  265. return NGX_OK;
  266. }
  267. static ngx_int_t
  268. ngx_http_geoip2_metadata(ngx_http_request_t *r, ngx_http_variable_value_t *v,
  269. uintptr_t data)
  270. {
  271. ngx_http_geoip2_metadata_t *metadata = (ngx_http_geoip2_metadata_t *) data;
  272. ngx_http_geoip2_db_t *database = metadata->database;
  273. u_char *p;
  274. if (ngx_strncmp(metadata->metavalue.data, "build_epoch", 11) == 0) {
  275. FORMAT("%uL", database->mmdb.metadata.build_epoch);
  276. } else if (ngx_strncmp(metadata->metavalue.data, "last_check", 10) == 0) {
  277. FORMAT("%T", database->last_check);
  278. } else if (ngx_strncmp(metadata->metavalue.data, "last_change", 11) == 0) {
  279. FORMAT("%T", database->last_change);
  280. } else {
  281. v->not_found = 1;
  282. return NGX_OK;
  283. }
  284. v->valid = 1;
  285. v->no_cacheable = 0;
  286. v->not_found = 0;
  287. return NGX_OK;
  288. }
  289. static void *
  290. ngx_http_geoip2_create_conf(ngx_conf_t *cf)
  291. {
  292. ngx_pool_cleanup_t *cln;
  293. ngx_http_geoip2_conf_t *conf;
  294. conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip2_conf_t));
  295. if (conf == NULL) {
  296. return NULL;
  297. }
  298. conf->proxy_recursive = NGX_CONF_UNSET;
  299. cln = ngx_pool_cleanup_add(cf->pool, 0);
  300. if (cln == NULL) {
  301. return NULL;
  302. }
  303. ngx_queue_init(&conf->databases);
  304. cln->handler = ngx_http_geoip2_cleanup;
  305. cln->data = conf;
  306. return conf;
  307. }
  308. static char *
  309. ngx_http_geoip2(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  310. {
  311. ngx_http_geoip2_conf_t *gcf = conf;
  312. ngx_str_t *value;
  313. int status;
  314. ngx_http_geoip2_db_t *database;
  315. char *rv;
  316. ngx_conf_t save;
  317. ngx_queue_t *q;
  318. value = cf->args->elts;
  319. if (value[1].data && value[1].data[0] != '/') {
  320. if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
  321. return NGX_CONF_ERROR;
  322. }
  323. }
  324. if (!ngx_queue_empty(&gcf->databases)) {
  325. for (q = ngx_queue_head(&gcf->databases);
  326. q != ngx_queue_sentinel(&gcf->databases);
  327. q = ngx_queue_next(q))
  328. {
  329. database = ngx_queue_data(q, ngx_http_geoip2_db_t, queue);
  330. if (ngx_strcmp(value[1].data, database->mmdb.filename) == 0) {
  331. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  332. "Duplicate GeoIP2 mmdb - %V", &value[1]);
  333. return NGX_CONF_ERROR;
  334. }
  335. }
  336. }
  337. database = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip2_db_t));
  338. if (database == NULL) {
  339. return NGX_CONF_ERROR;
  340. }
  341. ngx_queue_insert_tail(&gcf->databases, &database->queue);
  342. database->last_check = database->last_change = ngx_time();
  343. status = MMDB_open((char *) value[1].data, MMDB_MODE_MMAP, &database->mmdb);
  344. if (status != MMDB_SUCCESS) {
  345. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  346. "MMDB_open(\"%V\") failed - %s", &value[1],
  347. MMDB_strerror(status));
  348. return NGX_CONF_ERROR;
  349. }
  350. save = *cf;
  351. cf->handler = ngx_http_geoip2_parse_config;
  352. cf->handler_conf = (void *) database;
  353. rv = ngx_conf_parse(cf, NULL);
  354. *cf = save;
  355. return rv;
  356. }
  357. static char *
  358. ngx_http_geoip2_parse_config(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
  359. {
  360. ngx_http_geoip2_db_t *database;
  361. ngx_str_t *value;
  362. time_t interval;
  363. value = cf->args->elts;
  364. if (value[0].data[0] == '$') {
  365. return ngx_http_geoip2_add_variable(cf, dummy, conf);
  366. }
  367. if (value[0].len == 11
  368. && ngx_strncmp(value[0].data, "auto_reload", 11) == 0) {
  369. if ((int) cf->args->nelts != 2) {
  370. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  371. "invalid number of arguments for auto_reload");
  372. return NGX_CONF_ERROR;
  373. }
  374. interval = ngx_parse_time(&value[1], true);
  375. if (interval == (time_t) NGX_ERROR) {
  376. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  377. "invalid interval for auto_reload \"%V\"",
  378. &value[1]);
  379. return NGX_CONF_ERROR;
  380. }
  381. database = (ngx_http_geoip2_db_t *) conf;
  382. database->check_interval = interval;
  383. return NGX_CONF_OK;
  384. }
  385. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  386. "invalid setting \"%V\"", &value[0]);
  387. return NGX_CONF_ERROR;
  388. }
  389. static char *
  390. ngx_http_geoip2_add_variable(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
  391. {
  392. ngx_http_geoip2_db_t *database;
  393. ngx_str_t *value;
  394. int nelts;
  395. value = cf->args->elts;
  396. if (value[0].data[0] != '$') {
  397. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  398. "invalid variable name \"%V\"", &value[0]);
  399. return NGX_CONF_ERROR;
  400. }
  401. value[0].len--;
  402. value[0].data++;
  403. nelts = (int) cf->args->nelts;
  404. database = (ngx_http_geoip2_db_t *) conf;
  405. if (nelts > 0 && value[1].len == 8 && ngx_strncmp(value[1].data, "metadata", 8) == 0) {
  406. return ngx_http_geoip2_add_variable_metadata(cf, database);
  407. }
  408. return ngx_http_geoip2_add_variable_geodata(cf, database);
  409. }
  410. static char *
  411. ngx_http_geoip2_add_variable_metadata(ngx_conf_t *cf, ngx_http_geoip2_db_t *database)
  412. {
  413. ngx_http_geoip2_metadata_t *metadata;
  414. ngx_str_t *value, name;
  415. ngx_http_variable_t *var;
  416. metadata = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip2_metadata_t));
  417. if (metadata == NULL) {
  418. return NGX_CONF_ERROR;
  419. }
  420. value = cf->args->elts;
  421. name = value[0];
  422. metadata->database = database;
  423. metadata->metavalue = value[2];
  424. var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
  425. if (var == NULL) {
  426. return NGX_CONF_ERROR;
  427. }
  428. var->get_handler = ngx_http_geoip2_metadata;
  429. var->data = (uintptr_t) metadata;
  430. return NGX_CONF_OK;
  431. }
  432. static char *
  433. ngx_http_geoip2_add_variable_geodata(ngx_conf_t *cf, ngx_http_geoip2_db_t *database)
  434. {
  435. ngx_http_geoip2_ctx_t *geoip2;
  436. ngx_http_compile_complex_value_t ccv;
  437. ngx_str_t *value, name, source;
  438. ngx_http_variable_t *var;
  439. int i, nelts, idx;
  440. geoip2 = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip2_ctx_t));
  441. if (geoip2 == NULL) {
  442. return NGX_CONF_ERROR;
  443. }
  444. geoip2->database = database;
  445. ngx_str_null(&source);
  446. value = cf->args->elts;
  447. name = value[0];
  448. nelts = (int) cf->args->nelts;
  449. idx = 1;
  450. if (nelts > idx) {
  451. for (i = idx; i < nelts; i++) {
  452. if (ngx_strnstr(value[idx].data, "=", value[idx].len) == NULL) {
  453. break;
  454. }
  455. if (value[idx].len > 8 && ngx_strncmp(value[idx].data, "default=", 8) == 0) {
  456. if (geoip2->default_value.len > 0) {
  457. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  458. "default has already been declared for \"$%V\"", &name);
  459. return NGX_CONF_ERROR;
  460. }
  461. geoip2->default_value.len = value[idx].len - 8;
  462. geoip2->default_value.data = value[idx].data + 8;
  463. } else if (value[idx].len > 7 && ngx_strncmp(value[idx].data, "source=", 7) == 0) {
  464. if (source.len > 0) {
  465. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  466. "source has already been declared for \"$%V\"", &name);
  467. return NGX_CONF_ERROR;
  468. }
  469. source.len = value[idx].len - 7;
  470. source.data = value[idx].data + 7;
  471. if (source.data[0] != '$') {
  472. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  473. "invalid source variable name \"%V\"", &source);
  474. return NGX_CONF_ERROR;
  475. }
  476. ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
  477. ccv.cf = cf;
  478. ccv.value = &source;
  479. ccv.complex_value = &geoip2->source;
  480. if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  481. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  482. "unable to compile \"%V\" for \"$%V\"", &source, &name);
  483. return NGX_CONF_ERROR;
  484. }
  485. } else {
  486. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  487. "invalid setting \"%V\" for \"$%V\"", &value[idx], &name);
  488. return NGX_CONF_ERROR;
  489. }
  490. idx++;
  491. }
  492. }
  493. var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
  494. if (var == NULL) {
  495. return NGX_CONF_ERROR;
  496. }
  497. geoip2->lookup = ngx_pcalloc(cf->pool, sizeof(const char *) *
  498. (cf->args->nelts - (idx - 1)));
  499. if (geoip2->lookup == NULL) {
  500. return NGX_CONF_ERROR;
  501. }
  502. for (i = idx; i < nelts; i++) {
  503. geoip2->lookup[i - idx] = (char *) value[i].data;
  504. }
  505. geoip2->lookup[i - idx] = NULL;
  506. var->get_handler = ngx_http_geoip2_variable;
  507. var->data = (uintptr_t) geoip2;
  508. return NGX_CONF_OK;
  509. }
  510. static char *
  511. ngx_http_geoip2_init_conf(ngx_conf_t *cf, void *conf)
  512. {
  513. ngx_http_geoip2_conf_t *gcf = conf;
  514. ngx_conf_init_value(gcf->proxy_recursive, 0);
  515. return NGX_CONF_OK;
  516. }
  517. static char *
  518. ngx_http_geoip2_proxy(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  519. {
  520. ngx_http_geoip2_conf_t *gcf = conf;
  521. ngx_str_t *value;
  522. ngx_cidr_t cidr, *c;
  523. value = cf->args->elts;
  524. if (ngx_http_geoip2_cidr_value(cf, &value[1], &cidr) != NGX_OK) {
  525. return NGX_CONF_ERROR;
  526. }
  527. if (gcf->proxies == NULL) {
  528. gcf->proxies = ngx_array_create(cf->pool, 4, sizeof(ngx_cidr_t));
  529. if (gcf->proxies == NULL) {
  530. return NGX_CONF_ERROR;
  531. }
  532. }
  533. c = ngx_array_push(gcf->proxies);
  534. if (c == NULL) {
  535. return NGX_CONF_ERROR;
  536. }
  537. *c = cidr;
  538. return NGX_CONF_OK;
  539. }
  540. static ngx_int_t
  541. ngx_http_geoip2_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)
  542. {
  543. ngx_int_t rc;
  544. if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
  545. cidr->family = AF_INET;
  546. cidr->u.in.addr = 0xffffffff;
  547. cidr->u.in.mask = 0xffffffff;
  548. return NGX_OK;
  549. }
  550. rc = ngx_ptocidr(net, cidr);
  551. if (rc == NGX_ERROR) {
  552. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  553. "invalid network \"%V\"", net);
  554. return NGX_ERROR;
  555. }
  556. if (rc == NGX_DONE) {
  557. ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
  558. "low address bits of %V are meaningless", net);
  559. }
  560. return NGX_OK;
  561. }
  562. static void
  563. ngx_http_geoip2_cleanup(void *data)
  564. {
  565. ngx_http_geoip2_conf_t *gcf = data;
  566. ngx_queue_t *q;
  567. ngx_http_geoip2_db_t *database;
  568. while (!ngx_queue_empty(&gcf->databases)) {
  569. q = ngx_queue_head(&gcf->databases);
  570. ngx_queue_remove(q);
  571. database = ngx_queue_data(q, ngx_http_geoip2_db_t, queue);
  572. MMDB_close(&database->mmdb);
  573. }
  574. }
  575. static ngx_int_t
  576. ngx_http_geoip2_log_handler(ngx_http_request_t *r)
  577. {
  578. int status;
  579. MMDB_s tmpdb;
  580. ngx_queue_t *q;
  581. ngx_file_info_t fi;
  582. ngx_http_geoip2_db_t *database;
  583. ngx_http_geoip2_conf_t *gcf;
  584. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  585. "geoip2 http log handler");
  586. gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip2_module);
  587. if (ngx_queue_empty(&gcf->databases)) {
  588. return NGX_OK;
  589. }
  590. for (q = ngx_queue_head(&gcf->databases);
  591. q != ngx_queue_sentinel(&gcf->databases);
  592. q = ngx_queue_next(q))
  593. {
  594. database = ngx_queue_data(q, ngx_http_geoip2_db_t, queue);
  595. if (database->check_interval == 0) {
  596. continue;
  597. }
  598. if ((database->last_check + database->check_interval)
  599. > ngx_time())
  600. {
  601. continue;
  602. }
  603. database->last_check = ngx_time();
  604. if (ngx_file_info(database->mmdb.filename, &fi) == NGX_FILE_ERROR) {
  605. ngx_log_error(NGX_LOG_EMERG, r->connection->log, ngx_errno,
  606. ngx_file_info_n " \"%s\" failed",
  607. database->mmdb.filename);
  608. continue;
  609. }
  610. if (ngx_file_mtime(&fi) <= database->last_change) {
  611. continue;
  612. }
  613. /* do the reload */
  614. ngx_memzero(&tmpdb, sizeof(MMDB_s));
  615. status = MMDB_open(database->mmdb.filename, MMDB_MODE_MMAP, &tmpdb);
  616. if (status != MMDB_SUCCESS) {
  617. ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  618. "MMDB_open(\"%s\") failed to reload - %s",
  619. database->mmdb.filename, MMDB_strerror(status));
  620. continue;
  621. }
  622. database->last_change = ngx_file_mtime(&fi);
  623. MMDB_close(&database->mmdb);
  624. database->mmdb = tmpdb;
  625. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  626. "Reload MMDB \"%s\"",
  627. database->mmdb.filename);
  628. }
  629. return NGX_OK;
  630. }
  631. static ngx_int_t
  632. ngx_http_geoip2_init(ngx_conf_t *cf)
  633. {
  634. ngx_http_handler_pt *h;
  635. ngx_http_core_main_conf_t *cmcf;
  636. cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  637. h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
  638. if (h == NULL) {
  639. return NGX_ERROR;
  640. }
  641. *h = ngx_http_geoip2_log_handler;
  642. return NGX_OK;
  643. }