Skip to content

Commit

Permalink
updating tor
Browse files Browse the repository at this point in the history
  • Loading branch information
lunu-bounir committed Jan 17, 2021
1 parent 593e730 commit 43e0a54
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 85 deletions.
142 changes: 94 additions & 48 deletions extension/background.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* globals Tor, Events */
/* globals Tor, Events, browser */
'use strict';

var prefs = {
ports: [2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030],
threads: 2,
threads: 1,
badge: true,
start: false,
native: false
native: false,
arguments: ''
};
chrome.storage.local.get(prefs, ps => {
Object.assign(prefs, ps);
Expand All @@ -20,13 +21,8 @@ chrome.storage.local.get(prefs, ps => {
app.connect();
}
if (!r) {
chrome.windows.create({
url: chrome.extension.getURL('data/installer/index.html'),
width: 600,
height: 450,
left: screen.availLeft + Math.round((screen.availWidth - 600) / 2),
top: screen.availTop + Math.round((screen.availHeight - 450) / 2),
type: 'popup'
chrome.storage.local.set({
native: false
});
}
});
Expand All @@ -47,28 +43,41 @@ chrome.storage.onChanged.addListener(ps => {
var proxy = {};
proxy.update = () => {
if (app.connected.length || app.mode === 'connected') {
const config = {
mode: 'pac_script',
pacScript: {
data: `function FindProxyForURL() {
const ports = ${JSON.stringify(app.connected)};
if (ports.length) {
const port = ports[Math.floor(Math.random() * ports.length)];
return 'SOCKS localhost:' + port;
const data = `function FindProxyForURL() {
const ports = ${JSON.stringify(app.connected)};
if (ports.length) {
const port = ports[Math.floor(Math.random() * ports.length)];
return 'SOCKS localhost:' + port;
}
else {
return 'SOCKS localhost:2020';
}
}`;
if (/Firefox/.test(navigator.userAgent)) {
browser.proxy.settings.clear({}, () => {
browser.proxy.settings.set({
value: {
proxyType: 'autoConfig',
autoConfigUrl: 'data:text/javascript,' + encodeURIComponent(data),
proxyDNS: true
}
else {
return 'SOCKS localhost:2020';
});
});
}
else {
chrome.proxy.settings.clear({
scope: 'regular'
}, () => chrome.proxy.settings.set({
value: {
mode: 'pac_script',
pacScript: {
data,
mandatory: true
}
}`,
mandatory: true
}
};
chrome.proxy.settings.clear({
scope: 'regular'
}, () => chrome.proxy.settings.set({
value: config,
scope: 'regular'
}));
},
scope: 'regular'
}));
}
}
else {
chrome.proxy.settings.clear({
Expand Down Expand Up @@ -113,27 +122,42 @@ tor.on('disconnected', ids => {
app.connect = port => {
if (prefs.native === false) {
app.notify('Native Client is not found. Follow the instruction to install it');
chrome.windows.create({
url: chrome.extension.getURL('data/installer/index.html'),
width: 600,
height: 450,
left: screen.availLeft + Math.round((screen.availWidth - 600) / 2),
top: screen.availTop + Math.round((screen.availHeight - 450) / 2),
type: 'popup'
});
return;
}
app.mode = 'connected';
if (app.ids.length === 0) {
app.emit('connecting');
}
if (port) {
app.ids.push(port);
tor.connect(port);
}
else {
for (const port of prefs.ports.slice(0, prefs.threads)) {
chrome.proxy.settings.get({}, e => {
if (e.levelOfControl === 'controllable_by_this_extension' || e.levelOfControl === 'controlled_by_this_extension') {
app.mode = 'connected';
if (app.ids.length === 0) {
app.emit('connecting');
}
if (port) {
app.ids.push(port);
tor.connect(port);
tor.connect(port, prefs.arguments);
}
else {
console.error('no more ports to use');
for (const port of prefs.ports.slice(0, prefs.threads)) {
if (port) {
app.ids.push(port);
tor.connect(port, prefs.arguments);
}
else {
console.error('no more ports to use');
}
}
}
}
}
else {
app.notify('Cannot control your browser\'s proxy settings; ' + e.levelOfControl);
}
});
};

app.disconnect = (trusted = false) => {
Expand All @@ -155,7 +179,7 @@ app.fix = () => {
const n = prefs.threads - len;
const ports = prefs.ports.filter(p => app.ids.indexOf(p) === -1);
ports.slice(0, n).forEach(port => app.connect(port));
app.notify('Increasing # of channels by ' + n);
app.notify('Increasing # of channels by ' + n + '. Please wait...');
}
};

Expand Down Expand Up @@ -194,26 +218,22 @@ app.notify = message => chrome.notifications.create({

var timer;
app.on('connecting', () => {
console.log('connecting');
app.title(`Connecting to the Tor network (${prefs.threads} channels)`);
app.icon('connecting');
window.clearInterval(timer);
timer = window.setInterval(app.icon.toggle, 500);
proxy.update();
});
app.on('connected', () => {
console.log('connected');
window.clearInterval(timer);
app.icon('connected');
});
app.on('update', () => {
console.log('update');
app.title(`Connected to the Tor network (${app.connected.length} channels)`);
app.badge(app.connected.length ? String(app.connected.length) : '');
proxy.update();
});
app.on('disconnected', () => {
console.log('disconnected');
window.clearInterval(timer);
app.title('Disconnected from the Tor network');
app.icon(app.mode === 'disconnected' ? 'disconnected' : 'unexpected');
Expand All @@ -236,3 +256,29 @@ chrome.browserAction.onClicked.addListener(() => {
chrome.browserAction.setBadgeBackgroundColor({
color: '#7d4798'
});

{
const {onInstalled, setUninstallURL, getManifest} = chrome.runtime;
const {name, version} = getManifest();
const page = getManifest().homepage_url;
onInstalled.addListener(({reason, previousVersion}) => {
chrome.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) {
chrome.tabs.create({
url: page + '?version=' + version +
(previousVersion ? '&p=' + previousVersion : '') +
'&type=' + reason,
active: reason === 'install'
});
chrome.storage.local.set({'last-update': Date.now()});
}
}
});
});
setUninstallURL(page + '?rd=feedback&name=' + encodeURIComponent(name) + '&version=' + version);
}
33 changes: 21 additions & 12 deletions extension/data/installer/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
var name;
var filename;
var os;

(() => {
if (navigator.platform.startsWith('Mac')) {
os = 'mac';
name = 'mac.zip';
filename = 'mac.zip';
}
else if (navigator.platform.startsWith('Win')) {
os = 'windows';
if (navigator.userAgent.indexOf('WOW64') != -1 || navigator.userAgent.indexOf('Win64') != -1) {
name = 'win_64.zip';
filename = 'win_64.zip';
}
else {
name = 'win_32.zip';
filename = 'win_32.zip';
}
}
else {
os = 'linux';
name = 'linux_64.zip';
filename = 'linux_64.zip';
}
})();
const url = 'https://github.com/lunu-bounir/onion-helper/releases/download/0.1.0/' + name;
document.body.dataset.os = os;
const url = 'https://github.com/lunu-bounir/onion-vpn/releases/download/0.1.0/' + filename;

if (navigator.platform.startsWith('Mac') || navigator.platform.startsWith('Win')) {
document.getElementById('package').href = url;
document.getElementById('package').download = name;
document.getElementById('package').dataset.href = url;
document.getElementById('uninstall').textContent = 'uninstall.' + (os === 'windows' ? 'bat' : 'sh');
}
else {
alert('Your operating system is not yet supported');
}

fetch('https://api.github.com/repos/lunu-bounir/onion-helper/releases/latest').then(r => r.json()).then(obj => {
const link = obj.assets.filter(a => a.name === name)[0].browser_download_url;
console.log(link);
document.getElementById('package').href = link;
fetch('https://api.github.com/repos/lunu-bounir/onion-vpn/releases/latest').then(r => r.json()).then(obj => {
const link = obj.assets.filter(a => a.name === filename)[0].browser_download_url;
document.getElementById('package').dataset.href = link;
}).catch(e => alert(e.message));

chrome.runtime.sendNativeMessage('onion.vpn.helper', {
Expand All @@ -48,3 +47,13 @@ chrome.runtime.sendNativeMessage('onion.vpn.helper', {
document.getElementById('restart').addEventListener('click', () => {
chrome.runtime.reload();
});

document.getElementById('package').addEventListener('click', e => {
chrome.downloads.download({
url: e.target.dataset.href,
filename
});
e.preventDefault();
e.target.parentElement.style['text-decoration'] = 'line-through';
chrome.runtime.getBackgroundPage(b => b.app.notify('Please wait for the download to finish'));
});
55 changes: 55 additions & 0 deletions extension/data/options/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html>
<head>
<title>Onion VPN :: Options Page</title>
</head>
<body>
<style>
body {
min-width: 500px;
}
input[type=text] {
width: 100%;
box-sizing: border-box;
}
</style>
<table width=100%>
<tr>
<td><input type="checkbox" id="badge"></td>
<td><label for="badge">Show badge number (total number of active Tor instances)</label></td>
</tr>
<tr>
<td></td>
<td>Comma-separated list of ports<sup>1</sup></td>
</tr>
<tr>
<td></td>
<td><input type="text" id="ports"></td>
</tr>
<tr>
<td><input type="checkbox" id="start"></td>
<td><label for="start">Start Tor client when browser starts<sup>2</sup></label></td>
</tr>
<tr>
<td></td>
<td>Additional command-line arguments<sup>3</sup></td>
</tr>
<tr>
<td></td>
<td><input type="text" id="arguments"></td>
</tr>
</table>
<p>
<input type="button" id="support" value="Support Development">
<input type="button" id="reset" value="Reset"> - <input type="button" id="save" value="Save"> <span id="info"></span>
</p>
<hr>
1: When starting a new Tor instance, use a port number among this list.
<br>
2: Note that you will be disconnected until the extension successfully establishes its first connection
<br>
3: Comma-separated list of additional arguments to pass to the Tor executable. It is user responsibility to make sure these additional arguments are not going to conflict with the extension operation. For instance for the Tor client to make all OR connections through the SOCKS 5 proxy, use <code>Socks5Proxy, 127.0.0.1:9191</code>

<script src="index.js"></script>
</body>
</html>
49 changes: 49 additions & 0 deletions extension/data/options/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

var info = document.getElementById('info');

chrome.storage.local.get({
ports: [2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030],
badge: true,
start: false,
arguments: ''
}, prefs => {
document.getElementById('ports').value = prefs.ports.join(', ');
document.getElementById('badge').checked = prefs.badge;
document.getElementById('start').checked = prefs.start;
document.getElementById('arguments').value = prefs.arguments;
});

document.getElementById('save').addEventListener('click', () => {
let ports = document.getElementById('ports').value.split(/\s*,\s*/).filter(s => s && isNaN(s) === false);
ports = ports.length ? ports : [2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030];
chrome.storage.local.set({
ports,
badge: document.getElementById('badge').checked,
start: document.getElementById('start').checked,
arguments: document.getElementById('arguments').value
}, () => {
document.getElementById('ports').value = ports.join(', ');
info.textContent = 'Options saved';
window.setTimeout(() => info.textContent = '', 750);
});
});

// reset
document.getElementById('reset').addEventListener('click', e => {
if (e.detail === 1) {
info.textContent = 'Double-click to reset!';
window.setTimeout(() => info.textContent = '', 750);
}
else {
localStorage.clear();
chrome.storage.local.clear(() => {
chrome.runtime.reload();
window.close();
});
}
});
// support
document.getElementById('support').addEventListener('click', () => chrome.tabs.create({
url: chrome.runtime.getManifest().homepage_url + '?rd=donate'
}));
Loading

0 comments on commit 43e0a54

Please sign in to comment.