⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngx_http_userid_filter_module.c

📁 Nginx是一个高性能的HTTP和反向代理服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) Igor Sysoev */#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>#define NGX_HTTP_USERID_OFF   0#define NGX_HTTP_USERID_LOG   1#define NGX_HTTP_USERID_V1    2#define NGX_HTTP_USERID_ON    3/* 31 Dec 2037 23:55:55 GMT */#define NGX_HTTP_USERID_MAX_EXPIRES  2145916555typedef struct {    ngx_uint_t  enable;    ngx_int_t   service;    ngx_str_t   name;    ngx_str_t   domain;    ngx_str_t   path;    ngx_str_t   p3p;    time_t      expires;    u_char      mark;} ngx_http_userid_conf_t;typedef struct {    uint32_t    uid_got[4];    uint32_t    uid_set[4];    ngx_str_t   cookie;} ngx_http_userid_ctx_t;static void ngx_http_userid_get_uid(ngx_http_request_t *r,    ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf);static ngx_int_t ngx_http_userid_set_uid(ngx_http_request_t *r,    ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf);static ngx_int_t ngx_http_userid_add_variables(ngx_conf_t *cf);static ngx_int_t ngx_http_userid_variable(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data);static ngx_int_t ngx_http_userid_init(ngx_conf_t *cf);static void *ngx_http_userid_create_conf(ngx_conf_t *cf);static char *ngx_http_userid_merge_conf(ngx_conf_t *cf, void *parent,    void *child);static char *ngx_http_userid_domain(ngx_conf_t *cf, void *post, void *data);static char *ngx_http_userid_path(ngx_conf_t *cf, void *post, void *data);static char *ngx_http_userid_expires(ngx_conf_t *cf, ngx_command_t *cmd,    void *conf);static char *ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data);static char *ngx_http_userid_mark(ngx_conf_t *cf, ngx_command_t *cmd,    void *conf);static ngx_int_t ngx_http_userid_init_worker(ngx_cycle_t *cycle);static uint32_t  start_value;static uint32_t  sequencer_v1 = 1;static uint32_t  sequencer_v2 = 0x03030302;static u_char expires[] = "; expires=Thu, 31-Dec-37 23:55:55 GMT";static ngx_http_output_header_filter_pt  ngx_http_next_header_filter;static ngx_conf_enum_t  ngx_http_userid_state[] = {    { ngx_string("off"), NGX_HTTP_USERID_OFF },    { ngx_string("log"), NGX_HTTP_USERID_LOG },    { ngx_string("v1"), NGX_HTTP_USERID_V1 },    { ngx_string("on"), NGX_HTTP_USERID_ON },    { ngx_null_string, 0 }};static ngx_conf_post_handler_pt  ngx_http_userid_domain_p =    ngx_http_userid_domain;static ngx_conf_post_handler_pt  ngx_http_userid_path_p = ngx_http_userid_path;static ngx_conf_post_handler_pt  ngx_http_userid_p3p_p = ngx_http_userid_p3p;static ngx_command_t  ngx_http_userid_commands[] = {    { ngx_string("userid"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,      ngx_conf_set_enum_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_userid_conf_t, enable),      ngx_http_userid_state },    { ngx_string("userid_service"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,      ngx_conf_set_num_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_userid_conf_t, service),      NULL },    { ngx_string("userid_name"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,      ngx_conf_set_str_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_userid_conf_t, name),      NULL },    { ngx_string("userid_domain"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,      ngx_conf_set_str_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_userid_conf_t, domain),      &ngx_http_userid_domain_p },    { ngx_string("userid_path"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,      ngx_conf_set_str_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_userid_conf_t, path),      &ngx_http_userid_path_p },    { ngx_string("userid_expires"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,      ngx_http_userid_expires,      NGX_HTTP_LOC_CONF_OFFSET,      0,      NULL },    { ngx_string("userid_p3p"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,      ngx_conf_set_str_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_userid_conf_t, p3p),      &ngx_http_userid_p3p_p },    { ngx_string("userid_mark"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,      ngx_http_userid_mark,      NGX_HTTP_LOC_CONF_OFFSET,      0,      NULL },      ngx_null_command};static ngx_http_module_t  ngx_http_userid_filter_module_ctx = {    ngx_http_userid_add_variables,         /* preconfiguration */    ngx_http_userid_init,                  /* postconfiguration */    NULL,                                  /* create main configuration */    NULL,                                  /* init main configuration */    NULL,                                  /* create server configuration */    NULL,                                  /* merge server configuration */    ngx_http_userid_create_conf,           /* create location configration */    ngx_http_userid_merge_conf             /* merge location configration */};ngx_module_t  ngx_http_userid_filter_module = {    NGX_MODULE_V1,    &ngx_http_userid_filter_module_ctx,    /* module context */    ngx_http_userid_commands,              /* module directives */    NGX_HTTP_MODULE,                       /* module type */    NULL,                                  /* init master */    NULL,                                  /* init module */    ngx_http_userid_init_worker,           /* init process */    NULL,                                  /* init thread */    NULL,                                  /* exit thread */    NULL,                                  /* exit process */    NULL,                                  /* exit master */    NGX_MODULE_V1_PADDING};static ngx_str_t  ngx_http_userid_got = ngx_string("uid_got");static ngx_str_t  ngx_http_userid_set = ngx_string("uid_set");static ngx_int_tngx_http_userid_filter(ngx_http_request_t *r){    ngx_int_t                rc;    ngx_http_userid_ctx_t   *ctx;    ngx_http_userid_conf_t  *conf;    if (r != r->main) {        return ngx_http_next_header_filter(r);    }    conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);    if (conf->enable == NGX_HTTP_USERID_OFF) {        return ngx_http_next_header_filter(r);    }    ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_userid_ctx_t));    if (ctx == NULL) {        return NGX_ERROR;    }    ngx_http_set_ctx(r, ctx, ngx_http_userid_filter_module);    ngx_http_userid_get_uid(r, ctx, conf);    if (conf->enable == NGX_HTTP_USERID_LOG) {        return ngx_http_next_header_filter(r);    }    if (ctx->uid_got[3] != 0) {        if (conf->mark == '\0') {            return ngx_http_next_header_filter(r);        } else {            if (ctx->cookie.len > 23                && ctx->cookie.data[22] == conf->mark                && ctx->cookie.data[23] == '=')            {                return ngx_http_next_header_filter(r);            }        }    }    rc = ngx_http_userid_set_uid(r, ctx, conf);    if (rc != NGX_OK) {        return rc;    }    return ngx_http_next_header_filter(r);}static voidngx_http_userid_get_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx,    ngx_http_userid_conf_t *conf){    ngx_int_t          n;    ngx_str_t          src, dst;    ngx_table_elt_t  **cookies;    n = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &conf->name,                                          &ctx->cookie);    if (n == NGX_DECLINED) {        return;    }    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "uid cookie: \"%V\"", &ctx->cookie);    if (ctx->cookie.len < 22) {        cookies = r->headers_in.cookies.elts;        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                      "client sent too short userid cookie \"%V\"",                      &cookies[n]->value);        return;    }    src = ctx->cookie;    /*     * we have to limit the encoded string to 22 characters because     *  1) cookie may be marked by "userid_mark",     *  2) and there are already the millions cookies with a garbage     *     instead of the correct base64 trail "=="     */    src.len = 22;    dst.data = (u_char *) ctx->uid_got;    if (ngx_decode_base64(&dst, &src) == NGX_ERROR) {        cookies = r->headers_in.cookies.elts;        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                      "client sent invalid userid cookie \"%V\"",                      &cookies[n]->value);        return;    }    ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "uid: %08XD%08XD%08XD%08XD",                   ctx->uid_got[0], ctx->uid_got[1],                   ctx->uid_got[2], ctx->uid_got[3]);}static ngx_int_tngx_http_userid_set_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx,    ngx_http_userid_conf_t *conf){    u_char           *cookie, *p;    size_t            len;    ngx_str_t         src, dst;    ngx_table_elt_t  *set_cookie, *p3p;    /*     * TODO: in the threaded mode the sequencers should be in TLS and their     * ranges should be divided between threads     */    if (ctx->uid_got[3] == 0) {        if (conf->enable == NGX_HTTP_USERID_V1) {            if (conf->service == NGX_CONF_UNSET) {                ctx->uid_set[0] = 0;            } else {                ctx->uid_set[0] = conf->service;            }            ctx->uid_set[1] = (uint32_t) ngx_time();            ctx->uid_set[2] = start_value;            ctx->uid_set[3] = sequencer_v1;            sequencer_v1 += 0x100;        } else {            if (conf->service == NGX_CONF_UNSET) {                if (ngx_http_server_addr(r, NULL) != NGX_OK) {                    return NGX_ERROR;                }                ctx->uid_set[0] = htonl(r->in_addr);            } else {                ctx->uid_set[0] = htonl(conf->service);            }            ctx->uid_set[1] = htonl((uint32_t) ngx_time());            ctx->uid_set[2] = htonl(start_value);            ctx->uid_set[3] = htonl(sequencer_v2);            sequencer_v2 += 0x100;            if (sequencer_v2 < 0x03030302) {                sequencer_v2 = 0x03030302;            }        }    } else {        ctx->uid_set[0] = ctx->uid_got[0];        ctx->uid_set[1] = ctx->uid_got[1];        ctx->uid_set[2] = ctx->uid_got[2];        ctx->uid_set[3] = ctx->uid_got[3];    }    len = conf->name.len + 1 + ngx_base64_encoded_length(16) + conf->path.len;    if (conf->expires) {        len += sizeof(expires) - 1 + 2;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -