Skip to content

Commit c9bc77c

Browse files
committed
Someday I will learn how to program. Until then I will stumble through
poor choices in hopes of making something usable.
1 parent ef4bdf8 commit c9bc77c

8 files changed

+41
-62
lines changed

ngx_http_bot_verifier_address_tools.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <ngx_http.h>
22

33
ngx_int_t
4-
remote_address(u_char *connected_address, u_char *xff_header, ngx_str_t *address)
4+
remote_address(ngx_http_request_t *r, u_char *xff_header, char *address)
55
{
6-
if ((connected_address == NULL && xff_header == NULL) || address == NULL) {
6+
if ((r == NULL && xff_header == NULL) || address == NULL) {
77
return NGX_DECLINED;
88
}
99

@@ -25,22 +25,19 @@ remote_address(u_char *connected_address, u_char *xff_header, ngx_str_t *address
2525
unsigned char buf[sizeof(struct in_addr)];
2626

2727
if (inet_pton(AF_INET, (const char *)test_address, buf) == 1) {
28-
address->len = length + 1;
29-
address->data = malloc(sizeof(u_char *) * address->len);
30-
ngx_memcpy(address->data, test_address, address->len);
28+
memcpy(address, test_address, length);
3129
return NGX_OK;
3230
} else {
3331
return NGX_ERROR;
3432
}
3533
} else {
36-
address->data = connected_address;
37-
address->len = strlen((const char *)connected_address);
34+
memcpy(address, r->connection->addr_text.data, r->connection->addr_text.len);
3835
return NGX_OK;
3936
}
4037
}
4138

4239
ngx_int_t
43-
ngx_http_bot_verifier_module_determine_address(ngx_http_request_t *r, ngx_str_t *address)
40+
ngx_http_bot_verifier_module_determine_address(ngx_http_request_t *r, char *address)
4441
{
4542
ngx_int_t result;
4643
ngx_table_elt_t *xff = NULL;
@@ -51,19 +48,17 @@ ngx_http_bot_verifier_module_determine_address(ngx_http_request_t *r, ngx_str_t
5148
}
5249

5350
if (xff == NULL) {
54-
address->data = r->connection->addr_text.data;
55-
address->len = r->connection->addr_text.len;
56-
51+
memcpy(address, r->connection->addr_text.data, r->connection->addr_text.len);
5752
return NGX_OK;
5853
} else {
59-
result = remote_address(r->connection->addr_text.data, xff->value.data, address);
54+
result = remote_address(r, xff->value.data, address);
6055
if (result == NGX_OK) {
6156
return NGX_OK;
6257
} else if (result == NGX_DECLINED) {
63-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Not enough information to determine connecting IP address");
58+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cannot determine IP address");
6459
return NGX_ERROR;
6560
} else {
66-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "The address supplied is not a valid IP address");
61+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "IP address is not valid");
6762
return NGX_ERROR;
6863
}
6964
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __NGX_HTTP_BOT_VERIFIER_ADDRESS_TOOLS_H__
22
#define __NGX_HTTP_BOT_VERIFIER_ADDRESS_TOOLS_H__
33

4-
ngx_int_t remote_address(char *connected_address, char *xff_header, ngx_str_t *address);
5-
ngx_int_t ngx_http_bot_verifier_module_determine_address(ngx_http_request_t *r, ngx_str_t *address);
4+
ngx_int_t remote_address(ngx_http_request_t *r, char *xff_header, char *address);
5+
ngx_int_t ngx_http_bot_verifier_module_determine_address(ngx_http_request_t *r, char *address);
66

77
#endif

ngx_http_bot_verifier_cache.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ reset_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
6363
}
6464

6565
ngx_int_t
66-
lookup_verification_status(redisContext *context, ngx_str_t *address)
66+
lookup_verification_status(redisContext *context, char *address)
6767
{
6868
redisReply *reply;
6969

70-
reply = redisCommand(context, "GET %s:bvs", (char *)address->data);
70+
reply = redisCommand(context, "GET %s:bvs", address);
7171
if (reply) {
7272
if (reply->type == REDIS_REPLY_STRING) {
7373
if (strncmp("failure", reply->str, strlen("failure")) == 0) {
@@ -93,14 +93,14 @@ lookup_verification_status(redisContext *context, ngx_str_t *address)
9393
}
9494

9595
ngx_int_t
96-
persist_verification_status(redisContext *context, ngx_str_t *address, ngx_int_t status, ngx_int_t expiry)
96+
persist_verification_status(redisContext *context, char *address, ngx_int_t status, ngx_int_t expiry)
9797
{
9898
redisReply *reply = NULL;
9999

100100
if (status == NGX_OK) {
101-
reply = redisCommand(context, "SETEX %s:bvs %d %s", (char *)address->data, expiry, "success");
101+
reply = redisCommand(context, "SETEX %s:bvs %d %s", address, expiry, "success");
102102
} else if (status == NGX_DECLINED) {
103-
reply = redisCommand(context, "SETEX %s:bvs %d %s", (char *)address->data, expiry, "failure");
103+
reply = redisCommand(context, "SETEX %s:bvs %d %s", address, expiry, "failure");
104104
}
105105

106106
if (reply) {

ngx_http_bot_verifier_cache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ngx_int_t check_connection(redisContext *context);
55
void cleanup_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf);
66
ngx_int_t reset_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf);
7-
ngx_int_t lookup_verification_status(redisContext *context, ngx_str_t *address);
8-
ngx_int_t persist_verification_status(redisContext *context, ngx_str_t *address, ngx_int_t status, ngx_int_t expiry);
7+
ngx_int_t lookup_verification_status(redisContext *context, char *address);
8+
ngx_int_t persist_verification_status(redisContext *context, char *address, ngx_int_t status, ngx_int_t expiry);
99

1010
#endif

ngx_http_bot_verifier_identifier.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ ngx_http_bot_verifier_module_identifies_as_known_bot(ngx_http_request_t *r, ngx_
2424
n = ngx_regex_exec(re, &user_agent, captures, (1 + rc.captures) * 3);
2525

2626
if (n >= 0) {
27-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Search engine bot identified for %V", &user_agent);
27+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "User Agent identified as provider %V", &user_agent);
2828
return NGX_OK;
2929
}
3030

31-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "User Agent %V not identified", &user_agent);
32-
3331
return NGX_DECLINED;
3432
}

ngx_http_bot_verifier_module.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ngx_http_bot_verifier_module_handler(ngx_http_request_t *r)
2929

3030
ngx_int_t connection_status = check_connection(loc_conf->redis.connection);
3131
if (connection_status == NGX_ERROR) {
32-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No cache connection found, creating a new connection");
32+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No cache connection, creating new connection");
3333

3434
if (loc_conf->redis.connection != NULL) {
3535
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cache connection error: %s", loc_conf->redis.connection->errstr);
@@ -38,7 +38,7 @@ ngx_http_bot_verifier_module_handler(ngx_http_request_t *r)
3838
connection_status = reset_connection(loc_conf);
3939

4040
if (connection_status == NGX_ERROR) {
41-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Unable to establish a connection to cache, bypassing");
41+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Unable to establish cache connection, bypassing");
4242

4343
if (loc_conf->redis.connection != NULL) {
4444
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cache connection error: %s", loc_conf->redis.connection->errstr);
@@ -49,55 +49,48 @@ ngx_http_bot_verifier_module_handler(ngx_http_request_t *r)
4949
}
5050
}
5151

52-
ngx_str_t address;
53-
ngx_int_t address_status = ngx_http_bot_verifier_module_determine_address(r, &address);
52+
char address[INET_ADDRSTRLEN];
53+
memset(address, '\0', INET_ADDRSTRLEN);
54+
ngx_int_t address_status = ngx_http_bot_verifier_module_determine_address(r, address);
55+
5456
if (address_status == NGX_ERROR) {
5557
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Unable to determine connected address, bypassing");
5658
return NGX_DECLINED;
5759
}
5860

59-
ngx_int_t verification_status = lookup_verification_status(loc_conf->redis.connection, &address);
61+
ngx_int_t verification_status = lookup_verification_status(loc_conf->redis.connection, address);
6062
if (verification_status == NGX_ERROR) {
6163
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Unable to lookup verification status, bypassing");
6264
return NGX_DECLINED;
6365
}
6466

65-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Lookup result %d", verification_status);
66-
6767
if (verification_status == SUCCESS) {
68-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Actor has already been verified, bypassing");
68+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cache returned valid actor, bypassing verification and allowing request");
6969
return NGX_DECLINED;
7070
}
7171

7272
if (verification_status == FAILURE) {
73-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Actor previously failed verification, blocking request");
73+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cache returned invalid actor, bypassing verification and blocking request");
7474
return NGX_HTTP_FORBIDDEN;
7575
}
7676

7777
if (verification_status == ERROR) {
78-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "There was an error looking up the actor, failing open");
78+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cache error");
7979
return NGX_DECLINED;
8080
}
8181

82-
if (verification_status == NOT_FOUND) {
83-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Actor has not been verified, initiating verification process");
84-
}
85-
8682
ngx_int_t ret = ngx_http_bot_verifier_module_identifies_as_known_bot(r, loc_conf);
8783

8884
if (ret == NGX_OK) {
89-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Bot identity detected");
90-
ret = ngx_http_bot_verifier_module_verify_bot(r, loc_conf);
85+
ret = ngx_http_bot_verifier_module_verify_bot(r, loc_conf, address);
9186
if (ret == NGX_OK) {
92-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Verification successful");
93-
persist_verification_status(loc_conf->redis.connection, &address, ret, loc_conf->redis.expiry);
87+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Verification successful, allowing request");
88+
persist_verification_status(loc_conf->redis.connection, address, ret, loc_conf->redis.expiry);
9489
} else if (ret == NGX_DECLINED) {
95-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Verification failed");
96-
persist_verification_status(loc_conf->redis.connection, &address, ret, loc_conf->redis.expiry);
90+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Verification failed, blocking request");
91+
persist_verification_status(loc_conf->redis.connection, address, ret, loc_conf->redis.expiry);
9792
return NGX_HTTP_FORBIDDEN;
9893
}
99-
} else {
100-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Bot does not identify");
10194
}
10295

10396
return NGX_OK;

ngx_http_bot_verifier_verifier.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,20 @@ hostname_matches_provider_domain(ngx_http_request_t *r, char *hostname, ngx_http
4646
}
4747

4848
ngx_int_t
49-
ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r, ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
49+
ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r, ngx_http_bot_verifier_module_loc_conf_t *loc_conf, char *address)
5050
{
51-
ngx_str_t derived_address;
52-
ngx_int_t error = ngx_http_bot_verifier_module_determine_address(r, &derived_address);
53-
if (error == NGX_ERROR || error == NGX_DECLINED) {
54-
return NGX_ERROR;
55-
}
56-
5751
struct sockaddr_in sa;
5852
sa.sin_family = AF_INET;
59-
inet_pton(AF_INET, (const char *)derived_address.data, &(sa.sin_addr));
53+
inet_pton(AF_INET, (const char *)address, &(sa.sin_addr));
6054
char hostname[NI_MAXHOST];
6155

62-
error = getnameinfo((struct sockaddr *) &sa, sizeof(sa), hostname, sizeof(hostname), NULL, 0, NI_NAMEREQD);
63-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "result %d", error);
56+
int error = getnameinfo((struct sockaddr *) &sa, sizeof(sa), hostname, sizeof(hostname), NULL, 0, NI_NAMEREQD);
6457
if (error != 0) {
6558
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "getnameinfo() error: %s", gai_strerror(error));
6659
return NGX_DECLINED;
6760
}
6861

69-
ngx_int_t match_result = hostname_matches_provider_domain(r, (char *)hostname, loc_conf);
62+
ngx_int_t match_result = hostname_matches_provider_domain(r, hostname, loc_conf);
7063

7164
if (match_result == NGX_DECLINED) {
7265
return match_result;
@@ -83,9 +76,9 @@ ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r, ngx_http_bot_veri
8376
char *forward_result = inet_ntoa(forward->sin_addr);
8477

8578
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Forward Result %s", forward_result);
86-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Derived Address %s", derived_address.data);
79+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Actor Address %s", address);
8780

88-
if (strcmp((const char *)derived_address.data, forward_result) == 0) {
81+
if (strcmp((const char *)address, forward_result) == 0) {
8982
freeaddrinfo(result);
9083
return NGX_OK;
9184
} else {

ngx_http_bot_verifier_verifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef __NGX_HTTP_BOT_VERIFIER_VERIFIER_H__
22
#define __NGX_HTTP_BOT_VERIFIER_VERIFIER_H__
33

4-
ngx_int_t ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r, ngx_http_bot_verifier_module_loc_conf_t *loc_conf);
4+
ngx_int_t ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r, ngx_http_bot_verifier_module_loc_conf_t *loc_conf, char *address);
55

66
#endif

0 commit comments

Comments
 (0)