From 816715f4cc92912bad9edd1602850978d55672d3 Mon Sep 17 00:00:00 2001 From: Frank Ploss Date: Sat, 19 Dec 2015 15:29:05 +0100 Subject: [PATCH] Switching Tor identity works --- extension.js | 15 +++++++++++++-- tor_control_client.js | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/extension.js b/extension.js index 8036ab5..6ecb4a5 100644 --- a/extension.js +++ b/extension.js @@ -32,9 +32,19 @@ const TorButton = new Lang.Class({ this.actor.add_child(this._icon); this.actor.connect('button-press-event', Lang.bind(this, function(actor, event) { 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() { try { torControlClient = new TorControlClient(); - torButton = new TorButton(); + torButton = new TorButton(torControlClient); Main.panel.addToStatusArea(torButton.Name, torButton); } catch (e) { + log(e); Main.notifyError('Error starting extension: ' + e); disable(); } diff --git a/tor_control_client.js b/tor_control_client.js index f119d00..6e8f18d 100644 --- a/tor_control_client.js +++ b/tor_control_client.js @@ -11,6 +11,7 @@ const TorControlClient = new Lang.Class({ this._connect(); this._updateProtocolInfo(); this._ensureProtocolCompatibility(); + this.authenticate(); }, 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() { var socketClient = new Gio.SocketClient(); 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) { let cookieArg = tokens[2].split('='); - authCookieFile = cookieArg[1]; + authCookieFile = cookieArg[1].substr(1, cookieArg[1].length - 2); // strip quotes } break; } @@ -101,6 +119,24 @@ const TorControlClient = new Lang.Class({ isMidReplyLine: (line[3] == '-'), 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; } });