-
Notifications
You must be signed in to change notification settings - Fork 302
/
excludedlist.c
377 lines (303 loc) · 7.71 KB
/
excludedlist.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
#include <string.h>
#include <time.h>
#include "excludedlist.h"
#include "querydnsbase.h"
#include "utils.h"
#include "downloader.h"
#include "readline.h"
#include "array.h"
#include "common.h"
#include "rwlock.h"
static int *DisabledTypes = NULL;
static StringList DisabledDomains;
static Array PositionsOfDisabled;
static StringList ExcludedDomains;
static Array PositionsOfExcluded;
static RWLock ExcludedListLock;
BOOL IsDisabledType(int Type){
int *Itr;
if( DisabledTypes == NULL )
{
return FALSE;
}
for(Itr = DisabledTypes; *Itr != 0; ++Itr){
if(*Itr == Type)
{
return TRUE;
}
}
return FALSE;
}
BOOL IsDisabledDomain(const char *Domain){
const char *Itr;
int loop;
int Count;
Count = Array_GetUsed(&PositionsOfDisabled);
for(loop = 0; loop != Count; ++loop){
Itr = *(char **)Array_GetBySubscript(&PositionsOfDisabled, loop);
if( Itr != NULL )
{
if( strchr(Itr, '*') != NULL || strchr(Itr, '?') != NULL )
{
if( WILDCARD_MATCH(Itr, Domain) == WILDCARD_MATCHED )
{
return TRUE;
}
} else {
if( strcmp(Itr, Domain + (strlen(Domain) - strlen(Itr))) == 0 )
{
return TRUE;
}
}
}
}
return FALSE;
}
BOOL IsExcludedDomain(const char *Domain)
{
const char *Itr;
int loop;
int Count;
RWLock_RdLock(ExcludedListLock);
Count = Array_GetUsed(&PositionsOfExcluded);
for(loop = 0; loop != Count; ++loop){
Itr = *(char **)Array_GetBySubscript(&PositionsOfExcluded, loop);
if( Itr != NULL )
{
if( strchr(Itr, '*') != NULL || strchr(Itr, '?') != NULL )
{
if( WILDCARD_MATCH(Itr, Domain) == WILDCARD_MATCHED )
{
RWLock_UnRLock(ExcludedListLock);
return TRUE;
}
} else {
if( strcmp(Itr, Domain + (strlen(Domain) - strlen(Itr))) == 0 )
{
RWLock_UnRLock(ExcludedListLock);
return TRUE;
}
}
}
}
RWLock_UnRLock(ExcludedListLock);
return FALSE;
}
static int DisableType(void)
{
int loop, Count = 1;
char Tmp[10], *TmpItr;
const char *Types = ConfigGetString(&ConfigInfo, "DisabledType");
if(Types == NULL) return 0;
for(loop = 0; Types[loop] != '\0'; ++loop)
if(Types[loop] == ',') ++Count;
DisabledTypes = (int *)SafeMalloc((Count + 1) * sizeof(*(DisabledTypes)));
DisabledTypes[Count--] = 0;
for(loop = 0, TmpItr = Tmp; ; ++loop){
if(Types[loop] == '\0'){
*TmpItr = '\0';
DisabledTypes[Count--] = atoi(Tmp);
break;
}
if(Types[loop] != ',')
*TmpItr++ = Types[loop];
else{
*TmpItr = '\0';
DisabledTypes[Count--] = atoi(Tmp);
TmpItr = Tmp;
}
}
return 0;
}
static int LoadDomains(StringList *List, const char *Domains)
{
if( StringList_Init(List, Domains, ',') >= 0 )
return -1;
else
return 0;
}
static BOOL ParseGfwListItem(char *Item)
{
if( strchr(Item, '/') != NULL || strchr(Item, '*') != NULL || *Item == '@' || strchr(Item, '?') != NULL || *Item == '!' || strchr(Item, '.') == NULL || *Item == '[' )
{
return FALSE;
}
if( *Item == '|' )
{
for(++Item; *Item == '|'; ++Item);
}
if( *Item == '.' )
{
++Item;
}
if( StringList_Find(&ExcludedDomains, Item) == NULL )
{
StringList_Add(&ExcludedDomains, Item);
return TRUE;
} else {
return FALSE;
}
}
static int LoadGfwListFile(const char *File)
{
FILE *fp = fopen(File, "r");
ReadLineStatus Status;
char Buffer[64];
int Count = 0;
if( fp == NULL )
{
return -1;
}
while(TRUE)
{
Status = ReadLine(fp, Buffer, sizeof(Buffer));
switch(Status)
{
case READ_FAILED_OR_END:
goto DONE;
break;
case READ_DONE:
if( ParseGfwListItem(Buffer) == TRUE )
{
++Count;
}
break;
case READ_TRUNCATED:
ReadLine_GoToNextLine(fp);
break;
}
}
DONE:
fclose(fp);
return Count;
}
static int InitPositionsArray(Array *a, StringList *s)
{
const char *Ptr = NULL;
if( Array_Init(a, sizeof(const char *), StringList_Count(s), FALSE, NULL) != 0 )
{
return -1;
}
for(Ptr = StringList_GetNext(s, Ptr); Ptr != NULL; Ptr = StringList_GetNext(s, Ptr))
{
Array_PushBack(a, &Ptr, NULL);
}
return 0;
}
int LoadGfwList_Thread(void *Unused)
{
int FlushTime = ConfigGetInt32(&ConfigInfo, "GfwListFlushTime");
int FlushTimeOnFailed = ConfigGetInt32(&ConfigInfo, "GfwListFlushTimeOnFailed");
const char *GfwList = ConfigGetString(&ConfigInfo, "GfwList");
const char *ExcludedList = ConfigGetString(&ConfigInfo, "ExcludedDomain");
const char *File = ConfigGetString(&ConfigInfo, "GfwListDownloadPath");
int Count;
if( GfwList == NULL )
{
return 0;
}
if( FlushTimeOnFailed < 0 )
{
FlushTimeOnFailed = INT_MAX;
}
while( TRUE )
{
INFO("Loading GFW List From %s ...\n", GfwList);
if( GetFromInternet(GfwList, File) != 0 )
{
ERRORMSG("Downloading GFW List failed. Waiting %d second(s) for retry.\n", FlushTimeOnFailed);
SLEEP(FlushTimeOnFailed * 1000);
} else {
INFO("GFW List saved at %s.\n", File);
if( Base64Decode(File) != 0 )
{
ERRORMSG("Decoding GFW List failed. Waiting %d second(s) for retry.\n", FlushTimeOnFailed);
SLEEP(FlushTimeOnFailed * 1000);
continue;
}
RWLock_WrLock(ExcludedListLock);
StringList_Free(&ExcludedDomains);
Array_Free(&PositionsOfExcluded);
LoadDomains(&ExcludedDomains, ExcludedList);
Count = LoadGfwListFile(File);
if( Count < 0 )
{
ERRORMSG("Loading GFW List failed, cannot open file %s.\n", File);
RWLock_UnWLock(ExcludedListLock);
goto END;
}
if( InitPositionsArray(&PositionsOfExcluded, &ExcludedDomains) != 0 )
{
RWLock_UnWLock(ExcludedListLock);
ERRORMSG("Loading GFW List failed. Waiting %d second(s) for retry.\n", FlushTimeOnFailed);
continue;
}
RWLock_UnWLock(ExcludedListLock);
INFO("Loading GFW List done. %d effective items.\n", Count);
END:
if( FlushTime < 0 )
{
return 0;
}
SLEEP(FlushTime * 1000);
}
}
}
int LoadGfwList(void)
{
ThreadHandle gt;
const char *GfwList = ConfigGetString(&ConfigInfo, "GfwList");
const char *File = ConfigGetString(&ConfigInfo, "GfwListDownloadPath");
const char *ExcludedList = ConfigGetString(&ConfigInfo, "ExcludedDomain");
char ProtocolStr[8] = {0};
int Count;
strncpy(ProtocolStr, ConfigGetString(&ConfigInfo, "PrimaryServer"), 3);
StrToLower(ProtocolStr);
if( GfwList == NULL )
{
return 0;
}
if( strcmp(ProtocolStr, "udp") != 0 )
{
ERRORMSG("Cannot load GFW List when `PrimaryServer' is not udp.\n");
return -1;
}
if( !FileIsReadable(File) )
{
goto END;
}
INFO("Loading the existing GFW List ...\n");
RWLock_WrLock(ExcludedListLock);
StringList_Free(&ExcludedDomains);
Array_Free(&PositionsOfExcluded);
LoadDomains(&ExcludedDomains, ExcludedList);
Count = LoadGfwListFile(File);
if( Count < 0 )
{
RWLock_UnWLock(ExcludedListLock);
goto END;
}
if( InitPositionsArray(&PositionsOfExcluded, &ExcludedDomains) != 0 )
{
RWLock_UnWLock(ExcludedListLock);
goto END;
}
RWLock_UnWLock(ExcludedListLock);
INFO("Loading GFW List done. %d effective items.\n", Count);
END:
CREATE_THREAD(LoadGfwList_Thread, NULL, gt);
DETACH_THREAD(gt);
return 0;
}
int ExcludedList_Init(void)
{
DisabledTypes = NULL;
LoadDomains(&DisabledDomains, ConfigGetString(&ConfigInfo, "DisabledDomain"));
LoadDomains(&ExcludedDomains, ConfigGetString(&ConfigInfo, "ExcludedDomain"));
DisableType();
InitPositionsArray(&PositionsOfExcluded, &ExcludedDomains);
InitPositionsArray(&PositionsOfDisabled, &DisabledDomains);
RWLock_Init(ExcludedListLock);
INFO("Excluded & Disabled list initialized.\n");
return 0;
}