forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
domainstatistic.c
executable file
·368 lines (295 loc) · 8.69 KB
/
domainstatistic.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
#include <string.h>
#include <time.h>
#include "common.h"
#include "stringchunk.h"
#include "domainstatistic.h"
#include "utils.h"
#include "timedtask.h"
#include "logs.h"
typedef struct _DomainInfo{
int Count;
int Refused;
int Hosts;
int Cache;
int Udp;
int Tcp;
int BlockedMsg;
} DomainInfo;
typedef struct _RankList{
const char *Domain;
DomainInfo *Info;
} RankList;
static EFFECTIVE_LOCK StatisticLock;
static StringChunk MainChunk;
static FILE *MainFile = NULL;
static unsigned long int InitTime_Num;
static char *PreOutput = NULL;
static char *PostOutput = NULL;
volatile static BOOL SkipStatistic = FALSE;
static int GetPreAndPost(ConfigFileInfo *ConfigInfo)
{
const char *TemplateFile = ConfigGetRawString(ConfigInfo, "DomainStatisticTempletFile");
const char *InsertionPosString = ConfigGetRawString(ConfigInfo, "StatisticInsertionPosition");
char *ip = NULL;
int FileSize;
char *FileContent = NULL;
if( TemplateFile == NULL )
{
return -1;
}
FileSize = GetFileSizePortable(TemplateFile);
if( FileSize <= 0 )
{
return -1;
}
FileContent = SafeMalloc(FileSize + 1);
if( FileContent == NULL )
{
return -1;
}
memset(FileContent, 0, FileSize + 1);
if( GetTextFileContent(TemplateFile, FileContent) != 0 )
{
goto EXIT;
}
ip = strstr(FileContent, InsertionPosString);
if( ip == NULL )
{
goto EXIT;
}
PreOutput = FileContent;
PostOutput = ip + strlen(InsertionPosString);
*ip = '\0';
return 0;
EXIT:
SafeFree(FileContent);
return -1;
}
static int DomainStatistic_Works(void *Unused, void *Unused2)
{
const char *Str;
int32_t Enum_Start;
DomainInfo *Info;
DomainInfo Sum;
unsigned long int GenerateTime_Num;
if( MainFile == NULL )
{
return 0;
}
rewind(MainFile);
memset(&Sum, 0, sizeof(DomainInfo));
GenerateTime_Num = time(NULL);
fprintf(MainFile, "%s", PreOutput);
fprintf(MainFile,
"<script type=\"text/javascript\">"
" var StartUpTime = %lu;"
" var LastStatistic = %lu;"
" var InfoArray = [",
InitTime_Num,
GenerateTime_Num
);
Enum_Start = 0;
EFFECTIVE_LOCK_GET(StatisticLock);
SkipStatistic = TRUE;
EFFECTIVE_LOCK_RELEASE(StatisticLock);
Str = StringChunk_Enum_NoWildCard(&MainChunk, &Enum_Start, (void **)&Info);
while( Str != NULL )
{
if( Info != NULL )
{
Sum.Count += Info->Count;
Sum.Refused += Info->Refused;
Sum.Hosts += Info->Hosts;
Sum.Cache += Info->Cache;
Sum.Udp += Info->Udp;
Sum.Tcp += Info->Tcp;
Sum.BlockedMsg += Info->BlockedMsg;
fprintf(MainFile,
"{"
"Domain:\"%s\","
"Total:%d,"
"RaF:%d,"
"Hosts:%d,"
"Cache:%d,"
"UDP:%d,"
"TCP:%d,"
"BlockedMsg:%d"
"},",
Str,
Info->Count,
Info->Refused,
Info->Hosts,
Info->Cache,
Info->Udp,
Info->Tcp,
Info->BlockedMsg
);
}
Str = StringChunk_Enum_NoWildCard(&MainChunk, &Enum_Start, (void **)&Info);
}
EFFECTIVE_LOCK_GET(StatisticLock);
SkipStatistic = FALSE;
EFFECTIVE_LOCK_RELEASE(StatisticLock);
fprintf(MainFile, "];");
fprintf(MainFile,
"var Sum = { Total : %d,"
"RaF : %d,"
"Hosts : %d,"
"Cache : %d,"
"UDP : %d,"
"TCP : %d,"
"BlockedMsg : %d"
"};"
"</script>",
Sum.Count,
Sum.Refused,
Sum.Hosts,
Sum.Cache,
Sum.Udp,
Sum.Tcp,
Sum.BlockedMsg
);
fprintf(MainFile, "%s", PostOutput);
fflush(MainFile);
return 0;
}
static void DomainStatistic_Cleanup(void)
{
if( PreOutput != NULL )
{
SafeFree(PreOutput);
if( MainFile != NULL )
{
fclose(MainFile);
}
}
EFFECTIVE_LOCK_DESTROY(StatisticLock);
}
int DomainStatistic_Init(ConfigFileInfo *ConfigInfo)
{
BOOL DomainStatistic = ConfigGetBoolean(ConfigInfo, "DomainStatistic");
int OutputInterval;
char FilePath[1024];
if( !DomainStatistic )
{
return 0;
}
OutputInterval = ConfigGetInt32(ConfigInfo, "StatisticUpdateInterval");
if( OutputInterval < 1 )
{
ERRORMSG("`StatisticUpdateInterval' should be positive.\n");
return 1;
}
if( GetPreAndPost(ConfigInfo) != 0 )
{
WARNING("Domain statistic init failed, it may due to lack of memory or templet file.\n");
return 0;
}
atexit(DomainStatistic_Cleanup);
GetFileDirectory(FilePath);
strcat(FilePath, PATH_SLASH_STR);
strcat(FilePath, "statistic.html");
MainFile = fopen(FilePath, "w");
if( MainFile == NULL )
{
ERRORMSG("Writing %s failed.\n", FilePath);
return 3;
}
EFFECTIVE_LOCK_INIT(StatisticLock);
StringChunk_Init(&MainChunk, NULL);
InitTime_Num = time(NULL);
SkipStatistic = FALSE;
TimedTask_Add(TRUE,
FALSE,
OutputInterval * 1000,
DomainStatistic_Works,
NULL,
NULL,
FALSE
);
return 0;
}
int DomainStatistic_Add(IHeader *h, StatisticType Type)
{
DomainInfo *ExistInfo;
if( MainFile == NULL || h == NULL )
{
return 0;
}
EFFECTIVE_LOCK_GET(StatisticLock);
if( SkipStatistic == FALSE )
{
if( StringChunk_Match(&MainChunk,
h->Domain,
&(h->HashValue),
(void **)&ExistInfo,
NULL,
NULL
)
== FALSE )
{
DomainInfo NewInfo;
memset(&NewInfo, 0, sizeof(DomainInfo));
switch( Type )
{
case STATISTIC_TYPE_REFUSED:
NewInfo.Count = 1;
NewInfo.Refused = 1;
break;
case STATISTIC_TYPE_HOSTS:
NewInfo.Count = 1;
NewInfo.Hosts = 1;
break;
case STATISTIC_TYPE_CACHE:
NewInfo.Count = 1;
NewInfo.Cache = 1;
break;
case STATISTIC_TYPE_UDP:
NewInfo.Count = 1;
NewInfo.Udp = 1;
break;
case STATISTIC_TYPE_TCP:
NewInfo.Count = 1;
NewInfo.Tcp = 1;
break;
case STATISTIC_TYPE_BLOCKEDMSG:
NewInfo.Count = 0;
NewInfo.BlockedMsg = 1;
break;
}
StringChunk_Add(&MainChunk, h->Domain, (const char *)&NewInfo, sizeof(DomainInfo));
} else {
if( ExistInfo != NULL )
{
switch( Type )
{
case STATISTIC_TYPE_REFUSED:
++(ExistInfo->Count);
++(ExistInfo->Refused);
break;
case STATISTIC_TYPE_HOSTS:
++(ExistInfo->Count);
++(ExistInfo->Hosts);
break;
case STATISTIC_TYPE_CACHE:
++(ExistInfo->Count);
++(ExistInfo->Cache);
break;
case STATISTIC_TYPE_UDP:
++(ExistInfo->Count);
++(ExistInfo->Udp);
break;
case STATISTIC_TYPE_TCP:
++(ExistInfo->Count);
++(ExistInfo->Tcp);
break;
case STATISTIC_TYPE_BLOCKEDMSG:
++(ExistInfo->BlockedMsg);
break;
}
}
}
}
EFFECTIVE_LOCK_RELEASE(StatisticLock);
return 0;
}