Return the proxy to use for connecting to the given WebSocket URI, if any.
(uri: WebSocketURI)
| 96 | |
| 97 | |
| 98 | def get_proxy(uri: WebSocketURI) -> str | None: |
| 99 | """ |
| 100 | Return the proxy to use for connecting to the given WebSocket URI, if any. |
| 101 | |
| 102 | """ |
| 103 | if urllib.request.proxy_bypass(f"{uri.host}:{uri.port}"): |
| 104 | return None |
| 105 | |
| 106 | # According to the _Proxy Usage_ section of RFC 6455, use a SOCKS5 proxy if |
| 107 | # available, else favor the proxy for HTTPS connections over the proxy for |
| 108 | # HTTP connections. |
| 109 | |
| 110 | # The priority of a proxy for WebSocket connections is unspecified. We give |
| 111 | # it the highest priority. This makes it easy to configure a specific proxy |
| 112 | # for websockets. |
| 113 | |
| 114 | # getproxies() may return SOCKS proxies as {"socks": "http://host:port"} or |
| 115 | # as {"https": "socks5h://host:port"} depending on whether they're declared |
| 116 | # in the operating system or in environment variables. |
| 117 | |
| 118 | proxies = urllib.request.getproxies() |
| 119 | if uri.secure: |
| 120 | schemes = ["wss", "socks", "https"] |
| 121 | else: |
| 122 | schemes = ["ws", "socks", "https", "http"] |
| 123 | |
| 124 | for scheme in schemes: |
| 125 | proxy = proxies.get(scheme) |
| 126 | if proxy is not None: |
| 127 | if scheme == "socks" and proxy.startswith("http://"): |
| 128 | proxy = "socks5h://" + proxy[7:] |
| 129 | return proxy |
| 130 | else: |
| 131 | return None |
| 132 | |
| 133 | |
| 134 | def prepare_connect_request( |
searching dependent graphs…