-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (134 loc) · 3.61 KB
/
index.js
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
// Copyright (c) Kai Krause <kaikrause95@gmail.com>
var _this = module.exports = {};
var hostsSetup = require("./hostsSetup");
var helperFunctions = require("./util/helperFunctions");
var hosts = {};
var hostsData = [];
var hostsIndex = {};
var userblacklist = [];
var userblacklistIndex = {};
var userwhitelist = [];
function getHostData() {
hosts = hostsSetup.getHosts();
hostsData = hosts.hostsData;
hostsIndex = hosts.hostsIndex;
}
_this.blacklist = {
add : function(arr) {
if (arr) {
arr = hostsSetup.prepareArr(arr);
userblacklist = helperFunctions.uniqueArray([...userblacklist, ...arr]);
userblacklistIndex = hostsSetup.indexArr(userblacklist);
}
},
remove : function(arr) {
if (arr) {
arr = hostsSetup.prepareArr(arr);
for (let i = 0; i < arr.length; i++) {
let index;
while (index = userblacklistIndex.indexOf(arr[i]), index !== -1) {
userblacklistIndex.splice(index, 1);
}
}
userblacklistIndex = hostsSetup.indexArr(userblacklist);
}
},
reset : function() {
userblacklist = [];
userblacklistIndex = {};
},
get : function() {
return userblacklist;
}
}
_this.whitelist = {
add : function(arr) {
if (arr) {
arr = hostsSetup.prepareArr(arr);
userwhitelist = helperFunctions.uniqueArray([...userwhitelist, ...arr]);
}
},
remove : function(arr) {
if (arr) {
arr = hostsSetup.prepareArr(arr);
for (let i = 0; i < arr.length; i++) {
let index;
while (index = userwhitelist.indexOf(arr[i]), index !== -1) {
userwhitelist.splice(index, 1);
}
}
}
},
reset : function() {
userwhitelist = [];
},
get : function() {
return userwhitelist;
}
}
var updateTimer;
_this.init = async function(opts) {
return new Promise(async function (resolve,reject) {
try {
if (updateTimer) {
clearInterval(updateTimer);
updateTimer = null;
}
if (opts && opts.updateAfterSeconds) {
updateTimer = setInterval(() => {
_this.init(opts);
}, opts.updateAfterSeconds * 1000);
}
await hostsSetup.initialize(opts);
getHostData();
if (opts && opts.blacklist) _this.blacklist.add(opts.blacklist);
if (opts && opts.whitelist) _this.whitelist.add(opts.whitelist);
return resolve();
}
catch(e) {
return reject(e);
}
});
}
var httpPrefixReg = new RegExp(/^http(.)+\/\/(www\.|)/, "g");
_this.isBlacklisted = function(url) {
if (!url) throw "isBlacklisted requires a URL in string format";
url = url.replace(httpPrefixReg, "");
var checkIndex = function(arr, index) {
if (!index) return;
for (var i = 0; i < index.length; i++) {
var row = arr[index[i]];
if (userwhitelist.indexOf(row) === -1) {
if (row.startsWith("*.")) {
var wRow = row.substring(2);
if (!url.startsWith(wRow) && url.contains(wRow)) return true;
}
else if (url.startsWith(row)) return true;
}
}
}
var res = undefined;
if (res === undefined) res = checkIndex(hostsData, hostsIndex[url[0]]);
if (res === undefined) res = checkIndex(userblacklist, userblacklistIndex[url[0]]);
if (res !== undefined) return res;
else return false;
}
_this.filter = function(view, opts) {
const options = Object.assign({}, {
logger: undefined, // e.g. console.log
onRequest: undefined
}, opts);
view.webContents.session.webRequest.onBeforeRequest({urls: ["*://*/*"]}, (details, callback) => {
var isBlacklisted = _this.isBlacklisted(details.url);
if (options.onRequest) {
options.onRequest(details, callback, isBlacklisted);
}
else if (isBlacklisted) {
if (options.logger) options.logger("Hosts Blocked: " + details.url);
callback({ cancel: true });
}
else {
callback({ cancel: false });
}
});
}