-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_http_captcha_module.c
952 lines (802 loc) · 26.2 KB
/
ngx_http_captcha_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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
#include <hiredis/hiredis.h>
#include <time.h>
#include <gd.h>
#include <gd_errors.h>
#include <uuid/uuid.h>
#include <gdfontt.h> /* 1 Tiny font */
#include <gdfonts.h> /* 2 Small font */
#include <gdfontmb.h> /* 3 Medium bold font */
#include <gdfontl.h> /* 4 Large font */
#include <gdfontg.h> /* 5 Giant font */
#define M_PI 3.14159265358979323846
#define MAXPATHLEN 256
#define CHARSET "abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789"
#define CHARSET_LEN strlen(CHARSET)
#define FONT_SIZE 20
#define TTFTEXT_DRAW 0
#define CAPTCHA_CODE_LEN 4
#define CAPTCHA_CODE_LEN_MAX 6
#define CAPTCHA_WIDTH 130
#define CAPTCHA_HEIGHT 30
#define CAPTCHA_REDIS_HOST "127.0.0.1"
#define CAPTCHA_REDIS_PORT 6379
#define CAPTCHA_EXPIRE 3600
#define CAPTCHA_COOKIE_NAME "captcha_code"
#define CAPTCHA_ARG_NAME CAPTCHA_COOKIE_NAME
#define CAPTCHA_SECURITY_KEY "Secret"
#define AUTH_SUCCESS "{\"code\" : 0, \"data\" : [], \"message\" : \"SUCCESS\"}"
#define AUTH_FAIL "{\"code\" : -1, \"data\" : [], \"message\" : \"FAIL\"}"
unsigned seed;
typedef struct _ngx_captcha_redis_conf {
char *host;
int port;
} ngx_captcha_redis_conf;
typedef struct _ngx_captcha_conf {
ngx_int_t width;
ngx_int_t height;
ngx_int_t length;
char * font;
ngx_int_t expire;
ngx_captcha_redis_conf * redis_conf;
} ngx_captcha_conf;
typedef struct _png_stream_buffer {
char *buffer;
size_t size;
ngx_pool_t * pool;
} png_stream_buffer;
typedef struct _ngx_captcha_cookie{
ngx_str_t path;
ngx_str_t domain;
ngx_str_t expire;
ngx_str_t name;
ngx_str_t value;
} ngx_captcha_cookie;
static void * ngx_prealloc(ngx_pool_t *pool, void *p, size_t old_size, size_t new_size);
static int mt_rand(int min, int max);
static void create_code(char * code, int len);
static gdImagePtr create_bg(int width, int height);
static void gd_image_TTF_text(gdImagePtr img,int font_size, int angle, long x, long y, long font_color, const char * font, char *str);
static void create_font(gdImagePtr img, char *code, int len, int width, int height, char * font);
static void create_line(gdImagePtr img, int width, int height, char * font);
static void _image_output_putc(struct gdIOCtx *ctx, int c);
static int _image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int len);
static void _image_output_ctxfree(struct gdIOCtx *ctx);
static void freeCtx(ngx_pool_t * pool, gdIOCtx *ctx);
static void get_png_stream_buffer(ngx_pool_t * pool, gdImagePtr img, char *buf, int *len);
static void create_captcha_png(ngx_pool_t * pool, char *buf, int *len, char * code, ngx_captcha_conf * captcha);
static in_addr_t get_remote_ip(ngx_http_request_t *req);
static ngx_str_t get_user_agent(ngx_http_request_t *req);
static u_char * get_unique_id(ngx_http_request_t *req);
static u_char * user_crc_hash(ngx_http_request_t *req);
static redisContext * connectRedis(char *host, int port, ngx_log_t *log);
static ngx_int_t closeRedisConnect(redisContext *conn);
static ngx_int_t redisSetex(redisContext *conn, char *key, int expire_time, char *val, ngx_log_t *log);
static ngx_int_t redisGet(redisContext *conn, char *key, u_char * result, ngx_log_t *log);
static void create_captcha_img(ngx_pool_t * pool, char * img_buf, int *len, char * code, ngx_captcha_conf * captcha_conf);
static void save_captcha_code(ngx_http_request_t *req, char * code);
static ngx_captcha_cookie * generate_captcha_cookie(ngx_http_request_t *req, ngx_captcha_conf * captcha_conf);
static ngx_int_t set_captcha_cookie(ngx_http_request_t *req, ngx_captcha_conf * captcha_conf);
static ngx_uint_t get_captcha_cookie(ngx_http_request_t *req, u_char * captcha_id);
static ngx_uint_t get_query_param_value(ngx_http_request_t *req, const char * param_name, ngx_str_t *param_value);
static ngx_int_t get_user_captcha_code(ngx_http_request_t *req, u_char *input_code);
static char *set_captcha_init(ngx_conf_t *, ngx_command_t *, void *);
static char *set_captcha_font(ngx_conf_t *, ngx_command_t *, void *);
static char *set_captcha_width(ngx_conf_t *, ngx_command_t *, void *);
static char *set_captcha_height(ngx_conf_t *, ngx_command_t *, void *);
static char *set_captcha_length(ngx_conf_t *, ngx_command_t *, void *);
static char *set_captcha_expire(ngx_conf_t *, ngx_command_t *, void *);
static char *set_captcha_redis_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *set_captcha_output_handler(ngx_conf_t *, ngx_command_t *, void *);
static ngx_int_t captcha_output_handler(ngx_http_request_t *req);
static char *set_captcha_auth_handler(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t captcha_auth_handler(ngx_http_request_t *req);
static ngx_command_t captcha_commands[] = {
{
ngx_string("captcha_redis_conf"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
set_captcha_redis_conf,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("captcha_init"),
NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
set_captcha_init,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("captcha_font"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
set_captcha_font,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("captcha_width"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
set_captcha_width,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("captcha_height"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
set_captcha_height,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("captcha_length"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
set_captcha_length,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("captcha_expire"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
set_captcha_expire,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("captcha_output"),
NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
set_captcha_output_handler,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("captcha_auth"),
NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
set_captcha_auth_handler,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
};
static ngx_http_module_t captcha_ctx = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
ngx_module_t ngx_http_captcha_module = {
NGX_MODULE_V1,
&captcha_ctx,
captcha_commands,
NGX_HTTP_MODULE,
NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NGX_MODULE_V1_PADDING
};
ngx_captcha_conf * captcha_conf;
ngx_captcha_redis_conf * redis_conf;
static void *
ngx_prealloc(ngx_pool_t *pool, void *p, size_t old_size, size_t new_size)
{
void *new;
if (p == NULL) {
return ngx_palloc(pool, new_size);
}
if (new_size == 0) {
if ((u_char *) p + old_size == pool->d.last) {
pool->d.last = p;
} else {
ngx_pfree(pool, p);
}
return NULL;
}
if ((u_char *) p + old_size == pool->d.last
&& (u_char *) p + new_size <= pool->d.end)
{
pool->d.last = (u_char *) p + new_size;
return p;
}
new = ngx_palloc(pool, new_size);
if (new == NULL) {
return NULL;
}
ngx_memcpy(new, p, old_size);
ngx_pfree(pool, p);
return new;
}
static int
mt_rand(int min, int max)
{
srand(seed++);
return (rand() % (max-min+1)) + min;
}
static void
create_code(char * code, int len)
{
int i = 0;
int idx = 0;
for(i=0; i < len; i++)
{
idx = mt_rand(0, CHARSET_LEN-1);
code[i] = CHARSET[idx];
}
}
static gdImagePtr
create_bg(int width, int height)
{
gdImagePtr img;
int color;
img = gdImageCreateTrueColor(width, height);
color = gdImageColorAllocate(img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
gdImageFilledRectangle(img,0,height,width,0,color);
return img;
}
static void
gd_image_TTF_text(gdImagePtr img,int font_size, int angle, long x, long y, long font_color, const char * font, char *str)
{
int brect[8];
// char * error = NULL;
angle = angle * (M_PI/180);
gdImageStringFT(img, brect, font_color, (char *)font, font_size, angle, x, y, str);
}
static void
create_font(gdImagePtr img, char *code, int len, int width, int height, char * font)
{
int x = width / len;
int i = 0;
int font_color = 0;
char str[2] = "\0";
for (i=0; i<len; i++)
{
memcpy(str, code++, 1);
font_color = gdImageColorAllocate(img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
gd_image_TTF_text(img,FONT_SIZE,mt_rand(-30,30),x*i+mt_rand(1,5),height/1.4,font_color,font,str);
}
}
static void
create_line(gdImagePtr img, int width, int height, char * font)
{
int i, brect[8];
int color = 0;
const char * str = "*";
int font_size = 8;
int angle = 0;
for (i=0;i<6;i++)
{
color = gdImageColorAllocate(img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
gdImageLine(img,mt_rand(0,width),mt_rand(0,height),mt_rand(0,width),mt_rand(0,height),color);
}
for (i=0;i<100;i++) {
color = gdImageColorAllocate(img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
gdImageStringFT(img, brect, color, font, font_size, angle, mt_rand(0,width), mt_rand(0,height), (char *)str);
}
}
static void
_image_output_putc(struct gdIOCtx *ctx, int c)
{
}
static int
_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int len)
{
png_stream_buffer * p = (png_stream_buffer *)ctx->data;
size_t nsize = p->size + len;
if (p->buffer)
{
p->buffer = ngx_prealloc(p->pool, p->buffer, p->size, nsize);
}
else
{
p->buffer = ngx_pcalloc(p->pool, nsize);//alloc 1
}
if (!p->buffer)
{
return -1;
}
memcpy(p->buffer + p->size, buf, len);
p->size += len;
return 0;
}
static void
_image_output_ctxfree(struct gdIOCtx *ctx)
{
}
static void
freeCtx(ngx_pool_t *pool, gdIOCtx *ctx)
{
png_stream_buffer *p = (png_stream_buffer *)ctx->data;
ngx_pfree(pool, p->buffer);//free 3
ngx_pfree(pool, ctx->data);//free 1
//ctx->gd_free(ctx);
ngx_pfree(pool, ctx);//free 2
}
static void
get_png_stream_buffer(ngx_pool_t * pool, gdImagePtr img, char *buf, int *len)
{
int q = -1;
gdIOCtx *ctx;
png_stream_buffer * p;
ctx = (gdIOCtx *)ngx_pcalloc(pool, sizeof(gdIOCtx));//alloc 2
ctx->putC = _image_output_putc;
ctx->putBuf = _image_output_putbuf;
ctx->gd_free = _image_output_ctxfree;
p = (png_stream_buffer *)ngx_pcalloc(pool, sizeof(png_stream_buffer));//alloc 3
p->pool = pool;
ctx->data = p;
gdImagePngCtxEx(img, ctx, q);
p = (png_stream_buffer *)ctx->data;
buf = memcpy(buf, p->buffer, p->size);
*len = p->size;
freeCtx(pool, ctx);
}
static void
create_captcha_png(ngx_pool_t * pool, char *buf, int *len, char *code, ngx_captcha_conf * captcha)
{
gdImagePtr img;
seed = (unsigned int)time(NULL);
create_code(code, captcha->length);
img = create_bg(captcha->width, captcha->height);
create_font(img, code, captcha->length, captcha->width, captcha->height, captcha->font);
create_line(img, captcha->width, captcha->height, captcha->font);
get_png_stream_buffer(pool, img, buf, len);
gdImageDestroy(img);
}
static void
redis_conf_init(ngx_conf_t *cf)
{
redis_conf = (ngx_captcha_redis_conf *)ngx_pcalloc(cf->pool, sizeof(ngx_captcha_redis_conf));//alloc 4
redis_conf->host = (char *)CAPTCHA_REDIS_HOST;
redis_conf->port = CAPTCHA_REDIS_PORT;
};
static char *
set_captcha_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
if(!redis_conf)
{
redis_conf_init(cf);
}
captcha_conf = (ngx_captcha_conf *)ngx_pcalloc(cf->pool, sizeof(ngx_captcha_conf));//alloc 5
captcha_conf->width = CAPTCHA_WIDTH;
captcha_conf->height = CAPTCHA_HEIGHT;
captcha_conf->length = CAPTCHA_CODE_LEN;
captcha_conf->font = NULL;
captcha_conf->expire = CAPTCHA_EXPIRE;
captcha_conf->redis_conf = redis_conf;
return NGX_CONF_OK;
};
static char *
set_captcha_font(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_str_t *value;
value = cf->args->elts;
captcha_conf->font = (char *)value[1].data;
return NGX_CONF_OK;
};
static char *
set_captcha_width(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_str_t *value;
ngx_int_t w;
value = cf->args->elts;
w = ngx_atoi(value[1].data, value[1].len);
captcha_conf->width = w < 0 ? 0 : w;
return NGX_CONF_OK;
};
static char *
set_captcha_height(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_str_t *value;
ngx_int_t h;
value = cf->args->elts;
h = ngx_atoi(value[1].data, value[1].len);
captcha_conf->height = h < 0 ? 0 : h;
return NGX_CONF_OK;
};
static char *
set_captcha_length(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_str_t *value;
ngx_int_t len;
value = cf->args->elts;
len = ngx_atoi(value[1].data, value[1].len);
captcha_conf->length = len > CAPTCHA_CODE_LEN_MAX ? CAPTCHA_CODE_LEN_MAX : len;
return NGX_CONF_OK;
};
static char *
set_captcha_expire(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_int_t exp;
value = cf->args->elts;
exp = ngx_atoi(value[1].data, value[1].len);
captcha_conf->expire = exp < 0 ? 0 : exp;
return NGX_CONF_OK;
};
static char *
set_captcha_auth_handler(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *corecf;
corecf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
corecf->handler = captcha_auth_handler;
if(!redis_conf)
{
redis_conf_init(cf);
}
return NGX_CONF_OK;
};
static char *
set_captcha_redis_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
value = cf->args->elts;
if(!redis_conf)
{
redis_conf_init(cf);
}
redis_conf->host = (char *)value[1].data;
redis_conf->port = ngx_atoi(value[2].data, value[2].len);
return NGX_CONF_OK;
};
static char *
set_captcha_output_handler(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *corecf;
corecf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
corecf->handler = captcha_output_handler;
return NGX_CONF_OK;
};
static u_char *
get_unique_id(ngx_http_request_t *req)
{
return user_crc_hash(req);
}
static redisContext *
connectRedis(char *host, int port, ngx_log_t *log)
{
redisContext *conn = redisConnect(host, port);
if (conn != NULL && conn->err) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"can't connect to redis by host[%s] port[%d]",
host,port);
return conn;
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
"connect to redis by host[%s] port[%d]",
host,port);
return conn;
}
static ngx_int_t
closeRedisConnect(redisContext *conn)
{
redisFree(conn);
return NGX_OK;
}
static ngx_int_t
redisSetex(redisContext *conn, char *key, int expire_time, char *val, ngx_log_t *log)
{
redisReply *reply;
reply = redisCommand(conn, "SETEX %s %d %s", key, expire_time, val);
if (reply->type == REDIS_REPLY_ERROR ) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"redis command[SETEX %s %d %s] result[%s]",
key, expire_time, val, reply->str);
freeReplyObject(reply);
closeRedisConnect(conn);
return NGX_ERROR;
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
"redis command[SETEX %s %d %s] result[%s]",
key, expire_time, val, reply->str);
freeReplyObject(reply);
return NGX_OK;
}
static ngx_int_t
redisGet(redisContext *conn, char *key, u_char * result, ngx_log_t *log)
{
redisReply *reply;
reply = redisCommand(conn, "GET %s", key);
if (reply->type == REDIS_REPLY_ERROR || reply->type == REDIS_REPLY_NIL) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
"redis command[GET %s] result[%d]",
key, reply->type);
}
else
{
result = ngx_copy(result, (u_char *)reply->str, ngx_strlen(reply->str));
ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
"redis command[GET %s] result[%s]",
key, reply->str);
}
freeReplyObject(reply);
return NGX_OK;
}
static void
create_captcha_img(ngx_pool_t * pool, char * img_buf, int *len, char *code, ngx_captcha_conf * captcha_conf)
{
create_captcha_png(pool, img_buf, len, code, captcha_conf);
}
static void
save_captcha_code(ngx_http_request_t *req, char * code)
{
u_char * unique_id = get_unique_id(req);
redisContext * conn = connectRedis(redis_conf->host, redis_conf->port, req->connection->log);
redisSetex(conn, (char *)unique_id, CAPTCHA_EXPIRE, code, req->connection->log);
closeRedisConnect(conn);
ngx_pfree(req->pool, unique_id);//free 6
}
static ngx_captcha_cookie *
generate_captcha_cookie(ngx_http_request_t *req, ngx_captcha_conf * captcha_conf)
{
u_char * value;
size_t value_len;
u_char * expire, *p;
size_t expire_len;
size_t exp_len;
ngx_captcha_cookie * captcha_cookie;
captcha_cookie = (ngx_captcha_cookie *)ngx_pcalloc(req->pool, sizeof(ngx_captcha_cookie));//alloc 7
value = get_unique_id(req);
value_len = 16;
exp_len = ngx_strlen("; expires=");
expire = (u_char *)ngx_pcalloc(req->pool, exp_len+40);//alloc 8
p = expire;
p = ngx_copy(p, "; expires=", exp_len);
p = ngx_http_cookie_time(p, ngx_time()+ 8*3600 + captcha_conf->expire);
expire_len = ngx_strlen((const char *)expire);
captcha_cookie->name.data = (u_char *)CAPTCHA_COOKIE_NAME;
captcha_cookie->name.len = strlen(CAPTCHA_COOKIE_NAME);
captcha_cookie->value.data = value;
captcha_cookie->value.len = value_len;
captcha_cookie->expire.data = expire;
captcha_cookie->expire.len = expire_len;
captcha_cookie->path.data = (u_char *)"; path=/;";
captcha_cookie->path.len = ngx_strlen("; path=/;");
return captcha_cookie;
}
static ngx_int_t
set_captcha_cookie(ngx_http_request_t *req, ngx_captcha_conf * captcha_conf)
{
u_char *cookie, *p;
size_t len;
ngx_table_elt_t *set_cookie;
ngx_captcha_cookie * captcha_cookie = generate_captcha_cookie(req, captcha_conf);
len = captcha_cookie->name.len+1+captcha_cookie->value.len;
if (captcha_cookie->expire.len) {
len += captcha_cookie->expire.len;
}
if (captcha_cookie->path.len) {
len += captcha_cookie->path.len;
}
cookie = ngx_pnalloc(req->pool, len);//alloc 9
if (cookie == NULL) {
ngx_log_error(NGX_LOG_ERR, req->connection->log, 0,
"cookie ngx_pnalloc error length[%d]",
len);
return NGX_ERROR;
}
p = ngx_copy(cookie, captcha_cookie->name.data, captcha_cookie->name.len);
*p++ = '=';
p = ngx_copy(p, captcha_cookie->value.data, captcha_cookie->value.len);
if (captcha_cookie->expire.len) {
p = ngx_copy(p, captcha_cookie->expire.data, captcha_cookie->expire.len);
}
if (captcha_cookie->path.len) {
p = ngx_copy(p, captcha_cookie->path.data, captcha_cookie->path.len);
}
ngx_pfree(req->pool, captcha_cookie->value.data);//free 6
ngx_pfree(req->pool, captcha_cookie->expire.data);//free 8
ngx_pfree(req->pool, captcha_cookie);//free 7
set_cookie = ngx_list_push(&req->headers_out.headers);
if (set_cookie == NULL)
{
ngx_log_error(NGX_LOG_ERR, req->connection->log, 0,
"set_cookie ngx_list_push error cookie[%s]",
cookie);
return NGX_ERROR;
}
set_cookie->hash = 1;
ngx_str_set(&set_cookie->key, "Set-Cookie");
set_cookie->value.len = p - cookie;
set_cookie->value.data = cookie;
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"captcha cookie: \"%V\"", &set_cookie->value);
return NGX_OK;
}
static ngx_uint_t
get_captcha_cookie(ngx_http_request_t *req, u_char * captcha_id)
{
ngx_str_t cookie_name;
ngx_str_t cookie_value;
cookie_name.data = (u_char *)CAPTCHA_COOKIE_NAME;
cookie_name.len = ngx_strlen(CAPTCHA_COOKIE_NAME);
ngx_uint_t n = ngx_http_parse_multi_header_lines(&req->headers_in.cookies, &cookie_name, &cookie_value);
captcha_id = ngx_cpymem(captcha_id, cookie_value.data, 16);
return n;
}
static ngx_uint_t
get_query_param_value(ngx_http_request_t *req, const char * param_name, ngx_str_t *param_value)
{
size_t param_name_len;
param_name_len = ngx_strlen(param_name);
if (ngx_http_arg(req, (u_char *)param_name, param_name_len, param_value) != NGX_OK)
{
param_value->len = 0;
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"get_query_param_value param_name[%s:%d] param_value[%s:%d]",
param_name,param_name_len,param_value->data,param_value->len
);
return NGX_OK;
}
static ngx_int_t
get_user_captcha_code(ngx_http_request_t *req, u_char *input_code)
{
ngx_int_t ret;
ngx_str_t param_value;
if(!(req->method & NGX_HTTP_GET))
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"get_user_captcha_code request method error");
return NGX_ERROR;
}
ret = ngx_http_discard_request_body(req);
if(NGX_OK != ret)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"get_user_captcha_code discard_request_body error");
return NGX_ERROR;
}
ret = get_query_param_value(req, CAPTCHA_ARG_NAME, ¶m_value);
if(ret == NGX_OK)
{
input_code = ngx_cpymem(input_code, param_value.data, param_value.len);
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"get_user_captcha_code param_value[%s:%d]",
param_value.data,param_value.len
);
return NGX_OK;
}
return NGX_ERROR;
}
static in_addr_t
get_remote_ip(ngx_http_request_t *req)
{
in_addr_t inaddr;
struct sockaddr_in *sin;
sin = (struct sockaddr_in *) req->connection->sockaddr;
inaddr = ntohl(sin->sin_addr.s_addr);
return inaddr;
}
static ngx_str_t
get_user_agent(ngx_http_request_t *req)
{
return req->headers_in.user_agent->value;
}
static u_char *
user_crc_hash(ngx_http_request_t *req)
{
u_char * crc_str;
ngx_md5_t md5;
u_char *hash;
ngx_str_t ua;
in_addr_t ip;
ip = get_remote_ip(req);
ua = get_user_agent(req);
hash = ngx_pcalloc(req->pool, 17*sizeof(u_char));//alloc 6
crc_str = ngx_pcalloc(req->pool, ua.len+19);//alloc n
ngx_sprintf(crc_str, "%ul%s%s", ip, ua.data, CAPTCHA_SECURITY_KEY);
ngx_md5_init(&md5);
ngx_md5_update(&md5, crc_str, ngx_strlen(crc_str));
ngx_md5_final(hash, &md5);
ngx_pfree(req->pool, crc_str);//free n
return hash;
}
static ngx_int_t
captcha_output_handler(ngx_http_request_t *req)
{
int len = 0;
u_char img_buf[6144] = {"\0"};
u_char code[CAPTCHA_CODE_LEN_MAX] = {"\0"};
create_captcha_img(req->pool, (char *)img_buf, &len, (char *)code, captcha_conf);
save_captcha_code(req, (char *)code);
set_captcha_cookie(req, captcha_conf);
ngx_pfree(req->pool, captcha_conf);//free 5
ngx_pfree(req->pool, redis_conf);//free 4
req->headers_out.status = 200;
req->headers_out.content_length_n = len;
ngx_str_set(&req->headers_out.content_type, "image/png");
ngx_http_send_header(req);
ngx_buf_t *b;
b = ngx_pcalloc(req->pool, sizeof(ngx_buf_t));
ngx_chain_t out;
out.buf = b;
out.next = NULL;
b->pos = (u_char *)img_buf;
b->last = (u_char *)img_buf + len;
b->memory = 1;
b->last_buf = 1;
return ngx_http_output_filter(req, &out);
}
static ngx_int_t
captcha_auth_handler(ngx_http_request_t *req)
{
size_t len = 0;
ngx_int_t ret;
u_char * auth_result;
u_char captcha_id[17] = {"\0"};
u_char * unique_id;
u_char input_code[CAPTCHA_CODE_LEN_MAX] = {"\0"};;
u_char code[CAPTCHA_CODE_LEN_MAX] = {"\0"};
redisContext * conn = NULL;
ret = get_user_captcha_code(req, input_code);
if(ret != NGX_OK)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"captcha_auth_handler get_user_captcha_code fail"
);
goto auth_fail;
}
unique_id = get_unique_id(req);
get_captcha_cookie(req, captcha_id);
// if(0 != ngx_strcasecmp(unique_id, unique_id))
// {
// ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
// "captcha_auth_handler code is equal input_code[%s] redis_code[%s] unique_id[%s] captcha_id[%s]",
// input_code,code,unique_id,captcha_id
// );
// ngx_pfree(req->pool, unique_id);//free 6
// goto auth_fail;
// }
ngx_pfree(req->pool, unique_id);//free 6
conn = connectRedis(redis_conf->host, redis_conf->port, req->connection->log);
if(!conn)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"captcha_auth_handler code is equal input_code[%s] redis_code[%s]",
input_code,code
);
goto auth_fail;
}
redisGet(conn, (char *)captcha_id, code, req->connection->log);
closeRedisConnect(conn);
if(0 != ngx_strcasecmp(&input_code[0], code))
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"captcha_auth_handler code is not equal input_code[%s] redis_code[%s]",
input_code,code
);
goto auth_fail;
}
else
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, req->connection->log, 0,
"captcha_auth_handler code is equal input_code[%s] redis_code[%s]",
input_code,code
);
len = ngx_strlen(AUTH_SUCCESS);
auth_result = (u_char *)AUTH_SUCCESS;
goto output;
}
auth_fail:
len = ngx_strlen(AUTH_FAIL);
auth_result = (u_char *)AUTH_FAIL;
goto output;
output:
ngx_pfree(req->pool, captcha_conf);//free 5
ngx_pfree(req->pool, redis_conf);//free 4
req->headers_out.status = 200;
req->headers_out.content_length_n = len;
ngx_str_set(&req->headers_out.content_type, "application/json");
ngx_http_send_header(req);
ngx_buf_t *b;
b = ngx_pcalloc(req->pool, sizeof(ngx_buf_t));
ngx_chain_t out;
out.buf = b;
out.next = NULL;
b->pos = auth_result;
b->last = auth_result + len;
b->memory = 1;
b->last_buf = 1;
return ngx_http_output_filter(req, &out);
}