Switching Tor identity works

master
Frank Ploss 9 years ago
parent 94c0c27c25
commit 816715f4cc

@ -32,9 +32,19 @@ const TorButton = new Lang.Class({
this.actor.add_child(this._icon); this.actor.add_child(this._icon);
this.actor.connect('button-press-event', Lang.bind(this, function(actor, event) { this.actor.connect('button-press-event', Lang.bind(this, function(actor, event) {
if (event.get_click_count() >= 2) { if (event.get_click_count() >= 2) {
log('TOR DOUBLE CLICK!!!'); this._switchTorIdentity();
} }
})); }));
},
_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);
}
} }
}); });
@ -46,9 +56,10 @@ function init(extensionMeta) {
function enable() { function enable() {
try { try {
torControlClient = new TorControlClient(); torControlClient = new TorControlClient();
torButton = new TorButton(); torButton = new TorButton(torControlClient);
Main.panel.addToStatusArea(torButton.Name, torButton); Main.panel.addToStatusArea(torButton.Name, torButton);
} catch (e) { } catch (e) {
log(e);
Main.notifyError('Error starting extension: ' + e); Main.notifyError('Error starting extension: ' + e);
disable(); disable();
} }

@ -11,6 +11,7 @@ const TorControlClient = new Lang.Class({
this._connect(); this._connect();
this._updateProtocolInfo(); this._updateProtocolInfo();
this._ensureProtocolCompatibility(); this._ensureProtocolCompatibility();
this.authenticate();
}, },
close: function() { close: function() {
@ -20,6 +21,23 @@ const TorControlClient = new Lang.Class({
} }
}, },
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) {
throw 'Could not change Tor identity, reason: ' + reply.replyLines.join('\n');
}
},
_connect: function() { _connect: function() {
var socketClient = new Gio.SocketClient(); var socketClient = new Gio.SocketClient();
this._connection = socketClient.connect_to_host('127.0.0.1:9051', null, null); this._connection = socketClient.connect_to_host('127.0.0.1:9051', null, null);
@ -51,7 +69,7 @@ const TorControlClient = new Lang.Class({
if (authMethods.indexOf('COOKIE') != -1 || authMethods.indexOf('SAFECOOKIE') != -1) { if (authMethods.indexOf('COOKIE') != -1 || authMethods.indexOf('SAFECOOKIE') != -1) {
let cookieArg = tokens[2].split('='); let cookieArg = tokens[2].split('=');
authCookieFile = cookieArg[1]; authCookieFile = cookieArg[1].substr(1, cookieArg[1].length - 2); // strip quotes
} }
break; break;
} }
@ -101,6 +119,24 @@ const TorControlClient = new Lang.Class({
isMidReplyLine: (line[3] == '-'), isMidReplyLine: (line[3] == '-'),
replyLine: line.substring(4) replyLine: line.substring(4)
} }
},
_readAuthCookie: function() {
var file = Gio.File.new_for_path(this._protocolInfo.authCookieFile);
var inputStream = file.read(null);
var cookieData = inputStream.read_bytes(32, null, null).get_data();
inputStream.close(null);
var authCookie = '';
for (var i = 0; i < cookieData.length; i++) {
let hexByte = cookieData[i].toString(16);
if (hexByte.length == 1) {
hexByte = '0' + hexByte;
}
authCookie += hexByte;
}
return authCookie;
} }
}); });

Loading…
Cancel
Save