Skip to content

Commit b253787

Browse files
[py] Handle Socks Proxy for Remote Connections. Fixes #10091
1 parent 941da9b commit b253787

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

‎py/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ py_wheel(
129129
python_requires = "~=3.7",
130130
python_tag = "py3",
131131
requires = [
132-
"urllib3[secure]~=1.26",
132+
"urllib3[secure, socks]~=1.26",
133133
"trio~=0.17",
134134
"trio-websocket~=0.9",
135135
],

‎py/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ sortedcontainers
2020
sniffio
2121
trio~=0.17
2222
trio_websocket~=0.9
23-
urllib3~=1.26
23+
urllib3[secure, socks]~=1.26
2424
zipp

‎py/selenium/webdriver/remote/remote_connection.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,13 @@ def _get_connection_manager(self):
136136
pool_manager_init_args['cert_reqs'] = 'CERT_REQUIRED'
137137
pool_manager_init_args['ca_certs'] = self._ca_certs
138138

139-
return urllib3.PoolManager(**pool_manager_init_args) if not self._proxy_url else \
140-
urllib3.ProxyManager(self._proxy_url, **pool_manager_init_args)
139+
if self._proxy_url:
140+
if self._proxy_url.startswith('sock'):
141+
from urllib3.contrib.socks import SOCKSProxyManager
142+
return SOCKSProxyManager(self._proxy_url, **pool_manager_init_args)
143+
return urllib3.ProxyManager(self._proxy_url, **pool_manager_init_args)
144+
145+
return urllib3.PoolManager(**pool_manager_init_args)
141146

142147
def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=None, ignore_proxy=False):
143148
if resolve_ip:

‎py/test/unit/selenium/webdriver/remote/remote_connection_tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def test_ignore_proxy_env_vars(mock_proxy_settings):
118118
assert type(conn) == urllib3.PoolManager
119119

120120

121+
def test_get_socks_proxy_when_set(mock_socks_proxy_settings):
122+
remote_connection = RemoteConnection("http://127.0.0.1:4444/wd/hub")
123+
conn = remote_connection._get_connection_manager()
124+
from urllib3.contrib.socks import SOCKSProxyManager
125+
assert type(conn) == SOCKSProxyManager
126+
127+
121128
class MockResponse:
122129
code = 200
123130
headers = []
@@ -140,6 +147,16 @@ def mock_proxy_settings_missing(monkeypatch):
140147
monkeypatch.delenv("http_proxy", raising=False)
141148

142149

150+
@pytest.fixture(scope="function")
151+
def mock_socks_proxy_settings(monkeypatch):
152+
http_proxy = 'socks5://http_proxy.com:8080'
153+
https_proxy = 'socks5://https_proxy.com:8080'
154+
monkeypatch.setenv("HTTPS_PROXY", https_proxy)
155+
monkeypatch.setenv("HTTP_PROXY", http_proxy)
156+
monkeypatch.setenv("https_proxy", https_proxy)
157+
monkeypatch.setenv("http_proxy", http_proxy)
158+
159+
143160
@pytest.fixture(scope="function")
144161
def mock_proxy_settings(monkeypatch):
145162
http_proxy = 'http://http_proxy.com:8080'

0 commit comments

Comments
 (0)