Added password authentication for tor control port.
This commit is contained in:
parent
194ddc33f3
commit
c56fce51ec
|
@ -8,6 +8,7 @@ import urllib.parse as urlparse
|
||||||
import os
|
import os
|
||||||
from stem import Signal, SocketError
|
from stem import Signal, SocketError
|
||||||
from stem.control import Controller
|
from stem.control import Controller
|
||||||
|
from stem.connection import authenticate_cookie, authenticate_password
|
||||||
|
|
||||||
MAPS_URL = 'https://maps.google.com/maps'
|
MAPS_URL = 'https://maps.google.com/maps'
|
||||||
AUTOCOMPLETE_URL = ('https://suggestqueries.google.com/'
|
AUTOCOMPLETE_URL = ('https://suggestqueries.google.com/'
|
||||||
|
@ -38,15 +39,26 @@ class TorError(Exception):
|
||||||
|
|
||||||
def send_tor_signal(signal: Signal) -> bool:
|
def send_tor_signal(signal: Signal) -> bool:
|
||||||
try:
|
try:
|
||||||
|
# Try to authenticate with password.
|
||||||
with Controller.from_port(port=9051) as c:
|
with Controller.from_port(port=9051) as c:
|
||||||
c.authenticate()
|
with open("./misc/tor/control.conf", "r") as conf:
|
||||||
c.signal(signal)
|
for line in conf:
|
||||||
os.environ['TOR_AVAILABLE'] = '1'
|
pass
|
||||||
|
secret = line
|
||||||
|
authenticate_password(c, password=secret)
|
||||||
|
c.signal(signal)
|
||||||
|
os.environ['TOR_AVAILABLE'] = '1'
|
||||||
return True
|
return True
|
||||||
except (SocketError, ConnectionRefusedError, ConnectionError):
|
except (SocketError, ConnectionRefusedError, ConnectionError):
|
||||||
os.environ['TOR_AVAILABLE'] = '0'
|
# If password doesn't work try with cookie.
|
||||||
|
try:
|
||||||
return False
|
with Controller.from_port(port=9051) as c:
|
||||||
|
authenticate_cookie(c, cookie_path="/var/lib/tor/control_auth_cookie")
|
||||||
|
c.signal(signal)
|
||||||
|
os.environ['TOR_AVAILABLE'] = '1'
|
||||||
|
except (SocketError, ConnectionRefusedError, ConnectionError):
|
||||||
|
os.environ['TOR_AVAILABLE'] = '0'
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def gen_user_agent(is_mobile) -> str:
|
def gen_user_agent(is_mobile) -> str:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user