/* * Version 0.01 * 29.09.02 * */ /* * * Traf thief * * Tested on Apache 2.0.42 * Works only on Apache 2.x * * Outline: * - Redirects each %d request to spicified URL. * * * Module Directives: See README file or HTML documentation. * * ChangeLog: See Changelog for details * * Homepage http://web.god.net.ru/projects/mod_traf_thief/ * * Latest sources http://web.god.net.ru/projects/mod_traf_thief/dist/ * * Maintainer: * Yuri * gr_yuri@mail.ru * * Greets: * tf8 (silent@zolo.freelsd.net),delta(spn@g0v.ru) -- for C help * Alex Tatubalin (http://lexa.ru) -- mod_uid2.c was the best helper :) * * */ #include "httpd.h" #include "http_config.h" #include "http_log.h" #include "apr_strings.h" #include "apr_lib.h" #include "apr_general.h" #include "util_filter.h" #include "apr_buckets.h" #include "http_request.h" #define OFF 1 #define ON 2 #define DEFAULT_PERIOD 1000 module AP_MODULE_DECLARE_DATA traf_thief_module; #define OVER(field) (over->field?over->field:base->field) typedef struct { int enabled; char *url; int period; } thief_serv_rec; static int make_thief(request_rec *r); static const char *set_thief_enable(cmd_parms *cmd, void *mconfig, int arg); static const char *set_thief_period(cmd_parms *cmd, void *mconfig, const char *arg); static const char *set_thief_url(cmd_parms *cmd, void *mconfig, const char *arg); static void *make_thief_dir(apr_pool_t *p, char *d); static void *merge_thief_dir(apr_pool_t *p, void *basev, void *overridesv); static void register_hooks(apr_pool_t *p); static const command_rec thief_cmds[] = { AP_INIT_FLAG("ThiefEnable", set_thief_enable, NULL, ACCESS_CONF | RSRC_CONF, "Enable flag"), AP_INIT_TAKE1("ThiefPeriod", set_thief_period, NULL, ACCESS_CONF| RSRC_CONF, "Set period flag"), AP_INIT_TAKE1("ThiefURL", set_thief_url, NULL, ACCESS_CONF| RSRC_CONF, "Set traffic URL"), {NULL} }; static int make_thief(request_rec *r) { thief_serv_rec *tcfg = ap_get_module_config(r->per_dir_config, &traf_thief_module); const char *count; char *req_host; if (tcfg->enabled == ON) { req_host = apr_pstrdup(r->pool, r->server->server_hostname); srandom(time(NULL)); if ((random() % tcfg->period + 1) == tcfg->period) { apr_table_addn(r->headers_out, "Location",tcfg->url); return HTTP_MOVED_PERMANENTLY; } } return DECLINED; } static const char *set_thief_enable(cmd_parms *cmd, void *mconfig, int arg) { thief_serv_rec *tcfg = mconfig; tcfg->enabled = arg?ON:OFF; return NULL; } static const char *set_thief_period(cmd_parms *cmd, void *mconfig, const char *arg) { int i,check = 1; thief_serv_rec *tcfg = mconfig; for(i=0;i < strlen(arg);i++) if (!apr_isdigit(arg[i])) { check = 0; break; } tcfg->period = check ? atoi(arg) : DEFAULT_PERIOD; return NULL; } static const char *set_thief_url(cmd_parms *cmd, void *mconfig, const char *arg) { const char *url = arg; thief_serv_rec *tcfg = mconfig; if (!url) return "Thereis no URL to thief"; if (!ap_is_url(url)) return "Thief URL - non-URL"; tcfg->url = apr_pstrdup(cmd->pool, url); return NULL; } static void *make_thief_dir(apr_pool_t *p, char *d) { thief_serv_rec *tcfg = (thief_serv_rec *)apr_pcalloc (p, sizeof(thief_serv_rec)); tcfg->enabled = OFF; tcfg->period = 0; return tcfg; } static void * merge_thief_dir (apr_pool_t *p, void *basev, void *overridesv) { thief_serv_rec *base = (thief_serv_rec *)basev; thief_serv_rec *over = (thief_serv_rec *)overridesv; thief_serv_rec *tcfg; tcfg = (thief_serv_rec *) apr_pcalloc(p, sizeof(thief_serv_rec)); tcfg->enabled = OVER(enabled); tcfg->url = OVER(url); tcfg->period = OVER(period); return tcfg; } static void register_hooks(apr_pool_t *p) { ap_hook_fixups(make_thief, NULL, NULL, APR_HOOK_MIDDLE); } module AP_MODULE_DECLARE_DATA traf_thief_module = { STANDARD20_MODULE_STUFF, make_thief_dir, /* dir config creater */ merge_thief_dir, /* dir merger --- default is to override */ NULL, /* server config */ NULL, /* merge server config */ thief_cmds, /* command table */ register_hooks /* register_handlers */ };