Skip to content

Commit

Permalink
Move provider construction to module initialization. Closes #8.
Browse files Browse the repository at this point in the history
This commit moves provider construction into module init. I am still
to entirely happy with this but at least it avoids provider
construction on every request. Still need to move the allocation to
use the ngx provided allocators to avoid having to free the memory on
process exit.
  • Loading branch information
abedra committed Sep 9, 2018
1 parent 71a96ca commit 9e39e92
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
24 changes: 23 additions & 1 deletion ngx_http_bot_verifier_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ngx_http_bot_verifier_address_tools.h"
#include "ngx_http_bot_verifier_identifier.h"
#include "ngx_http_bot_verifier_verifier.h"
#include "ngx_http_bot_verifier_provider.h"

ngx_module_t ngx_http_bot_verifier_module;

Expand Down Expand Up @@ -86,7 +87,7 @@ ngx_http_bot_verifier_module_handler(ngx_http_request_t *r)

if (ret == NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Bot identity detected");
ret = ngx_http_bot_verifier_module_verify_bot(r);
ret = ngx_http_bot_verifier_module_verify_bot(r, loc_conf);
if (ret == NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Verification successful");
persist_verification_status(loc_conf->redis.connection, address, ret, loc_conf->redis.expiry);
Expand Down Expand Up @@ -190,6 +191,27 @@ ngx_http_bot_verifier_module_create_loc_conf(ngx_conf_t *cf)
conf->redis.expiry = NGX_CONF_UNSET_UINT;
conf->redis.connection = NULL;

size_t len;

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);

char *bing_domains[1] = {"search.msn.com"};
len = sizeof(bing_domains) / sizeof(bing_domains[0]);
provider_t *bing = 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);

conf->provider_len = 3;
// TODO: use nginx allocation
conf->providers = malloc(sizeof(provider_t**) + conf->provider_len * sizeof(provider_t*));
conf->providers[0] = google;
conf->providers[1] = yahoo;
conf->providers[2] = bing;

return conf;
}

Expand Down
5 changes: 5 additions & 0 deletions ngx_http_bot_verifier_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#define FAILURE 3
#define ERROR 2

#include <hiredis/hiredis.h>
#include "ngx_http_bot_verifier_provider.h"

typedef struct {
ngx_str_t host;
ngx_uint_t port;
Expand All @@ -18,6 +21,8 @@ typedef struct {
typedef struct {
ngx_flag_t enabled;
redis_t redis;
size_t provider_len;
provider_t **providers;
} ngx_http_bot_verifier_module_loc_conf_t;

#endif
43 changes: 8 additions & 35 deletions ngx_http_bot_verifier_verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,13 @@
#include <ngx_core.h>
#include <ngx_http.h>

#include "ngx_http_bot_verifier_module.h"
#include "ngx_http_bot_verifier_address_tools.h"
#include "ngx_http_bot_verifier_provider.h"

ngx_int_t
hostname_matches_provider_domain(ngx_http_request_t *r, char *hostname)
hostname_matches_provider_domain(ngx_http_request_t *r, char *hostname, ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
{
// TODO: move this into module init so it's not done every time
size_t len;

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);

char *bing_domains[1] = {"search.msn.com"};
len = sizeof(bing_domains) / sizeof(bing_domains[0]);
provider_t *bing = 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);

provider_t *providers[] = { google, bing, yahoo };
size_t provider_len = sizeof(providers) / sizeof(providers[0]);
// END TODO


ngx_regex_t *re;
ngx_regex_compile_t rc;
u_char errstr[NGX_MAX_CONF_ERRSTR];
Expand Down Expand Up @@ -60,16 +41,12 @@ hostname_matches_provider_domain(ngx_http_request_t *r, char *hostname)
capture.data = ngx_hostname.data + captures[i];
capture.len = captures[i + 1] - captures[i];
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Capture: %V", &capture);
for (j = 0; j < provider_len; j++) {
for (j = 0; j < loc_conf->provider_len; j++) {
// TODO: This could be optimized capturing the name of the provider that matched and only iteration through that providers valid domains.
for (k = 0; k < providers[j]->len; k++) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Comparing %s against: %s", capture.data, providers[j]->valid_domains[k]);
if (ngx_strncmp(capture.data, providers[j]->valid_domains[k], strlen(providers[j]->valid_domains[k])) == 0) {
for (k = 0; k < loc_conf->providers[j]->len; k++) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Comparing %s against: %s", capture.data, loc_conf->providers[j]->valid_domains[k]);
if (ngx_strncmp(capture.data, loc_conf->providers[j]->valid_domains[k], strlen(loc_conf->providers[j]->valid_domains[k])) == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Found match for %V with %V", &ngx_hostname, &capture);
// TODO remove this once provider init moves to module init
free(google);
free(yahoo);
free(bing);
return NGX_OK;
}
}
Expand All @@ -78,15 +55,11 @@ hostname_matches_provider_domain(ngx_http_request_t *r, char *hostname)
}

ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Result does not match known domain");
// TODO remove this once provider init moves to module init
free(google);
free(yahoo);
free(bing);
return NGX_DECLINED;
}

ngx_int_t
ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r)
ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r, ngx_http_bot_verifier_module_loc_conf_t *loc_conf)
{
char dervied_address[INET_ADDRSTRLEN];
ngx_int_t error = ngx_http_bot_verifier_module_determine_address(r, dervied_address);
Expand All @@ -108,7 +81,7 @@ ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r)
}

ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Lookup hostname %s", &hostname);
ngx_int_t match_result = hostname_matches_provider_domain(r, (char *)hostname);
ngx_int_t match_result = hostname_matches_provider_domain(r, (char *)hostname, loc_conf);

if (match_result == NGX_DECLINED) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Match result %d", match_result);
Expand Down
2 changes: 1 addition & 1 deletion ngx_http_bot_verifier_verifier.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef __NGX_HTTP_BOT_VERIFIER_VERIFIER_H__
#define __NGX_HTTP_BOT_VERIFIER_VERIFIER_H__

ngx_int_t ngx_http_bot_verifier_module_verify_bot(ngx_http_request_t *r);
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);

#endif

0 comments on commit 9e39e92

Please sign in to comment.