torpyorg / torpy

Pure python Tor client implementation
Apache License 2.0
403 stars 48 forks source link

frequent breakdowns #43

Open davejakenic opened 2 years ago

davejakenic commented 2 years ago

I implemented a class torsocket to replace socket for P2P communication. It works in principle, but of 1000 attempts for connection I might have one successful send/recv and 999 TimeOutErrors.

class Torsocket:
    def __init__(self,ip,port):
        self.mgr1    = TorClient()
        self.tor     = type(self.mgr1).__enter__(self.mgr1)
        self.mgr2    = self.tor.create_circuit(3)
        self.circuit = type(self.mgr2).__enter__(self.mgr2)
        self.mgr3    = self.circuit.create_stream((ip,port))
        self.stream  = type(self.mgr3).__enter__(self.mgr3)
    def send(self,data):
        self.stream.send(data)
    def recv(self,size):
        return self.stream.recv(size)
    def __del__(self):
        for bla in [self.mgr3,self.mgr2,self.mgr1]: type(bla).__exit__(bla, None, None, None)

Imagine an arbitrary software, replacing the two lines

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(server_data.server_address)

with the one-liner

client_socket = Torsocket(*server_data.server_address)

And the code works perfect before the replacement into torsocket.

What could be the source of the time-out-error then?

Also, how large is the network? Is your package creating a connection to the entire TOR network of the TOR project? Or is this code building and residing on its own little twin? Because that might explain the issue.