✨feat: 增加对了proxy_manger工具类,支持了socks代理支持,并将代理功能封装进工具类
解决了 #1268
Motivation
代理设置管理方面有几个问题:
- 代理相关代码集中在
core_lifecycle.py中,增加了该类的复杂度,违反单一职责原则 - 当添加对socks代理的支持后,
check_port_in_use的connect_ex(("127.0.0.1", port))返回结果总会是0,因为该函数通过判断socket能否正常连接来检查端口是否被占用,而socket类在初始化时已经被替换为socks代理,本地回环地址也会通过代理。
Modifications
- 创建了专门的代理管理模块
astrbot/core/utils/proxy_manager.py,将代理相关功能封装到ProxyManager类中 - 增加了对socks代理的支持
- 修改了
core_lifecycle.py,使用新的代理管理器替换原有的代理设置逻辑 - 优化了
check_port_in_use函数,使其使用未被代理修改的原始socket进行端口检测 - 确保在代理设置为空或被删除时,会正确清除所有相关的环境变量和socket设置
- 在pyproject.toml文件中添加了
httpx[socks]>=0.28.1和pysocks>=1.7.1作为依赖,以支持使用httpx库的组件(如Google Generative AI)通过SOCKS代理连接
过程中发现的其他问题(未修改)
check_port_in_use函数始终使用connect_ex(("127.0.0.1", port)),只检测了127.0.0.1本地回环地址的端口是否被占用,当host设置为其他地址时该函数并不能发挥预期的效果。
Check
- [✅] 😊 我的 Commit Message 符合良好的规范
- [✅] 👀 我的更改经过良好的测试
- [✅] 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了
requirements.txt和pyproject.toml文件相应位置。 - [✅] 😮 我的更改没有引入恶意代码
好的,这是将pull request summary翻译成中文的结果:
Sourcery 总结
重构代理管理,引入专用的 ProxyManager 实用程序类,增强对 HTTP 和 SOCKS 代理的支持
新特性:
- 增加了全面的代理管理,支持 HTTP 和 SOCKS 代理
- 实现了灵活的代理配置,可以自动设置环境变量
Bug 修复:
- 修复了 socket 代理配置干扰本地端口检查的问题
- 确保在未配置代理时正确清理代理设置
增强功能:
- 将与代理相关的逻辑从 core_lifecycle.py 提取到单独的 ProxyManager 类中
- 改进了端口检查机制,使其可以在不同的代理配置下正常工作
- 增强了代理配置的灵活性和错误处理
部署:
- 增加了 SOCKS 代理支持依赖项:httpx[socks] 和 pysocks
Original summary in English
Summary by Sourcery
Refactor proxy management by introducing a dedicated ProxyManager utility class with enhanced support for HTTP and SOCKS proxies
New Features:
- Added comprehensive proxy management with support for both HTTP and SOCKS proxies
- Implemented flexible proxy configuration with automatic environment variable setup
Bug Fixes:
- Fixed issues with socket proxy configuration interfering with local port checking
- Ensured proper cleanup of proxy settings when no proxy is configured
Enhancements:
- Extracted proxy-related logic from core_lifecycle.py into a separate ProxyManager class
- Improved port checking mechanism to work correctly with different proxy configurations
- Enhanced proxy configuration flexibility and error handling
Deployment:
- Added SOCKS proxy support dependencies: httpx[socks] and pysocks
Sourcery 审查者指南
此 Pull Request 引入了一个 ProxyManager 类来处理代理配置,支持 HTTP 和 SOCKS 代理。它更新了核心逻辑以使用此管理器,确保正确的代理设置和清理,并修改了端口检查函数以绕过代理,从而实现准确的本地端口检测。此外,它还添加了 SOCKS 代理支持所需的依赖项,并更新了配置以使用统一的 proxy 设置。
设置代理的顺序图
sequenceDiagram
participant CoreLifecycle
participant ProxyManager
CoreLifecycle->>ProxyManager: setup_proxy(proxy_url)
alt proxy_url starts with 'socks'
ProxyManager->>ProxyManager: _setup_socks_proxy(proxy_url)
else proxy_url starts with 'http'
ProxyManager->>ProxyManager: _setup_http_proxy(proxy_url)
end
ProxyManager-->>CoreLifecycle: return success
检查端口是否被使用的顺序图
sequenceDiagram
participant DashboardServer
participant CoreLifecycle
participant ProxyManager
participant socket
DashboardServer->>CoreLifecycle: check_port_in_use(port)
CoreLifecycle->>ProxyManager: get_direct_socket()
ProxyManager-->>CoreLifecycle: return original_socket
CoreLifecycle->>socket: socket(AF_INET, SOCK_STREAM)
socket->>socket: connect_ex(("127.0.0.1", port))
socket-->>CoreLifecycle: return result
CoreLifecycle-->>DashboardServer: return result == 0
ProxyManager 的类图
classDiagram
class ProxyManager {
-original_socket
-current_proxy
-is_socks_proxy
+__init__()
+setup_proxy(proxy_url: Optional[str]) bool
-_setup_socks_proxy(proxy_url: str) bool
-_setup_http_proxy(proxy_url: str) bool
+clear_proxy() None
-_clear_proxy_env() None
+get_current_proxy() Optional[str]
+is_using_proxy() bool
+setup_no_proxy_hosts(hosts: list = None) None
+get_direct_socket()
}
文件级别变更
| 变更 | 详情 | 文件 |
|---|---|---|
引入了一个 ProxyManager 类来封装与代理相关的功能,包括设置和清除 HTTP 和 SOCKS 代理。 |
|
astrbot/core/utils/proxy_manager.py |
将 ProxyManager 集成到 core_lifecycle.py 中,以根据配置管理代理设置。 |
|
astrbot/core/core_lifecycle.py |
修改了 check_port_in_use 以使用直接套接字连接,绕过任何配置的代理,以准确确定端口可用性。 |
|
astrbot/dashboard/server.py |
更新了配置以使用支持 HTTP 和 SOCKS 代理的单个 proxy 设置。 |
|
astrbot/core/config/default.py |
添加了 httpx[socks] 和 pysocks 作为依赖项,以支持 httpx 的 SOCKS 代理并提供 SOCKS 代理功能。 |
|
requirements.txtpyproject.toml |
更新了 openai_source.py 以使用通用的 proxy 环境变量。 |
|
astrbot/core/provider/sources/openai_source.py |
可能相关的 issue
- #1268: 此 PR 实现了 issue 中请求的 SOCKS 代理支持。
- #1268: 此 PR 实现了 issue 中请求的代理设置。
提示和命令
与 Sourcery 互动
- 触发新的审查: 在 Pull Request 上评论
@sourcery-ai review。 - 继续讨论: 直接回复 Sourcery 的审查评论。
- 从审查评论生成 GitHub issue: 通过回复审查评论,要求 Sourcery 从审查评论创建一个 issue。您也可以回复审查评论并使用
@sourcery-ai issue从该评论创建一个 issue。 - 生成 Pull Request 标题: 在 Pull Request 标题中的任何位置写入
@sourcery-ai以随时生成标题。您也可以在 Pull Request 上评论@sourcery-ai title以随时(重新)生成标题。 - 生成 Pull Request 摘要: 在 Pull Request 正文中的任何位置写入
@sourcery-ai summary以随时在您想要的位置生成 PR 摘要。您也可以在 Pull Request 上评论@sourcery-ai summary以随时(重新)生成摘要。 - 生成审查者指南: 在 Pull Request 上评论
@sourcery-ai guide以随时(重新)生成审查者指南。 - 解决所有 Sourcery 评论: 在 Pull Request 上评论
@sourcery-ai resolve以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。 - 驳回所有 Sourcery 审查: 在 Pull Request 上评论
@sourcery-ai dismiss以驳回所有现有的 Sourcery 审查。如果您想从新的审查开始,这将特别有用 - 不要忘记评论@sourcery-ai review以触发新的审查!
自定义您的体验
访问您的 仪表板 以:
- 启用或禁用审查功能,例如 Sourcery 生成的 Pull Request 摘要、审查者指南等。
- 更改审查语言。
- 添加、删除或编辑自定义审查说明。
- 调整其他审查设置。
获得帮助
Original review guide in English
Reviewer's Guide by Sourcery
This pull request introduces a ProxyManager class to handle proxy configurations, supporting both HTTP and SOCKS proxies. It updates the core logic to use this manager, ensuring proper proxy setup and teardown, and modifies the port checking function to bypass proxies for accurate local port detection. Additionally, it adds necessary dependencies for SOCKS proxy support and updates the configuration to use a unified proxy setting.
Sequence diagram for setting up a proxy
sequenceDiagram
participant CoreLifecycle
participant ProxyManager
CoreLifecycle->>ProxyManager: setup_proxy(proxy_url)
alt proxy_url starts with 'socks'
ProxyManager->>ProxyManager: _setup_socks_proxy(proxy_url)
else proxy_url starts with 'http'
ProxyManager->>ProxyManager: _setup_http_proxy(proxy_url)
end
ProxyManager-->>CoreLifecycle: return success
Sequence diagram for checking port in use
sequenceDiagram
participant DashboardServer
participant CoreLifecycle
participant ProxyManager
participant socket
DashboardServer->>CoreLifecycle: check_port_in_use(port)
CoreLifecycle->>ProxyManager: get_direct_socket()
ProxyManager-->>CoreLifecycle: return original_socket
CoreLifecycle->>socket: socket(AF_INET, SOCK_STREAM)
socket->>socket: connect_ex(("127.0.0.1", port))
socket-->>CoreLifecycle: return result
CoreLifecycle-->>DashboardServer: return result == 0
Class diagram for ProxyManager
classDiagram
class ProxyManager {
-original_socket
-current_proxy
-is_socks_proxy
+__init__()
+setup_proxy(proxy_url: Optional[str]) bool
-_setup_socks_proxy(proxy_url: str) bool
-_setup_http_proxy(proxy_url: str) bool
+clear_proxy() None
-_clear_proxy_env() None
+get_current_proxy() Optional[str]
+is_using_proxy() bool
+setup_no_proxy_hosts(hosts: list = None) None
+get_direct_socket()
}
File-Level Changes
| Change | Details | Files |
|---|---|---|
Introduced a ProxyManager class to encapsulate proxy-related functionalities, including setting up and clearing HTTP and SOCKS proxies. |
|
astrbot/core/utils/proxy_manager.py |
Integrated the ProxyManager into core_lifecycle.py to manage proxy settings based on the configuration. |
|
astrbot/core/core_lifecycle.py |
Modified check_port_in_use to use a direct socket connection, bypassing any configured proxies, to accurately determine port availability. |
|
astrbot/dashboard/server.py |
Updated the configuration to use a single proxy setting that supports both HTTP and SOCKS proxies. |
|
astrbot/core/config/default.py |
Added httpx[socks] and pysocks as dependencies to support SOCKS proxies with httpx and provide SOCKS proxy functionality. |
|
requirements.txtpyproject.toml |
Updated openai_source.py to use the generic proxy environment variable. |
|
astrbot/core/provider/sources/openai_source.py |
Possibly linked issues
- #1268: The PR implements the SOCKS proxy support requested in the issue.
- #1268: The PR implements the proxy setting requested by the issue.
Tips and commands
Interacting with Sourcery
- Trigger a new review: Comment
@sourcery-ai reviewon the pull request. - Continue discussions: Reply directly to Sourcery's review comments.
- Generate a GitHub issue from a review comment: Ask Sourcery to create an
issue from a review comment by replying to it. You can also reply to a
review comment with
@sourcery-ai issueto create an issue from it. - Generate a pull request title: Write
@sourcery-aianywhere in the pull request title to generate a title at any time. You can also comment@sourcery-ai titleon the pull request to (re-)generate the title at any time. - Generate a pull request summary: Write
@sourcery-ai summaryanywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment@sourcery-ai summaryon the pull request to (re-)generate the summary at any time. - Generate reviewer's guide: Comment
@sourcery-ai guideon the pull request to (re-)generate the reviewer's guide at any time. - Resolve all Sourcery comments: Comment
@sourcery-ai resolveon the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore. - Dismiss all Sourcery reviews: Comment
@sourcery-ai dismisson the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment@sourcery-ai reviewto trigger a new review!
Customizing Your Experience
Access your dashboard to:
- Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
- Change the review language.
- Add, remove or edit custom review instructions.
- Adjust other review settings.
Getting Help
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
修改配置文件之后,尽量考虑一下前向兼容性(比如之前设置了代理的用户在更新了项目之后就会因为这个改动而使得代理失效)。
比如可以同时检测 http_proxy 的值。但是 proxy 的值的优先级大于 http_proxy 的。
大佬,您好,按照文档使用 Docker 部署 AstrBot,如何配置socks代理处理异地登录问题
大佬,您好,按照文档使用 Docker 部署 AstrBot,如何配置socks代理处理异地登录问题
socks代理功能未合并,如果是Linux的话,使用Privoxy将socks代理转为HTTP代理,在配置文件中设置。