mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
tor: Minor improvements
- Minimize loading of Augeas since it takes time. - Make some methods private - PEP8 fixes
This commit is contained in:
parent
2afae80dd8
commit
70001c841f
40
actions/tor
40
actions/tor
@ -107,7 +107,7 @@ def subcommand_setup(_):
|
|||||||
|
|
||||||
# wait until hidden service information is available
|
# wait until hidden service information is available
|
||||||
tries = 0
|
tries = 0
|
||||||
while not get_hidden_service()['enabled']:
|
while not _get_hidden_service()['enabled']:
|
||||||
tries += 1
|
tries += 1
|
||||||
if tries >= 12:
|
if tries >= 12:
|
||||||
return
|
return
|
||||||
@ -131,7 +131,7 @@ def subcommand_configure(arguments):
|
|||||||
elif arguments.relay == 'disable':
|
elif arguments.relay == 'disable':
|
||||||
_disable_relay(restart=restart)
|
_disable_relay(restart=restart)
|
||||||
|
|
||||||
restart = arguments.service == None
|
restart = arguments.service is None
|
||||||
if arguments.hidden_service == 'enable':
|
if arguments.hidden_service == 'enable':
|
||||||
_enable_hs(restart=restart)
|
_enable_hs(restart=restart)
|
||||||
elif arguments.hidden_service == 'disable':
|
elif arguments.hidden_service == 'disable':
|
||||||
@ -148,20 +148,20 @@ def subcommand_configure(arguments):
|
|||||||
|
|
||||||
def get_status():
|
def get_status():
|
||||||
"""Return dict with Tor 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()
|
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')
|
orport = aug.get(TOR_CONFIG + '/ORPort')
|
||||||
bridge = aug.get(TOR_CONFIG + '/BridgeRelay')
|
bridge = aug.get(TOR_CONFIG + '/BridgeRelay')
|
||||||
return orport == 'auto' and bridge == '1'
|
return orport == 'auto' and bridge == '1'
|
||||||
|
|
||||||
|
|
||||||
def get_ports():
|
def _get_ports():
|
||||||
"""Return dict mapping port names to numbers."""
|
"""Return dict mapping port names to numbers."""
|
||||||
ports = {}
|
ports = {}
|
||||||
try:
|
try:
|
||||||
@ -203,14 +203,16 @@ QUIT
|
|||||||
return matches.group(1)
|
return matches.group(1)
|
||||||
|
|
||||||
|
|
||||||
def get_hidden_service():
|
def _get_hidden_service(aug=None):
|
||||||
"""Return a string with configured Tor hidden service information"""
|
"""Return a string with configured Tor hidden service information"""
|
||||||
hs_enabled = False
|
hs_enabled = False
|
||||||
hs_status = 'Ok'
|
hs_status = 'Ok'
|
||||||
hs_hostname = None
|
hs_hostname = None
|
||||||
hs_ports = []
|
hs_ports = []
|
||||||
|
|
||||||
aug = augeas_load()
|
if not aug:
|
||||||
|
aug = augeas_load()
|
||||||
|
|
||||||
hs_dir = aug.get(TOR_CONFIG + '/HiddenServiceDir')
|
hs_dir = aug.get(TOR_CONFIG + '/HiddenServiceDir')
|
||||||
hs_port_paths = aug.match(TOR_CONFIG + '/HiddenServicePort')
|
hs_port_paths = aug.match(TOR_CONFIG + '/HiddenServicePort')
|
||||||
|
|
||||||
@ -271,10 +273,11 @@ def _disable_relay(restart=False):
|
|||||||
|
|
||||||
def _enable_hs(restart=True):
|
def _enable_hs(restart=True):
|
||||||
"""Enable Tor hidden service"""
|
"""Enable Tor hidden service"""
|
||||||
if get_hidden_service()['enabled']:
|
aug = augeas_load()
|
||||||
|
|
||||||
|
if _get_hidden_service(aug)['enabled']:
|
||||||
return
|
return
|
||||||
|
|
||||||
aug = augeas_load()
|
|
||||||
aug.set(TOR_CONFIG + '/HiddenServiceDir',
|
aug.set(TOR_CONFIG + '/HiddenServiceDir',
|
||||||
'/var/lib/tor/hidden_service')
|
'/var/lib/tor/hidden_service')
|
||||||
aug.set(TOR_CONFIG + '/HiddenServicePort[1]',
|
aug.set(TOR_CONFIG + '/HiddenServicePort[1]',
|
||||||
@ -291,7 +294,7 @@ def _enable_hs(restart=True):
|
|||||||
|
|
||||||
# wait until hidden service information is available
|
# wait until hidden service information is available
|
||||||
tries = 0
|
tries = 0
|
||||||
while not get_hidden_service()['enabled']:
|
while not _get_hidden_service()['enabled']:
|
||||||
tries += 1
|
tries += 1
|
||||||
if tries >= 12:
|
if tries >= 12:
|
||||||
return
|
return
|
||||||
@ -301,10 +304,11 @@ def _enable_hs(restart=True):
|
|||||||
|
|
||||||
def _disable_hs(restart=True):
|
def _disable_hs(restart=True):
|
||||||
"""Disable Tor hidden service"""
|
"""Disable Tor hidden service"""
|
||||||
if not get_hidden_service()['enabled']:
|
aug = augeas_load()
|
||||||
|
|
||||||
|
if not _get_hidden_service(aug)['enabled']:
|
||||||
return
|
return
|
||||||
|
|
||||||
aug = augeas_load()
|
|
||||||
aug.remove(TOR_CONFIG + '/HiddenServiceDir')
|
aug.remove(TOR_CONFIG + '/HiddenServiceDir')
|
||||||
aug.remove(TOR_CONFIG + '/HiddenServicePort')
|
aug.remove(TOR_CONFIG + '/HiddenServicePort')
|
||||||
aug.save()
|
aug.save()
|
||||||
@ -356,7 +360,7 @@ def _update_ports():
|
|||||||
|
|
||||||
# port information may not be available immediately after Tor started
|
# port information may not be available immediately after Tor started
|
||||||
while not ready:
|
while not ready:
|
||||||
ports = get_ports()
|
ports = _get_ports()
|
||||||
ready = 'orport' in ports and 'obfs3' in ports and 'obfs4' in ports
|
ready = 'orport' in ports and 'obfs3' in ports and 'obfs4' in ports
|
||||||
if ready:
|
if ready:
|
||||||
break
|
break
|
||||||
|
|||||||
@ -77,7 +77,7 @@ def init():
|
|||||||
hostname = status['hs_hostname']
|
hostname = status['hs_hostname']
|
||||||
hs_virtports = [port['virtport'] for port in status['hs_ports']]
|
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']:
|
status['hs_enabled'] and status['hs_hostname']:
|
||||||
hs_services = []
|
hs_services = []
|
||||||
for service_type in SERVICES:
|
for service_type in SERVICES:
|
||||||
@ -120,7 +120,6 @@ def update_hidden_service_domain(status=None):
|
|||||||
services=status['hs_services'])
|
services=status['hs_services'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def diagnose():
|
def diagnose():
|
||||||
"""Run diagnostics and return the results."""
|
"""Run diagnostics and return the results."""
|
||||||
results = []
|
results = []
|
||||||
|
|||||||
@ -94,34 +94,34 @@
|
|||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% if status.relay_enabled %}
|
{% if status.relay_enabled %}
|
||||||
<h3>{% trans "Bridge Relay" %}</h3>
|
<h3>{% trans "Bridge Relay" %}</h3>
|
||||||
<p>
|
<p>
|
||||||
{% blocktrans trimmed %}
|
{% blocktrans trimmed %}
|
||||||
If your {{ box_name }} is behind a router or firewall, you should
|
If your {{ box_name }} is behind a router or firewall, you should
|
||||||
make sure the following ports are open, and port-forwarded, if
|
make sure the following ports are open, and port-forwarded, if
|
||||||
necessary:
|
necessary:
|
||||||
{% endblocktrans %}
|
{% endblocktrans %}
|
||||||
</p>
|
</p>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-sm-3">
|
<div class="col-sm-3">
|
||||||
<table class="table table-bordered table-condensed table-striped">
|
<table class="table table-bordered table-condensed table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
|
||||||
<th>{% trans "Service" %}</th>
|
|
||||||
<th>{% trans "Port" %}</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{% for name, port in status.ports.items %}
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ name }}</td>
|
<th>{% trans "Service" %}</th>
|
||||||
<td>{{ port }}</td>
|
<th>{% trans "Port" %}</th>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
</thead>
|
||||||
</tbody>
|
<tbody>
|
||||||
</table>
|
{% for name, port in status.ports.items %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ name }}</td>
|
||||||
|
<td>{{ port }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<h3>{% trans "SOCKS" %}</h3>
|
<h3>{% trans "SOCKS" %}</h3>
|
||||||
|
|||||||
@ -48,7 +48,6 @@ def get_status():
|
|||||||
"""Return current Tor status."""
|
"""Return current Tor status."""
|
||||||
output = actions.superuser_run('tor', ['get-status'])
|
output = actions.superuser_run('tor', ['get-status'])
|
||||||
status = json.loads(output)
|
status = json.loads(output)
|
||||||
ports = status['ports']
|
|
||||||
|
|
||||||
hs_info = status['hidden_service']
|
hs_info = status['hidden_service']
|
||||||
hs_services = []
|
hs_services = []
|
||||||
@ -60,21 +59,21 @@ def get_status():
|
|||||||
return {'enabled': is_enabled(),
|
return {'enabled': is_enabled(),
|
||||||
'is_running': is_running(),
|
'is_running': is_running(),
|
||||||
'relay_enabled': status['relay_enabled'],
|
'relay_enabled': status['relay_enabled'],
|
||||||
'ports': ports,
|
'ports': status['ports'],
|
||||||
'hs_enabled': hs_info['enabled'],
|
'hs_enabled': hs_info['enabled'],
|
||||||
'hs_status': hs_info['status'],
|
'hs_status': hs_info['status'],
|
||||||
'hs_hostname': hs_info['hostname'],
|
'hs_hostname': hs_info['hostname'],
|
||||||
'hs_ports': hs_info['ports'],
|
'hs_ports': hs_info['ports'],
|
||||||
'hs_services': hs_services,
|
'hs_services': hs_services,
|
||||||
'apt_transport_tor_enabled': \
|
'apt_transport_tor_enabled':
|
||||||
_is_apt_transport_tor_enabled()
|
_is_apt_transport_tor_enabled()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def iter_apt_uris(aug):
|
def iter_apt_uris(aug):
|
||||||
"""Iterate over all the APT source URIs."""
|
"""Iterate over all the APT source URIs."""
|
||||||
return itertools.chain.from_iterable([aug.match(path) for \
|
return itertools.chain.from_iterable([aug.match(path)
|
||||||
path in APT_SOURCES_URI_PATHS])
|
for path in APT_SOURCES_URI_PATHS])
|
||||||
|
|
||||||
|
|
||||||
def get_real_apt_uri_path(aug, path):
|
def get_real_apt_uri_path(aug, path):
|
||||||
@ -104,7 +103,8 @@ def get_augeas():
|
|||||||
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
||||||
augeas.Augeas.NO_MODL_AUTOLOAD)
|
augeas.Augeas.NO_MODL_AUTOLOAD)
|
||||||
aug.set('/augeas/load/Aptsources/lens', 'Aptsources.lns')
|
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]',
|
aug.set('/augeas/load/Aptsources/incl[last() + 1]',
|
||||||
'/etc/apt/sources.list.d/*.list')
|
'/etc/apt/sources.list.d/*.list')
|
||||||
aug.load()
|
aug.load()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user