From 3d5f68381d0e8511afc1b3b702608aab9eaef8bd Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 28 Jul 2015 16:40:51 +0530 Subject: [PATCH] tor: Reimplement getting ports in Python --- actions/tor | 51 ++++++++++++++++++++++++++++++++------- actions/tor-get-ports | 28 --------------------- plinth/modules/tor/tor.py | 2 +- 3 files changed, 43 insertions(+), 38 deletions(-) delete mode 100755 actions/tor-get-ports diff --git a/actions/tor b/actions/tor index 22fab6238..d26b0bce4 100755 --- a/actions/tor +++ b/actions/tor @@ -22,11 +22,16 @@ Configuration helper for the Tor service """ import argparse +import codecs import os +import re +import socket from plinth import action_utils TOR_CONFIG = '/etc/tor/torrc' +TOR_STATE_FILE = '/var/lib/tor/state' +TOR_AUTH_COOKIE = '/var/run/tor/control.authcookie' def parse_arguments(): @@ -34,20 +39,12 @@ def parse_arguments(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') - # Enable and start the service subparsers.add_parser('enable', help='Enable and start Tor service') - - # Disable and stop the service subparsers.add_parser('disable', help='Disable and stop Tor service') - - # Get currently configured Tor hidden service information subparsers.add_parser('get-hs', help='Get hidden service') - - # Enable Tor hidden service subparsers.add_parser('enable-hs', help='Enable hidden service') - - # Disable Tor hidden service subparsers.add_parser('disable-hs', help='Disable hidden service') + subparsers.add_parser('get-ports', help='Get list of Tor ports') return parser.parse_args() @@ -146,6 +143,42 @@ def get_hidden_service(): return hs_hostname + ' ' + ','.join(hs_ports) +def subcommand_get_ports(_): + """Return a list of running Tor ports.""" + try: + print('orport', _get_orport()) + except Exception: + pass + + with open(TOR_STATE_FILE, 'r') as state_file: + for line in state_file: + matches = re.match(r'^\s*TransportProxy\s+(\S*)\s+\S+:(\d+)\s*$', + line) + if matches: + print('{0} {1}'.format(matches.group(1), matches.group(2))) + + +def _get_orport(): + """Return the ORPort by querying running instance.""" + cookie = open(TOR_AUTH_COOKIE, 'rb').read() + cookie = codecs.encode(cookie, 'hex').decode() + + commands = '''AUTHENTICATE {cookie} +GETINFO net/listeners/or +QUIT +'''.format(cookie=cookie) + + tor_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tor_socket.connect(('localhost', 9051)) + tor_socket.send(commands.encode()) + response = tor_socket.recv(1024) + tor_socket.close() + + line = response.split(b'\r\n')[1].decode() + matches = re.match(r'.*="[^:]+:(\d+)"', line) + return matches.group(1) + + def main(): """Parse arguments and perform all duties""" arguments = parse_arguments() diff --git a/actions/tor-get-ports b/actions/tor-get-ports deleted file mode 100755 index de50f4237..000000000 --- a/actions/tor-get-ports +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/sh - -# -# This file is part of Plinth. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program 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 Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# - -# Action to get ports used by Tor. - -echo "ORPort" `tor-get-orport` - -transports="obfs3 scramblesuit" -for transport in $transports -do - echo $transport `grep $transport /var/lib/tor/state | awk -F'[: ]*' '{print $4}'` -done diff --git a/plinth/modules/tor/tor.py b/plinth/modules/tor/tor.py index ee3e18c7d..e35b1a584 100644 --- a/plinth/modules/tor/tor.py +++ b/plinth/modules/tor/tor.py @@ -73,7 +73,7 @@ def index(request): def get_status(): """Return the current status""" - output = actions.superuser_run('tor-get-ports') + output = actions.superuser_run('tor', ['get-ports']) port_info = output.split('\n') ports = {} for line in port_info: