Skip to content

Commit

Permalink
Rename all methods to ensure uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
abedra committed Jun 25, 2019
1 parent f13c26a commit 907eab2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 37 deletions.
1 change: 0 additions & 1 deletion ngx_http_bot_verifier_address_tools.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef __NGX_HTTP_BOT_VERIFIER_ADDRESS_TOOLS_H__
#define __NGX_HTTP_BOT_VERIFIER_ADDRESS_TOOLS_H__

ngx_int_t remote_address(ngx_http_request_t *r, char *xff_header, char *address);
ngx_int_t ngx_http_bot_verifier_module_determine_address(ngx_http_request_t *r, char *address);

#endif
12 changes: 6 additions & 6 deletions ngx_http_bot_verifier_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "ngx_http_bot_verifier_cache.h"

ngx_int_t
check_connection(redisContext *context) {
ngx_http_bot_verifier_module_check_connection(redisContext *context) {
if (context == NULL || context->err) {
return NGX_ERROR;
}
Expand All @@ -29,7 +29,7 @@ check_connection(redisContext *context) {
}

void
cleanup_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
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);
Expand All @@ -38,9 +38,9 @@ cleanup_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
}

ngx_int_t
reset_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
ngx_http_bot_verifier_module_reset_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
{
cleanup_connection(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;
Expand All @@ -63,7 +63,7 @@ reset_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
}

ngx_int_t
lookup_verification_status(redisContext *context, char *address)
ngx_http_bot_verifier_module_lookup_verification_status(redisContext *context, char *address)
{
redisReply *reply;

Expand Down Expand Up @@ -93,7 +93,7 @@ lookup_verification_status(redisContext *context, char *address)
}

ngx_int_t
persist_verification_status(ngx_http_bot_verifier_module_loc_conf_t *loc_conf, char *address, ngx_int_t status)
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;

Expand Down
10 changes: 5 additions & 5 deletions ngx_http_bot_verifier_cache.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef __NGX_HTTP_BOT_VERIFIER_CACHE_H__
#define __NGX_HTTP_BOT_VERIFIER_CACHE_H__

ngx_int_t check_connection(redisContext *context);
void cleanup_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf);
ngx_int_t reset_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf);
ngx_int_t lookup_verification_status(redisContext *context, char *address);
ngx_int_t persist_verification_status(ngx_http_bot_verifier_module_loc_conf_t *loc_conf, char *address, ngx_int_t status);
ngx_int_t ngx_http_bot_verifier_module_check_connection(redisContext *context);
void ngx_http_bot_verifier_module_cleanup_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf);
ngx_int_t ngx_http_bot_verifier_module_reset_connection(ngx_http_bot_verifier_module_loc_conf_t *loc_conf);
ngx_int_t ngx_http_bot_verifier_module_lookup_verification_status(redisContext *context, char *address);
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);

#endif
28 changes: 14 additions & 14 deletions ngx_http_bot_verifier_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ ngx_http_bot_verifier_module_handler(ngx_http_request_t *r)
return NGX_DECLINED;
}

ngx_int_t connection_status = check_connection(loc_conf->redis.connection);
ngx_int_t connection_status = ngx_http_bot_verifier_module_check_connection(loc_conf->redis.connection);
if (connection_status == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "No cache connection, creating new connection");

if (loc_conf->redis.connection != NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cache connection error: %s", loc_conf->redis.connection->errstr);
}

connection_status = reset_connection(loc_conf);
connection_status = ngx_http_bot_verifier_module_reset_connection(loc_conf);

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

if (loc_conf->redis.connection != NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cache connection error: %s", loc_conf->redis.connection->errstr);
cleanup_connection(loc_conf);
ngx_http_bot_verifier_module_cleanup_connection(loc_conf);
}

return NGX_DECLINED;
Expand All @@ -58,7 +58,7 @@ ngx_http_bot_verifier_module_handler(ngx_http_request_t *r)
return NGX_DECLINED;
}

ngx_int_t verification_status = lookup_verification_status(loc_conf->redis.connection, address);
ngx_int_t verification_status = ngx_http_bot_verifier_module_lookup_verification_status(loc_conf->redis.connection, address);
if (verification_status == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Unable to lookup verification status, bypassing");
return NGX_DECLINED;
Expand All @@ -85,10 +85,10 @@ ngx_http_bot_verifier_module_handler(ngx_http_request_t *r)
ret = ngx_http_bot_verifier_module_verify_bot(r, loc_conf, address);
if (ret == NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Verification successful, allowing request");
persist_verification_status(loc_conf, address, ret);
ngx_http_bot_verifier_module_persist_verification_status(loc_conf, address, ret);
} else if (ret == NGX_DECLINED) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Verification failed, blocking request");
persist_verification_status(loc_conf, address, ret);
ngx_http_bot_verifier_module_persist_verification_status(loc_conf, address, ret);
return NGX_HTTP_FORBIDDEN;
}
}
Expand Down Expand Up @@ -197,38 +197,38 @@ ngx_http_bot_verifier_module_create_loc_conf(ngx_conf_t *cf)

char *google_domains[2] = {"google.com", "googlebot.com"};
len = sizeof(google_domains) / sizeof(google_domains[0]);
provider_t *google = make_provider("Google", google_domains, len);
ngx_http_bot_verifier_module_provider_t *google = ngx_http_bot_verifier_module_make_provider("Google", google_domains, len);

char *bing_domains[1] = {"search.msn.com"};
len = sizeof(bing_domains) / sizeof(bing_domains[0]);
provider_t *bing = make_provider("Bing", bing_domains, len);
ngx_http_bot_verifier_module_provider_t *bing = ngx_http_bot_verifier_module_make_provider("Bing", bing_domains, len);

char *yahoo_domains[1] = {"yahoo.com"};
len = sizeof(yahoo_domains) / sizeof(yahoo_domains[0]);
provider_t *yahoo = make_provider("Yahoo", yahoo_domains, len);
ngx_http_bot_verifier_module_provider_t *yahoo = ngx_http_bot_verifier_module_make_provider("Yahoo", yahoo_domains, len);

char *baidu_domains[1] = {"crawl.baidu.com"};
len = sizeof(baidu_domains) / sizeof(baidu_domains[0]);
provider_t *baidu = make_provider("Baidu", baidu_domains, len);
ngx_http_bot_verifier_module_provider_t *baidu = ngx_http_bot_verifier_module_make_provider("Baidu", baidu_domains, len);

char *yandex_domains[3] = {"yandex.com", "yandex.net", "yandex.ru"};
len = sizeof(yandex_domains) / sizeof(yandex_domains[0]);
provider_t *yandex = make_provider("Yandex", yandex_domains, len);
ngx_http_bot_verifier_module_provider_t *yandex = ngx_http_bot_verifier_module_make_provider("Yandex", yandex_domains, len);

conf->provider_len = 5;
conf->providers = ngx_pcalloc(cf->pool, sizeof(provider_t**) + conf->provider_len * sizeof(provider_t*));
conf->providers = ngx_pcalloc(cf->pool, sizeof(ngx_http_bot_verifier_module_provider_t**) + conf->provider_len * sizeof(ngx_http_bot_verifier_module_provider_t*));
conf->providers[0] = google;
conf->providers[1] = yahoo;
conf->providers[2] = bing;
conf->providers[3] = baidu;
conf->providers[4] = yandex;

ngx_str_t identifier_pattern = ngx_string("google|bing|yahoo|baidu|yandex");
conf->identifier_regex = make_regex(cf->pool, &identifier_pattern);
conf->identifier_regex = ngx_http_bot_verifier_module_make_regex(cf->pool, &identifier_pattern);

ngx_str_t domain_pattern = ngx_string("[^.]*\\.[^.]{2,3}(?:\\.[^.]{2,3})?$");
// ngx_str_t domain_pattern = ngx_string("\\.(.*)");
conf->domain_regex = make_regex(cf->pool, &domain_pattern);
conf->domain_regex = ngx_http_bot_verifier_module_make_regex(cf->pool, &domain_pattern);

return conf;
}
Expand Down
6 changes: 3 additions & 3 deletions ngx_http_bot_verifier_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ typedef struct {
ngx_uint_t read_timeout;
ngx_uint_t expiry;
redisContext *connection;
} redis_t;
} ngx_http_bot_verifier_module_redis_t;

typedef struct {
ngx_flag_t enabled;
ngx_flag_t repsheet_enabled;
redis_t redis;
ngx_http_bot_verifier_module_redis_t redis;
size_t provider_len;
provider_t **providers;
ngx_http_bot_verifier_module_provider_t **providers;
ngx_regex_compile_t *identifier_regex;
ngx_regex_compile_t *domain_regex;
} ngx_http_bot_verifier_module_loc_conf_t;
Expand Down
4 changes: 2 additions & 2 deletions ngx_http_bot_verifier_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "ngx_http_bot_verifier_provider.h"

provider_t *make_provider(char *name, char *valid_domains[], size_t len) {
provider_t *provider = (provider_t*) malloc(sizeof(provider_t) + sizeof(char*) * len);
ngx_http_bot_verifier_module_provider_t *ngx_http_bot_verifier_module_make_provider(char *name, char *valid_domains[], size_t len) {
ngx_http_bot_verifier_module_provider_t *provider = (ngx_http_bot_verifier_module_provider_t*) malloc(sizeof(ngx_http_bot_verifier_module_provider_t) + sizeof(char*) * len);
provider->name = name;
provider->len = len;

Expand Down
4 changes: 2 additions & 2 deletions ngx_http_bot_verifier_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ typedef struct {
size_t len;
const char *name;
const char *valid_domains[];
} provider_t;
} ngx_http_bot_verifier_module_provider_t;

provider_t *make_provider(char *name, char *valid_domains[], size_t len);
ngx_http_bot_verifier_module_provider_t *ngx_http_bot_verifier_module_make_provider(char *name, char *valid_domains[], size_t len);

#endif
2 changes: 1 addition & 1 deletion ngx_http_bot_verifier_regex.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ngx_http_bot_verifier_module.h"
#include "ngx_http_bot_verifier_regex.h"

ngx_regex_compile_t *make_regex(ngx_pool_t *pool, ngx_str_t *pattern) {
ngx_regex_compile_t *ngx_http_bot_verifier_module_make_regex(ngx_pool_t *pool, ngx_str_t *pattern) {
ngx_regex_compile_t *rc = ngx_pcalloc(pool, sizeof(ngx_regex_compile_t));
u_char errstr[NGX_MAX_CONF_ERRSTR];

Expand Down
2 changes: 1 addition & 1 deletion ngx_http_bot_verifier_regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include <ngx_core.h>

ngx_regex_compile_t *make_regex(ngx_pool_t *pool, ngx_str_t *pattern);
ngx_regex_compile_t *ngx_http_bot_verifier_module_make_regex(ngx_pool_t *pool, ngx_str_t *pattern);

#endif
4 changes: 2 additions & 2 deletions ngx_http_bot_verifier_verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "ngx_http_bot_verifier_provider.h"

ngx_int_t
hostname_matches_provider_domain(ngx_http_request_t *r, char *hostname, ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
ngx_http_bot_verifier_module_hostname_matches_provider_domain(ngx_http_request_t *r, char *hostname, ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
{
ngx_regex_t *re = loc_conf->domain_regex->regex;
ngx_regex_compile_t rc = *loc_conf->domain_regex;
Expand Down Expand Up @@ -59,7 +59,7 @@ ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r, ngx_http_bot_veri
return NGX_DECLINED;
}

ngx_int_t match_result = hostname_matches_provider_domain(r, hostname, loc_conf);
ngx_int_t match_result = ngx_http_bot_verifier_module_hostname_matches_provider_domain(r, hostname, loc_conf);

if (match_result == NGX_DECLINED) {
return match_result;
Expand Down

0 comments on commit 907eab2

Please sign in to comment.