[tor-dev] TOR socket for P2P in Python
Matt Traudt
pastly at torproject.org
Mon Aug 15 13:11:06 UTC 2022
On 8/11/22 11:13, Martin Neuenhofen via tor-dev wrote:
> Sorry to bother again.
> An equally good solution to replacing
> /client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> client_socket.connect(ip,port)/
> with a TOR solution could be via a command line interface:
> os.system("torsocketprogram -send 123.45.67.89 9000 filename.bytes") and
> os.system("torsocketprogram -recv 123.45.67.89 9000 filename.bytes").
>
> Am Do., 11. Aug. 2022 um 11:27 Uhr schrieb Martin Neuenhofen
> <martinneuenhofen at googlemail.com <mailto:martinneuenhofen at googlemail.com>>:
>
>
> *The ideal solution:* is device & OS agnostic, portable, uses no
> peripherals or configuration (i.e., just one pip package to
> install), and works reliably.
> Thus, the below torpy solution would have been ideal if it had
> worked reliably:
>
Some assorted responses to your messages:
- torpy[0] looks like a bad thing to base a revolutionary p2p tor-based
thing on. No commits since last year. Not maintained by Tor Project or
anyone associated with it. Touts v2 onion support as a positive when (1)
the network has dropped support such that v2 onions will not work, even
with torpy, and (2) it doesn't mention v3 onion support, which is the
new thing. If your thing uses onion services, it needs v3 onion support
- You can get pretty far by "guessing" where a Tor client is going to
be. E.g. try 127.0.0.1:9050, then :9150, then look for common control
port and socket paths and if found, hope no auth is required and you can
query tor this way to ask it where its SocksPort is. This is what nyx
does (guess common ControlPort/Socket paths while allowing the user to
specify it directly). This is what I'd do.
- Stem[1] is *the* tool to use to interact with a tor client from
Python. Carml[2] and txtorcon[3] are great tools maintained by a
well-known Tor person. I am most familiar with stem. All the things
mentioned in this bullet point require the standard tor client to be
available. I know stem can launch it if it isn't already running. AFAIK
this is still the recommended route for integrating Python things with
Tor. (1) If necessary, bundle a tor executable with your thing, and (2)
launch it from your thing with the parameters you want. This is how I've
done all the Python + Tor stuff I've ever written. Once you've
determined the host:port of Tor's SocksPort, you can simply use PySocks:
s = socks.sockssocket()
socks_host = "something"
socks_port = an_int
s.set_proxy(socks.SOCKS5, socks_host, socks_port)
As an example, here's a simplified/censored function from code I wrote.
It is naive and may not be suitable for your purposes. Adapt it and make
it better if you want to use it.
def function(
dest: Tuple[str, int],
socks_addrport: Tuple[str, int]) -> \
Tuple[bool, str]:
msg = 'some bytes to send'
resp = ''
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, *socks_addrport)
s.settimeout(15)
try:
s.connect(dest)
s.sendall(msg.encode('utf-8'))
while True:
new = s.recv(4096).decode('utf-8')
if not new:
break
resp += new
except Exception as e:
return False, f'{type(e).__name__} {e}'
return True, resp
The Python + Tor stuff I write tends to be pretty big/complex. It's
probably not the best for explaining the basics, but I'll point you
towards how flashflow configures and launches a tor client in case you
will find it more useful than confusing[5]. Remember, this requires the
standard tor binary to exist on the host.
Matt
[0]: https://github.com/torpyorg/torpy/
[1]: https://stem.torproject.org/
[2]: https://github.com/meejah/carml
[3]: https://txtorcon.readthedocs.io/en/latest/
[4]: https://pypi.org/project/PySocks/
[5]: https://github.com/pastly/flashflow/blob/master/flashflow/tor_client.py
More information about the tor-dev
mailing list