-
Notifications
You must be signed in to change notification settings - Fork 78
/
worker.js
144 lines (135 loc) · 3.85 KB
/
worker.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
/* globals Tor, proxy, privacy, ui */
'use strict';
self.importScripts('EventEmitter.js', 'tor.js', 'proxy.js', 'privacy.js', 'ui.js');
const prefs = {
'webrtc': /Firefox/.test(navigator.userAgent) ? 3 : 2,
// 0: turn on when tor is active and turn off when tor is disabled;
// 1: turn on when browser starts and do not turn off when tor is disabled
// -1: ignore changing
'policy.webrtc': 0,
'policy.proxy': 0,
'auto-run': false,
'directory': null
};
const tor = new Tor({
directory: ''
});
// get external IP address
tor.on('status', status => {
ui.emit('title', {status});
if (status === 'connected') {
tor.getIP();
}
});
// Set proxy
tor.on('status', s => {
if (s === 'connected') {
if (prefs['policy.proxy'] === 0) {
proxy.set(tor.info);
}
if (prefs['policy.webrtc'] === 0) {
privacy.set(prefs.webrtc);
}
}
else if (s === 'disconnected') {
if (prefs['policy.proxy'] === 0) {
proxy.reset();
}
if (prefs['policy.webrtc'] === 0) {
privacy.reset();
}
}
});
// ip changes
tor.on('ip', ip => ui.emit('title', {ip}));
//
proxy.addListener('change', bol => ui.emit('title', {
proxy: bol ? 'SOCKS' : 'default'
}));
chrome.storage.local.get(prefs, p => {
Object.assign(prefs, p);
// directory
tor.directory = p.directory;
// auto run?
if (prefs['auto-run']) {
tor.refresh();
}
// if (prefs.policy.proxy === 1) {
// privacy.set(prefs.proxy);
// }
if (prefs['policy.webrtc'] === 1) {
privacy.set(prefs.webrtc);
}
});
// logs
proxy.addListener('change', s => {
tor.emit('stdout', `Proxy status is "${s}"`);
});
privacy.addListener('change', (type, state) => {
tor.emit('stdout', `Protection: module -> ${type}, status -> ${state}`);
});
chrome.runtime.onMessage.addListener((request, sender, response) => {
if (request.method === 'popup-command') {
if (request.cmd) {
tor.command(request.cmd);
}
}
else if (request.method === 'popup-action') {
if (request.cmd === 'connection') {
if (request.action === 'disconnect') {
tor.disconnect();
}
else {
if (prefs.directory) {
tor.refresh();
}
else {
ui.notification('Tor Bundle path is not set in the options page');
chrome.runtime.openOptionsPage();
}
}
}
}
else if (request.method === 'popup-report') {
response({
stdout: tor.info.stdout,
status: tor.info.status
});
}
});
// pref updates
chrome.storage.onChanged.addListener(ps => {
Object.keys(ps).forEach(p => {
prefs[p] = ps[p].newValue;
if (p === 'directory') {
tor.directory = ps.directory.newValue;
}
});
});
/* FAQs & Feedback */
{
const {management, runtime: {onInstalled, setUninstallURL, getManifest}, storage, tabs} = chrome;
if (navigator.webdriver !== true) {
const page = getManifest().homepage_url;
const {name, version} = getManifest();
onInstalled.addListener(({reason, previousVersion}) => {
management.getSelf(({installType}) => installType === 'normal' && storage.local.get({
'faqs': true,
'last-update': 0
}, prefs => {
if (reason === 'install' || (prefs.faqs && reason === 'update')) {
const doUpdate = (Date.now() - prefs['last-update']) / 1000 / 60 / 60 / 24 > 45;
if (doUpdate && previousVersion !== version) {
tabs.query({active: true, currentWindow: true}, tbs => tabs.create({
url: page + '?version=' + version + (previousVersion ? '&p=' + previousVersion : '') + '&type=' + reason,
active: reason === 'install',
...(tbs && tbs.length && {index: tbs[0].index + 1})
}));
storage.local.set({'last-update': Date.now()});
}
}
}));
});
setUninstallURL(page + '?rd=feedback&name=' + encodeURIComponent(name) + '&version=' + version);
}
}