Skip to content
This repository has been archived by the owner on Oct 8, 2019. It is now read-only.

Revert "Page to popup" #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function () {
'use strict';
var mainWindowTabId = null;

chrome.tabs.onRemoved.addListener(function (tabId) {
if (tabId === mainWindowTabId) {
chrome.browserAction.setIcon({path: 'logos/psiphon-logo-bw-38.png'});
}
});

chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.create({'url': chrome.extension.getURL('index.html')}, function (tab) {
mainWindowTabId = tab.id;
});
});
})();
32 changes: 32 additions & 0 deletions app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<title>Psiphon</title>
</head>
<body>
<h2>Psiphon</h2>
<div id="controls">
<button id="connect-button">Connect</button>
<button id="disconnect-button" disabled>Disconnect</button>
<button id="settings-button" >Open Settings</button>
<button id="force-system-proxy-button">Force Chrome back to system proxy</button>
</div>
<div id="status" style="height: 15%;">
<div id="left" style="float: left; width: 50%;">
<p>HTTP Proxy Port: <span id="ListeningHttpProxyPort"></span></p>
<p>SOCKS Proxy Port: <span id="ListeningSocksProxyPort"></span></p>
<p>Proxy Status: <span id="ProxyStatus"><span style="color: gold;">Unknown</span></span></p>
<button id="select-response-text">Select All Log Messages</button>
</div>
<div id="right" style="float: right; width: 50%;">
<p>Egress Regions: <span id="AvailableEgressRegions"></span></p>
<p>Tunnels: <span id="Tunnels"></span></p>
<p>Landing Page: <span id="Homepage"></span></p>
</div>
</div>
<hr/>
<div style="border: 1px solid black; margin-top: 1em; margin-bottom: 1em; overflow: scroll; max-height: 60%;" id="response"></div>

<script src="main.js"></script>
</body>
</html>
196 changes: 196 additions & 0 deletions app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
(function () {
'use strict';

var nativeHost = null;

var connectionState = {
active: false,
tunnels: 0,
egressRegions: [],
homepage: {
url: null,
shown: false
},
proxies: {
socks: null,
http: null
}
};

var proxySettings = {
initial: null,
getCurrent: function () {
chrome.proxy.settings.get({'incognito': false}, function(config) {
proxySettings.initial = config.value;
console.log(JSON.stringify(config));
});
},
restoreInitial: function () {
if (proxySettings.initial) {
chrome.proxy.settings.set({value: proxySettings.initial, scope: 'regular'}, function() {});
} else {
console.warn('No initial settings to restore');
}
},
forceToSystemProxy: function () {
chrome.proxy.settings.set({value: {mode: "system"}}, function() {});
proxySettings.getCurrent()
},
set: function (port, bypassList) {
if (typeof port === 'undefined') {
console.warn('proxySettings.set - port was undefined, defaulting to 1080');
port = 1080;
}
if (typeof bypassList === 'undefined') {
console.warn('proxySettings.set - bypassList was undefined, defaulting to []');
bypassList = [];
}

chrome.proxy.settings.set({value: {
mode: 'fixed_servers',
rules: {
singleProxy: {
scheme: 'socks5',
host: 'localhost',
port: port
},
bypassList: bypassList
}
}, scope: 'regular'}, function() {});
}
};

var psiphon = {
connect: function () {
nativeHost.postMessage({'psi':'connect'});
document.getElementById('connect-button').disabled = true;
document.getElementById('disconnect-button').disabled = false;
},
disconnect: function () {
nativeHost.postMessage({'psi':'disconnect'});
document.getElementById('connect-button').disabled = false;
document.getElementById('disconnect-button').disabled = true;
}
};

function appendMessage(text) {
document.getElementById('response').innerHTML += '<p>' + text + '</p>';
}

function updateStatus(type, text) {
document.getElementById(type).innerHTML = text;
}

function onNativeMessage(message) {
if (message.noticeType) {
switch (message.noticeType) {
case 'AvailableEgressRegions':
updateStatus(message.noticeType, message.data.regions);
connectionState.egressRegions = message.data.regions;
break;
case 'ListeningHttpProxyPort':
updateStatus(message.noticeType, message.data.port);
connectionState.proxies.http = message.data.port;
break;
case 'ListeningSocksProxyPort':
updateStatus(message.noticeType, message.data.port);
connectionState.proxies.socks = message.data.port;
break;
case 'Tunnels':
updateStatus(message.noticeType, message.data.count);
connectionState.tunnels = message.data.count;
break;
case 'Homepage':
updateStatus(message.noticeType, message.data.url);
connectionState.homepage.url = message.data.url;
break;
default:
break;
}

if (!connectionState.active && connectionState.tunnels > 0 && connectionState.proxies.socks && connectionState.homepage.url) {
connectionState.active = true;
tunnelAvailable();
} else if (connectionState.active && (connectionState.tunnels < 1 || !connectionState.proxies.socks)) {
connectionState.active = false;
noTunnelAvailable();
}

appendMessage(JSON.stringify(message));
} else {
appendMessage('<span style="font-weight:bold;color:gold;">Unhandled Message: ' + JSON.stringify(message) + '</span>');
}
}

function tunnelAvailable() {
console.log('A tunnel is available, setting proxy');

proxySettings.set(connectionState.proxies.socks, ['localhost']);
document.getElementById('ProxyStatus').innerHTML = '<span style="color: green;">In Use</span>';
chrome.browserAction.setIcon({path: 'logos/psiphon-logo-38.png'});

if (connectionState.homepage.shown === false) {
connectionState.homepage.shown = true;
window.setTimeout(function () {
window.open(connectionState.homepage.url);
}, 1000);
}
}

function noTunnelAvailable() {
console.log('No tunnel is available, resetting proxy');

proxySettings.restoreInitial();
document.getElementById('ProxyStatus').innerHTML = '<span style="color: red;">Not Used</span>';
chrome.browserAction.setIcon({path: 'logos/psiphon-logo-bw-38.png'});
}

document.addEventListener('DOMContentLoaded', function () {
// Register an unload handler to cleanup when the tab is closing
window.addEventListener('beforeunload', function (event) {
psiphon.disconnect();
proxySettings.restoreInitial();
});

// Store proxy settings prior to beginning managing them in the extension
proxySettings.getCurrent();

// Initialize connection to native host
nativeHost = chrome.runtime.connectNative('ca.psiphon.chrome');
nativeHost.onMessage.addListener(onNativeMessage);
nativeHost.onDisconnect.addListener(function() {
appendMessage('Failed to connect: ' + chrome.runtime.lastError.message);
nativeHost = null;
});

// Set up click handlers for connect and disconnect buttons
document.getElementById('connect-button').addEventListener('click', psiphon.connect);
document.getElementById('disconnect-button').addEventListener('click', psiphon.disconnect);
document.getElementById('force-system-proxy-button').addEventListener('click', proxySettings.forceToSystemProxy);
document.getElementById('settings-button').addEventListener('click', function () {
if (chrome.runtime.openOptionsPage) {
// chrome 42+ preferred
chrome.runtime.openOptionsPage();
} else {
// fallback
window.open(chrome.runtime.getURL('options/options.html'));
}
});

document.getElementById('select-response-text').addEventListener('click', function() {
var range = document.createRange();
var toSelect = document.getElementById('response');

range.selectNodeContents(toSelect);

var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
});

// Set up event handler for Chrome proxy errors
chrome.proxy.onProxyError.addListener(function(error) {
appendMessage('<b style="color: red;">Proxy Error: </b>' + JSON.stringify(error));
});
});
})();
14 changes: 6 additions & 8 deletions extension/manifest.json → app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
"name": "Psiphon",
"short_name": "Psiphon",
"description": "Psiphon Circumvention System",
"version": "0.0.2",
"version": "0.0.1",
"minimum_chrome_version": "40",
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Psiphon",
"default_popup": "popup/popup.html",
"default_icon": {
"19": "img/logos/bw-19.png",
"38": "img/logos/bw-38.png"
"19": "logos/psiphon-logo-bw-19.png",
"38": "logos/psiphon-logo-bw-38.png"
}
},
"options_ui": {
"page": "options/options.html",
"chrome_style": true
},
"icons": {
"16": "img/logos/16.png",
"48": "img/logos/48.png",
"128": "img/logos/128.png"
"16": "logos/psiphon-logo-16.png",
"48": "logos/psiphon-logo-48.png",
"128": "logos/psiphon-logo-128.png"
},
"permissions": [
"storage",
Expand Down
15 changes: 15 additions & 0 deletions app/options/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Psiphon - Settings</title>
<style>
body: { padding: 10px; }
</style>
</head>

<body>
<button id="save">Save</button>

<script src="options.js"></script>
</body>
</html>
10 changes: 2 additions & 8 deletions extension/options/options.js → app/options/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var bgPage = chrome.extension.getBackgroundPage();

function saveOptions () {
chrome.storage.local.set({
// Settings object to save
Expand All @@ -26,9 +24,5 @@ function restoreOptions () {
});
}

document.addEventListener('DOMContentLoaded', function() {
//restoreOptions();
//document.getElementById('save-button').addEventListener('click', saveOptions);
document.getElementById('view-logs-button').addEventListener('click', bgPage.viewLogs);
document.getElementById('force-system-proxy-button').addEventListener('click', bgPage.proxySettings.forceToSystemProxy);
});
document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('save').addEventListener('click', saveOptions);
Loading