feat: add SOCKS proxy support via aiohttp-socks ProxyConnector

MA
5mo ago
+65-75

fixes #23797

This PR adds SOCKS proxy support to the asyncio Python client template.
When a kubeconfig cluster entry contains a proxy-url with a SOCKS
scheme (e.g. socks5://localhost:1080), the async client currently
fails with:

aiohttp.client_exceptions.ServerDisconnectedError: Server disconnected

This happens because aiohttp.TCPConnector does not support SOCKS
schemes at the request level — a dedicated ProxyConnector from
aiohttp-socks is required at the session level instead.

Changes

modules/openapi-generator/src/main/resources/python-asyncio/rest.mustache

  • Add SUPPORTED_SOCKS_PROXIES frozenset constant for the four SOCKS
    schemes (socks4, socks4a, socks5, socks5h)
  • Detect SOCKS scheme via partition("://") — no extra dependencies
  • Route SOCKS proxies to aiohttp_socks.ProxyConnector instead of
    aiohttp.TCPConnector
  • Lazy import of ProxyConnector so non-SOCKS users are unaffected
  • Set self.proxy = None when using SOCKS connector to avoid double
    proxy application per-request

Notes

  • HTTP/HTTPS proxy code path completely unchanged
  • All four SOCKS schemes supported: socks4, socks4a, socks5, socks5h
  • Requires aiohttp>=3.10.0 and aiohttp-socks>=0.9.0
  • Complements the urllib3 SOCKS fix for the sync Python client template
  • Non-SOCKS users are completely unaffected — aiohttp-socks is never
    imported unless a SOCKS proxy URL is configured
  • Users with a SOCKS proxy who don't have aiohttp-socks installed will
    receive a clear ImportError instead of the previous cryptic
    ServerDisconnectedError
  • self.proxy is set to None when using ProxyConnector to avoid
    double proxy application — the connector handles routing at the
    connection level, not per-request

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Add SOCKS proxy support to the asyncio Python client via aiohttp-socks ProxyConnector, fixing failures when proxy-url uses socks* schemes. Fixes #23797.

  • New Features

    • Support socks4, socks4a, socks5, socks5h via session-level ProxyConnector.from_url with trust_env=True; HTTP/HTTPS path unchanged; requires aiohttp-socks>=0.9.0.
    • Auto-detect socks* and create the aiohttp ClientSession with ProxyConnector vs TCPConnector; for SOCKS, disable per-request proxy/proxy_headers.
  • Refactors

    • Sync template: use partition("://") and a frozenset for SOCKS detection; updated generated samples.
    • Async template: build the session inside the async context and select connector by scheme; removed unnecessary comments.