Asynchronous Tor controller library for asyncio and Python
Project description
aiostem is an asyncio python library that provides a controller to connect and interact with the Tor control port. It therefore acts as an alternative to the community-maintained stem controller.
What about Stem?
Stem was not meant to be used with asynchronous python and despite an attempt to support this framework, it has never really worked well and was never merged. Additionally it does not use a true asynchronous connection but instead uses worker threads in order not to break existing code.
The initial goal of aiostem was to offer better support for events, as there can be many of them coming at a high rate and I noticed that stem quickly ran into deadlocks and high CPU usage. Moreover, I feel like stem provides too many high level APIs and it is hard to know exactly what is performed under the hood. It has also become too complex and bloated with legacy code, both for a large range of Python versions and support for old versions of Tor.
Tor v0.4.x has been released for many years now, therefore aiostem focuses the support for Tor v0.4.5 and later, as well as Python 3.10 and later.
Additionally, stem does not provide a low-level API around the control protocol, which means that there is time wasted registering and unregistering events all around. aiostem focuses on a clean implementation of the low level protocol, providing far better performances when dealing with a higher number of events.
However, aiostem is not a drop-in replacement for stem since we do not handle the following features:
Parsing of server and relay descriptors as in stem.descriptor (we have HS descriptors).
Higher (and easier) level APIs mixing commands and events in a single call.
Run a Tor daemon from library calls as in stem.process.
Download server descriptors as in stem.descriptor.remote.
Command line interpreter as in stem.interpreter.
Support for older versions of Tor and Python.
Installation
This package requires Python ≥ 3.10 and pulls a few other packages as dependencies such as pydantic for serialization, deserialization and validation of received data, and cryptography to deal with the various keys used by Tor.
To install the latest version use the following command:
python -m pip install aiostem
Usage
This simple example shows how to use the controller in asynchronous python. No extra thread is involved here, everything runs in the event loop.
It shows how to open a controller, authenticate, subscribe to an event, run a command and wait for the DNS resolution event to complete.
#!/usr/bin/env python
import asyncio
from functools import partial
from aiostem import Controller
from aiostem.event import EventAddrMap
def on_addrmap_event(done, event):
if isinstance(event, EventAddrMap):
print(f'{event.original} is at {event.replacement}')
done.set()
async def main():
# Simple asyncio event to exit when the event has been received.
done = asyncio.Event()
# Create a new controller with the default port (9051).
async with Controller.from_port() as ctrl:
# Authenticate automatically with a secure method (on localhost only).
reply = await ctrl.authenticate()
reply.raise_for_status()
# Register a callback for ``ADDRMAP`` events.
await ctrl.add_event_handler('ADDRMAP', partial(on_addrmap_event, done))
# Request DNS resolution for ``github.com``.
# The output here is received as an ``ADDRMAP`` event.
reply = await ctrl.resolve(['github.com'])
reply.raise_for_status()
# Wait until the address is resolved.
await done.wait()
if __name__ == '__main__':
asyncio.run(main())
This code, when executed displays the following output:
$ python examples/usage.py
github.com is at 140.82.121.4
For further details, please refer to the documentation.
Contributing
Contributions, bug reports and feedback are very welcome, feel free to open an issue, send a pull request. or start a discussion.
Participants must uphold the Code of Conduct.
aiostem is released under the MIT license.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiostem-0.4.7.tar.gz.
File metadata
- Download URL: aiostem-0.4.7.tar.gz
- Upload date:
- Size: 164.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6549fb86ec3a3b02f7dc8007f1690f1a29b8db9f3c407379fcaa8fce3581e1a4
|
|
| MD5 |
cdb05cc06921035831b9a6acce5fd5eb
|
|
| BLAKE2b-256 |
cbbfcff75452db45f0630ad09cc5edc877efb0460944cfb0ec9a9c50a8a4f948
|
Provenance
The following attestation bundles were made for aiostem-0.4.7.tar.gz:
Publisher:
release.yml on morian/aiostem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiostem-0.4.7.tar.gz -
Subject digest:
6549fb86ec3a3b02f7dc8007f1690f1a29b8db9f3c407379fcaa8fce3581e1a4 - Sigstore transparency entry: 953286141
- Sigstore integration time:
-
Permalink:
morian/aiostem@cb72215cd8a2af00bfef2aa634aa0a687649db7a -
Branch / Tag:
refs/tags/v0.4.7 - Owner: https://github.com/morian
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@cb72215cd8a2af00bfef2aa634aa0a687649db7a -
Trigger Event:
push
-
Statement type:
File details
Details for the file aiostem-0.4.7-py3-none-any.whl.
File metadata
- Download URL: aiostem-0.4.7-py3-none-any.whl
- Upload date:
- Size: 91.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69e849e5c97065331d6ab69c17dae9e1b10c89d8aee83108613aef294a20b157
|
|
| MD5 |
69f8967debbeb7ae90f0c0cf544e68f8
|
|
| BLAKE2b-256 |
21ff76f8c7520f96411772008cffca4bb7950be9fe2fdebb5889a2b82a569a6e
|
Provenance
The following attestation bundles were made for aiostem-0.4.7-py3-none-any.whl:
Publisher:
release.yml on morian/aiostem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiostem-0.4.7-py3-none-any.whl -
Subject digest:
69e849e5c97065331d6ab69c17dae9e1b10c89d8aee83108613aef294a20b157 - Sigstore transparency entry: 953286146
- Sigstore integration time:
-
Permalink:
morian/aiostem@cb72215cd8a2af00bfef2aa634aa0a687649db7a -
Branch / Tag:
refs/tags/v0.4.7 - Owner: https://github.com/morian
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@cb72215cd8a2af00bfef2aa634aa0a687649db7a -
Trigger Event:
push
-
Statement type: