-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_behavior_click_module.c
executable file
·379 lines (372 loc) · 12.5 KB
/
ngx_http_behavior_click_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* * Copyright (C) Eric Cai
* */
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <uuid/uuid.h>
#include "rdkafka.h"
#include "rdposix.h"
#define MAXLEN 4096
/* Module config */
typedef struct {
ngx_str_t serid;
ngx_str_t datadir;
ngx_str_t kafka;
ngx_str_t topic;
} ngx_http_behavior_click_loc_conf_t;
static FILE *logfp;
static ngx_http_behavior_click_loc_conf_t *cfg;
static rd_kafka_t *rk; /* Producer instance handle */
static rd_kafka_topic_t *rkt; /* Topic object */
static char *ngx_http_behavior_click(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_http_behavior_click_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_behavior_click_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static void ngx_http_behavior_click_worker_exit(ngx_cycle_t *cycle);
static ngx_int_t ngx_http_behavior_click_worker_init(ngx_cycle_t *cycle);
char *getUUID(char *str)
{
uuid_t uuid;
uuid_generate(uuid);
uuid_unparse(uuid, str);
return str;
}
/* Directives */
void ngx_mylog(const char* format, ... )
{
va_list args;
va_start (args, format);
vfprintf (logfp, format, args);
fflush(logfp);
va_end (args);
}
void ngx_fmylog(const char* format, ... )
{
va_list args;
va_start (args, format);
FILE *fp=fopen("/tmp/john.log","a+");
vfprintf (fp, format, args);
fflush(fp);
va_end (args);
fclose(fp);
fp=NULL;
}
static u_char *getCookie(ngx_list_t *h) {
ngx_uint_t y;
ngx_list_part_t *part = &(h->part);
ngx_table_elt_t *header = part->elts;
for (y = 0; ; y++) {
if (y >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
y = 0;
}
if (ngx_strcmp(header[y].key.data,"Cookie") == 0) {
//ngx_mylog("cookie: %s len: %d\n",header[y].value.data,header[y].value.len);
return header[y].value.data;
}
}
return (u_char *)"";
}
long int getMicroseconds() {
struct timeval tv;
if (gettimeofday(&tv,NULL) == -1)
return -1;
return tv.tv_sec*1000000 + tv.tv_usec;
}
static ngx_command_t ngx_http_behavior_click_commands[] = {
{
ngx_string("behavior_click_ser"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_behavior_click,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_behavior_click_loc_conf_t, serid),
NULL
},
{
ngx_string("behavior_click_datadir"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_behavior_click,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_behavior_click_loc_conf_t, datadir),
NULL
},
{
ngx_string("behavior_click_kafka"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_behavior_click,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_behavior_click_loc_conf_t, kafka),
NULL
},
{
ngx_string("behavior_click_topic"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_behavior_click,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_behavior_click_loc_conf_t, topic),
NULL
},
ngx_null_command
};
/* Http context of the module */
static ngx_http_module_t ngx_http_behavior_click_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_behavior_click_create_loc_conf, /* create location configration */
ngx_http_behavior_click_merge_loc_conf /* merge location configration */
};
/* Module */
ngx_module_t ngx_http_behavior_click_module = {
NGX_MODULE_V1,
&ngx_http_behavior_click_module_ctx, /* module context */
ngx_http_behavior_click_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_behavior_click_worker_init, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_behavior_click_worker_exit, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void ngx_http_behavior_click_worker_exit(ngx_cycle_t *cycle)
{
if (rk != NULL) {
rd_kafka_flush(rk, 1000 /* wait for max 10 seconds */);
rd_kafka_topic_destroy(rkt);
rd_kafka_destroy(rk);
}
if (logfp != NULL) {
fclose(logfp);
//ngx_fmylog("[process exit] logfp: %p closed\n",logfp);
logfp=NULL;
}
}
static void dr_msg_cb (rd_kafka_t *rk,
const rd_kafka_message_t *rkmessage, void *opaque) {
if (rkmessage->err)
ngx_fmylog("%% Message delivery failed: %s\n",rd_kafka_err2str(rkmessage->err));
}
static int connectKafka(const u_char *kafka,const u_char *topic) {
rd_kafka_conf_t *conf; /* Temporary configuration object */
char errstr[512]; /* librdkafka API error reporting buffer */
conf = rd_kafka_conf_new();
if (rd_kafka_conf_set(conf, "bootstrap.servers", (char *)kafka, errstr, sizeof(errstr)) != RD_KAFKA_CONF_OK) {
ngx_fmylog("%s\n", errstr);
return 1;
}
rd_kafka_conf_set_dr_msg_cb(conf, dr_msg_cb);
rk = rd_kafka_new(RD_KAFKA_PRODUCER, conf, errstr, sizeof(errstr));
if (!rk) {
ngx_fmylog("%% Failed to create new producer: %s\n", errstr);
return 1;
}
rkt = rd_kafka_topic_new(rk, (char *)topic, NULL);
if (!rkt) {
ngx_fmylog("%% Failed to create topic object: %s\n",rd_kafka_err2str(rd_kafka_last_error()));
rd_kafka_destroy(rk);
return 1;
}
return 0;
}
static int priductKafkaForstr(char *message) {
int i=0;
if (strlen(message) == 0) return 0;
retry:
if (rd_kafka_produce(rkt,RD_KAFKA_PARTITION_UA,RD_KAFKA_MSG_F_COPY,message,strlen(message),NULL, 0,NULL) == -1) {
ngx_fmylog("%% Failed to produce to topic %s: %s\n",rd_kafka_topic_name(rkt),rd_kafka_err2str(rd_kafka_last_error()));
if (rd_kafka_last_error() == RD_KAFKA_RESP_ERR__QUEUE_FULL) {
rd_kafka_poll(rk, 1000);
if (i++ >5) return -1;
goto retry;
}
}
rd_kafka_poll(rk, 0);
return 0;
}
static ngx_int_t ngx_http_behavior_click_worker_init(ngx_cycle_t *cycle) {
u_char path[MAXLEN];
time_t tmt=time(NULL);
struct tm *tmp=localtime(&tmt);
ngx_fmylog("[ERROR] %s/%s\n",cfg->kafka.data,cfg->topic.data);
ngx_sprintf(path,"%s/behavior_click_%s_%d%02d%02d.log%c",
cfg->datadir.data,cfg->serid.data,
tmp->tm_year+1900,tmp->tm_mon+1,tmp->tm_mday,'\0');
if ((logfp=fopen((char *)path,"a+")) == NULL)
return NGX_ERROR;
if (connectKafka(cfg->kafka.data,cfg->topic.data) == 1)
return NGX_ERROR;
return NGX_OK;
}
int getValue(u_char *source,u_char *key, u_char *value) {
int i=0;
u_char *http=(u_char *)strstr((char *)source,(char *)"HTTP/");
u_char *p=(u_char *)strstr((char *)source,(char *)key);
if (p != NULL && (p == source || *(p-1) == '&')) {
p=p+strlen((char *)key)+1;
for (i=0;*(p+i) != '&'&& p+i != http-1;i++)
value[i]=*(p+i);
}
value[i]='\0';
return i;
}
/* Handler function */
static ngx_int_t ngx_http_behavior_click_handler(ngx_http_request_t *r)
{
if(!(r->method & (NGX_HTTP_GET|NGX_HTTP_POST)))
{
return NGX_HTTP_NOT_ALLOWED;
}
ngx_chain_t out;
char arg_str[MAXLEN]={'\0'};
char ref_str[MAXLEN]={'\0'};
long tmlog=getMicroseconds();
char alldata[MAXLEN*2]={'\0'};
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
r->headers_out.status = NGX_HTTP_OK;
ngx_uint_t i,alliplen=0;
ngx_table_elt_t **arr=r->headers_in.x_forwarded_for.elts;
u_char allip[MAXLEN]={'\0'};
for (i=0;i<r->headers_in.x_forwarded_for.nelts;i++) {
if (i == 0) {
strcpy((char *)allip,(char *)arr[i]->value.data);
alliplen=arr[i]->value.len;
} else {
strcat((char *)allip,(char *)",");
strcpy((char *)allip,(char *)arr[i]->value.data);
alliplen=alliplen+arr[i]->value.len+1;
}
}
if (i!=0) {
allip[alliplen+1]='\0';
}
//client real ip output
u_char *rip;
if (r->headers_in.x_real_ip != NULL) {
rip=r->headers_in.x_real_ip->value.data;
} else {
r->connection->addr_text.data[r->connection->addr_text.len]='\0';
rip=r->connection->addr_text.data;
}
ngx_str_t mystr=ngx_string("ok");
if (r->args.len > 0) {
strcpy(arg_str,(char *)r->args.data);
arg_str[r->args.len]='\0';
}
if (r->headers_in.referer != NULL) {
strcpy(ref_str,(char *)r->headers_in.referer->value.data);
ref_str[r->headers_in.referer->value.len+1]='\0';
}
int newuser=0;
u_char *cookie=getCookie(&r->headers_in.headers);
if (!ngx_strstr(cookie,"BeUid")) {
ngx_table_elt_t* coks=ngx_list_push(&r->headers_out.headers);
coks->hash = 1;
char cookiestr[36];
char cookieskv[43];
getUUID(cookiestr);
sprintf(cookieskv,"BeUid=%s",cookiestr);
ngx_str_set(&coks->key, "Set-Cookie");
//ngx_str_set(&coks->value, cookieskv);
coks->value.data=(u_char *)&cookieskv;
coks->value.len=ngx_strlen(cookieskv);
newuser=1;
sprintf(alldata,"%s%c%s%c%s%c%ld%c%s%c%s%c%s%c%d%c%s",
r->headers_in.server.data,22,
rip,22,
ref_str,22,
tmlog,22,
allip,22,
r->headers_in.user_agent->value.data,22,
cookieskv,22,
newuser,22,
arg_str
);
} else {
sprintf(alldata,"%s%c%s%c%s%c%ld%c%s%c%s%c%s%c%d%c%s",
r->headers_in.server.data,22,
rip,22,
ref_str,22,
tmlog,22,
allip,22,
r->headers_in.user_agent->value.data,22,
cookie,22,
newuser,22,
arg_str
);
}
ngx_mylog("%s\n",alldata);
int kafka_status=priductKafkaForstr(alldata);
if (kafka_status == -1) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed Send message to kafka, work process will exit.");
ngx_http_behavior_click_worker_exit(NULL);
exit(1);
}
//==================================================
r->headers_out.content_length_n = 2;
out.buf = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if(out.buf == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.next = NULL;
out.buf->pos = mystr.data;
out.buf->last = mystr.data+2;
out.buf->memory = 1;
out.buf->last_buf = 1;
if(ngx_http_send_header(r) != NGX_OK)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
//ngx_mylog("http_in_Cookie: %s\n",getCookie2(&r->headers_in.headers,1));
//ngx_mylog("http_out_Cookie: %s\n",getCookie2(&r->headers_out.headers,2));
return ngx_http_output_filter(r, &out);
}
static char *
ngx_http_behavior_click(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_behavior_click_handler;
ngx_conf_set_str_slot(cf,cmd,conf);
return NGX_CONF_OK;
}
static void *
ngx_http_behavior_click_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_behavior_click_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_behavior_click_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->serid.len = 0;
conf->serid.data = NULL;
conf->datadir.len = 0;
conf->datadir.data = NULL;
conf->topic.len = 0;
conf->topic.data = NULL;
conf->kafka.len = 0;
conf->kafka.data = NULL;
return conf;
}
static char *
ngx_http_behavior_click_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_behavior_click_loc_conf_t *prev = parent;
ngx_http_behavior_click_loc_conf_t *conf = child;
ngx_conf_merge_str_value(conf->serid, prev->serid, "");
ngx_conf_merge_str_value(conf->datadir, prev->datadir, "");
ngx_conf_merge_str_value(conf->kafka, prev->kafka, "");
ngx_conf_merge_str_value(conf->topic, prev->topic, "");
cfg=conf;
return NGX_CONF_OK;
}