PySocks icon indicating copy to clipboard operation
PySocks copied to clipboard

Compatibility with `asyncore`

Open cladmi opened this issue 8 years ago • 4 comments

PySocks fails when used with asyncore:

  • asyncore uses connect_ex instead of connect which is not implemented by PySocks and silently calls socket.connect_ex
  • socksocket.connect fails with non blocking sockets.

I managed to make it work on Linux by re-implementing asyncore connect and create_socket. The code is a draft and could be cleaned up.

https://github.com/iot-lab/aggregation-tools/commit/c0fa46d775999785c116cb9b14cd008f27d53b9c#diff-08168129224d3895b73aa8df60b11a26L63

For connect_ex I just called connect and catched the exceptions and tried to handle them. Maybe just implementing connect_hex that return 0 or ERROR would be enough for asyncore, even if not all errors are correctly implemented.

I then got the issues with non blocking sockets. Looking at the implementation it's normal that it fails when connecting. However just passing it 'blocking' for the connection time would be enough to be usable directly, and then restore the previous configuration after connecting.

cladmi avatar Dec 02 '15 16:12 cladmi

I will accept any pull requests for this.

Anorov avatar Apr 05 '17 00:04 Anorov

I will attempt something when I have some free time but hoping someone else can pick this up.

obnoxioustbh avatar Apr 05 '17 01:04 obnoxioustbh

Is it working okay with asyncio now?

Anorov avatar Jul 24 '18 08:07 Anorov

I'm still having issues with connect_ex not working properly. I monkeypatched the socket library with pysocks, attempted to connect through Tor port 9050 with Tor turned off, and then used connect_ex to connect to google.com port 80. It connected just fine, when it should have returned an error. The strange thing is, when I tried to connect to port 80 on an .onion site, it gave errno 11 as expected. And when I turned on Tor and tried again, it gave errno 0, indicating a successful connection.

According to this testing, it appears as if the monkeypatched socket.connect_ex function will attempt to connect through the SOCKS proxy, but if it can't, it will fall back to connecting without the proxy. This is troublesome, as this makes it difficult to know whether the proxy is working or not.

Python: 3.7.1 PySocks: 1.6.8

haxys avatar Feb 21 '19 12:02 haxys