tor: Minor improvements

- Minimize loading of Augeas since it takes time.

- Make some methods private

- PEP8 fixes
This commit is contained in:
Sunil Mohan Adapa 2016-07-08 16:19:52 +05:30 committed by James Valleroy
parent 2afae80dd8
commit 70001c841f
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 55 additions and 52 deletions

View File

@ -107,7 +107,7 @@ def subcommand_setup(_):
# wait until hidden service information is available
tries = 0
while not get_hidden_service()['enabled']:
while not _get_hidden_service()['enabled']:
tries += 1
if tries >= 12:
return
@ -131,7 +131,7 @@ def subcommand_configure(arguments):
elif arguments.relay == 'disable':
_disable_relay(restart=restart)
restart = arguments.service == None
restart = arguments.service is None
if arguments.hidden_service == 'enable':
_enable_hs(restart=restart)
elif arguments.hidden_service == 'disable':
@ -148,20 +148,20 @@ def subcommand_configure(arguments):
def get_status():
"""Return dict with Tor status."""
return {'relay_enabled': is_relay_enabled(),
'ports': get_ports(),
'hidden_service': get_hidden_service()}
def is_relay_enabled():
"""Return whether bridge relay is enabled."""
aug = augeas_load()
return {'relay_enabled': _is_relay_enabled(aug),
'ports': _get_ports(),
'hidden_service': _get_hidden_service(aug)}
def _is_relay_enabled(aug):
"""Return whether bridge relay is enabled."""
orport = aug.get(TOR_CONFIG + '/ORPort')
bridge = aug.get(TOR_CONFIG + '/BridgeRelay')
return orport == 'auto' and bridge == '1'
def get_ports():
def _get_ports():
"""Return dict mapping port names to numbers."""
ports = {}
try:
@ -203,14 +203,16 @@ QUIT
return matches.group(1)
def get_hidden_service():
def _get_hidden_service(aug=None):
"""Return a string with configured Tor hidden service information"""
hs_enabled = False
hs_status = 'Ok'
hs_hostname = None
hs_ports = []
aug = augeas_load()
if not aug:
aug = augeas_load()
hs_dir = aug.get(TOR_CONFIG + '/HiddenServiceDir')
hs_port_paths = aug.match(TOR_CONFIG + '/HiddenServicePort')
@ -271,10 +273,11 @@ def _disable_relay(restart=False):
def _enable_hs(restart=True):
"""Enable Tor hidden service"""
if get_hidden_service()['enabled']:
aug = augeas_load()
if _get_hidden_service(aug)['enabled']:
return
aug = augeas_load()
aug.set(TOR_CONFIG + '/HiddenServiceDir',
'/var/lib/tor/hidden_service')
aug.set(TOR_CONFIG + '/HiddenServicePort[1]',
@ -291,7 +294,7 @@ def _enable_hs(restart=True):
# wait until hidden service information is available
tries = 0
while not get_hidden_service()['enabled']:
while not _get_hidden_service()['enabled']:
tries += 1
if tries >= 12:
return
@ -301,10 +304,11 @@ def _enable_hs(restart=True):
def _disable_hs(restart=True):
"""Disable Tor hidden service"""
if not get_hidden_service()['enabled']:
aug = augeas_load()
if not _get_hidden_service(aug)['enabled']:
return
aug = augeas_load()
aug.remove(TOR_CONFIG + '/HiddenServiceDir')
aug.remove(TOR_CONFIG + '/HiddenServicePort')
aug.save()
@ -356,7 +360,7 @@ def _update_ports():
# port information may not be available immediately after Tor started
while not ready:
ports = get_ports()
ports = _get_ports()
ready = 'orport' in ports and 'obfs3' in ports and 'obfs4' in ports
if ready:
break

View File

@ -77,7 +77,7 @@ def init():
hostname = status['hs_hostname']
hs_virtports = [port['virtport'] for port in status['hs_ports']]
if utils.is_enabled() and utils.is_running() and \
if status['enabled'] and status['is_running'] and \
status['hs_enabled'] and status['hs_hostname']:
hs_services = []
for service_type in SERVICES:
@ -120,7 +120,6 @@ def update_hidden_service_domain(status=None):
services=status['hs_services'])
def diagnose():
"""Run diagnostics and return the results."""
results = []

View File

@ -94,34 +94,34 @@
</form>
{% if status.relay_enabled %}
<h3>{% trans "Bridge Relay" %}</h3>
<p>
{% blocktrans trimmed %}
If your {{ box_name }} is behind a router or firewall, you should
make sure the following ports are open, and port-forwarded, if
necessary:
{% endblocktrans %}
</p>
<div class="row">
<div class="col-sm-3">
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>{% trans "Service" %}</th>
<th>{% trans "Port" %}</th>
</tr>
</thead>
<tbody>
{% for name, port in status.ports.items %}
<h3>{% trans "Bridge Relay" %}</h3>
<p>
{% blocktrans trimmed %}
If your {{ box_name }} is behind a router or firewall, you should
make sure the following ports are open, and port-forwarded, if
necessary:
{% endblocktrans %}
</p>
<div class="row">
<div class="col-sm-3">
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<td>{{ name }}</td>
<td>{{ port }}</td>
<th>{% trans "Service" %}</th>
<th>{% trans "Port" %}</th>
</tr>
{% endfor %}
</tbody>
</table>
</thead>
<tbody>
{% for name, port in status.ports.items %}
<tr>
<td>{{ name }}</td>
<td>{{ port }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endif %}
<h3>{% trans "SOCKS" %}</h3>

View File

@ -48,7 +48,6 @@ def get_status():
"""Return current Tor status."""
output = actions.superuser_run('tor', ['get-status'])
status = json.loads(output)
ports = status['ports']
hs_info = status['hidden_service']
hs_services = []
@ -60,21 +59,21 @@ def get_status():
return {'enabled': is_enabled(),
'is_running': is_running(),
'relay_enabled': status['relay_enabled'],
'ports': ports,
'ports': status['ports'],
'hs_enabled': hs_info['enabled'],
'hs_status': hs_info['status'],
'hs_hostname': hs_info['hostname'],
'hs_ports': hs_info['ports'],
'hs_services': hs_services,
'apt_transport_tor_enabled': \
_is_apt_transport_tor_enabled()
'apt_transport_tor_enabled':
_is_apt_transport_tor_enabled()
}
def iter_apt_uris(aug):
"""Iterate over all the APT source URIs."""
return itertools.chain.from_iterable([aug.match(path) for \
path in APT_SOURCES_URI_PATHS])
return itertools.chain.from_iterable([aug.match(path)
for path in APT_SOURCES_URI_PATHS])
def get_real_apt_uri_path(aug, path):
@ -104,7 +103,8 @@ def get_augeas():
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Aptsources/lens', 'Aptsources.lns')
aug.set('/augeas/load/Aptsources/incl[last() + 1]', '/etc/apt/sources.list')
aug.set('/augeas/load/Aptsources/incl[last() + 1]',
'/etc/apt/sources.list')
aug.set('/augeas/load/Aptsources/incl[last() + 1]',
'/etc/apt/sources.list.d/*.list')
aug.load()