-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_limit_req_rw_module.c
320 lines (258 loc) · 9 KB
/
ngx_http_limit_req_rw_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
TODO: copyright
*/
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_buf.h>
#include <ngx_conf_file.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http_request.h>
#include <ngx_string.h>
#include <ngx_times.h>
#include <ngx_http.h>
#include <ngx_log.h>
#include <stdio.h>
#include <time.h>
#include <msgpack.h>
#include "ngx_http_limit_req_module.h"
static ngx_int_t ngx_http_limit_req_read_handler(ngx_http_request_t *r);
static char *ngx_http_limit_req_rw_handler(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static void strip_zone_name_from_uri(ngx_str_t *uri, ngx_str_t *zone_name);
static ngx_int_t dump_rate_limit_zones(ngx_pool_t *pool, ngx_buf_t *b);
static ngx_int_t dump_req_zone(ngx_pool_t *pool, ngx_buf_t *b,
ngx_str_t *zone_name);
static ngx_int_t dump_req_limits(ngx_pool_t *pool, ngx_shm_zone_t *shm_zone,
ngx_buf_t *buf);
static ngx_command_t ngx_http_limit_req_rw_commands[] = {
{ngx_string("limit_req_rw_handler"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF |
NGX_CONF_NOARGS | NGX_CONF_TAKE1,
ngx_http_limit_req_rw_handler, 0, 0, NULL},
ngx_null_command};
static ngx_http_module_t ngx_http_limit_req_rw_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_limit_req_rw_module = {NGX_MODULE_V1,
&ngx_http_limit_req_rw_module_ctx,
ngx_http_limit_req_rw_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING};
static ngx_int_t ngx_http_limit_req_handler(ngx_http_request_t *r) {
if (r->method != NGX_HTTP_GET) {
return NGX_HTTP_NOT_ALLOWED;
}
return ngx_http_limit_req_read_handler(r);
}
static ngx_int_t ngx_http_limit_req_read_handler(ngx_http_request_t *r) {
ngx_str_t content_type, zone_name;
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
ngx_http_core_loc_conf_t *clcf;
if (r->method != NGX_HTTP_GET) {
return NGX_HTTP_NOT_ALLOWED;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
b = ngx_create_temp_buf(r->pool, 1024 * 10);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// When request location is /api
if (clcf->name.len == r->uri.len) {
rc = dump_rate_limit_zones(r->pool, b);
// When request location is /api/{zone_name}
} else {
strip_zone_name_from_uri(&r->uri, &zone_name);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "zone name: %.*s",
(int)zone_name.len, zone_name.data);
rc = dump_req_zone(r->pool, b, &zone_name);
}
if (rc != NGX_OK) {
return rc;
}
ngx_str_set(&content_type, "application/vnd.msgpack");
r->headers_out.content_type = content_type;
r->headers_out.content_length_n = b->last - b->pos;
r->headers_out.status = NGX_HTTP_OK; /* 200 OK */
b->last_buf = (r == r->main) ? 1 : 0; /* if subrequest 0 else 1 */
b->last_in_chain = 1;
out.buf = b;
out.next = NULL;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);
}
static char *ngx_http_limit_req_rw_handler(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf) {
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_limit_req_handler;
return NGX_CONF_OK;
}
static void strip_zone_name_from_uri(ngx_str_t *uri, ngx_str_t *zone_name) {
zone_name->data = (u_char *)ngx_strlchr(uri->data, uri->data + uri->len, '/');
zone_name->len = 0;
if (zone_name->data) {
zone_name->data =
(u_char *)(ngx_strlchr(zone_name->data + 1, uri->data + uri->len, '/') +
1);
zone_name->len = uri->len - (zone_name->data - uri->data);
}
}
static inline int msgpack_ngx_buf_write(void *data, const char *buf,
size_t len) {
ngx_buf_t *b = (ngx_buf_t *)data;
b->last = ngx_cpymem(b->last, buf, len);
return 0;
}
static ngx_int_t dump_rate_limit_zones(ngx_pool_t *pool, ngx_buf_t *buf) {
ngx_array_t *zones;
ngx_str_t *zone_name;
ngx_uint_t i;
ngx_shm_zone_t *shm_zone;
volatile ngx_list_part_t *part;
if (ngx_cycle == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
msgpack_packer pk;
msgpack_packer_init(&pk, buf, msgpack_ngx_buf_write);
zones = ngx_array_create(pool, 0, sizeof(ngx_str_t));
if (zones == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
part = &ngx_cycle->shared_memory.part;
shm_zone = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
shm_zone = part->elts;
i = 0;
}
if (shm_zone == NULL) {
continue;
}
if (shm_zone[i].tag != &ngx_http_limit_req_module) {
continue;
}
zone_name = ngx_array_push(zones);
if (zone_name == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
zone_name->len = shm_zone[i].shm.name.len;
zone_name->data = ngx_pnalloc(pool, zone_name->len);
if (zone_name->data == NULL) {
zones->nelts--;
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memcpy(zone_name->data, shm_zone[i].shm.name.data, zone_name->len);
}
msgpack_pack_array(&pk, zones->nelts);
zone_name = zones->elts;
for (i = 0; i < zones->nelts; i++) {
msgpack_pack_str(&pk, zone_name[i].len);
msgpack_pack_str_body(&pk, zone_name[i].data, zone_name[i].len);
}
return NGX_OK;
}
static ngx_int_t dump_req_zone(ngx_pool_t *pool, ngx_buf_t *b,
ngx_str_t *zone_name) {
ngx_uint_t i;
ngx_shm_zone_t *shm_zone;
volatile ngx_list_part_t *part;
if (ngx_cycle == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
part = &ngx_cycle->shared_memory.part;
shm_zone = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
shm_zone = part->elts;
i = 0;
}
if (shm_zone == NULL) {
continue;
}
if (shm_zone[i].tag != &ngx_http_limit_req_module) {
continue;
}
if (ngx_strcmp(zone_name, &shm_zone[i].shm.name) == 0) {
return dump_req_limits(pool, &shm_zone[i], b);
}
}
return NGX_HTTP_NOT_FOUND;
}
static ngx_int_t dump_req_limits(ngx_pool_t *pool, ngx_shm_zone_t *shm_zone,
ngx_buf_t *buf) {
ngx_http_limit_req_ctx_t *ctx;
ngx_queue_t *head, *q, *last;
ngx_http_limit_req_node_t *lr;
char str_addr[INET_ADDRSTRLEN];
time_t now, now_monotonic, last_request_timestamp;
ctx = shm_zone->data;
ngx_log_debug4(
NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0, "shm.name %p -> %.*s - rate: %lu",
ctx, (int)shm_zone->shm.name.len, shm_zone->shm.name.data, ctx->rate);
ngx_shmtx_lock(&ctx->shpool->mutex);
if (ngx_queue_empty(&ctx->sh->queue)) {
ngx_shmtx_unlock(&ctx->shpool->mutex);
return NGX_HTTP_NO_CONTENT;
}
head = ngx_queue_head(&ctx->sh->queue);
last = ngx_queue_last(head);
q = head;
// retrieving current timestamp in milliseconds
now = ngx_cached_time->sec * 1000 + ngx_cached_time->msec;
msgpack_packer pk;
msgpack_packer_init(&pk, buf, msgpack_ngx_buf_write);
while (q != last) {
lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
// retrieving current monotonic timestamp in milliseconds
now_monotonic = ngx_current_msec;
// calculate last request timestamp based on this equation:
// NOW - (NOW_MONOTONIC - LAST_MONOTONIC)
last_request_timestamp = now - (now_monotonic - lr->last);
if (inet_ntop(AF_INET, lr->data, str_addr, sizeof(str_addr)) == NULL) {
perror("inet_ntop");
} else {
ngx_log_debug4(
NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"key: %s - excess: %lu - last_request_timestamp: %lu - now(var): %lu",
str_addr, lr->excess, last_request_timestamp, now)
}
msgpack_pack_array(&pk, 3);
msgpack_pack_str(&pk, lr->len);
msgpack_pack_str_body(&pk, lr->data, lr->len);
msgpack_pack_uint64(&pk, last_request_timestamp);
msgpack_pack_int(&pk, lr->excess);
q = q->next;
}
ngx_shmtx_unlock(&ctx->shpool->mutex);
return NGX_OK;
}