This document provides API reference documentation for the Tor VPN System Python modules.
Main GUI application with country-specific exit node selection using Tkinter.
setup_logging(log_file=LOG_FILE)Setup logging with rotating file handlers.
Parameters:
log_file (str): Path to log file (default: "vpn_app_advanced.log")Returns:
logger: Configured logger instanceExample:
logger = setup_logging("custom.log")
setup_signal_handlers()Setup signal handlers for clean termination (SIGTERM, SIGINT).
Example:
setup_signal_handlers()
ensure_admin_privileges()Ensure the script is running with administrator privileges.
Raises:
SystemExit: If not running with appropriate privilegesExample:
ensure_admin_privileges()
connect_to_tor(country_code)Connect to Tor using the specified country code for the exit node.
Parameters:
country_code (str): Two-letter country code (e.g., "us", "de")Raises:
ValueError: If invalid country code providedException: For connection failuresExample:
from tor_vpn_beta import connect_to_tor
connect_to_tor("us")
disconnect_tor()Disconnect from Tor by resetting exit node configurations.
Raises:
Exception: For disconnection failuresExample:
from tor_vpn_beta import disconnect_tor
disconnect_tor()
VPNInterfaceMain GUI class for the Tor VPN client.
Methods:
__init__(master): Initialize the GUI windowcreate_widgets(): Build all GUI componentscreate_status_tab(): Create connection status tabcreate_servers_tab(): Create server list tabconnect(): Connect to Tor with user-specified countrydisconnect(): Disconnect from TorExample:
import tkinter as tk
from tor_vpn_beta import VPNInterface
root = tk.Tk()
app = VPNInterface(root)
root.mainloop()
TOR_PASSWORD_ENV = "TOR_PASSWORD"
TOR_DEFAULT_DIR = os.path.join(os.path.expanduser("~"), ".tor_config")
TOR_CONFIG_FILE = os.path.join(TOR_DEFAULT_DIR, "torrc")
DEFAULT_PASSWORD = "467rSeG7%tGd757575EwPLsaQ$BplwEQJ7676RLsa$3@4161"
PRECOMPUTED_HASHED_PASSWORD = "16:B76A6ED6F4E32AB16028702348A5E765C6A53BCE1F82E467C614392ECD"
LOG_FILE = "vpn_app_advanced.log"
Custom Tor configuration generator with user-specified password and ownership management.
generate_hashed_password(password)Generate the hashed control password for Tor.
Parameters:
password (str): Plain-text passwordReturns:
str: Hashed password stringRaises:
subprocess.CalledProcessError: If hashing failsExample:
from tor_custom_config import generate_hashed_password
hashed = generate_hashed_password("my_password")
change_ownership(file_path, user_name, group_name)Changes the ownership of a file to the specified user and group.
Parameters:
file_path (str): Path to the fileuser_name (str): Username for ownershipgroup_name (str): Group name for ownershipRaises:
KeyError: If user or group doesn't existPermissionError: If insufficient permissionsExample:
from tor_custom_config import change_ownership
change_ownership("/path/to/file", "user", "group")
create_directory(directory)Creates the specified directory with proper permissions (700).
Parameters:
directory (str): Directory path to createExample:
from tor_custom_config import create_directory
create_directory("/home/user/.tor_config")
create_torrc_file()Creates the torrc file with:
Example:
from tor_custom_config import create_torrc_file
create_torrc_file()
verify_file_access(file_path)Verifies file permissions and accessibility.
Parameters:
file_path (str): Path to file to verifyExample:
from tor_custom_config import verify_file_access
verify_file_access("/home/user/.tor_config/torrc")
torrc_directory = os.path.join(os.path.expanduser("~"), ".tor_config")
torrc_path = os.path.join(torrc_directory, "torrc")
control_port = 9051
Automated Tor setup and configuration script with cross-platform support.
check_if_tor_installed()Checks if Tor is already installed.
Returns:
bool: True if installed, False otherwiseExample:
from tor_auto_torrc_config import check_if_tor_installed
if not check_if_tor_installed():
install_tor()
install_tor()Installs Tor depending on the operating system.
Example:
from tor_auto_torrc_config import install_tor
install_tor()
generate_hashed_password(password)Generates hashed control password for Tor.
Parameters:
password (str): Plain-text passwordReturns:
str: Hashed passwordExample:
from tor_auto_torrc_config import generate_hashed_password
hashed = generate_hashed_password("password")
setup_directories(data_dir, log_dir)Creates necessary directories for Tor data and logs.
Parameters:
data_dir (str): Data directory pathlog_dir (str): Log directory pathExample:
from tor_auto_torrc_config import setup_directories
setup_directories("/var/lib/tor", "/var/log/tor")
apply_torrc(data_dir, log_dir, custom_torrc_path=None)Generates and applies the custom torrc configuration file.
Parameters:
data_dir (str): Data directory pathlog_dir (str): Log directory pathcustom_torrc_path (str, optional): Custom torrc pathExample:
from tor_auto_torrc_config import apply_torrc
apply_torrc("/var/lib/tor", "/var/log/tor")
restart_tor()Handles Tor service restart securely with password prompt.
Example:
from tor_auto_torrc_config import restart_tor
restart_tor()
DEFAULT_DATA_DIR = "/var/lib/tor"
DEFAULT_LOG_DIR = "/var/log/tor"
DEFAULT_USER_TORRC_PATH = os.path.expanduser("~/.tor/torrc")
DEFAULT_CONTROL_PASSWORD = "TorSecurePassword123!"
Comprehensive Tor diagnostic and repair tool with init system detection.
parse_arguments()Parses command-line arguments for custom paths and commands.
Returns:
argparse.Namespace: Parsed argumentsCommand-line Arguments:
--tor-binary: Path to Tor binary (default: 'tor')--tor-config: Path to Tor config (default: '/etc/tor/torrc')--custom-start-command: Custom command to start Tor--custom-stop-command: Custom command to stop TorExample:
python tor_diagnostic_repair.py --tor-binary /usr/bin/tor --tor-config /etc/tor/torrc
detect_init_system()Detects the initialization system used by the host system.
Returns:
str: 'systemd', 'sysvinit', or 'manual'Example:
from tor_diagnostic_repair import detect_init_system
init_system = detect_init_system()
validate_tor_configuration()Validates the provided Tor binary and checks the configuration file.
Returns:
bool: True if valid, False otherwiseExample:
from tor_diagnostic_repair import validate_tor_configuration
if validate_tor_configuration():
print("Configuration is valid")
find_tor_process()Locates the Tor process by checking active processes.
Returns:
int or None: PID of Tor process if foundExample:
from tor_diagnostic_repair import find_tor_process
pid = find_tor_process()
if pid:
print(f"Tor running with PID: {pid}")
restart_tor_service(init_system, sudo_password)Restarts the Tor service using the detected init system.
Parameters:
init_system (str): Detected init systemsudo_password (str): Sudo passwordReturns:
bool: Success statusExample:
from tor_diagnostic_repair import restart_tor_service
restart_tor_service("systemd", "password")
collect_diagnostics(output_dir="diagnostics")Collects diagnostic logs and custom configuration files.
Parameters:
output_dir (str): Directory for diagnostic outputExample:
from tor_diagnostic_repair import collect_diagnostics
collect_diagnostics("my_diagnostics")
Tor network connectivity and performance testing tool.
is_tor_running()Checks if the Tor service is running.
Returns:
bool: True if running, False otherwiseExample:
from tor_network_test import is_tor_running
if is_tor_running():
print("Tor is running")
check_port_status(host, port)Checks if the specified port is open and listening.
Parameters:
host (str): Host addressport (int): Port numberReturns:
bool: True if open, False otherwiseExample:
from tor_network_test import check_port_status
if check_port_status("127.0.0.1", 9051):
print("Control port is accessible")
detect_tor_control_port()Detects the Tor control port from the torrc file.
Returns:
int: Control port number (default: 9051)Example:
from tor_network_test import detect_tor_control_port
port = detect_tor_control_port()
detect_tor_password()Tries to auto-detect the Tor Control Port password from torrc files.
Returns:
str or None: Detected password (or None if hashed)Example:
from tor_network_test import detect_tor_password
password = detect_tor_password()
detect_auth_cookie()Tries to auto-detect the Tor Control Port authentication cookie.
Returns:
str or None: Path to auth cookie if foundExample:
from tor_network_test import detect_auth_cookie
cookie_path = detect_auth_cookie()
test_tor_connection()Main function to test Tor network connectivity.
Returns:
dict: Test results containing:connection (bool): Connection statuslatency (float): Latency in secondsexit_ip (str): Exit IP addresserrors (list): List of error messagesExample:
from tor_network_test import test_tor_connection
results = test_tor_connection()
print(f"Connection: {results['connection']}")
print(f"Latency: {results['latency']}s")
print(f"Exit IP: {results['exit_ip']}")
TORRC_PATHS = [
"/etc/tor/torrc",
"/usr/local/etc/tor/torrc",
os.path.expanduser("~/.torrc")
]
AUTH_COOKIE_PATHS = [
"/run/tor/control.authcookie",
"/var/lib/tor/control_auth_cookie"
]
Transparent proxy setup script for routing all system traffic through Tor.
is_root()Checks if the script is being run as root.
Returns:
bool: True if root, False otherwiseExample:
from tor_route_traffic_setup import is_root
if not is_root():
print("This script requires root privileges")
stop_tor_if_running()Checks if Tor is running and stops it if necessary.
Example:
from tor_route_traffic_setup import stop_tor_if_running
stop_tor_if_running()
create_directory(directory)Creates the specified directory with proper permissions (700).
Parameters:
directory (str): Directory pathExample:
from tor_route_traffic_setup import create_directory
create_directory("/home/user/.tor_config")
create_torrc_file()Creates the torrc file with transparent proxy configuration.
Example:
from tor_route_traffic_setup import create_torrc_file
create_torrc_file()
install_packages()Installs necessary packages (tor, iptables-persistent).
Example:
from tor_route_traffic_setup import install_packages
install_packages()
get_tor_uid()Gets the UID of the 'debian-tor' user.
Returns:
str: UID as stringExample:
from tor_route_traffic_setup import get_tor_uid
uid = get_tor_uid()
setup_iptables(tor_uid)Sets up iptables rules to redirect all traffic through Tor.
Parameters:
tor_uid (str): Tor user ID for owner matchingExample:
from tor_route_traffic_setup import setup_iptables
setup_iptables("100")
verify_tor_connection()Verifies that traffic is routed through Tor.
Example:
from tor_route_traffic_setup import verify_tor_connection
verify_tor_connection()
torrc_directory = os.path.join(os.path.expanduser("~"), ".tor_config")
torrc_path = os.path.join(torrc_directory, "torrc")
control_port = 9051
hashed_control_password = "16:B76A6ED6F4E32AB16028702348A5E765C6A53BCE1F82E467C614392ECD"
Tor startup validation and configuration management script.
is_tor_running()Checks if Tor is running on the current platform.
Returns:
bool: True if running, False otherwiseExample:
from tor_vpn_inclued import is_tor_running
if is_tor_running():
print("Tor is already running")
generate_hashed_password(password)Generates the hashed control password for Tor.
Parameters:
password (str): Plain-text passwordReturns:
str: Hashed passwordExample:
from tor_vpn_inclued import generate_hashed_password
hashed = generate_hashed_password("password")
start_tor_manual(torrc_path)Starts Tor manually without relying on system services.
Parameters:
torrc_path (str): Path to torrc configuration fileReturns:
bool: True if successful, False otherwiseExample:
from tor_vpn_inclued import start_tor_manual
if start_tor_manual("/path/to/torrc"):
print("Tor started successfully")
validate_and_generate_config()Validates and generates the Tor configuration file if missing.
Returns:
tuple: (torrc_path, data_directory) or (None, None) on failureExample:
from tor_vpn_inclued import validate_and_generate_config
torrc_path, data_dir = validate_and_generate_config()
TORRC_DEFAULT_CONTENT = """SocksPort 9050
ControlPort 9051
HashedControlPassword {hashed_password}
DataDirectory {data_directory}
"""
OPERATING_SYSTEM = platform.system()
The system uses the Stem library for Tor control port communication.
from stem.control import Controller
with Controller.from_port(port=9051) as controller:
controller.authenticate(password="your_password")
# Perform operations
controller.authenticate(password="plain_password")
controller.authenticate(cookie_path="/run/tor/control.authcookie")
controller.authenticate()
controller.set_conf("ExitNodes", "{us}")
controller.signal(Signal.NEWNYM)
circuits = controller.get_circuits()
for circuit in circuits:
print(f"Circuit {circuit.id}: {circuit.purpose}")
version = controller.get_version()
print(f"Tor version: {version}")
from stem import Signal
controller.signal(Signal.NEWNYM)
Raised when Tor connection fails.
try:
connect_to_tor("us")
except TorConnectionError as e:
print(f"Connection failed: {e}")
Raised when authentication fails.
try:
controller.authenticate(password="password")
except AuthenticationError as e:
print(f"Authentication failed: {e}")
Raised when configuration is invalid.
try:
validate_tor_configuration()
except ConfigurationError as e:
print(f"Configuration error: {e}")
Last Updated: 2024-04-23