Skip to content

✨feat: 增加对了proxy_manger工具类,支持了socks代理支持,并将代理功能封装进工具类 #1400

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

AliveGh0st
Copy link
Contributor

@AliveGh0st AliveGh0st commented Apr 25, 2025

解决了 #1268

Motivation

代理设置管理方面有几个问题:

  1. 代理相关代码集中在core_lifecycle.py中,增加了该类的复杂度,违反单一职责原则
  2. 当添加对socks代理的支持后,check_port_in_useconnect_ex(("127.0.0.1", port)) 返回结果总会是0,因为该函数通过判断socket能否正常连接来检查端口是否被占用,而socket类在初始化时已经被替换为socks代理,本地回环地址也会通过代理。

Modifications

  1. 创建了专门的代理管理模块astrbot/core/utils/proxy_manager.py,将代理相关功能封装到ProxyManager类中
  2. 增加了对socks代理的支持
  3. 修改了core_lifecycle.py,使用新的代理管理器替换原有的代理设置逻辑
  4. 优化了check_port_in_use函数,使其使用未被代理修改的原始socket进行端口检测
  5. 确保在代理设置为空或被删除时,会正确清除所有相关的环境变量和socket设置
  6. 在pyproject.toml文件中添加了httpx[socks]>=0.28.1pysocks>=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.txtpyproject.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

Copy link
Contributor

sourcery-ai bot commented Apr 25, 2025

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
Loading

检查端口是否被使用的顺序图

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
Loading

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()
  }
Loading

文件级别变更

变更 详情 文件
引入了一个 ProxyManager 类来封装与代理相关的功能,包括设置和清除 HTTP 和 SOCKS 代理。
  • 创建了 astrbot/core/utils/proxy_manager.py,其中包含 ProxyManager 类。
  • 添加了基于给定 URL 设置 HTTP 和 SOCKS 代理的方法。
  • 实现了清除代理设置的方法,包括环境变量和套接字配置。
  • 添加了一个检索原始的、未代理的套接字以进行直接连接的方法。
  • 添加了一个设置不应使用代理的主机的方法。
astrbot/core/utils/proxy_manager.py
ProxyManager 集成到 core_lifecycle.py 中,以根据配置管理代理设置。
  • CoreLifecycle 中初始化了 ProxyManager
  • 使用 ProxyManager 方法替换了直接操作环境变量以进行代理设置。
  • 使用 ProxyManager 配置了 no_proxy 主机。
astrbot/core/core_lifecycle.py
修改了 check_port_in_use 以使用直接套接字连接,绕过任何配置的代理,以准确确定端口可用性。
  • ProxyManager 获取了原始套接字类。
  • 使用原始套接字创建套接字连接以进行端口检查。
astrbot/dashboard/server.py
更新了配置以使用支持 HTTP 和 SOCKS 代理的单个 proxy 设置。
  • http_proxy 配置替换为更通用的 proxy 配置。
  • 更新了配置描述以反映对 HTTP 和 SOCKS 代理的支持。
astrbot/core/config/default.py
添加了 httpx[socks]pysocks 作为依赖项,以支持 httpx 的 SOCKS 代理并提供 SOCKS 代理功能。
  • httpx[socks]>=0.28.1 添加到 pyproject.toml
  • pysocks>=1.7.1 添加到 pyproject.toml
  • pysocks 添加到 requirements.txt
requirements.txt
pyproject.toml
更新了 openai_source.py 以使用通用的 proxy 环境变量。
  • 从环境变量检索代理时,将 http_proxy 替换为 proxy
astrbot/core/provider/sources/openai_source.py

可能相关的 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
Loading

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
Loading

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()
  }
Loading

File-Level Changes

Change Details Files
Introduced a ProxyManager class to encapsulate proxy-related functionalities, including setting up and clearing HTTP and SOCKS proxies.
  • Created astrbot/core/utils/proxy_manager.py with the ProxyManager class.
  • Added methods to set up HTTP and SOCKS proxies based on a given URL.
  • Implemented methods to clear proxy settings, including environment variables and socket configurations.
  • Added a method to retrieve the original, unproxied socket for direct connections.
  • Added a method to set up hosts that should not use a proxy.
astrbot/core/utils/proxy_manager.py
Integrated the ProxyManager into core_lifecycle.py to manage proxy settings based on the configuration.
  • Initialized ProxyManager in CoreLifecycle.
  • Replaced direct environment variable manipulation for proxy settings with ProxyManager methods.
  • Configured no_proxy hosts using the ProxyManager.
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.
  • Obtained the original socket class from ProxyManager.
  • Used the original socket to create a socket connection for port checking.
astrbot/dashboard/server.py
Updated the configuration to use a single proxy setting that supports both HTTP and SOCKS proxies.
  • Replaced http_proxy configuration with a more generic proxy configuration.
  • Updated the configuration description to reflect support for 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.
  • Added httpx[socks]>=0.28.1 to pyproject.toml.
  • Added pysocks>=1.7.1 to pyproject.toml.
  • Added pysocks to requirements.txt.
requirements.txt
pyproject.toml
Updated openai_source.py to use the generic proxy environment variable.
  • Replaced http_proxy with proxy when retrieving the proxy from environment variables.
astrbot/core/provider/sources/openai_source.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on 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 issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on 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 dismiss on 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 review to 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

Copy link
Contributor

@sourcery-ai sourcery-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.

@AliveGh0st - 我已经查看了你的更改 - 这里有一些反馈:

总体评论

  • 考虑在 ProxyManager 中添加一个方法来检索代理配置作为字典,以便更轻松地访问。
  • 可以通过从配置文件中读取默认值来改进 ProxyManager 中的 setup_no_proxy_hosts 方法。
这是我在审查期间查看的内容
  • 🟡 一般问题:发现 2 个问题
  • 🟢 安全性:一切看起来都不错
  • 🟢 测试:一切看起来都不错
  • 🟡 复杂性:发现 1 个问题
  • 🟢 文档:一切看起来都不错

Sourcery 对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @AliveGh0st - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a method to ProxyManager to retrieve the proxy configuration as a dictionary for easier access.
  • The setup_no_proxy_hosts method in ProxyManager could be improved by reading default values from a configuration file.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

AliveGh0st and others added 2 commits April 25, 2025 10:34
眼花了以为是self.astrbot_config["proxy"]

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@AliveGh0st AliveGh0st requested a review from Raven95676 April 25, 2025 16:17
@Soulter
Copy link
Member

Soulter commented Apr 26, 2025

修改配置文件之后,尽量考虑一下前向兼容性(比如之前设置了代理的用户在更新了项目之后就会因为这个改动而使得代理失效)。

比如可以同时检测 http_proxy 的值。但是 proxy 的值的优先级大于 http_proxy 的。

Copy link
Member

@Raven95676 Raven95676 left a comment

Choose a reason for hiding this comment

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

另:socks代理存在url内包含用户名和密码的情况,可以考虑支持

@Raven95676 Raven95676 self-requested a review April 27, 2025 03:28
@baiyajin
Copy link

baiyajin commented Jun 5, 2025

大佬,您好,按照文档使用 Docker 部署 AstrBot,如何配置socks代理处理异地登录问题

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.

4 participants