Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing socketpair() hang on Windows #132

Open
hanayashiki opened this issue Nov 8, 2019 · 0 comments
Open

Fixing socketpair() hang on Windows #132

hanayashiki opened this issue Nov 8, 2019 · 0 comments

Comments

@hanayashiki
Copy link

hanayashiki commented Nov 8, 2019

On windows if you run the following:

import socks
import socket
import requests

version = "SOCKS5"
addr = "localhost"
port = 1080

socks.set_default_proxy(socks.PROXY_TYPES[version], addr, port)
socket.socket = socks.socksocket

print("Start...")
socket.socketpair()
print("End")

You will get

Start...

And python hangs and does not react to ctrl+c.

After looking into the code, I find that socketpair uses 127.0.0.1 addresses, which calls socks.socksocket to connect to 127.0.0.1, which is not what we desire. A choice to resolve this might be only running socket.socket = socks.socksocket after any socketpair is made, or the following monkeypatch to avoid connection to a local address by socks:

  def wrap_connect(func):
    def _connect(self, address: Union[tuple, str, bytes]):
      dest_addr, dest_port, *_ = address
      if dest_addr.startswith("127") or dest_addr == "localhost":
        self.set_proxy()
      return func(self, address)
    return _connect

  socks.socksocket.connect = wrap_connect(socks.socksocket.connect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant