-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathngx_http_ip2location_module.c
146 lines (110 loc) · 3.62 KB
/
ngx_http_ip2location_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* COPYRIGHT (C) IP2LOCATION. ALL RIGHTS RESERVED.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "../ip2location-c-7.0.0/libIP2Location/IP2Location.h"
/* Directive handlers */
static char *ngx_http_ip2location_database(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
/* Variable handlers */
static ngx_int_t ngx_http_ip2location_country_code(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_ip2location_country_name(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
/* Directives */
static ngx_command_t ngx_http_ip2location_commands[] = {
{ ngx_string("ip2location_database"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_http_ip2location_database,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_ip2location_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
ngx_module_t ngx_http_ip2location_module = {
NGX_MODULE_V1,
&ngx_http_ip2location_module_ctx,
ngx_http_ip2location_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_ip2location_country_code(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data){
ngx_http_variable_value_t *vv = v;
IP2LocationRecord *record = IP2Location_get_country_short((IP2Location *)data, (char *)r->connection->addr_text.data);
vv->data = (u_char *)record->country_short;
if (vv->data == NULL) {
vv->valid = 0;
vv->not_found = 1;
} else {
vv->len = ngx_strlen( vv->data );
vv->valid = 1;
vv->no_cacheable = 0;
vv->not_found = 0;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http ip2location_country_code: %V %v", &r->connection->addr_text, v);
return NGX_OK;
}
static ngx_int_t
ngx_http_ip2location_country_name(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data){
ngx_http_variable_value_t *vv = v;
IP2LocationRecord *record = IP2Location_get_country_long((IP2Location *)data, (char *)r->connection->addr_text.data);
vv->data = (u_char *)record->country_long;
if (vv->data == NULL) {
vv->valid = 0;
vv->not_found = 1;
} else {
vv->len = ngx_strlen( vv->data );
vv->valid = 1;
vv->no_cacheable = 0;
vv->not_found = 0;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http ip2location_country_name: %V %v", &r->connection->addr_text, v);
return NGX_OK;
}
static char *
ngx_http_ip2location_database(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value, bin_file;
ngx_http_variable_t *country_code_var;
ngx_str_t ngx_country_code_var_name = ngx_string("ip2location_country_code");
ngx_http_variable_t *country_name_var;
ngx_str_t ngx_country_name_var_name = ngx_string("ip2location_country_name");
value = cf->args->elts;
bin_file = value[1];
IP2Location *IP2LocationObj = IP2Location_open((char *)bin_file.data);
IP2Location_open_mem(IP2LocationObj, IP2LOCATION_CACHE_MEMORY);
if(IP2LocationObj == NULL)
return NGX_CONF_ERROR;
country_code_var = ngx_http_add_variable(cf, &ngx_country_code_var_name, NGX_HTTP_VAR_CHANGEABLE );
if (country_code_var == NULL) {
return NGX_CONF_ERROR;
}
country_name_var = ngx_http_add_variable(cf, &ngx_country_name_var_name, NGX_HTTP_VAR_CHANGEABLE );
if (country_name_var == NULL) {
return NGX_CONF_ERROR;
}
country_code_var->get_handler = ngx_http_ip2location_country_code;
country_code_var->data = (uintptr_t)IP2LocationObj;
country_name_var->get_handler = ngx_http_ip2location_country_name;
country_name_var->data = (uintptr_t)IP2LocationObj;
return NGX_CONF_OK;
}