Skip to content

feat: add SOCKS proxy support via aiohttp-socks ProxyConnector#23798

Open
MagicAbdel wants to merge 2 commits into
OpenAPITools:masterfrom
MagicAbdel:master
Open

feat: add SOCKS proxy support via aiohttp-socks ProxyConnector#23798
MagicAbdel wants to merge 2 commits into
OpenAPITools:masterfrom
MagicAbdel:master

Conversation

@MagicAbdel
Copy link
Copy Markdown

@MagicAbdel MagicAbdel commented May 15, 2026

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.

Written for commit 8808735. Summary will update on new commits. Review in cubic

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 7 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache:82">
P2: Configured SOCKS proxy may be conflicted by environment proxy settings because `self.proxy = None` removes the explicit proxy override while `trust_env=True` remains active on the session.</violation>
</file>

<file name="samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py">

<violation number="1" location="samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py:82">
P1: Connector instantiated in synchronous `__init__` may fail on aiohttp 3.10+ outside a running event loop</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Re-trigger cubic

Comment thread samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py Outdated
Comment thread modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache Outdated
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache:219">
P2: Potential credential disclosure: the ImportError includes the raw proxy URL (`self.proxy`), which may contain embedded credentials (e.g. `socks5://user:pass@host:1080`). This leaks proxy credentials into logs, CI output, or telemetry when aiohttp-socks is missing.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Re-trigger cubic

Comment thread modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache Outdated
@MagicAbdel MagicAbdel force-pushed the master branch 2 times, most recently from 1393509 to 3c298c2 Compare May 16, 2026 07:21
@MagicAbdel
Copy link
Copy Markdown
Author

@cubic-dev-ai comments addressed

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.

[REQ] Socks python asyncio

1 participant