-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathbackground.js
191 lines (156 loc) · 5.63 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
var localStorage = {};
function setProxyIcon() {
var icon = {
path: "images/off.png",
}
chrome.proxy.settings.get(
{'incognito': false},
function(config) {
if (config["value"]["mode"] == "system") {
chrome.action.setIcon(icon);
} else if (config["value"]["mode"] == "direct") {
chrome.action.setIcon(icon);
} else {
icon["path"] = "images/on.png";
chrome.action.setIcon(icon);
}
}
);
}
function gotoPage(url) {
var fulurl = chrome.runtime.getURL(url);
chrome.tabs.query({ url: fulurl }, function(tabs) {
if (tabs.length) {
chrome.tabs.update(tabs[0].id, { selected: true });
chrome.windows.update(tabs[0].windowId, { focused: true });
return;
}
chrome.tabs.create({url: url, active: true});
});
}
async function callbackFn(details, cb) {
console.log("%s onAuthRequiredCB", new Date(Date.now()).toISOString());
if (localStorage.proxySetting == undefined)
await getLocalStorage();
var proxySetting = JSON.parse(localStorage.proxySetting);
if (proxySetting){
var auth = proxySetting['auth'];
var username = auth['user'];
var password = auth['pass'];
}
if (proxySetting['auth']['user'] == '' &&
proxySetting['auth']['pass'] == '')
cb({});
cb({ authCredentials: {username: username, password: password} });
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['asyncBlocking'] );
chrome.runtime.onMessage.addListener((msg, sender, res) => {
if (msg.action != "authUpdate")
return;
(async () => {
console.log("%s receive authUpdate", new Date(Date.now()).toISOString());
if (localStorage.proxySetting == undefined)
await getLocalStorage();
var proxySetting = JSON.parse(localStorage.proxySetting);
proxySetting['auth'] = msg.data;
localStorage.proxySetting = JSON.stringify(proxySetting);
await chrome.storage.local.set(localStorage);
console.log("%s sending authUpdate response", new Date(Date.now()).toISOString());
res('done');
})();
return true;
});
var proxySetting = {
'pac_script_url' : {'http': '', 'https': '', 'file' : ''},
'pac_type' : 'file://',
'http_host' : '',
'http_port' : '',
'https_host' : '',
'https_port' : '',
'socks_host' : '',
'socks_port' : '',
'socks_type' : 'socks5',
'bypasslist' : '<local>,192.168.0.0/16,172.16.0.0/12,169.254.0.0/16,10.0.0.0/8',
'proxy_rule' : 'singleProxy',
'internal' : '',
'auth' : {'enable': '', 'user': '', 'pass': ''},
'rules_mode' : 'Whitelist'
}
var chinaList = ['*.cn']
localStorage.chinaList = JSON.stringify(chinaList);
function getBypass() {
var req = new XMLHttpRequest();
var url = "https://raw.github.com/henices/Chrome-proxy-helper/master/data/cn.bypasslist";
req.open('GET', url, true);
req.onreadystatechange = processResponse;
req.send(null);
function processResponse() {
if (req.readyState == 4 &&
req.status == 200) {
localStorage.chinaList = JSON.stringify(req.responseText.split(','));
} else
localStorage.chinaList = JSON.stringify(chinaList);
}
}
chrome.runtime.onInstalled.addListener(async details => {
var store = await getLocalStorage();
if (store.proxySetting == undefined) {
localStorage.proxySetting = JSON.stringify(proxySetting);
await chrome.storage.local.set(localStorage);
if (details.reason == "update") {
chrome.runtime.onMessage.addListener((msg, sender, res) => {
if (msg.action != "migrationDone")
return;
console.log("%s data migration done", new Date(Date.now()).toISOString());
chrome.offscreen.closeDocument();
});
console.log("%s starting data migration", new Date(Date.now()).toISOString());
chrome.offscreen.createDocument({
url: 'migration.html',
reasons: ['LOCAL_STORAGE'],
justification: 'Migrate storage data for MV2 to MV3',
});
}
}
if(details.reason == "install") {
gotoPage('options.html');
}
/*
else if(details.reason == "update") {
gotoPage('CHANGELOG');
}
*/
});
function getLocalStorage() {
console.trace("%s getLocalStorage", new Date(Date.now()).toISOString());
return chrome.storage.local.get(null).then(result => {
console.log("%s getLocalStorage: result = %O", new Date(Date.now()).toISOString(), result);
if (result.proxySetting != undefined) {
Object.assign(localStorage, result);
}
return result;
});
}
chrome.commands.onCommand.addListener(function(command) {
if (command == 'open-option')
gotoPage('options.html');
});
// sync extension settings from google cloud
//chrome.storage.sync.get('proxySetting', function(val) {
// if (typeof val.proxySetting !== "undefined")
// localStorage.proxySetting = val.proxySetting;
//});
chrome.proxy.onProxyError.addListener(function(details) {
console.log("fatal: ", details.fatal);
console.log("error: ", details.error);
console.log("details: ", details.details)
});
console.log("%s service worker initialized", new Date(Date.now()).toISOString());
setProxyIcon();
// sync bypass list from github.com
//getBypass();
//setInterval(function() { getBypass(); }, interval);
//var interval = 1000 * 60 * 60;