forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfwlist.c
executable file
·296 lines (231 loc) · 5.03 KB
/
gfwlist.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
#include <string.h>
#include "gfwlist.h"
#include "querydnsbase.h"
#include "excludedlist.h"
#include "downloader.h"
#include "readline.h"
#include "rwlock.h"
#include "utils.h"
static const char *GfwList = NULL;
static const char *File = NULL;
typedef struct _GFWListContainer{
StringChunk GFWList;
} GFWListContainer;
static volatile GFWListContainer *MainContainer = NULL;
static RWLock GFWListLock;
static BOOL ParseGfwListItem(char *Item, GFWListContainer *Container)
{
char *Itr = NULL;
if( strchr(Item, '/') != NULL || *Item == '@' || strchr(Item, '?') != NULL || *Item == '!' || strchr(Item, '.') == NULL || *Item == '[' )
{
return FALSE;
}
if( strchr(Item, '*') != NULL )
{
if( strstr("wikipedia.org", Item) == 0 )
{
Itr = strchr(Item + 13, '*');
if( Itr != NULL )
{
*Itr = '\0';
}
} else {
return FALSE;
}
}
if( *Item == '|' )
{
for(++Item; *Item == '|'; ++Item);
}
if( *Item == '.' )
{
++Item;
}
if( strncmp("http://", Item, 7) == 0 )
{
Item += 7;
}
if( strncmp("https://", Item, 8) == 0 )
{
Item += 8;
}
Itr = strchr(Item, '/');
if( Itr != NULL )
{
*Itr = '\0';
}
if( strchr(Item, '%') != NULL )
{
return FALSE;
}
if( MatchDomain(&(Container -> GFWList), Item, NULL) == FALSE )
{
StringChunk_Add(&(Container -> GFWList), Item, NULL, 0);
return TRUE;
} else {
return FALSE;
}
}
static int LoadGfwListFile(const char *File, BOOL NeedBase64Decode)
{
FILE *fp;
ReadLineStatus Status;
char Buffer[256];
int Count = 0;
GFWListContainer *Container;
if( (NeedBase64Decode == TRUE) && (Base64Decode(File) != 0) )
{
return -1;
}
fp = fopen(File, "r");
if( fp == NULL )
{
return -2;
}
Container = SafeMalloc(sizeof(GFWListContainer));
if( Container == NULL )
{
return -3;
}
if( StringChunk_Init(&(Container -> GFWList), NULL) != 0 )
{
return -4;
}
while(TRUE)
{
Status = ReadLine(fp, Buffer, sizeof(Buffer));
switch(Status)
{
case READ_FAILED_OR_END:
goto DONE;
break;
case READ_DONE:
if( ParseGfwListItem(Buffer, Container) == TRUE )
{
++Count;
}
break;
case READ_TRUNCATED:
INFO("GFWList Item is too long : %s\n", Buffer);
ReadLine_GoToNextLine(fp);
break;
}
}
DONE:
fclose(fp);
if( Count == 0 )
{
StringChunk_Free((StringChunk *)&(Container -> GFWList), TRUE);
SafeFree(Container);
return -4;
}
/* Evict old container */
RWLock_WrLock(GFWListLock);
if( MainContainer != NULL )
{
StringChunk_Free((StringChunk *)&(MainContainer -> GFWList), TRUE);
SafeFree((void *)MainContainer);
}
MainContainer = Container;
RWLock_UnWLock(GFWListLock);
return Count;
}
static int LoadGfwList_Thread(ConfigFileInfo *ConfigInfo)
{
int UpdateInterval = ConfigGetInt32(ConfigInfo, "GfwListUpdateInterval");
int RetryInterval = ConfigGetInt32(ConfigInfo, "GfwListRetryInterval");
BOOL NeedBase64Decode = ConfigGetBoolean(ConfigInfo, "GfwListBase64Decode");
int Count;
if( RetryInterval < 0 )
{
RetryInterval = 0;
}
while( TRUE )
{
INFO("Loading GFW List From %s ...\n", GfwList);
if( GetFromInternet(GfwList, File) != 0 )
{
ERRORMSG("Downloading GFW List failed. Waiting %d second(s) to try again.\n", RetryInterval);
SLEEP(RetryInterval * 1000);
} else {
INFO("GFW List saved at %s.\n", File);
Count = LoadGfwListFile(File, NeedBase64Decode);
switch( Count )
{
case -1:
case -2:
case -3:
break;
case -4:
ERRORMSG("Loading GFW List failed, cannot open file %s. Stop loading.\n", File);
return -1;
break;
default:
INFO("Loading GFW List completed. %d effective items.\n", Count);
break;
}
if( UpdateInterval < 0 )
{
return 0;
}
SLEEP(UpdateInterval * 1000);
}
}
return 0;
}
int GfwList_PeriodicWork(ConfigFileInfo *ConfigInfo)
{
ThreadHandle gt;
if( GfwList != NULL )
{
CREATE_THREAD(LoadGfwList_Thread, ConfigInfo, gt);
DETACH_THREAD(gt);
}
return 0;
}
int GfwList_Init(ConfigFileInfo *ConfigInfo, BOOL StartPeriodWork)
{
int Count;
GfwList = ConfigGetRawString(ConfigInfo, "GfwList");
if( GfwList == NULL )
{
return 0;
}
File = ConfigGetRawString(ConfigInfo, "GfwListDownloadPath");
RWLock_Init(GFWListLock);
if( FileIsReadable(File) )
{
INFO("Loading the existing GFW List ...\n");
Count = LoadGfwListFile(File, FALSE);
switch( Count )
{
case -1:
case -2:
case -3:
break;
case -4:
ERRORMSG("Loading the existing GFW List failed, cannot open file %s.\n", File);
break;
default:
INFO("Loading the existing GFW List completed. %d effective items.\n", Count);
break;
}
}
if( StartPeriodWork == TRUE )
{
GfwList_PeriodicWork(ConfigInfo);
}
return 0;
}
BOOL GfwList_Match(const char *Domain, int *HashValue)
{
BOOL Result;
if( MainContainer == NULL )
{
return FALSE;
}
RWLock_RdLock(GFWListLock);
Result = MatchDomain(&(MainContainer -> GFWList), Domain, HashValue);
RWLock_UnRLock(GFWListLock);
return Result;
}