-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
76 lines (68 loc) · 2.75 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
// Based off github.com/Tagide/chrome-bit-domain-extension
chrome.runtime.connectNative("enslite");
var transferUrl = "";
chrome.windows.onRemoved.addListener(function(windowId){
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:31313/resolver/dns/", false);
xhr.send();
});
function sleep(milliseconds, bithost) {
// synchronous XMLHttpRequests from Chrome extensions are not blocking event handlers. That's why we use this
// pretty little sleep function to try to get the IP of a .eth domain before the request times out.
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if (((new Date().getTime() - start) > milliseconds) || (sessionStorage.getItem(bithost) != null)){
break;
}
}
}
// run script when a request is about to occur
chrome.webRequest.onBeforeRequest.addListener(function (details) {
// get the parts of the url (hostname, port) by creating an 'a' element
var parser = document.createElement('a');
parser.href = details.url;
// Make sure the domain ends with .eth.
var tld = parser.hostname.slice(-3);
if (tld != 'eth') {
return;
};
var bithost = parser.hostname;
var port = (parser.protocol == "https:" ? "443" : "80");
var access = (parser.protocol == "https:" ? "HTTPS" : "PROXY");
// This .eth domain is not in cache, get the IP from the resolver
var xhr = new XMLHttpRequest();
var url = "http://localhost:31313/resolver/"+bithost;
// synchronous XMLHttpRequest is actually asynchronous
// check out https://developer.chrome.com/extensions/webRequest
xhr.open("GET", url, false);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// Get the ip address returned from the DNS proxy server.
var bitip = xhr.responseText;
// store the IP for .eth hostname in the local cache which is reset on each browser restart
sessionStorage.setItem(bithost, bitip);
}
}
xhr.send();
// block the request until the new proxy settings are set. Block for up to two seconds.
sleep(2000, bithost);
// 503 means were syncing with the ethereum network. Show loading screen.
if (xhr.status == 503) {
transferUrl = details.url;
return {redirectUrl: chrome.extension.getURL("sync.html")};
}
// Get the IP from the session storage.
var bitip = sessionStorage.getItem(bithost);
var config = {
mode: "pac_script",
pacScript: {
data: "function FindProxyForURL(url, host) {\n" +
" if (dnsDomainIs(host, '"+bithost+"'))\n" +
" return '"+access+" "+bitip+":"+port+"';\n" +
" return 'DIRECT';\n" +
"}"
}
};
chrome.proxy.settings.set({value: config, scope: 'regular'},function() {});
console.log('IP '+bitip+' for '+bithost+' found, config is changed: '+JSON.stringify(config));
}, { urls: ["<all_urls>"] }, ["blocking"]);