Improved Tor Error messages
This commit is contained in:
parent
093e9aacd8
commit
a1409fd1d5
7 changed files with 77 additions and 30 deletions
|
|
@ -59,7 +59,7 @@ class ClientController:
|
||||||
if client_observer is not None:
|
if client_observer is not None:
|
||||||
client_observer.notify('synchronizing', "Fetching list of new data ..")
|
client_observer.notify('synchronizing', "Fetching list of new data ..")
|
||||||
|
|
||||||
result = coordinate_cache_sync(ClientObserver, ConnectionObserver)
|
result = coordinate_cache_sync(client_observer, connection_observer)
|
||||||
|
|
||||||
# logger.info(f"We got a Result from the API of {result}")
|
# logger.info(f"We got a Result from the API of {result}")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -221,9 +221,16 @@ class ConnectionController:
|
||||||
ConnectionController.terminate_tor_connection()
|
ConnectionController.terminate_tor_connection()
|
||||||
time.sleep(1.0)
|
time.sleep(1.0)
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def test_observer(connection_observer: Optional[ConnectionObserver] = None):
|
||||||
|
if connection_observer is not None:
|
||||||
|
connection_observer.notify('custom_message', "HELLO")
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def establish_tor_connection(connection_observer: Optional[ConnectionObserver] = None):
|
def establish_tor_connection(connection_observer: Optional[ConnectionObserver] = None):
|
||||||
|
try:
|
||||||
tor_module = TorModule(Constants.HV_TOR_STATE_HOME)
|
tor_module = TorModule(Constants.HV_TOR_STATE_HOME)
|
||||||
tor_module.start_service(connection_observer)
|
tor_module.start_service(connection_observer)
|
||||||
|
|
||||||
|
|
@ -231,6 +238,9 @@ class ConnectionController:
|
||||||
|
|
||||||
for port_number in session_state.network_port_numbers.tor:
|
for port_number in session_state.network_port_numbers.tor:
|
||||||
tor_module.create_session(port_number)
|
tor_module.create_session(port_number)
|
||||||
|
except:
|
||||||
|
if connection_observer is not None:
|
||||||
|
connection_observer.notify('custom_message', "Tor Can't Initialize")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def terminate_tor_connection():
|
def terminate_tor_connection():
|
||||||
|
|
@ -245,6 +255,8 @@ class ConnectionController:
|
||||||
tor_module.create_session(port_number, connection_observer)
|
tor_module.create_session(port_number, connection_observer)
|
||||||
except TorServiceInitializationError as e:
|
except TorServiceInitializationError as e:
|
||||||
logger.error(f"TorServiceInitializationError. Tor Can't Start: {e}")
|
logger.error(f"TorServiceInitializationError. Tor Can't Start: {e}")
|
||||||
|
if connection_observer is not None:
|
||||||
|
connection_observer.notify('custom_message', "Tor Can't Initialize")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Tor Can't Start: {e}")
|
logger.error(f"Tor Can't Start: {e}")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,7 @@ from core.errors.logger import logger
|
||||||
def evaluate_regular_networking_problem(
|
def evaluate_regular_networking_problem(
|
||||||
url: str,
|
url: str,
|
||||||
connection_observer: ConnectionObserver,
|
connection_observer: ConnectionObserver,
|
||||||
port_number: int | None = None,
|
client_observer: Optional[ClientObserver] = None
|
||||||
proxies: dict | None = None,
|
|
||||||
) -> dict:
|
) -> dict:
|
||||||
|
|
||||||
domain_only = extract_domain(url)
|
domain_only = extract_domain(url)
|
||||||
|
|
@ -46,27 +45,34 @@ def evaluate_regular_networking_problem(
|
||||||
def evaluate_tor_networking_problem(
|
def evaluate_tor_networking_problem(
|
||||||
url: str,
|
url: str,
|
||||||
connection_observer: ConnectionObserver,
|
connection_observer: ConnectionObserver,
|
||||||
|
client_observer: Optional[ClientObserver] = None,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
|
|
||||||
|
print("evaluate_tor_networking_problem is called")
|
||||||
|
|
||||||
|
if client_observer is not None:
|
||||||
|
print("client observer is not none")
|
||||||
|
client_observer.notify('synchronizing', f"Evaluating Tor..")
|
||||||
|
|
||||||
domain_only = extract_domain(url)
|
domain_only = extract_domain(url)
|
||||||
logger.debug(f"Evaluating a networking problem for a Tor connection")
|
logger.debug(f"Evaluating a networking problem for a Tor connection")
|
||||||
|
|
||||||
logger.debug("checking if Tor works against the official Tor Project API..")
|
logger.debug("checking if Tor works against the official Tor Project API..")
|
||||||
if not is_tor_working(connection_observer):
|
# if not is_tor_working(connection_observer):
|
||||||
return {"valid": False, "error_code": "tor_issue"}
|
# return {"valid": False, "error_code": "tor_issue"}
|
||||||
else:
|
# else:
|
||||||
tor_fine = f"While there were connection issues, but Tor is working fine!"
|
# tor_fine = f"While there were connection issues, but Tor is working fine!"
|
||||||
logger.error(tor_fine, exc_info=True)
|
# logger.error(tor_fine, exc_info=True)
|
||||||
logger.debug(tor_fine)
|
# logger.debug(tor_fine)
|
||||||
|
|
||||||
logger.debug("Check the original DNS via Tor...")
|
logger.debug("Check the original DNS via Tor...")
|
||||||
|
|
||||||
if is_dns_problem_via_tor(domain_only, connection_observer):
|
# if is_dns_problem_via_tor(domain_only, connection_observer):
|
||||||
return {"valid": False, "error_code": "dns_issue"}
|
# return {"valid": False, "error_code": "dns_issue"}
|
||||||
else:
|
# else:
|
||||||
dns_fine = f"There were connection issues, but the DNS for {domain_only} via Tor is working fine!"
|
# dns_fine = f"There were connection issues, but the DNS for {domain_only} via Tor is working fine!"
|
||||||
logger.error(dns_fine, exc_info=True)
|
# logger.error(dns_fine, exc_info=True)
|
||||||
logger.debug(dns_fine)
|
# logger.debug(dns_fine)
|
||||||
|
|
||||||
# can't solve it:
|
# can't solve it:
|
||||||
return {"valid": False, "error_code": "unknown"}
|
return {"valid": False, "error_code": "unknown"}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
# if TYPE_CHECKING:
|
||||||
|
from core.observers.ClientObserver import ClientObserver
|
||||||
from essentials.observers.ConnectionObserver import ConnectionObserver
|
from essentials.observers.ConnectionObserver import ConnectionObserver
|
||||||
|
|
||||||
|
from core.observers.ClientObserver import ClientObserver
|
||||||
|
from core.services.networking.evaluate_networking_problem import evaluate_tor_networking_problem
|
||||||
|
|
||||||
# services
|
# services
|
||||||
from core.services.networking.get_connection_type import get_connection_type
|
from core.services.networking.get_connection_type import get_connection_type
|
||||||
from core.services.networking.use_tor import tor_get_request
|
from core.services.networking.use_tor import tor_get_request
|
||||||
|
|
@ -14,13 +19,13 @@ import traceback
|
||||||
|
|
||||||
|
|
||||||
# Generic GET request to an endpoint, filtered by the user's preference of connection type (Tor or Not)
|
# Generic GET request to an endpoint, filtered by the user's preference of connection type (Tor or Not)
|
||||||
def get_data_from_server(url: str, connection_observer: ConnectionObserver) -> dict:
|
def get_data_from_server(url: str, connection_observer: ConnectionObserver, client_observer: Optional[ClientObserver] = None) -> dict:
|
||||||
try:
|
try:
|
||||||
connection_type = get_connection_type()
|
connection_type = get_connection_type()
|
||||||
logger.debug(f"connection type is: {connection_type}")
|
logger.debug(f"connection type is: {connection_type}")
|
||||||
|
|
||||||
if connection_type == "tor":
|
if connection_type == "tor":
|
||||||
response = tor_get_request(url, connection_observer)
|
response = tor_get_request(url, connection_observer, client_observer)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
response = regular_get_request(url, connection_observer)
|
response = regular_get_request(url, connection_observer)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@ if TYPE_CHECKING:
|
||||||
from essentials.modules.TorModule import TorModule
|
from essentials.modules.TorModule import TorModule
|
||||||
from essentials.observers.ConnectionObserver import ConnectionObserver
|
from essentials.observers.ConnectionObserver import ConnectionObserver
|
||||||
from essentials.services.ConnectionService import ConnectionService
|
from essentials.services.ConnectionService import ConnectionService
|
||||||
|
|
||||||
|
from core.observers.ClientObserver import ClientObserver
|
||||||
|
|
||||||
# services
|
# services
|
||||||
from core.services.networking.evaluate_networking_problem import evaluate_tor_networking_problem
|
from core.services.networking.evaluate_networking_problem import evaluate_tor_networking_problem
|
||||||
# errors
|
# errors
|
||||||
|
|
@ -18,7 +21,7 @@ from typing import Any
|
||||||
from core.Constants import Constants
|
from core.Constants import Constants
|
||||||
import time
|
import time
|
||||||
|
|
||||||
def tor_get_request(custom_url: str, connection_observer: ConnectionObserver) -> dict:
|
def tor_get_request(custom_url: str, connection_observer: ConnectionObserver, client_observer: Optional[ClientObserver] = None) -> dict:
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|
@ -26,18 +29,25 @@ def tor_get_request(custom_url: str, connection_observer: ConnectionObserver) ->
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
port_number = ConnectionService.get_random_available_port_number()
|
port_number = ConnectionService.get_random_available_port_number()
|
||||||
logger.debug(f"[USE TOR SERVICE] Using Tor on port number {port_number}")
|
logger.debug(f"[USE TOR SERVICE] Using Tor on port number {port_number}")
|
||||||
|
if client_observer is not None:
|
||||||
|
client_observer.notify('synchronizing', f"Using Tor on Port {port_number}..")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tor_module = TorModule(Constants.HV_TOR_STATE_HOME)
|
tor_module = TorModule(Constants.HV_TOR_STATE_HOME)
|
||||||
tor_module.create_session(port_number, connection_observer)
|
tor_module.create_session(port_number, connection_observer)
|
||||||
logger.debug(f"[USE TOR SERVICE] Tor Started a Session")
|
logger.debug(f"[USE TOR SERVICE] Tor Started a Session")
|
||||||
|
if client_observer is not None:
|
||||||
|
client_observer.notify('synchronizing', f"Initialized Tor..")
|
||||||
|
|
||||||
except TorServiceInitializationError as e:
|
except TorServiceInitializationError as e:
|
||||||
error_msg = f"[USE TOR SERVICE] Tor failed to start: {e}"
|
error_msg = f"[USE TOR SERVICE] Tor failed to start: {e}"
|
||||||
tor_module.destroy_session(port_number)
|
tor_module.destroy_session(port_number)
|
||||||
logger.error(error_msg, exc_info=True)
|
logger.error(error_msg, exc_info=True)
|
||||||
|
if client_observer is not None:
|
||||||
|
client_observer.notify('synchronizing', f"Tor Failed to Initialize")
|
||||||
|
|
||||||
problem_results = evaluate_tor_networking_problem(
|
problem_results = evaluate_tor_networking_problem(
|
||||||
custom_url, connection_observer
|
custom_url, connection_observer, client_observer
|
||||||
)
|
)
|
||||||
return problem_results
|
return problem_results
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -73,6 +83,9 @@ def tor_get_request(custom_url: str, connection_observer: ConnectionObserver) ->
|
||||||
return final_reply
|
return final_reply
|
||||||
|
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
|
if client_observer is not None:
|
||||||
|
client_observer.notify('synchronizing', f"Tor Connection Error")
|
||||||
|
|
||||||
tor_module.destroy_session(port_number)
|
tor_module.destroy_session(port_number)
|
||||||
problem_results = evaluate_tor_networking_problem(
|
problem_results = evaluate_tor_networking_problem(
|
||||||
custom_url, connection_observer
|
custom_url, connection_observer
|
||||||
|
|
@ -81,6 +94,9 @@ def tor_get_request(custom_url: str, connection_observer: ConnectionObserver) ->
|
||||||
|
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
logger.debug("Connection timed out")
|
logger.debug("Connection timed out")
|
||||||
|
if client_observer is not None:
|
||||||
|
client_observer.notify('synchronizing', f"Tor Connection Timeout")
|
||||||
|
|
||||||
tor_module.destroy_session(port_number)
|
tor_module.destroy_session(port_number)
|
||||||
problem_results = evaluate_tor_networking_problem(
|
problem_results = evaluate_tor_networking_problem(
|
||||||
custom_url, connection_observer
|
custom_url, connection_observer
|
||||||
|
|
@ -88,7 +104,11 @@ def tor_get_request(custom_url: str, connection_observer: ConnectionObserver) ->
|
||||||
return problem_results
|
return problem_results
|
||||||
|
|
||||||
except requests.exceptions.HTTPError:
|
except requests.exceptions.HTTPError:
|
||||||
logger.debug(f"HTTP error: {response.status_code}")
|
tor_http_error = f"Tor HTTP error: {response.status_code}"
|
||||||
|
logger.debug(tor_http_error)
|
||||||
|
if client_observer is not None:
|
||||||
|
client_observer.notify('synchronizing', tor_http_error)
|
||||||
|
|
||||||
tor_module.destroy_session(port_number)
|
tor_module.destroy_session(port_number)
|
||||||
problem_results = evaluate_tor_networking_problem(
|
problem_results = evaluate_tor_networking_problem(
|
||||||
custom_url, connection_observer
|
custom_url, connection_observer
|
||||||
|
|
@ -105,6 +125,9 @@ def tor_get_request(custom_url: str, connection_observer: ConnectionObserver) ->
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[USE TOR SERVICE] Unexpected Tor error: {e}")
|
logger.error(f"[USE TOR SERVICE] Unexpected Tor error: {e}")
|
||||||
|
if client_observer is not None:
|
||||||
|
client_observer.notify('synchronizing', f"Tor Error: {e}")
|
||||||
|
|
||||||
tor_module.destroy_session(port_number)
|
tor_module.destroy_session(port_number)
|
||||||
problem_results = evaluate_tor_networking_problem(
|
problem_results = evaluate_tor_networking_problem(
|
||||||
custom_url, connection_observer
|
custom_url, connection_observer
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ def get_data_from_api(
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logger.debug("Fetching data from API server...")
|
logger.debug("Fetching data from API server...")
|
||||||
sync_results = get_data_from_server(url, connection_observer)
|
sync_results = get_data_from_server(url, connection_observer, client_observer)
|
||||||
|
|
||||||
if sync_results in rejected_list:
|
if sync_results in rejected_list:
|
||||||
error_msg = "API returned no data"
|
error_msg = "API returned no data"
|
||||||
|
|
@ -39,6 +39,7 @@ def get_data_from_api(
|
||||||
|
|
||||||
logger.debug(f"Raw API response: {sync_results}")
|
logger.debug(f"Raw API response: {sync_results}")
|
||||||
|
|
||||||
|
final_result = sync_results.get("data", None)
|
||||||
final_result = sync_results.get("data", None)
|
final_result = sync_results.get("data", None)
|
||||||
|
|
||||||
if final_result:
|
if final_result:
|
||||||
|
|
|
||||||
|
|
@ -147,7 +147,7 @@ def coordinate_cache_sync(
|
||||||
- 'filtered_metadata': the metadata from the server after removing keys not in the ORM
|
- 'filtered_metadata': the metadata from the server after removing keys not in the ORM
|
||||||
"""
|
"""
|
||||||
|
|
||||||
api_result = get_data_from_api("cachedsync", ClientObserver, ConnectionObserver)
|
api_result = get_data_from_api("cachedsync", client_observer, connection_observer)
|
||||||
|
|
||||||
# we trust the 'get_data_from_api' function to give us a dictionary:
|
# we trust the 'get_data_from_api' function to give us a dictionary:
|
||||||
if not api_result.get("success"):
|
if not api_result.get("success"):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue