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

Conversation

flyinghyrax
Copy link
Contributor

This unifies proxy configuration handling for HTTP and SOCKS, so that:

  • HTTP proxy URLs can be read from environment variables as well as the mirror.proxy option
  • SOCKS proxy URLs can be read from the mirror.proxy option as well as environment variables

Fixes #1674

Both HTTP and SOCKS proxy URL can be read from either the
'mirror.proxy' configuration option or <PROTO>_PROXY environment
variables.
Copy link

codecov bot commented Jan 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.35%. Comparing base (4d020e8) to head (de8fd22).
Report is 215 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1861      +/-   ##
==========================================
+ Coverage   79.69%   86.35%   +6.65%     
==========================================
  Files          31       35       +4     
  Lines        4324     4440     +116     
  Branches      780      467     -313     
==========================================
+ Hits         3446     3834     +388     
+ Misses        721      429     -292     
- Partials      157      177      +20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks great. Many thanks!

Just wondering if we should always log when we're applying proxy config and maybe test IPv6 URLs (they should) work ...

If you don't have time to test IPv6 and don't agree we should log cause somewhere else already does, just say so, that's fine :)

Comment on lines 77 to 81
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.info("using SOCKS ProxyConnector for %s", proxy_url)
return {"connector": ProxyConnector.from_url(proxy_url)}
if lowered.startswith("http"):
logger.info("using HTTP proxy address %s", proxy_url)

Wonder if we want to always tell people we're using a proxy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point, if someone configured a proxy (...or didn't but one is used because of an environment variable), it would be important to know.

I left the messages in bandersnatch.config.proxy at 'debug', and added an 'info' level message to the Master initializer, with the idea that it's a little less verbose by default but can still optionally be traced in more detail.

"SOCKS4_PROXY": "socks4://192.0.2.113:1080",
"SOCKS5_PROXY": "socks5://192.0.2.114:1080",
},
"socks5://192.0.2.114:1080",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put in an IPv6 URL for good measure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is a good idea - I was tripped up several times by ProxyConnector.from_url rejecting things I expected to be valid, so it's definitely worth verifying.

None of these new tests cover all the way through to when the aiohttp.ClientSession is constructed - do you think that is worth covering?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think there we should rely on aiohttp tests. But I just want to know we pass IPv6 correctly if the libraries support it (pretty sure they do).

@flyinghyrax
Copy link
Contributor Author

All looks great. Many thanks!

Just wondering if we should always log when we're applying proxy config and maybe test IPv6 URLs (they should) work ...

If you don't have time to test IPv6 and don't agree we should log cause somewhere else already does, just say so, that's fine :)

I think these are both excellent points! Ideally both should be straightforward. Thanks for the fast review!

- 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
Copy link
Contributor

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All LGTM - Many thanks for the improvement and cleanup and extra testing.

@cooperlees cooperlees merged commit d11b6b5 into pypa:main Jan 26, 2025
14 checks passed
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

Successfully merging this pull request may close these issues.

Add SOCKS support to proxy configuration parameter
2 participants