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

Feature: 使 websockets 支持 proxy #2908

Closed
shoucandanghehe opened this issue Aug 19, 2024 · 4 comments · Fixed by #2916
Closed

Feature: 使 websockets 支持 proxy #2908

shoucandanghehe opened this issue Aug 19, 2024 · 4 comments · Fixed by #2916
Labels
enhancement New feature or request

Comments

@shoucandanghehe
Copy link
Contributor

shoucandanghehe commented Aug 19, 2024

希望能解决的问题

目前 websockets 官方不支持 proxy python-websockets/websockets#364
这导致 adapter-discord 在需要 proxy 的时候不能正常工作 (还有其他适配器可能需要通过代理连接ws吗?)

描述所需要的功能

但是有一些社区实现可以让 websockets 支持 proxy
比如:racinette/websockets_proxy
我们是否可以在 NoneBot 内部使用/重新实现 这些补丁让 websockets 支持 proxy

P.S. 如果认为使用补丁不好的话,是否至少应该抛出警告,告知用户设置了无用的 proxy

@shoucandanghehe shoucandanghehe added the enhancement New feature or request label Aug 19, 2024
@yanyongyu
Copy link
Member

nb 本来就没有地方设置 proxy 吧

@shoucandanghehe
Copy link
Contributor Author

nb 本来就没有地方设置 proxy 吧

nb driver 抽象的 Request 有)

@shoucandanghehe
Copy link
Contributor Author

@override
@asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]:
if setup.version == HTTPVersion.H10:
version = aiohttp.HttpVersion10
elif setup.version == HTTPVersion.H11:
version = aiohttp.HttpVersion11
else:
raise RuntimeError(f"Unsupported HTTP version: {setup.version}")
async with aiohttp.ClientSession(version=version, trust_env=True) as session:
async with session.ws_connect(
setup.url,
method=setup.method,
timeout=setup.timeout or 10,
headers=setup.headers,
proxy=setup.proxy,
) as ws:
yield WebSocket(request=setup, session=session, websocket=ws)

@override
@asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]:
connection = Connect(
str(setup.url),
extra_headers={**setup.headers, **setup.cookies.as_header(setup)},
open_timeout=setup.timeout,
)
async with connection as ws:
yield WebSocket(request=setup, websocket=ws)

如果通过nb的Adapter去建立websocket连接的话
在aiohttp的177行,proxy是传递过去可以正常使用的
而websockets里,proxy的设置并不会生效

@yanyongyu
Copy link
Member

看上去 websockets 短时间内不会支持 proxy,暂时先添加个 warning 提示吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

2 participants