From 0e582d15a3d505073a0627c392f7e4318731e479 Mon Sep 17 00:00:00 2001 From: MadcowOG Date: Thu, 5 May 2022 22:50:13 -0700 Subject: [PATCH] Made password auth independent of other method calls. --- app/__init__.py | 8 +------- app/request.py | 51 ++++++++++++++++++++++--------------------------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 9c7aed5..ae8ac06 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -149,13 +149,7 @@ app.jinja_env.globals.update( Session(app) # Attempt to acquire tor identity, to determine if Tor config is available -confloc = os.getenv('WHOOGLE_TOR_CONF') -if confloc is not None and os.path.isfile(confloc): - send_tor_signal( - Signal.HEARTBEAT, - confloc=confloc) -else: - send_tor_signal(Signal.HEARTBEAT) +send_tor_signal(Signal.HEARTBEAT) from app import routes # noqa diff --git a/app/request.py b/app/request.py index a14a177..7cd8dc9 100644 --- a/app/request.py +++ b/app/request.py @@ -1,4 +1,5 @@ from app.models.config import Config +from app.utils.misc import read_config_bool from datetime import datetime from defusedxml import ElementTree as ET import random @@ -37,39 +38,33 @@ class TorError(Exception): super().__init__(message) -def send_tor_signal(signal: Signal, confloc='./misc/tor/control.conf') -> bool: +def send_tor_signal(signal: Signal) -> bool: + use_pass = read_config_bool('WHOOGLE_TOR_USE_PASS') + + confloc = './misc/tor/control.conf' + # Check that the custom location of conf real. + temp = os.getenv('WHOOGLE_TOR_CONF', '') + if os.path.isfile(temp): + confloc = temp + + # Attempt to authenticate and send signal. try: - # Try to authenticate with password. - with open(confloc, "r") as conf: - for line in conf: - pass - secret = line with Controller.from_port(port=9051) as c: - authenticate_password(c, password=secret) - c.signal(signal) - os.environ['TOR_AVAILABLE'] = '1' - return True - except ( - SocketError, - ConnectionRefusedError, - ConnectionError, - FileNotFoundError - ): - # If password doesn't work try with cookie. - try: - with Controller.from_port(port=9051) as c: + if use_pass: + with open(confloc, "r") as conf: + for line in conf: + pass + secret = line + authenticate_password(c, password=secret) + else: cookie_path = '/var/lib/tor/control_auth_cookie' authenticate_cookie(c, cookie_path=cookie_path) - c.signal(signal) - os.environ['TOR_AVAILABLE'] = '1' + c.signal(signal) + os.environ['TOR_AVAILABLE'] = '1' return True - except (SocketError, ConnectionRefusedError, ConnectionError): - # If neither words tor isn't configured correctly, or not set up. - os.environ['TOR_AVAILABLE'] = '0' - print( - "Unable to authenticate with tor control port." + - " Tor will be unavailable.") - return False + except (SocketError, ConnectionRefusedError, ConnectionError): + os.environ['TOR_AVAILABLE'] = '0' + return False def gen_user_agent(is_mobile) -> str: