-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-logs.cpp
More file actions
188 lines (163 loc) · 4.79 KB
/
read-logs.cpp
File metadata and controls
188 lines (163 loc) · 4.79 KB
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
#include "logline.hpp"
#include "cookies_utils.hpp"
#include "typedefs2.hpp"
#include <unistd.h> // for write
#include <fcntl.h> // for O_NOATIME etc
#include <cstdio> // for printf
#include <cstring> // for memset
#include <cstdlib> // for malloc
#include <arpa/inet.h> // for inet_ntop
enum {
AGG_NONE,
AGG_IP,
AGG_PATH,
AGG_HOUR,
AGG_METHOD,
AGG_N
};
struct UserCookie {
char value[user_cookie_len];
};
int main(const int argc, const char* argv[]){
in_addr_t ignore_localhost_address = 0;
unsigned aggregate_by = 0;
bool verbose = false;
if (argc < 2){
goto help;
}
for (unsigned i = 1; i < argc-1; ++i){
if (argv[i][0] != '-'){
goto help;
}
if (argv[i][1] == '\0'){
goto help;
}
if (argv[i][2] != '\0'){
goto help;
}
switch(argv[i][1]){
/*case 'a': {
++i;
const char* const arg = argv[i];
if (arg == nullptr)
goto help;
switch(arg[0]){
case 'i':
aggregate_by = AGG_NONE;
break;
case 'p':
aggregate_by = AGG_PATH;
break;
case 'm':
aggregate_by = AGG_METHOD;
break;
case 'h':
aggregate_by = AGG_HOUR;
break;
default:
goto help;
}
break;
}*/
case 'v': {
verbose = true;
break;
}
case 'l': {
const int rc = inet_pton(AF_INET, "127.0.0.1", &ignore_localhost_address);
if (rc <= 0){
write(2, "inet_pton error\n", 16);
return 1;
}
break;
}
default:
goto help;
}
}
if (false){
help:
write(
2,
"USAGE: [[OPTIONS]] [log_filepath]\n"
"\n"
"OPTIONS:\n"
" -a [WHICH]\n"
" Aggregate by WHICH:\n"
" ip\n"
" path\n"
" method\n"
" hour\n"
" -l Ignore localhost requests\n"
" -v Verbose\n",
154
);
return 1;
}
const char* const logfile_fp = argv[argc-1];
const int logfile_fd = open(logfile_fp, O_NOATIME|O_RDONLY);
if (logfile_fd == -1){
[[unlikely]]
write(1, "Cannot open file\n", 17);
}
unsigned n_response_names;
read(logfile_fd, &n_response_names, sizeof(unsigned));
char* const all_response_names = reinterpret_cast<char*>(malloc(n_response_names*100));
memset(all_response_names, 0, n_response_names*100);
for (unsigned i = 0; i < n_response_names; ++i){
unsigned char response_name_len;
read(logfile_fd, &response_name_len, sizeof(unsigned char));
read(logfile_fd, all_response_names + 100*i, response_name_len);
printf("Registered response name: [%u] %.*s\n", (unsigned)response_name_len, (int)response_name_len, all_response_names + 100*i);
}
unsigned n_users;
read(logfile_fd, &n_users, sizeof(unsigned));
unsigned usernames_buf_len;
read(logfile_fd, &usernames_buf_len, sizeof(unsigned));
char* const usernames_buf = reinterpret_cast<char*>(malloc(usernames_buf_len));
if (usernames_buf == nullptr){
write(2, "Cannot allocate memory\n", 23);
return 1;
}
read(logfile_fd, usernames_buf, usernames_buf_len);
UserCookie* const user_cookies = reinterpret_cast<UserCookie*>(malloc(n_users*sizeof(UserCookie)));
for (unsigned i = 0; i < n_users; ++i){
read(logfile_fd, user_cookies[i].value, user_cookie_len);
}
while(true){
if (read(logfile_fd, logline, logline_sz) != logline_sz){
write(1, "Reached end of file or unexpected end\n", 38);
break;
}
const time_t t = reinterpret_cast<time_t*>(logline)[0];
struct tm* local_time = gmtime(&t);
const int hour = local_time->tm_hour;
const int mins = local_time->tm_min;
const in_addr_t ip_address = reinterpret_cast<in_addr_t*>(logline+logline_ipaddrindx)[0];
if (ip_address == ignore_localhost_address)
continue;
const unsigned custom_strview_size = reinterpret_cast<unsigned*>(logline+logline_datetime_sz)[0];
const unsigned response_indx = logline[logline_respindxindx];
const bool keepalive = logline[logline_keepaliveindx];
const char* const logline_reqheaders = logline + logline_reqheadersindx;
char _buf[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &ip_address, _buf, INET_ADDRSTRLEN);
unsigned user_indx = n_users;
const char* cookies_startatspace = get_cookies_startatspace(logline_reqheaders, logline_reqheaders+logline_reqheaders_len);
if (cookies_startatspace != nullptr){
cookies_startatspace = find_start_of_u_cookie(cookies_startatspace);
if (cookies_startatspace[2] != '\r'){
const char* const user_cookie = cookies_startatspace + 3;
for (user_indx = 0; user_indx < n_users; ++user_indx){
if (compare_secret_path_hashes(user_cookies[user_indx].value, user_cookie)){
break;
}
}
}
}
if (verbose)
printf("%2d:%2d %.*s user=%u response=%s custom_strview_size %u keepalive=%u\n%.*s\n\n", hour, mins, (int)INET_ADDRSTRLEN, _buf, user_indx, all_response_names+100*response_indx, custom_strview_size, (unsigned)keepalive, (int)logline_reqheaders_len, logline_reqheaders);
}
close(logfile_fd);
return 0;
}