-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
221 lines (206 loc) · 4.79 KB
/
background.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
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
var options = {
debug : false,
autoreplace : false,
suffix: '.dnscache'
};
var dns = {};
chrome.storage.local.get('DNShosts', function (data) {
dns = data.DNShosts || {};
displayHostsToReplace();
});
function debug() {
if (!options.debug)
return;
console.log.apply(console, arguments);
}
function addIp(host, ip) {
if (!host)
return;
host = host.toLowerCase();
var data = dns[host];
if (!data || !data.ips) {
data = {
ips : []
};
dns[host] = data;
}
if (!data.ips.includes(ip))
data.ips.push(ip);
chrome.storage.local.set({
'DNShosts' : dns
});
}
function deleteIp(host, ip) {
if (!host)
return;
host = host.toLowerCase();
var data = dns[host];
if (!data || !data.ips)
return;
var index = data.ips.indexOf(ip);
if (index < 0)
return;
data.ips.splice(index, 1);
chrome.storage.local.set({
'DNShosts' : dns
});
}
function replaceHost(host, value) {
if (!host)
return;
host = host.toLowerCase();
if (typeof(value) === 'undefined')
value = true;
var data = dns[host];
if (!data) {
data = {
ips : []
};
dns[host] = data;
}
data.replaceHost = value;
chrome.storage.local.set({
'DNShosts' : dns
});
displayHostsToReplace();
}
function countHostsToReplace() {
var count = Object.keys(dns).filter(function (host) {
return dns[host].replaceHost;
}).length;
return count ? count.toString() : '';
}
function getHostData(url) {
var m = /^(?:(\w+):)?\/\/([^\/\?#]+)/.exec(url);
if (!m || !m[2]) {
debug('Could not find host for: ' + url);
return null;
}
var result = {
scheme:m[1],
initialHost:m[2],
host:m[2],
forceReplace:false
};
if (result.host.endsWith(options.suffix)) {
result.host=result.host.substr(0,result.host.length-options.suffix.length);
result.forceReplace=true;
}
return result;
}
function isNetError(error) {
switch (error) {
case 'net::ERR_NAME_NOT_RESOLVED':
case 'net::ERR_CONNECTION_REFUSED':
// case 'net::DNS_PROBE_FINISHED_NXDOMAIN':
return true;
default:
return false;
}
}
function setToRed(tabId) {
chrome.browserAction.setIcon({
path : 'icon_red.png',
tabId : tabId
});
}
function displayHostsToReplace(tabId) {
chrome.browserAction.setBadgeBackgroundColor({
tabId : tabId,
color : [190, 190, 230, 190]
});
chrome.browserAction.setBadgeText({
tabId : tabId,
text : countHostsToReplace()
});
}
chrome.browserAction.onClicked.addListener(function () {
chrome.tabs.create({
url : 'hosts.html'
});
});
chrome.webRequest.onCompleted.addListener(function (details) {
var hostData = getHostData(details.url);
if (!hostData)
return;
var ip = details.ip;
if (ip == hostData.host)
return;
if (ip) {
debug(hostData.host + ' resolved to ' + ip);
addIp(hostData.host, ip);
}
}, {
urls : ["<all_urls>"]
},
[]);
var requests={};
chrome.webRequest.onBeforeRequest.addListener(function (details) {
var hostData = getHostData(details.url);
if (!hostData)
return;
if (!options.autoreplace&&!hostData.forceReplace)
return;
var data = dns[hostData.host];
if (!data || !data.ips.length || (!data.replaceHost&&!hostData.forceReplace))
return;
if (hostData.scheme=='https') {
debug('cannot replace host with https protocol: '+hostData.initialHost);
return {
//cancel:true,
redirectUrl : chrome.extension.getURL('httpsErrorPage.html')
};
}
var ip = data.ips[data.ips.length - 1];
setToRed(details.tabId);
var newUrl = details.url.replace(hostData.initialHost, ip);
debug(hostData.host + ' had DNS resolution problems. Attempting to use ' + ip);
debug(details.url + ' -> ' + newUrl);
var result = {
redirectUrl : newUrl
};
requests[ip]=hostData;
return result;
}, {
urls : ["<all_urls>"]
},
["blocking"]);
chrome.webRequest.onBeforeSendHeaders.addListener(function (details) {
var hostData=getHostData(details.url);
if (!hostData) return;
var ip=hostData.host; //already replaced
hostData = requests[ip];
if (!hostData)
return;
if (!options.autoreplace&&!hostData.forceReplace) {
delete requests[ip];
return;
}
details.requestHeaders.push({ name: "Host", value: hostData.host });
return {requestHeaders: details.requestHeaders};
}, {
urls : ["<all_urls>"]
},
["blocking", "requestHeaders"]);
chrome.webRequest.onErrorOccurred.addListener(function (details) {
var hostData = getHostData(details.url);
var error = details.error;
if (!isNetError(error))
return;
debug('DNS error:', hostData.host);
var data = dns[hostData.host];
if (!data || !data.ips.length)
return;
var ip = data.ips[data.ips.length - 1];
if (ip == hostData.host) {
debug('Obsolete DNS resolution: ' + hostData.host + ' to ' + ip + '. Removing it.');
replaceHost(hostData.host, false);
deleteIp(hostData.host, ip);
} else {
debug('Previously working IP: ' + ip + '. Attempting to replace from now on');
replaceHost(hostData.host, true);
setToRed(details.tabId);
}
}, {
urls : ["<all_urls>"]
});