Show different menu when not connected to Tor control port

master
Frank Ploss 9 years ago
parent 220b33380e
commit c85bc9d1d2

@ -47,7 +47,7 @@ function disable() {
torButton = null;
}
if (torControlClient !== null) {
torControlClient.close();
torControlClient.closeConnection();
torControlClient = null;
}
}

@ -23,40 +23,54 @@ const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Signals = imports.signals;
const TorProtocolError = new Lang.Class({
Name: 'TorProtocolError',
Extends: Error,
_init: function(message, statusCode) {
this.parent(message);
this.statusCode = statusCode;
}
});
const TorControlClient = new Lang.Class({
Name: 'TorControlClient',
_init: function() {
this._fail_reason = null;
},
openConnection: function() {
this._connect();
this._updateProtocolInfo();
this._ensureProtocolCompatibility();
this._authenticate();
this.emit('changed-connection-state', 'connected');
try {
this._connect();
this._updateProtocolInfo();
this._ensureProtocolCompatibility();
this._authenticate();
this.emit('changed-connection-state', 'connected');
} catch (e if (e instanceof Gio.IOErrorEnum || e instanceof TorProtocolError)) {
this.emit('changed-connection-state', 'disconnected', e.message);
}
},
closeConnection: function() {
if (this._connection.is_connected()) {
if (this._connection && this._connection.is_connected()) {
this._outputStream.close(null);
this._inputStream.close(null);
this.emit('changed-connection-state', 'disconnected');
}
},
_authenticate: function() {
var cookie = this._readAuthCookie();
var reply = this._runCommand('AUTHENTICATE ' + cookie);
if (reply.statusCode != 250) {
throw 'Could not authenticate, reason: ' + reply.replyLines.join('\n');
}
},
switchIdentity: function() {
var reply = this._runCommand('SIGNAL NEWNYM');
if (reply.statusCode != 250) {
this.emit('changed-connection-state', 'failed');
throw 'Could not change Tor identity, reason: ' + reply.replyLines.join('\n');
this.emit(
'protocol-error',
'Could not switch Tor identity: ' + reply.replyLines.join('\n'),
reply.statusCode
);
} else {
this.emit('switched-tor-identity');
}
},
@ -71,7 +85,9 @@ const TorControlClient = new Lang.Class({
var reply = this._runCommand('PROTOCOLINFO');
if (reply.statusCode != 250) {
throw "Could not read protocol info";
throw new TorProtocolError(
'Could not read protocol info, reason: ' + reply.replyLines.join('\n'),
reply.statusCode);
}
var protocolInfoVersion;
@ -106,7 +122,19 @@ const TorControlClient = new Lang.Class({
_ensureProtocolCompatibility: function() {
if (this._protocolInfo.protocolInfoVersion != 1) {
throw 'Cannot handle tor protocol version ' + this._protocolInfo.protocolInfoVersion;
throw new TorProtocolError('Cannot handle tor protocol version ' + this._protocolInfo.protocolInfoVersion);
}
},
_authenticate: function() {
var cookie = this._readAuthCookie();
var reply = this._runCommand('AUTHENTICATE ' + cookie);
if (reply.statusCode != 250) {
throw new TorProtocolError(
'Could not authenticate, reason: ' + reply.replyLines.join('\n'),
statusCode
);
}
},

@ -22,9 +22,11 @@ along with gnome-shell-extension-tor. If not, see <http://www.gnu.org/licenses/
const Lang = imports.lang;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const St = imports.gi.St;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const TorDisconnectedMenu = Me.imports.ui.tor_disconnected_menu.TorDisconnectedMenu;
const TorPopupMenu = Me.imports.ui.tor_popup_menu.TorPopupMenu;
const TorConnectedIcon = 'tor-connected';
@ -41,6 +43,8 @@ const TorButton = new Lang.Class({
this._buildUi();
this._bindEvents();
this._currentState = null;
},
_buildUi: function() {
@ -51,23 +55,45 @@ const TorButton = new Lang.Class({
this.actor.add_child(this._icon);
this._menu = new TorPopupMenu(this.actor, this._torControlClient);
this.setMenu(this._menu);
//var dummyMenu = new PopupMenu.PopupDummyMenu(this.actor);
//this.setMenu(dummyMenu);
//Main.panel.menuManager.addMenu(dummyMenu);
},
_bindEvents: function() {
this._torControlClient.connect('changed-connection-state', Lang.bind(this, this._changedConnectionState));
this._torControlClient.connect('changed-connection-state', Lang.bind(this, this._onChangedConnectionState));
this._torControlClient.connect('switched-tor-identity', Lang.bind(this, this._onSwitchedTorIdentity));
this._torControlClient.connect('protocol-error', Lang.bind(this, this._onProtocolError));
},
_changedConnectionState: function(source, state) {
log('NEW STATE: ' + state);
_onChangedConnectionState: function(source, state, message) {
if (this._currentState == state)
return;
this._currentState = state;
switch (state) {
case 'connected':
this._icon.icon_name = TorConnectedIcon;
this._menu = new TorPopupMenu(this.actor, this._torControlClient);
this.setMenu(this._menu);
Main.panel.menuManager.addMenu(this._menu);
break;
case 'disconnected':
this._icon.icon_name = TorDisconnectedIcon;
this._menu = new TorDisconnectedMenu(this.actor, this._torControlClient);
this.setMenu(this._menu);
Main.panel.menuManager.addMenu(this._menu);
break;
}
},
_onSwitchedTorIdentity: function() {
Main.notify('Switched to a new Tor identity!');
},
_onProtocolError: function(source, message, statusCode) {
Main.notifyError(message);
log('Tor control procotol error (status code ' + statusCode + '): ' + reason)
}
});

@ -0,0 +1,56 @@
// vim: set sw=4:ts=4
/*
Copyright 2015 Frank Ploss <frank@fqxp.de>.
This file is part of gnome-shell-extension-tor.
gnome-shell-extension-tor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
gnome-shell-extension-tor is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with gnome-shell-extension-tor. If not, see <http://www.gnu.org/licenses/>./
*/
'use strict';
const Lang = imports.lang;
const PopupMenu = imports.ui.popupMenu;
const St = imports.gi.St;
const TorDisconnectedMenu = new Lang.Class({
Name: 'TorDisconnectedMenu',
Extends: PopupMenu.PopupMenu,
_init: function(actor, torControlClient) {
this._torControlClient = torControlClient;
this.parent(actor, 0.25, St.Side.TOP);
this._addActions();
},
destroy: function() {
this.parent(arguments);
},
_addActions: function() {
var errorMessageMenuItem = new PopupMenu.PopupBaseMenuItem({reactive: false});
errorMessageMenuItem.setSensitive(false);
errorMessageMenuItem.actor.add_actor(new St.Label({
text: 'ERROR running'
}));
this.addMenuItem(errorMessageMenuItem);
this.addAction('Reconnect', Lang.bind(this, this._reconnect));
},
_reconnect: function() {
this._torControlClient.connect('changed-connection-state', function() {})
this._torControlClient.openConnection();
}
});

@ -20,7 +20,6 @@ along with gnome-shell-extension-tor. If not, see <http://www.gnu.org/licenses/
'use strict';
const Lang = imports.lang;
const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu;
const St = imports.gi.St;
@ -40,12 +39,6 @@ const TorPopupMenu = new Lang.Class({
},
_switchTorIdentity: function() {
try {
this._torControlClient.switchIdentity();
Main.notify('Switched to a new Tor identity!');
} catch (e) {
log(e);
Main.notifyError('Could not switch Tor identity: ' + e);
}
this._torControlClient.switchIdentity();
}
});

Loading…
Cancel
Save