jmcore.tor_control
jmcore.tor_control
Tor control port client for creating ephemeral hidden services.
This module provides async interface to Tor's control protocol (spec v1) for dynamically creating hidden services with cookie authentication.
Reference: https://spec.torproject.org/control-spec/index.html
DoS Defense Options: - Introduction Point Rate Limiting: HiddenServiceEnableIntroDoSDefense - Proof of Work: HiddenServicePoWDefensesEnabled (Tor 0.4.8+) - Max Streams: Limit concurrent streams per circuit
Reference: https://community.torproject.org/onion-services/advanced/dos/
Classes
EphemeralHiddenService
Represents an ephemeral hidden service created via Tor control port.
Ephemeral hidden services are transient - they exist only while the control connection is open. When the connection closes, the hidden service is automatically removed.
Source code in jmcore/src/jmcore/tor_control.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
Attributes
onion_address: str
property
Get the full .onion address.
ports = ports or []
instance-attribute
private_key = private_key
instance-attribute
service_id = service_id
instance-attribute
Functions
__init__(service_id: str, private_key: str | None = None, ports: list[tuple[int, str]] | None = None)
Initialize ephemeral hidden service info.
Args: service_id: The .onion address without .onion suffix (56 chars for v3) private_key: Optional private key for recreating the service ports: List of (virtual_port, target) mappings
Source code in jmcore/src/jmcore/tor_control.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
__repr__() -> str
Source code in jmcore/src/jmcore/tor_control.py
196 197 | |
HiddenServiceDoSConfig
dataclass
Configuration for Tor hidden service DoS defenses.
These settings control Tor-level DoS protection. Note that different features are available depending on whether you use ephemeral or persistent hidden services:
- Ephemeral HS (ADD_ONION, used by JoinMarket makers for privacy):
- PoW defense: Requires Tor 0.4.9.2+ (auto-enabled when available)
- Intro point rate limiting: NOT supported (Tor protocol limitation)
-
MaxStreams: Supported on all versions
-
Persistent HS (torrc HiddenServiceDir):
- PoW defense: Requires Tor 0.4.8+ with --enable-gpl
- Intro point rate limiting: Supported since Tor 0.4.2+
- MaxStreams: Supported on all versions
Attributes: intro_dos_enabled: Enable rate limiting at introduction points. Only works for persistent HS defined in torrc, not ephemeral HS. Disabled by default since JoinMarket uses ephemeral HS. intro_dos_rate_per_sec: Allowed introduction rate per second at intro point. Default 25 provides reasonable protection while allowing legitimate traffic. intro_dos_burst_per_sec: Allowed introduction burst per second at intro point. Should be >= intro_dos_rate_per_sec. Default 200. pow_enabled: Enable Proof-of-Work defense. Clients must solve a computational puzzle to connect, making flooding expensive. The effort required auto-adjusts based on queue depth (starts at 0, scales up under attack). Enabled by default. For ephemeral HS, requires Tor 0.4.9.2+. pow_queue_rate: Rate to process rendezvous requests from PoW queue (per second). Lower values provide more protection but may delay legitimate connections. pow_queue_burst: Burst size for processing from PoW queue. max_streams: Maximum concurrent streams per rendezvous circuit. None for unlimited. Works on all Tor versions. max_streams_close_circuit: If True, exceeding max_streams tears down the circuit. If False, excess stream requests are silently ignored.
Source code in jmcore/src/jmcore/tor_control.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
Attributes
intro_dos_burst_per_sec: int = 200
class-attribute
instance-attribute
intro_dos_enabled: bool = False
class-attribute
instance-attribute
intro_dos_rate_per_sec: int = 25
class-attribute
instance-attribute
max_streams: int | None = None
class-attribute
instance-attribute
max_streams_close_circuit: bool = True
class-attribute
instance-attribute
pow_enabled: bool = True
class-attribute
instance-attribute
pow_queue_burst: int = 250
class-attribute
instance-attribute
pow_queue_rate: int = 25
class-attribute
instance-attribute
TorAuthenticationError
Bases: TorControlError
Authentication with Tor control port failed.
Source code in jmcore/src/jmcore/tor_control.py
32 33 34 35 | |
TorCapabilities
dataclass
Tor daemon capabilities detected at runtime.
Used to determine which DoS defense features are available.
Note on ADD_ONION vs SETCONF: - PoW defense via SETCONF/torrc: Available since 0.4.8 - PoW defense via ADD_ONION: Available since 0.4.9.2-alpha - Intro DoS defense: Only available via SETCONF/torrc for persistent HS
Source code in jmcore/src/jmcore/tor_control.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
Attributes
has_add_onion_pow: bool = False
class-attribute
instance-attribute
has_intro_dos: bool = False
class-attribute
instance-attribute
has_pow_module: bool = False
class-attribute
instance-attribute
version: str = ''
class-attribute
instance-attribute
version_tuple: tuple[int, int, int] = field(default_factory=(lambda: (0, 0, 0)))
class-attribute
instance-attribute
Functions
from_version(version_str: str) -> TorCapabilities
classmethod
Parse Tor version string and determine capabilities.
Source code in jmcore/src/jmcore/tor_control.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
TorControlClient
Async client for Tor control protocol.
Supports cookie authentication and ephemeral hidden service creation. The client maintains a persistent connection to control port.
Example: async with TorControlClient() as client: hs = await client.create_ephemeral_hidden_service( ports=[(8765, "127.0.0.1:8765")] ) print(f"Hidden service: {hs.onion_address}") # Service exists while connection is open # Service removed when context exits
Source code in jmcore/src/jmcore/tor_control.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |
Attributes
control_host = control_host
instance-attribute
control_port = control_port
instance-attribute
cookie_path = Path(cookie_path) if cookie_path else None
instance-attribute
hidden_services: list[EphemeralHiddenService]
property
Get list of active ephemeral hidden services created by this client.
is_authenticated: bool
property
Check if authenticated.
is_connected: bool
property
Check if connected to control port.
password = password
instance-attribute
Functions
__aenter__() -> TorControlClient
async
Async context manager entry - connect and authenticate.
Source code in jmcore/src/jmcore/tor_control.py
248 249 250 251 252 | |
__aexit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: object) -> None
async
Async context manager exit - close connection.
Source code in jmcore/src/jmcore/tor_control.py
254 255 256 257 258 259 260 261 | |
__init__(control_host: str = '127.0.0.1', control_port: int = 9051, cookie_path: str | Path | None = None, password: str | None = None)
Initialize Tor control client.
Args: control_host: Tor control port host control_port: Tor control port number cookie_path: Path to cookie auth file (usually /var/lib/tor/control_auth_cookie) password: Optional password for HASHEDPASSWORD auth (not recommended)
Source code in jmcore/src/jmcore/tor_control.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
authenticate() -> None
async
Authenticate with Tor control port.
Tries cookie authentication first if cookie_path is set, then falls back to password if provided.
Source code in jmcore/src/jmcore/tor_control.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
close() -> None
async
Close connection to Tor control port.
Source code in jmcore/src/jmcore/tor_control.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
configure_hidden_service_dos_defense(service_id: str, config: HiddenServiceDoSConfig) -> None
async
Configure DoS defenses for a PERSISTENT hidden service defined in torrc.
This applies introduction point rate limiting and optionally PoW defense using SETCONF commands. These settings take effect immediately.
IMPORTANT: This method does NOT work for ephemeral hidden services created via ADD_ONION. For ephemeral services, pass the dos_config parameter to create_ephemeral_hidden_service() instead.
Args: service_id: The hidden service ID (without .onion suffix) config: DoS defense configuration
Raises: TorControlError: If configuration fails (e.g., for ephemeral services) TorHiddenServiceError: If the service doesn't exist
Source code in jmcore/src/jmcore/tor_control.py
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 | |
connect() -> None
async
Connect to Tor control port.
Source code in jmcore/src/jmcore/tor_control.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
create_ephemeral_hidden_service(ports: list[tuple[int, str]], key_type: str = 'NEW', key_blob: str = 'ED25519-V3', discard_pk: bool = False, detach: bool = False, await_publication: bool = False, max_streams: int | None = None, dos_config: HiddenServiceDoSConfig | None = None) -> EphemeralHiddenService
async
Create an ephemeral hidden service using ADD_ONION.
Ephemeral services exist only while the control connection is open. When the connection closes, the hidden service is automatically removed.
Args: ports: List of (virtual_port, target) tuples. Target is "host:port" or just "port" for localhost. key_type: "NEW" for new key, "ED25519-V3" or "RSA1024" for existing key key_blob: For NEW: "ED25519-V3" (recommended) or "RSA1024" For existing: base64-encoded private key discard_pk: If True, don't return the private key detach: If True, service persists after control connection closes await_publication: If True, wait for HS descriptor to be published max_streams: Maximum concurrent streams (None for unlimited) dos_config: DoS defense configuration (intro rate limiting, PoW, etc.)
Returns: EphemeralHiddenService with the created service details
Example: # Create service that forwards port 80 to local 8080 hs = await client.create_ephemeral_hidden_service( ports=[(80, "127.0.0.1:8080")] )
Source code in jmcore/src/jmcore/tor_control.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | |
delete_ephemeral_hidden_service(service_id: str) -> None
async
Delete an ephemeral hidden service.
Args: service_id: The service ID (without .onion suffix)
Source code in jmcore/src/jmcore/tor_control.py
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
get_capabilities() -> TorCapabilities
async
Detect Tor daemon capabilities.
Returns: TorCapabilities with detected features
Source code in jmcore/src/jmcore/tor_control.py
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |
get_info(key: str) -> str
async
Get information from Tor.
Args: key: Information key (e.g., "version", "config-file")
Returns: The requested information value
Source code in jmcore/src/jmcore/tor_control.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | |
get_version() -> str
async
Get Tor version string.
Source code in jmcore/src/jmcore/tor_control.py
658 659 660 | |
set_config(settings: dict[str, str | int | bool]) -> None
async
Set Tor configuration options using SETCONF.
Args: settings: Dictionary of config option -> value pairs
Raises: TorControlError: If setting configuration fails
Source code in jmcore/src/jmcore/tor_control.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 | |
TorControlError
Bases: Exception
Base exception for Tor control errors.
Source code in jmcore/src/jmcore/tor_control.py
26 27 28 29 | |
TorHiddenServiceError
Bases: TorControlError
Failed to create or manage hidden service.
Source code in jmcore/src/jmcore/tor_control.py
38 39 40 41 | |