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

shouldn env_proxy affect after proxy that provided by users from code #10025

Open
1 task done
NewUserHa opened this issue Nov 21, 2024 · 9 comments · May be fixed by #10147
Open
1 task done

shouldn env_proxy affect after proxy that provided by users from code #10025

NewUserHa opened this issue Nov 21, 2024 · 9 comments · May be fixed by #10147

Comments

@NewUserHa
Copy link
Contributor

NewUserHa commented Nov 21, 2024

Is your feature request related to a problem?

aiohttp/aiohttp/client.py

Lines 502 to 507 in e79b2d5

if proxy is None:
proxy = self._default_proxy
if proxy_auth is None:
proxy_auth = self._default_proxy_auth

aiohttp/aiohttp/client.py

Lines 609 to 617 in e79b2d5

if proxy is not None:
proxy = URL(proxy)
elif self._trust_env:
with suppress(LookupError):
proxy, proxy_auth = get_env_proxy_for_url(url)
req = self._request_class(
method,

currently, the async def _request(...)

  • uses proxy from env first than from code
  • it at the same time causes calling get env proxy from env and registry in each request(aka _request()) call.

Describe the solution you'd like

keep env proxy in session, and use after code proxy that provided by users
also should throw error for socks5 proxy since aiohttp does not work with socks5 currently

Describe alternatives you've considered

N/A

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@asvetlov
Copy link
Member

Sorry, I re-read the message several times and still cannot figure out what is the question.

@NewUserHa
Copy link
Contributor Author

NewUserHa commented Nov 22, 2024

  • when both proxy settings in environment variable and ClientSession(proxy='...') exist, the async def _request(...) will use the former. see line 614 and 504, the _default_proxy in 504 is read from latter.
  • also here get_env_proxy_for_url(url) will read env and registry each time _request(...) is called, however the env proxy should be kept in session rather than lookup for each request
  • aiohttp should explicitly throw error for socks5 proxy if provided, since aiohttp only support HTTP proxy does not support socks5 proxy at all

@asvetlov
Copy link
Member

Thanks for the clarification.

@Dreamsorcerer
Copy link
Member

Can you create a test in a PR to reproduce the issue?
Looks to me like the provided proxy parameter will override the env var, which I think is the expected behaviour.

@Dreamsorcerer Dreamsorcerer added the needs-info Issue is lacking sufficient information and will be closed if not provided label Nov 30, 2024
@NewUserHa
Copy link
Contributor Author

was mistake.
correct, the proxy user provided does will override the env var by

 if proxy is not None: 
     proxy = URL(proxy) 
 elif self._trust_env: 
     with suppress(LookupError): 
         proxy, proxy_auth = get_env_proxy_for_url(url) 

the issues only are:

  • get_env_proxy_for_url(url) will read env and registry each time _request(...) is called
  • the env proxy maybe should be kept in session rather than lookup for each request
  • aiohttp should explicitly throw error if socks5 proxy provided, since aiohttp only support HTTP proxy and does not support socks5 proxy at all, and currently will ignore the schema of URL of the provided proxy cause the proxy server directly close the connection

@Dreamsorcerer
Copy link
Member

Could probably cache one of those functions if you wanted to open a PR.
Not sure if there was any reason to not cache them originally (maybe memory use, or being able to update the file while an application is running)...

@Dreamsorcerer Dreamsorcerer removed the needs-info Issue is lacking sufficient information and will be closed if not provided label Dec 1, 2024
@NewUserHa
Copy link
Contributor Author

aiohttp/aiohttp/helpers.py

Lines 306 to 320 in 1fa237f

def get_env_proxy_for_url(url: URL) -> Tuple[URL, Optional[BasicAuth]]:
"""Get a permitted proxy for the given URL from the env."""
if url.host is not None and proxy_bypass(url.host):
raise LookupError(f"Proxying is disallowed for `{url.host!r}`")
proxies_in_env = proxies_from_env()
try:
proxy_info = proxies_in_env[url.scheme]
except KeyError:
raise LookupError(f"No proxies found for `{url!s}` in the env")
else:
return proxy_info.proxy, proxy_info.proxy_auth

but the proxies_from_env() is no parameter, compared to cache it, I think it can just be stored in session level.
and the explicitly throw error for socks5 proxy not supported should be in a different PR?

@Dreamsorcerer
Copy link
Member

Yeah, I think that sounds reasonable.

@NewUserHa
Copy link
Contributor Author

the get_env_proxy_for_url issue part needs to wait python/cpython#127767 to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants