-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngx_http_ratelimiter_module.c
More file actions
359 lines (290 loc) · 9.75 KB
/
ngx_http_ratelimiter_module.c
File metadata and controls
359 lines (290 loc) · 9.75 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "ngx_core.h"
#include "ngx_string.h"
#include "ngx_buf.h"
#include "ngx_module.h"
#include "ngx_conf_file.h"
#include "ngx_http.h"
#include "ngx_http_request.h"
#include "ngx_http_config.h"
#include "ngx_http_core_module.h"
typedef struct{
u_char rbtree_node_data;
ngx_queue_t queue;
ngx_msec_t last;
u_short len;
u_char data[1];
} ngx_http_ratelimiter_node_t;
typedef struct{
ngx_rbtree_t rbtree;
ngx_rbtree_node_t sentinel;
ngx_queue_t queue;
} ngx_http_ratelimiter_shm_t;
typedef struct{
ssize_t shmsize;
ngx_int_t interval;
ngx_slab_pool_t *shpool;
ngx_http_ratelimiter_shm_t *sh;
} ngx_http_ratelimiter_conf_t;
ngx_module_t ngx_http_ratelimiter_module;
static ngx_int_t
ngx_http_ratelimiter_lookup(ngx_http_request_t *r, ngx_http_ratelimiter_conf_t *conf, ngx_uint_t hash, u_char* data, size_t len);
static char *
ngx_http_ratelimiter_createmem(ngx_conf_t *cf, ngx_command_t *cmd, void* conf);
static void
ngx_http_ratelimiter_expire(ngx_http_request_t *r,ngx_http_ratelimiter_conf_t *conf);
static ngx_int_t
ngx_http_ratelimiter_shm_init(ngx_shm_zone_t *shm_zone, void *data);
static ngx_int_t
ngx_http_ratelimiter_handler(ngx_http_request_t *r);
static ngx_int_t
ngx_http_ratelimiter_init(ngx_conf_t *cf);
static void *
ngx_http_ratelimiter_create_main_conf(ngx_conf_t *cf);
static ngx_http_module_t ngx_http_ratelimiter_module_ctx =
{
NULL, /* preconfiguration */
ngx_http_ratelimiter_init, /* postconfiguration */
ngx_http_ratelimiter_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
static ngx_command_t ngx_http_ratelimiter_commands[] = {
{
ngx_string("ratelimiter"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
ngx_http_ratelimiter_createmem,
0,
0,
NULL
},
ngx_null_command
};
ngx_module_t ngx_http_ratelimiter_module={
NGX_MODULE_V1,
&ngx_http_ratelimiter_module_ctx,
ngx_http_ratelimiter_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static void
ngx_http_ratelimiter_rbtree_insert_value(ngx_rbtree_node_t *temp,ngx_rbtree_node_t *node,ngx_rbtree_node_t *sentinel);
static ngx_int_t
ngx_http_ratelimiter_lookup(ngx_http_request_t *r, ngx_http_ratelimiter_conf_t *conf, ngx_uint_t hash, u_char* data, size_t len);
static void
ngx_http_ratelimiter_expire(ngx_http_request_t *r,ngx_http_ratelimiter_conf_t *conf) ;
static char *
ngx_http_ratelimiter_createmem(ngx_conf_t *cf, ngx_command_t* cmd, void* conf);
static ngx_int_t
ngx_http_ratelimiter_shm_init(ngx_shm_zone_t *shm_zone, void *data);
static ngx_int_t
ngx_http_ratelimiter_handler(ngx_http_request_t *r) {
size_t len;
uint32_t hash;
ngx_int_t rc;
ngx_http_ratelimiter_conf_t *conf;
conf = ngx_http_get_module_main_conf(r, ngx_http_ratelimiter_module);
rc = NGX_DECLINED;
if (conf->interval == -1)
return rc;
len = r->connection->addr_text.len + r->uri.len;
u_char* data = ngx_palloc(r->pool, len);
ngx_memcpy(data, r->uri.data, r->uri.len);
ngx_memcpy(data+r->uri.len, r->connection->addr_text.data, r->connection->addr_text.len);
hash = ngx_crc32_short(data, len);
ngx_shmtx_lock(&conf->shpool->mutex);
rc = ngx_http_ratelimiter_lookup(r, conf, hash, data, len);
ngx_shmtx_unlock(&conf->shpool->mutex);
return rc;
}
static ngx_int_t
ngx_http_ratelimiter_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_ratelimiter_handler;
return NGX_OK;
}
static void *
ngx_http_ratelimiter_create_main_conf(ngx_conf_t *cf) {
ngx_http_ratelimiter_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ratelimiter_conf_t));
if (conf == NULL) {
return NULL;
}
conf->interval = -1;
conf->shmsize = -1;
return conf;
}
static void
ngx_http_ratelimiter_rbtree_insert_value(ngx_rbtree_node_t *temp,ngx_rbtree_node_t *node,ngx_rbtree_node_t *sentinel){
ngx_rbtree_node_t **p;
ngx_http_ratelimiter_node_t* lrn, *lrnt;
for ( ;; ) {
if (node->key < temp->key) {
p = &temp->left;
}
else if (node->key > temp->key) {
p = &temp->right;
} else {
lrn = (ngx_http_ratelimiter_node_t*) &node->data;
lrnt = (ngx_http_ratelimiter_node_t*) &temp->data;
p = (ngx_memn2cmp(lrn->data, lrnt->data, lrn->len, lrnt->len) < 0)? &temp->left : &temp->right;
}
if (*p == sentinel) {
break;
}
temp = *p;
}
*p = node;
node->parent = temp;
node->left = sentinel;
node->right = sentinel;
ngx_rbt_red(node);
}
static ngx_int_t
ngx_http_ratelimiter_lookup(ngx_http_request_t *r, ngx_http_ratelimiter_conf_t *conf, ngx_uint_t hash, u_char* data, size_t len){
size_t size;
ngx_int_t rc;
ngx_time_t *tp;
ngx_msec_t now;
ngx_msec_int_t ms;
ngx_rbtree_node_t *node, *sentinel;
ngx_http_ratelimiter_node_t *lr;
tp = ngx_timeofday();
now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
node = conf->sh->rbtree.root;
sentinel = conf->sh->rbtree.sentinel;
while (node != sentinel) {
if (hash < node->key) {
node = node->left;
continue;
}
if (hash > node->key) {
node = node->right;
continue;
}
lr = (ngx_http_ratelimiter_node_t *) &node->data;
rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len);
if (rc == 0) {
ms = (ngx_msec_int_t) (now - lr->last);
if (ms > conf->interval) {
lr->last = now;
ngx_queue_remove(&lr->queue);
ngx_queue_insert_head(&conf->sh->queue, &lr->queue);
return NGX_DECLINED;
}
else {
lr->last = now;
return NGX_HTTP_FORBIDDEN;
}
}
node = (rc < 0) ? node->left : node->right;
}
size = offsetof(ngx_rbtree_node_t, data) + offsetof(ngx_http_ratelimiter_node_t, data) + len;
ngx_http_ratelimiter_expire(r, conf);
node = ngx_slab_alloc_locked(conf->shpool, size);
if (node == NULL) {
return NGX_ERROR;
}
node->key = hash;
lr = (ngx_http_ratelimiter_node_t *) &node->data;
lr->last = now;
lr->len = (u_char) len;
ngx_memcpy(lr->data, data, len);
ngx_rbtree_insert(&conf->sh->rbtree, node);
ngx_queue_insert_head(&conf->sh->queue, &lr->queue);
return NGX_DECLINED;
}
static void
ngx_http_ratelimiter_expire(ngx_http_request_t *r,ngx_http_ratelimiter_conf_t *conf) {
ngx_time_t *tp;
ngx_msec_t now;
ngx_queue_t *q;
ngx_msec_int_t ms;
ngx_rbtree_node_t *node;
ngx_http_ratelimiter_node_t *lr;
tp = ngx_timeofday();
now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
while (1) {
if (ngx_queue_empty(&conf->sh->queue)) {
return;
}
q = ngx_queue_last(&conf->sh->queue);
lr = ngx_queue_data(q, ngx_http_ratelimiter_node_t, queue);
node = (ngx_rbtree_node_t*)((u_char*)lr-offsetof(ngx_rbtree_node_t,data));
ms = (ngx_msec_int_t) (now - lr->last);
if (ms < conf->interval) {
return;
}
ngx_queue_remove(q);
ngx_rbtree_delete(&conf->sh->rbtree, node);
ngx_slab_free_locked(conf->shpool, node);
}
}
static char *
ngx_http_ratelimiter_createmem(ngx_conf_t *cf, ngx_command_t* cmd, void* conf) {
ngx_str_t *value;
ngx_shm_zone_t *shm_zone;
ngx_http_ratelimiter_conf_t* mconf = (ngx_http_ratelimiter_conf_t* )conf;
ngx_str_t name = ngx_string("ratelimiter_shm");
value = cf->args->elts;
mconf->interval = ngx_atoi(value[1].data, value[1].len);
if (mconf->interval == NGX_ERROR || mconf->interval == 0) {
mconf->interval = -1;
return "invalid value";
}
mconf->shmsize = ngx_parse_size(&value[2]);
if (mconf->shmsize == (ssize_t) NGX_ERROR || mconf->shmsize == 0) {
mconf->interval = -1;
return "invalid value";
}
shm_zone = ngx_shared_memory_add(cf, &name, mconf->shmsize, &ngx_http_ratelimiter_module);
if (shm_zone == NULL) {
mconf->interval = -1;
return NGX_CONF_ERROR;
}
shm_zone->init = ngx_http_ratelimiter_shm_init;
shm_zone->data = mconf;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_ratelimiter_shm_init(ngx_shm_zone_t *shm_zone, void *data) {
ngx_http_ratelimiter_conf_t *conf;
ngx_http_ratelimiter_conf_t *oconf = data;
conf = (ngx_http_ratelimiter_conf_t *)shm_zone->data;
if (oconf) {
conf->sh = oconf->sh;
conf->shpool = oconf->shpool;
return NGX_OK;
}
conf->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
conf->sh = ngx_slab_alloc(conf->shpool, sizeof(ngx_http_ratelimiter_shm_t));
if (conf->sh == NULL) {
return NGX_ERROR;
}
conf->shpool->data = conf->sh;
ngx_rbtree_init(&conf->sh->rbtree, &conf->sh->sentinel, ngx_http_ratelimiter_rbtree_insert_value);
ngx_queue_init(&conf->sh->queue);
size_t len = sizeof(" in ratelimiter \"\"") + shm_zone->shm.name.len;
conf->shpool->log_ctx = ngx_slab_alloc(conf->shpool, len);
if (conf->shpool->log_ctx == NULL) {
return NGX_ERROR;
}
ngx_sprintf(conf->shpool->log_ctx, " in ratelimiter \"%V\"%Z", &shm_zone->shm.name);
return NGX_OK;
}