-
Notifications
You must be signed in to change notification settings - Fork 5
/
ngx_http_bot_verifier_cache.c
114 lines (101 loc) · 3.18 KB
/
ngx_http_bot_verifier_cache.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
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <hiredis/hiredis.h>
#include "ngx_http_bot_verifier_module.h"
#include "ngx_http_bot_verifier_cache.h"
ngx_int_t
ngx_http_bot_verifier_module_check_connection(redisContext *context) {
if (context == NULL || context->err) {
return NGX_ERROR;
}
redisReply *reply;
reply = redisCommand(context, "PING");
if (reply) {
if (reply->type == REDIS_REPLY_ERROR) {
freeReplyObject(reply);
return NGX_ERROR;
} else {
freeReplyObject(reply);
return NGX_OK;
}
} else {
return NGX_ERROR;
}
}
void
ngx_http_bot_verifier_module_cleanup_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
{
if (loc_conf->redis.connection != NULL) {
redisFree(loc_conf->redis.connection);
loc_conf->redis.connection = NULL;
}
}
ngx_int_t
ngx_http_bot_verifier_module_reset_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
{
ngx_http_bot_verifier_module_cleanup_connection(loc_conf);
const char *host = (const char *)loc_conf->redis.host.data;
int port = loc_conf->redis.port;
int connection_timeout = loc_conf->redis.connection_timeout;
int read_timeout = loc_conf->redis.read_timeout;
struct timeval ct = {0, (connection_timeout > 0) ? (connection_timeout * 1000) : 10000};
struct timeval rt = {0, (read_timeout > 0) ? (read_timeout * 1000) : 10000};
redisContext *context = redisConnectWithTimeout(host, port, ct);
if (context == NULL || context->err) {
if (context) {
redisFree(context);
}
return NGX_ERROR;
} else {
redisSetTimeout(context, rt);
loc_conf->redis.connection = context;
return NGX_OK;
}
}
ngx_int_t
ngx_http_bot_verifier_module_lookup_verification_status(redisContext *context, char *address)
{
redisReply *reply;
reply = redisCommand(context, "GET %s:bvs", address);
if (reply) {
if (reply->type == REDIS_REPLY_STRING) {
if (strncmp("failure", reply->str, strlen("failure")) == 0) {
freeReplyObject(reply);
return FAILURE;
} else if (strncmp("success", reply->str, strlen("success")) == 0) {
freeReplyObject(reply);
return SUCCESS;
} else {
freeReplyObject(reply);
return ERROR;
}
} else if (reply->type == REDIS_REPLY_NIL) {
freeReplyObject(reply);
return NOT_FOUND;
} else {
freeReplyObject(reply);
return ERROR;
}
} else {
return ERROR;
}
}
ngx_int_t
ngx_http_bot_verifier_module_persist_verification_status(ngx_http_bot_verifier_module_loc_conf_t *loc_conf, char *address, ngx_int_t status)
{
redisReply *reply = NULL;
if (status == NGX_OK) {
reply = redisCommand(loc_conf->redis.connection, "SETEX %s:bvs %d %s", address, loc_conf->redis.expiry, "success");
} else if (status == NGX_DECLINED) {
reply = redisCommand(loc_conf->redis.connection, "SETEX %s:bvs %d %s", address, loc_conf->redis.expiry, "failure");
if (loc_conf->repsheet_enabled) {
reply = redisCommand(loc_conf->redis.connection, "REPSHEET.BLACKLIST %s %s %d", address, "http.bot.provider_validation", loc_conf->redis.expiry);
}
}
if (reply) {
freeReplyObject(reply);
}
return NGX_OK;
}