/* * Copyright 2018 Rubén Beltrán del Río * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ #include #include #include /* * Declarations */ /* Constants */ const ngx_uint_t WEEK_LENGTH = 7; const char * CLOSED_TOKEN = "closed"; const ngx_str_t TIME_REGEX = ngx_string("([0-9]{1,2})(?:\\:([0-9]{2}))?\\-([0-9]{1,2})(?:\\:([0-9]{2}))?"); /* Main Configuration Structure */ typedef struct { ngx_array_t *office_hours; } ngx_http_office_hours_conf_t; /* Lifecycle Functions For Module Context */ static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf); static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child); static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf); /* Configuration Handler */ static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf); /* Body Filter Storage */ static ngx_http_output_body_filter_pt ngx_http_next_body_filter; /* Utility Functions */ static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours); static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours); static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours); static ngx_uint_t get_day_of_week(time_t time); static ngx_uint_t get_seconds_of_day(time_t time); /* Compiled Regex */ ngx_regex_compile_t rc; /* * Module Definitions */ /* Module Directives */ static ngx_command_t ngx_http_office_hours_commands[] = { { ngx_string("office_hours"), NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 | NGX_CONF_TAKE2 | NGX_CONF_TAKE3 | NGX_CONF_TAKE4 | NGX_CONF_TAKE5 | NGX_CONF_TAKE6 | NGX_CONF_TAKE7, ngx_http_office_hours, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_office_hours_conf_t, office_hours), NULL }, ngx_null_command }; /* Module Context */ static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = { NULL, /* Preconfiguration */ ngx_http_office_hours_init, /* Postconfiguration */ NULL, /* Create main configuration */ NULL, /* Initialize main configuration */ NULL, /* Create server configuration */ NULL, /* Merge server configuration */ ngx_http_office_hours_create_conf, /* Create location configuration */ ngx_http_office_hours_merge_conf /* Merge location configuration */ }; /* Module Definition */ ngx_module_t ngx_http_office_hours_filter_module = { NGX_MODULE_V1, //Module Version &ngx_http_office_hours_filter_module_ctx, //Module context ngx_http_office_hours_commands, //Module commands NGX_HTTP_MODULE, //Module Type NULL, //Initialize Master NULL, //Initialize Module NULL, //Initialize Process NULL, //Initialize Thread NULL, //Exit Thread NULL, //Exit Process NULL, //Exit Master NGX_MODULE_V1_PADDING }; /* * Main Body Filter * If the current time is within office hours, it goes to the next * handler. Otherwise it returns 403 and the office hour listing. */ static ngx_int_t ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in) { ngx_uint_t ** parsed_office_hours; ngx_http_office_hours_conf_t *conf; conf = ngx_http_get_module_loc_conf(r, ngx_http_office_hours_filter_module); if (conf->office_hours == NULL) { ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Office hours disabled"); return ngx_http_next_body_filter(r, in); } parsed_office_hours = parse_office_hours(conf->office_hours); if (within_office_hours(parsed_office_hours)) { ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Within office hours"); return ngx_http_next_body_filter(r, in); } ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Outside office hours"); r->keepalive = 0; return NGX_HTTP_FORBIDDEN; } /* * Callback for `office_hours ` directive * Reads the configuration loaded from the config file(cf) * And writes it to the right place in the module configuration(conf) */ static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf) { char *conf_structure = conf; ngx_str_t *hours, *value; ngx_array_t **office_hours; ngx_uint_t i; /* Gets the array from the config structure using the defined * offset, and if the pointer is unset it creates a new one. * (The first element is the directive itself, so we should be * offset by 1) */ office_hours = (ngx_array_t **) (conf_structure + cmd->offset); if (*office_hours == NGX_CONF_UNSET_PTR) { *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1, sizeof(ngx_str_t)); if (*office_hours == NULL) { return NGX_CONF_ERROR; } } value = cf->args->elts; for (i = 1; i < cf->args->nelts; ++i) { hours = ngx_array_push(*office_hours); if (hours == NULL) { return NGX_CONF_ERROR; } *hours = value[i]; } return NGX_CONF_OK; } /* * Config Creator * Initializes the configuration structure */ static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf) { ngx_http_office_hours_conf_t *conf; conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t)); if (conf == NULL) { return NULL; } conf->office_hours = NGX_CONF_UNSET_PTR; return conf; } /* * Merge Config Values * Sets the defaults for the configuration and merges * with other configurations */ static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child) { ngx_http_office_hours_conf_t *prev = parent; ngx_http_office_hours_conf_t *conf = child; ngx_conf_merge_ptr_value(conf->office_hours, prev->office_hours, NULL); return NGX_CONF_OK; } /* * Postconfig Initialization Handler * Sets the request filter at the top of the chain */ static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf) { ngx_http_next_body_filter = ngx_http_top_body_filter; ngx_http_top_body_filter = ngx_http_office_hours_body_filter; rc.pattern = TIME_REGEX; rc.pool = cf->pool; if (ngx_regex_compile(&rc) != NGX_OK) { return NGX_ERROR; } return NGX_OK; } /* * Parse the office hour strings in the configuration file * to fill out the hours array (in seconds) */ static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours) { ngx_str_t *hours; ngx_uint_t ** parsed_office_hours; ngx_uint_t i, j; parsed_office_hours = malloc(7 * sizeof(ngx_uint_t *)); hours = office_hours->elts; /* * On the configuration file, the leftmost element * always applies to all remaining days, all others * are read from right to left. So first we will apply * the initial override, and then iterate based on the * number of overrides */ for (i = 0; i < WEEK_LENGTH + 1 - office_hours->nelts; ++i) { parsed_office_hours[i] = parse_office_hours_string(hours[0]); } for (i = 1; i < office_hours->nelts; ++i) { j = WEEK_LENGTH - office_hours->nelts + i; parsed_office_hours[j] = parse_office_hours_string(hours[i]); } return parsed_office_hours; } /* * Given a time string or the closed token, return a tuple * of numbers representing opening and closing hours */ static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours) { int captures[(1 + 4) * 3]; ngx_int_t n; ngx_uint_t * parsed_hours; parsed_hours = malloc(2 * sizeof(ngx_uint_t)); if(ngx_strcmp(office_hours.data, CLOSED_TOKEN) == 0) { parsed_hours[0] = 0; parsed_hours[1] = 0; return parsed_hours; } n = ngx_regex_exec(rc.regex, &office_hours, captures, (1 + 4) * 3); if (n >= 0) { /* Opening Hours */ parsed_hours[0] = 60 * 60 * ngx_atoi(&office_hours.data[captures[2]], captures[3] - captures[2]); parsed_hours[0] = parsed_hours[0] + 60 * ngx_atoi(&office_hours.data[captures[4]], captures[5] - captures[4]); parsed_hours[1] = 60 * 60 * ngx_atoi(&office_hours.data[captures[6]], captures[7] - captures[6]); parsed_hours[1] = parsed_hours[1] + 60 * ngx_atoi(&office_hours.data[captures[8]], captures[9] - captures[8]); return parsed_hours; } /* Non-matching strings count as open */ parsed_hours[0] = 0; parsed_hours[1] = 86400; return parsed_hours; } /* * Given an office hours array, it returns whether or not * it is currently within office hours. */ static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours) { time_t now; ngx_uint_t day_of_week, seconds_of_day; ngx_uint_t * current_hours; ngx_time_update(); now = ngx_time(); day_of_week = get_day_of_week(now); seconds_of_day = get_seconds_of_day(now); current_hours = office_hours[day_of_week]; return seconds_of_day >= current_hours[0] && seconds_of_day <= current_hours[1]; } /* * Calculate the day of the week given a timestamp */ static ngx_uint_t get_day_of_week(time_t time) { /* Epoch was thursday, so add 3 so we start on monday */ return (time / 86400 + 3) % 7; } /* * Calculate the number of seconds elapsed today */ static ngx_uint_t get_seconds_of_day(time_t time) { return time - (time / 86400) * 86400; }