Skip to main content

A Tor process management library

Project description

dtor - Tor Process Management Library

Tests PyPI version Python 3.8+ License: MIT

A comprehensive Python library for managing Tor processes with full lifecycle control. dtor handles everything from automatic binary downloads to hidden service management, providing a simple and powerful interface for Tor integration.

Features

🚀 Automatic Setup

  • Automatic Tor binary download for your platform (Linux, Windows, macOS)
  • Smart version detection and caching
  • Zero manual configuration required

🔒 Hidden Services

  • Easy hidden service creation and management
  • Runtime hidden service support (ADD_ONION)
  • Automatic hostname and key generation
  • Port mapping and collision resolution

⚙️ Process Management

  • Full Tor process lifecycle control (start/stop/restart)
  • Process monitoring and health checks
  • Stale process cleanup
  • Cross-platform support

🌐 Port Management

  • SOCKS and Control port configuration
  • Runtime port addition
  • Automatic port conflict detection and resolution
  • Multiple port support

🎛️ Control Protocol

  • Full Tor control protocol support
  • Cookie authentication
  • Command execution and response parsing
  • Real-time configuration updates

Installation

pip install dtor

Quick Start

from dtor import TorHandler

# Initialize handler
handler = TorHandler(recover=False)

# Download Tor binaries (automatic platform detection)
handler.download_and_install_tor_binaries()

# Configure ports
handler.add_socks_port(9050)
handler.add_control_port(9051)

# Start Tor
handler.start_tor_service()

# Create a hidden service
handler.register_hidden_service(port=80, target_port=8080)
handler.save_torrc_configuration()

# Stop Tor when done
handler.stop_tor_service()

Advanced Usage

Port Configuration with Collision Resolution

handler = TorHandler()

# Enable automatic port conflict resolution
handler.socks_port_collision_resolve = True
handler.control_port_collision_resolve = True

# Add ports - will auto-resolve if already in use
handler.add_socks_port(9050)
handler.add_control_port(9051)

Runtime Hidden Services

# Start Tor first
handler.start_tor_service()

# Add hidden service at runtime (no restart needed)
result = handler.register_runtime_hidden_service(
    port=80, 
    target_port=8080, 
    temporary=False
)

if result:
    print(f"Onion address: {result['onion_address']}")
    print(f"Service key: {result['service_key']}")

Control Protocol Commands

# Send commands to Tor control port
responses = handler.send_control_commands([
    "GETINFO version",
    "GETINFO status/bootstrap-phase",
    "GETINFO network-status"
])

for idx, response in responses.items():
    print(f"{response['command']}: {response['response']}")

Process Monitoring

# Get process information
process = handler.get_tor_process()
if process:
    print(f"PID: {process.pid}")
    print(f"Status: {process.status()}")
    print(f"Memory: {process.memory_info().rss / 1024 / 1024:.2f} MB")
    print(f"CPU: {process.cpu_percent()}%")

Multiple Hidden Services

handler = TorHandler()

# Register multiple services
services = [
    (80, 8080, "HTTP"),
    (443, 8443, "HTTPS"),
    (22, 2222, "SSH")
]

for port, target, name in services:
    result = handler.register_hidden_service(
        port=port, 
        target_port=target
    )
    print(f"Registered {name}: {port} -> {target}")

# Save configuration
handler.save_torrc_configuration()

Configuration Options

Initialization Parameters

handler = TorHandler(
    recover=True  # Load existing configuration on startup
)

Port Limits

handler.max_socks_ports = 3       # Maximum SOCKS ports
handler.max_control_ports = 3     # Maximum control ports
handler.max_hidden_services = 3   # Maximum hidden services

Debug Mode

handler.debug = True              # Enable debug logging
handler.log_level = 0             # 0=INFO, 1=WARNING, 2=ERROR

Directory Structure

By default, dtor creates the following directory structure:

tor_handler_files/
├── tor_binaries/          # Tor executables
│   ├── tor/
│   └── data/
├── cache/                 # Downloaded archives
└── config/
    └── torrc              # Tor configuration

Platform Support

  • Linux (Ubuntu, Debian, RHEL, etc.)
  • Windows (7, 8, 10, 11)
  • macOS (Intel & Apple Silicon)

Python Version Support

  • Python 3.8+
  • Python 3.9
  • Python 3.10
  • Python 3.11
  • Python 3.12

Requirements

  • psutil>=5.9.0 - Process management
  • requests>=2.28.0 - HTTP requests for binary downloads

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Testing

Run the comprehensive test suite:

python test.py

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Ahmad Yousuf

Acknowledgments

  • Tor Project for the amazing anonymity network
  • All contributors and users of this library

Links

Disclaimer

This tool is for educational and research purposes. Users are responsible for complying with all applicable laws and regulations. The authors are not responsible for any misuse of this software.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dtor-0.0.6.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dtor-0.0.6-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file dtor-0.0.6.tar.gz.

File metadata

  • Download URL: dtor-0.0.6.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for dtor-0.0.6.tar.gz
Algorithm Hash digest
SHA256 4fe26dd4a45d6fd66ac17eea9b8f4b330c88994efe6f2b05d9bd3ef7ac6d9042
MD5 413cddcbb425253fb9d03de1e52c16a7
BLAKE2b-256 7b3c4c01a1bff63fd891de28d5fe9766e2ef3777dadf37802d92ea2e317a491a

See more details on using hashes here.

File details

Details for the file dtor-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: dtor-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for dtor-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fcce4ae6282d212204c8dc12e6bc7b1f9f75261363350b5444ce275d5b673959
MD5 bd076b2ea0c11ea0e77e10557f45a716
BLAKE2b-256 3bdce56e7cff4cfb81964419754a6143e60a6a3d1441ac4b22e8153599b0f2ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page