Made password auth independent of other method calls.

This commit is contained in:
MadcowOG 2022-05-05 22:50:13 -07:00
parent bd333c42b3
commit 0e582d15a3
2 changed files with 24 additions and 35 deletions

View File

@ -149,12 +149,6 @@ app.jinja_env.globals.update(
Session(app) Session(app)
# Attempt to acquire tor identity, to determine if Tor config is available # 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 from app import routes # noqa

View File

@ -1,4 +1,5 @@
from app.models.config import Config from app.models.config import Config
from app.utils.misc import read_config_bool
from datetime import datetime from datetime import datetime
from defusedxml import ElementTree as ET from defusedxml import ElementTree as ET
import random import random
@ -37,38 +38,32 @@ class TorError(Exception):
super().__init__(message) 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:
# Try to authenticate with password. with Controller.from_port(port=9051) as c:
if use_pass:
with open(confloc, "r") as conf: with open(confloc, "r") as conf:
for line in conf: for line in conf:
pass pass
secret = line secret = line
with Controller.from_port(port=9051) as c:
authenticate_password(c, password=secret) authenticate_password(c, password=secret)
c.signal(signal) else:
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:
cookie_path = '/var/lib/tor/control_auth_cookie' cookie_path = '/var/lib/tor/control_auth_cookie'
authenticate_cookie(c, cookie_path=cookie_path) authenticate_cookie(c, cookie_path=cookie_path)
c.signal(signal) c.signal(signal)
os.environ['TOR_AVAILABLE'] = '1' os.environ['TOR_AVAILABLE'] = '1'
return True return True
except (SocketError, ConnectionRefusedError, ConnectionError): except (SocketError, ConnectionRefusedError, ConnectionError):
# If neither words tor isn't configured correctly, or not set up.
os.environ['TOR_AVAILABLE'] = '0' os.environ['TOR_AVAILABLE'] = '0'
print(
"Unable to authenticate with tor control port." +
" Tor will be unavailable.")
return False return False