Torpy

A pure python Tor client implementation of the Tor protocol.
Torpy can be used to communicate with clearnet hosts or hidden services through the Tor Network.
Features
- No Stem or official Tor client required
- Support v2 hidden services (v2 specification)
- Support Basic and Stealth authorization protocol
- Provide simple TorHttpAdapter for requests library
- Provide simple urllib tor_opener for making requests without any dependencies
- Provide simple Socks5 proxy
Donation
If you find this project interesting, you can send some Bitcoins to address: 16mF9TYaJKkb9eGbZ5jGuJbodTF3mYvcRF
Note
This product is produced independently from the Tor® anonymity software and carries no guarantee from The Tor Project about quality, suitability or anything else.
Console examples
There are several console utilities to test the client.
A simple HTTP/HTTPS request:
$ torpy_cli --url https://ifconfig.me --header "User-Agent" "curl/7.37.0"
Loading cached NetworkStatusDocument from TorCacheDirStorage: .local/share/torpy/network_status
Loading cached DirKeyCertificateList from TorCacheDirStorage: .local/share/torpy/dir_key_certificates
Connecting to guard node 141.98.136.79:443 (Poseidon; Tor 0.4.3.6)... (TorClient)
Sending: GET https://ifconfig.me
Creating new circuit
...
Building 3 hops circuit...
Extending the circuit
...
Extending the circuit
...
Stream
Stream
Stream
Stream
Stream
Response status: 200
Stream
Stream
Closing guard connections (TorClient)...
Destroy circuit
Closing guard connections (Router descriptor downloader)...
Destroy circuit
> 199.249.230.175
Create Socks5 proxy to relay requests via the Tor Network:
$ torpy_socks -p 1050 --hops 3
Loading cached NetworkStatusDocument from TorCacheDirStorage: .local/share/torpy/network_status
Connecting to guard node 89.142.75.60:9001 (spongebobness; Tor 0.3.5.8)...
Creating new circuit #80000001 with 89.142.75.60:9001 (spongebobness; Tor 0.3.5.8) router...
Building 3 hops circuit...
Extending the circuit #80000001 with 185.248.143.42:9001 (torciusv; Tor 0.3.5.8)...
Extending the circuit #80000001 with 158.174.122.199:9005 (che1; Tor 0.4.1.6)...
Start socks proxy at 127.0.0.1:1050
...
Torpy module also has a command-line interface:
$ python3.7 -m torpy --url https://facebookcorewwwi.onion --to-file index.html
Loading cached NetworkStatusDocument from TorCacheDirStorage: .local/share/torpy/network_status
Connecting to guard node 185.2.31.8:443 (cx10TorServer; Tor 0.4.0.5)...
Sending: GET https://facebookcorewwwi.onion
Creating new circuit
Building 3 hops circuit...
Extending the circuit
Extending the circuit
Creating stream
Stream
Extending
Rendezvous established (CellRelayRendezvousEstablished())
Iterate over responsible dirs of the hidden service
Iterate over introduction points of the hidden service
Create circuit for hsdir
Creating new circuit
Building 0 hops circuit...
Extending the circuit
Creating stream
Stream
Stream
Destroy circuit
Creating new circuit
Building 0 hops circuit...
Extending the circuit
Introduced (CellRelayIntroduceAck())
Destroy circuit
Creating stream
Stream
Extending
Response status: 200
Writing to file index.html
Stream
Stream
Closing guard connections...
Destroy circuit
Usage examples
A basic example of how to send some data to a clearnet host or a hidden service:
from torpy import TorClient
hostname = 'ifconfig.me'
with TorClient() as tor:
with tor.create_circuit(3) as circuit:
with circuit.create_stream((hostname, 80)) as stream:
stream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hostname.encode())
recv = stream.recv(1024)
TorHttpAdapter is a convenient Tor adapter for the requests library.
The following example shows the usage of TorHttpAdapter for multi-threaded HTTP requests:
from multiprocessing.pool import ThreadPool
from torpy.http.requests import tor_requests_session
with tor_requests_session() as s:
links = ['http://nzxj65x32vh2fkhk.onion', 'http://facebookcorewwwi.onion'] * 2
with ThreadPool(3) as pool:
pool.map(s.get, links)
For more examples see test_integration.py
Installation
- Just
pip3 install torpy
- Or for using TorHttpAdapter with requests library you need install extras:
pip3 install torpy[requests]
Contribute
- Use It
- Code review is appreciated
- Open Issue, send PR
TODO
License
Licensed under the Apache License, Version 2.0
References