Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #617 from wkeeling/fix_socks_no_proxy_bug
Browse files Browse the repository at this point in the history
Fix no_proxy when using upstream SOCKS proxies
  • Loading branch information
wkeeling authored Nov 5, 2022
2 parents 6cd6bd9 + 5a18388 commit 79fa2e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion seleniumwire/thirdparty/mitmproxy/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,17 @@ class SocksServerConnection(ServerConnection):
def __init__(self, socks_config, *args, **kwargs):
super().__init__(*args, **kwargs)
self.socks_config = socks_config
self.use_socks = True

def getaddrinfo(self, host, port, *args, **kwargs):
if self.socks_config.scheme == "socks5h":
if self.use_socks and self.socks_config.scheme == "socks5h":
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (host, port))]
return super().getaddrinfo(host, port, *args, **kwargs)

def makesocket(self, family, type, proto):
if not self.use_socks:
return super().makesocket(family, type, proto)

try:
socks_type = dict(
socks4=socks.PROXY_TYPE_SOCKS4,
Expand Down
6 changes: 4 additions & 2 deletions seleniumwire/thirdparty/mitmproxy/server/protocol/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,11 @@ def _process_flow(self, f):

f.request = request

if self.mode is HTTPMode.upstream and self.should_bypass_upstream_proxy(f.request):
if (self.mode is HTTPMode.upstream or "socks" in self.config.options.mode) and \
self.matches_no_proxy(f.request):
self.set_server((f.request.host, f.request.port))
self.mode = HTTPMode.regular
self.server_conn.use_socks = False

if request.first_line_format == "authority":
# The standards are silent on what we should do with a CONNECT
Expand Down Expand Up @@ -538,7 +540,7 @@ def establish_server_connection(self, host: str, port: int, scheme: str):
if tls:
raise exceptions.HttpProtocolException("Cannot change scheme in upstream mitmproxy mode.")

def should_bypass_upstream_proxy(self, request):
def matches_no_proxy(self, request):
"""Whether we should bypass any upstream proxy.
This checks whether the request address is in the no_proxy list.
Expand Down
9 changes: 9 additions & 0 deletions tests/end2end/test_end2end.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@ def test_upstream_socks_proxy(driver_path, chrome_options, httpbin, socksproxy):
assert 'This passed through a socks proxy' in driver.page_source


def test_bypass_upstream_socks_proxy(driver_path, chrome_options, httpbin, socksproxy):
sw_options = {'proxy': {'https': f'{socksproxy}', 'no_proxy': 'localhost:8085'}}

with create_driver(driver_path, chrome_options, sw_options) as driver:
driver.get(f'{httpbin}/html')

assert 'This passed through a socks proxy' not in driver.page_source


def test_bypass_upstream_proxy_when_target_http(driver_path, chrome_options, httpproxy):
sw_options = {'proxy': {'https': f'{httpproxy}', 'no_proxy': 'localhost:9091'}}

Expand Down

0 comments on commit 79fa2e8

Please sign in to comment.