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

Add SOCKS support to proxy configuration parameter #1861

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! Unify proxy URL handling for HTTP and SOCKS
- Add IPv6 addresses to test cases (excellent sanity
  check since aiohttp_socks does some url validation)
- Always log an 'info' level message if proxy
  configuration is being used
  • Loading branch information
flyinghyrax committed Jan 25, 2025
commit de8fd228bd10b2f56b3301bec9351528ef7d9b76
6 changes: 3 additions & 3 deletions src/bandersnatch/config/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def proxy_address_from_env() -> str | None:
for proto in _supported_protocols:
if proto in proxies_in_env:
address = proxies_in_env[proto]
logger.debug("found %s proxy address in environment: %s", proto, address)
logger.debug("Found %s proxy address in environment: %s", proto, address)
return address
return None

Expand Down Expand Up @@ -74,11 +74,11 @@ def get_aiohttp_proxy_kwargs(proxy_url: str) -> Mapping[str, Any]:
"""
lowered = proxy_url.lower()
if lowered.startswith("socks"):
logger.debug("using SOCKS ProxyConnector for %s", proxy_url)
logger.debug("Using SOCKS ProxyConnector for %s", proxy_url)
return {"connector": ProxyConnector.from_url(proxy_url)}

if lowered.startswith("http"):
logger.debug("using HTTP proxy address %s", proxy_url)
logger.debug("Using HTTP proxy address %s", proxy_url)
return {"proxy": proxy_url, "trust_env": True}

return {}
4 changes: 4 additions & 0 deletions src/bandersnatch/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def __init__(

proxy_url = proxy if proxy else proxy_address_from_env()
self.proxy_kwargs = get_aiohttp_proxy_kwargs(proxy_url) if proxy_url else {}
# testing self.proxy_kwargs b/c even if there is a proxy_url, get_aiohttp_proxy_kwargs may
# still return {} if the url is invalid somehow
if self.proxy_kwargs:
logging.info("Using proxy URL %s", proxy_url)

self.allow_non_https = allow_non_https
if self.url.startswith("http://") and not self.allow_non_https:
Expand Down
19 changes: 17 additions & 2 deletions src/bandersnatch/tests/test_proxy_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@pytest.mark.parametrize(
("mock_env", "expected_result"),
# The IP values here don't matter for the test and could be any sentinel value
[
# No environment variables => no configuration
({}, None),
Expand Down Expand Up @@ -53,7 +54,17 @@ def test_get_aiohttp_proxy_kwargs__unsupported_arguments(


@pytest.mark.parametrize(
"arg", ["http://192.0.2.111", "https://192.0.2.112", "HTTPS://192.0.2.112"]
"arg",
[
# testing schemes
"http://192.0.2.111",
"https://192.0.2.112",
"HTTPS://192.0.2.112",
# testing host addresses
"http://192.0.2.111:8080/",
"http://[2001:db8::1234]",
"http://[2001:0db8:0:0:0:0:0:1234]:8080",
],
)
def test_get_aiohttp_proxy_kwargs__http_urls(arg: str) -> None:
assert get_aiohttp_proxy_kwargs(arg) == {"proxy": arg, "trust_env": True}
Expand All @@ -73,8 +84,10 @@ def test_get_aiohttp_proxy_kwargs__http_urls(arg: str) -> None:
"arg",
[
"socks4://198.51.100.111:1080",
"socks5://198.51.100.112:1080",
"socks5://198.51.100.112:1080/",
"SOCKS5://198.51.100.112:1080",
"socks4://[2001:db8:f1::1234]:1080",
"socks5://[2001:0db8:00f1:0000:0000:0000:0000:1234]:1080/",
],
)
async def test_get_aiohttp_proxy_kwargs__socks_urls(arg: str) -> None:
Expand All @@ -99,7 +112,9 @@ async def test_get_aiohttp_proxy_kwargs__socks_urls(arg: str) -> None:
(None, {}),
("", {}),
("http://192.0.2.111", {"proxy": str}),
("https://[2001:db8:f1::1234]", {"proxy": str}),
("socks4://198.51.100.111:1080", {"connector": ProxyConnector}),
("socks5://[2001:db8:f1::1234]:1080", {"connector": ProxyConnector}),
],
)
async def test_master_init__with_proxy_kwarg(
Expand Down
Loading