-
-
Notifications
You must be signed in to change notification settings - Fork 420
/
Blacklist_p.java
620 lines (517 loc) · 26.6 KB
/
Blacklist_p.java
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Blacklist_p.java
// -----------------------
// part of YaCy
// (C) by Michael Peter Christen; mc@yacy.net
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
//
// This File is contributed by Alexander Schier
//
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// You must compile this file with
// javac -classpath .:../classes Blacklist_p.java
// if the shell's current path is HTROOT
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;
import net.yacy.cora.document.id.DigestURL;
import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.data.ListManager;
import net.yacy.data.WorkTables;
import net.yacy.kelondro.util.FileUtils;
import net.yacy.repository.Blacklist;
import net.yacy.repository.Blacklist.BlacklistType;
import net.yacy.search.Switchboard;
import net.yacy.search.query.SearchEventCache;
import net.yacy.server.serverObjects;
import net.yacy.server.serverSwitch;
public class Blacklist_p {
private final static String EDIT = "edit_";
private final static String DISABLED = "disabled_";
private final static String BLACKLIST = "blackLists_";
private final static String BLACKLIST_MOVE = "blackListsMove_";
private final static String BLACKLIST_SHARED = "BlackLists.Shared";
public static serverObjects respond(final RequestHeader header, final serverObjects post, @SuppressWarnings("unused") final serverSwitch env) {
// load all blacklist files located in the directory
List<String> dirlist = FileUtils.getDirListing(ListManager.listsPath, Blacklist.BLACKLIST_FILENAME_FILTER);
String blacklistToUse = null;
final serverObjects prop = new serverObjects();
prop.putHTML("blacklistEngine", Blacklist.getEngineInfo());
// do all post operations
if (post != null) {
final String action = post.get("action", "");
if(post.containsKey("testList")) {
prop.put("testlist", "1");
String urlstring = post.get("testurl", "");
if (!urlstring.startsWith("http://") &&
!urlstring.startsWith("https://") &&
!urlstring.startsWith("ftp://") &&
!urlstring.startsWith("smb://") &&
!urlstring.startsWith("file://")) {
urlstring = "http://"+urlstring;
}
DigestURL testurl;
try {
testurl = new DigestURL(urlstring);
} catch (final MalformedURLException e) {
testurl = null;
}
if(testurl != null) {
prop.putHTML("testlist_url",testurl.toString());
if (Switchboard.urlBlacklist.isListed(BlacklistType.CRAWLER, testurl)) {
prop.put("testlist_listedincrawler", "1");
}
if (Switchboard.urlBlacklist.isListed(BlacklistType.DHT, testurl)) {
prop.put("testlist_listedindht", "1");
}
if (Switchboard.urlBlacklist.isListed(BlacklistType.NEWS, testurl)) {
prop.put("testlist_listedinnews", "1");
}
if (Switchboard.urlBlacklist.isListed(BlacklistType.PROXY, testurl)) {
prop.put("testlist_listedinproxy", "1");
}
if (Switchboard.urlBlacklist.isListed(BlacklistType.SEARCH, testurl)) {
prop.put("testlist_listedinsearch", "1");
}
if (Switchboard.urlBlacklist.isListed(BlacklistType.SURFTIPS, testurl)) {
prop.put("testlist_listedinsurftips", "1");
}
} else {
prop.put("testlist_url","not valid");
}
}
if (post.containsKey("selectList")) {
blacklistToUse = post.get("selectedListName");
if (blacklistToUse != null && blacklistToUse.isEmpty()) {
blacklistToUse = null;
}
}
if (post.containsKey("createNewList")) {
/* ===========================================================
* Creation of a new blacklist
* =========================================================== */
blacklistToUse = post.get("newListName", "").trim();
if (blacklistToUse.isEmpty()) {
prop.put("LOCATION","");
return prop;
}
// Check if blacklist name only consists of "legal" characters.
// This is mainly done to prevent files from being written to other directories
// than the LISTS directory.
if (!blacklistToUse.matches("^[\\p{L}\\d\\+\\-_]+[\\p{L}\\d\\+\\-_.]*(\\.black){0,1}$")) {
prop.put("error", 1);
prop.putHTML("error_name", blacklistToUse);
blacklistToUse = null;
} else {
if (!blacklistToUse.endsWith(".black")) {
blacklistToUse += ".black";
}
if (!dirlist.contains(blacklistToUse)) {
try {
final File newFile = new File(ListManager.listsPath, blacklistToUse);
newFile.createNewFile();
// share the newly created blacklist
ListManager.updateListSet(BLACKLIST_SHARED, blacklistToUse);
// activate it for all known blacklist types
for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {
ListManager.updateListSet(supportedBlacklistType + ".BlackLists", blacklistToUse);
}
} catch (final IOException e) {/* */}
} else {
prop.put("error", 2);
prop.putHTML("error_name", blacklistToUse);
blacklistToUse = null;
}
// reload Blacklists
dirlist = FileUtils.getDirListing(ListManager.listsPath, Blacklist.BLACKLIST_FILENAME_FILTER);
}
} else if (post.containsKey("deleteList")) {
/* ===========================================================
* Delete a blacklist
* =========================================================== */
blacklistToUse = post.get("selectedListName");
if (blacklistToUse == null || blacklistToUse.isEmpty()) {
prop.put("LOCATION","");
return prop;
}
final File blackListFile = new File(ListManager.listsPath, blacklistToUse);
if(!blackListFile.delete()) {
ConcurrentLog.warn("Blacklist", "file "+ blackListFile +" could not be deleted!");
}
for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {
ListManager.removeFromListSet(supportedBlacklistType + ".BlackLists",blacklistToUse);
}
// remove it from the shared list
ListManager.removeFromListSet(BLACKLIST_SHARED, blacklistToUse);
blacklistToUse = null;
// reload Blacklists
dirlist = FileUtils.getDirListing(ListManager.listsPath, Blacklist.BLACKLIST_FILENAME_FILTER);
} else if (post.containsKey("activateList")) {
/* ===========================================================
* Activate/Deactivate a blacklist
* =========================================================== */
blacklistToUse = post.get("selectedListName", "").trim();
if (blacklistToUse == null || blacklistToUse.isEmpty()) {
prop.put("LOCATION", "");
return prop;
}
for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {
if (post.containsKey("activateList4" + supportedBlacklistType)) {
ListManager.updateListSet(supportedBlacklistType + ".BlackLists",blacklistToUse);
} else {
ListManager.removeFromListSet(supportedBlacklistType + ".BlackLists",blacklistToUse);
}
}
Switchboard.urlBlacklist.clear();
ListManager.reloadBlacklists();
} else if (post.containsKey("shareList")) {
/* ===========================================================
* Share a blacklist
* =========================================================== */
blacklistToUse = post.get("selectedListName", "").trim();
if (blacklistToUse == null || blacklistToUse.isEmpty()) {
prop.put("LOCATION", "");
return prop;
}
if (ListManager.listSetContains(BLACKLIST_SHARED, blacklistToUse)) {
// Remove from shared BlackLists
ListManager.removeFromListSet(BLACKLIST_SHARED, blacklistToUse);
} else { // inactive list -> enable
ListManager.updateListSet(BLACKLIST_SHARED, blacklistToUse);
}
} else if ("deleteBlacklistEntry".equals(action)) {
/* ===========================================================
* Delete an entry from a blacklist
* =========================================================== */
blacklistToUse = post.get("currentBlacklist", "").trim();
final String[] selectedBlacklistEntries = post.getAll("selectedEntry.*");
if (selectedBlacklistEntries.length > 0) {
String temp = null;
for (final String selectedBlacklistEntry : selectedBlacklistEntries) {
if ((temp = deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntry, header)) != null) {
prop.put("LOCATION", temp);
return prop;
}
}
}
Switchboard.urlBlacklist.clear();
ListManager.reloadBlacklists();
} else if (post.containsKey("addBlacklistEntry")) {
/* ===========================================================
* Add new entry to blacklist
* =========================================================== */
blacklistToUse = post.get("currentBlacklist", "").trim();
final String blentry = post.get("newEntry", "").trim();
// store this call as api call
ListManager.switchboard.tables.recordAPICall(post, "Blacklist_p.html", WorkTables.TABLE_API_TYPE_CONFIGURATION, "add to blacklist: " + blentry);
final String temp = addBlacklistEntry(blacklistToUse, blentry, header);
if (temp != null) {
prop.put("LOCATION", temp);
return prop;
}
Switchboard.urlBlacklist.clear();
ListManager.reloadBlacklists();
} else if ("moveBlacklistEntry".equals(action)) {
/* ===========================================================
* Move an entry from one blacklist to another
* =========================================================== */
blacklistToUse = post.get("currentBlacklist", "").trim();
final String targetBlacklist = post.get("targetBlacklist");
final String[] selectedBlacklistEntries = post.getAll("selectedEntry.*");
if (selectedBlacklistEntries != null &&
selectedBlacklistEntries.length > 0 &&
targetBlacklist != null &&
blacklistToUse != null &&
!targetBlacklist.equals(blacklistToUse)) {
String temp;
for (final String selectedBlacklistEntry : selectedBlacklistEntries) {
if ((temp = addBlacklistEntry(targetBlacklist, selectedBlacklistEntry, header)) != null) {
prop.put("LOCATION", temp);
return prop;
}
if ((temp = deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntry, header)) != null) {
prop.put("LOCATION", temp);
return prop;
}
}
}
Switchboard.urlBlacklist.clear();
ListManager.reloadBlacklists();
} else if ("editBlacklistEntry".equals(action)) {
/* ===========================================================
* Edit entry of a blacklist
* =========================================================== */
blacklistToUse = post.get("currentBlacklist", "").trim();
final String[] editedBlacklistEntries = post.getAll("editedBlacklistEntry.*");
// if edited entry has been posted, save changes
if (editedBlacklistEntries.length > 0) {
final String[] selectedBlacklistEntries = post.getAll("selectedBlacklistEntry.*");
if (selectedBlacklistEntries.length != editedBlacklistEntries.length) {
prop.put("LOCATION", "");
return prop;
}
String temp = null;
for (int i = 0; i < selectedBlacklistEntries.length; i++) {
if (!selectedBlacklistEntries[i].equals(editedBlacklistEntries[i])) {
if ((temp = deleteBlacklistEntry(blacklistToUse, selectedBlacklistEntries[i], header)) != null) {
prop.put("LOCATION", temp);
return prop;
}
if ((temp = addBlacklistEntry(blacklistToUse, editedBlacklistEntries[i], header)) != null) {
prop.put("LOCATION", temp);
return prop;
}
}
}
Switchboard.urlBlacklist.clear();
ListManager.reloadBlacklists();
prop.putHTML(DISABLED + EDIT + "currentBlacklist", blacklistToUse);
// else return entry to be edited
} else {
final String[] selectedEntries = post.getAll("selectedEntry.*");
if (selectedEntries != null && selectedEntries.length > 0 && blacklistToUse != null) {
for (int i = 0; i < selectedEntries.length; i++) {
prop.putHTML(DISABLED + EDIT + "editList_" + i + "_item", selectedEntries[i]);
prop.put(DISABLED + EDIT + "editList_" + i + "_count", i);
}
prop.putHTML(DISABLED + EDIT + "currentBlacklist", blacklistToUse);
prop.put(DISABLED + "edit", "1");
prop.put(DISABLED + EDIT + "editList", selectedEntries.length);
}
}
} else if ("selectRange".equals(action)) {
blacklistToUse = post.get("currentBlacklist");
}
}
// if we have not chosen a blacklist until yet we use the first file
if (blacklistToUse == null && dirlist != null && !dirlist.isEmpty()) {
blacklistToUse = dirlist.get(0);
}
// Read the blacklist items from file
if (blacklistToUse != null) {
int entryCount = 0;
final List<String> list = FileUtils.getListArray(new File(ListManager.listsPath, blacklistToUse));
// sort them
final String[] sortedlist = new String[list.size()];
Arrays.sort(list.toArray(sortedlist));
// display them
boolean dark = true;
int offset = 0;
int size = 50;
int to = 50;
if (post != null) {
offset = post.getInt("offset", 0);
size = post.getInt("size", 50);
to = offset + size;
}
if (offset > sortedlist.length || offset < 0) {
offset = 0;
}
if (to > sortedlist.length || size < 1) {
to = sortedlist.length;
}
for (int j = offset; j < to; ++j){
final String nextEntry = sortedlist[j];
if (nextEntry.isEmpty()) continue;
if (nextEntry.charAt(0) == '#') continue;
prop.put(DISABLED + EDIT + "Itemlist_" + entryCount + "_dark", dark ? "1" : "0");
dark = !dark;
prop.putHTML(DISABLED + EDIT + "Itemlist_" + entryCount + "_item", nextEntry);
prop.put(DISABLED + EDIT + "Itemlist_" + entryCount + "_count", entryCount);
entryCount++;
}
prop.put(DISABLED + EDIT + "Itemlist", entryCount);
// create selection of sublist
entryCount = 0;
int end = -1;
int start = -1;
if (sortedlist.length > 0) {
while (end < sortedlist.length) {
if (size > 0) {
start = entryCount * size;
end = (entryCount + 1) * size;
} else {
start = 0;
end = sortedlist.length;
}
prop.put(DISABLED + EDIT + "subListOffset_" + entryCount + "_value", start);
prop.put(DISABLED + EDIT + "subListOffset_" + entryCount + "_fvalue", start + 1);
if (end > sortedlist.length) {
end = sortedlist.length;
}
prop.put(DISABLED + EDIT + "subListOffset_" + entryCount + "_tvalue", end);
if (start == offset) {
prop.put(DISABLED + EDIT + "subListOffset_" + entryCount + "_selected", 1);
}
entryCount++;
}
} else {
prop.put(DISABLED + EDIT + "subListOffset_0_value", 0);
prop.put(DISABLED + EDIT + "subListOffset_0_fvalue", 0);
prop.put(DISABLED + EDIT + "subListOffset_0_tvalue", 0);
entryCount++;
}
prop.put(DISABLED + EDIT + "subListOffset", entryCount);
// create selection of list size
final int[] sizes = {10,25,50,100,250,-1};
for (int i = 0; i < sizes.length; i++) {
prop.put(DISABLED + EDIT + "subListSize_" + i + "_value", sizes[i]);
if (sizes[i] == -1) {
prop.put(DISABLED + EDIT + "subListSize_" + i + "_text", "all");
} else {
prop.put(DISABLED + EDIT + "subListSize_" + i + "_text", sizes[i]);
}
if (sizes[i] == size) {
prop.put(DISABLED + EDIT + "subListSize_" + i + "_selected", 1);
}
}
prop.put(DISABLED + EDIT + "subListSize", sizes.length);
}
// List BlackLists
int blacklistCount = 0;
int blacklistMoveCount = 0;
if (dirlist != null) {
for (final String element : dirlist) {
prop.putXML(DISABLED + BLACKLIST + blacklistCount + "_name", element);
prop.put(DISABLED + BLACKLIST + blacklistCount + "_selected", "0");
if (element.equals(blacklistToUse)) { //current List
prop.put(DISABLED + BLACKLIST + blacklistCount + "_selected", "1");
for (int blTypes=0; blTypes < BlacklistType.values().length; blTypes++) {
prop.putXML(DISABLED + "currentActiveFor_" + blTypes + "_blTypeName",BlacklistType.values()[blTypes].toString());
prop.put(DISABLED + "currentActiveFor_" + blTypes + "_checked",
ListManager.listSetContains(BlacklistType.values()[blTypes] + ".BlackLists", element) ? "0" : "1");
}
prop.put(DISABLED + "currentActiveFor", BlacklistType.values().length);
} else {
prop.putXML(DISABLED + EDIT + BLACKLIST_MOVE + blacklistMoveCount + "_name", element);
blacklistMoveCount++;
}
if (ListManager.listSetContains(BLACKLIST_SHARED, element)) {
prop.put(DISABLED + BLACKLIST + blacklistCount + "_shared", "1");
} else {
prop.put(DISABLED + BLACKLIST + blacklistCount + "_shared", "0");
}
int activeCount = 0;
for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {
if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists", element)) {
prop.putHTML(DISABLED + BLACKLIST + blacklistCount + "_active_" + activeCount + "_blTypeName", supportedBlacklistType.toString());
activeCount++;
}
}
prop.put(DISABLED + BLACKLIST + blacklistCount + "_active", activeCount);
blacklistCount++;
}
}
prop.put(DISABLED + "blackLists", blacklistCount);
prop.put(DISABLED + EDIT + "blackListsMove", blacklistMoveCount);
prop.putXML(DISABLED + "currentBlacklist", (blacklistToUse==null) ? "" : blacklistToUse);
prop.putXML(DISABLED + EDIT + "currentBlacklist", (blacklistToUse==null) ? "" : blacklistToUse);
prop.put("disabled", (blacklistToUse == null) ? "1" : "0");
return prop;
}
/**
* This method deletes a blacklist entry.
* @param blacklistToUse the name of the blacklist the entry is to be deleted from
* @param entry the entry that is to be deleted
* @param header
* @param supportedBlacklistTypes
* @return null if no error occurred, else a String to put into LOCATION
*/
private static String deleteBlacklistEntry(
final String blacklistToUse,
final String entry,
final RequestHeader header) {
String oldEntry = entry;
if (blacklistToUse == null || blacklistToUse.isEmpty()) {
return "";
}
if (oldEntry == null || oldEntry.isEmpty()) {
return header.get(HeaderFramework.CONNECTION_PROP_PATH) + "?selectList=&selectedListName=" + blacklistToUse;
}
// remove the entry from the running blacklist engine
int pos = oldEntry.indexOf('/',0);
String host = oldEntry.substring(0, pos);
String path = "";
if (pos > 0) {
path = oldEntry.substring(pos + 1);
}
for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {
if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists",blacklistToUse)) {
Switchboard.urlBlacklist.remove(supportedBlacklistType, blacklistToUse, host, path);
}
}
SearchEventCache.cleanupEvents(true);
return null;
}
/**
* This method adds a new entry to the chosen blacklist.
* @param blacklistToUse the name of the blacklist the entry is to be added to
* @param newEntry the entry that is to be added
* @param header
* @param supportedBlacklistTypes
* @return null if no error occurred, else a String to put into LOCATION
*/
private static String addBlacklistEntry(
final String blacklistToUse,
final String entry,
final RequestHeader header) {
String newEntry = entry;
if (blacklistToUse == null || blacklistToUse.isEmpty()) {
return "";
}
if (newEntry == null || newEntry.isEmpty()) {
return header.get(HeaderFramework.CONNECTION_PROP_PATH) + "?selectList=&selectedListName=" + blacklistToUse;
}
// ignore empty entries
if(newEntry == null || newEntry.isEmpty()) {
ConcurrentLog.warn("Blacklist", "skipped adding an empty entry");
return "";
}
if (newEntry.startsWith("http://") ){
newEntry = newEntry.substring(7);
} else if (newEntry.startsWith("https://")) {
newEntry = newEntry.substring(8);
}
if (newEntry.indexOf("*") < 0) {
// user did not use any wild cards and just submitted a word
newEntry = ".*" + newEntry + ".*/.*";
newEntry = ".*.*/.*" + newEntry + ".*";
} else {
int pos = newEntry.indexOf('/',0);
if (pos < 0) {
// add default empty path pattern
newEntry = newEntry + "/.*";
}
}
int pos = newEntry.indexOf('/',0);
String host = newEntry.substring(0, pos);
String path = newEntry.substring(pos + 1);
for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {
if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists",blacklistToUse)) {
Switchboard.urlBlacklist.add(supportedBlacklistType, blacklistToUse, host, path);
}
}
SearchEventCache.cleanupEvents(true);
return null;
}
}