Skip to content

feat: add custom User-Agent support for profiles#2127

Open
MacroVoid wants to merge 1 commit into
chen08209:mainfrom
MacroVoid:main
Open

feat: add custom User-Agent support for profiles#2127
MacroVoid wants to merge 1 commit into
chen08209:mainfrom
MacroVoid:main

Conversation

@MacroVoid

Copy link
Copy Markdown

⚠️ AI-Generated Code / AI 生成代码

Note: The code in this Pull Request was written entirely by AI.
注意: 此 Pull Request 中的代码完全由 AI 编写。


feat: Per-Profile Custom User-Agent Support


Summary

Introduces per-profile User-Agent override capability, enabling fine-grained control over HTTP headers used during profile downloads and updates. This addresses compatibility issues with subscription providers that enforce strict UA-based access policies.


Motivation

The existing implementation applies a single global User-Agent to all outbound profile requests. A subset of proxy subscription providers requires specific UA strings — such as Clash.Meta, Clash for Windows, or standard browser identifiers — to serve well-formed configuration payloads or to pass provider-side access controls. A global UA cannot satisfy these requirements simultaneously across multiple profiles.


Changes

Data Layer

  • Added optional userAgent field to the Profile model (freezed-managed).
  • Extended the Drift SQLite schema with a user_agent column on the profiles table.
  • Implemented a safe onUpgrade migration handler: duplicate-column SqliteException is caught and suppressed, preventing crashes on existing database instances.
  • Regenerated freezed, json_serializable, and drift derived files.

Network Layer (lib/common/request.dart)

  • Modified the Request utility to conditionally inject a User-Agent header into outgoing dio requests when a profile-level override is present.

State Management (lib/providers/action.dart)

  • Extended ProfilesAction to accept and propagate a userAgent parameter through both the URL-based profile creation and the manual profile update flows.
  • Refactored URL addition return type from String to (String, String?) tuple to carry both the URL and the selected UA in a strongly-typed manner.

UI (lib/views/config/general.dart)

  • UaDialog — new reusable dialog component providing:
    • Selection from a curated preset list (Clash.Meta, Clash for Windows, Chrome, Safari, etc.).
    • Free-form custom UA input field.
  • URLFormDialog — added UA selection control to the "Add Profile" flow.
  • EditProfileView — added UA configuration field to the "Edit Profile" screen; displays Global as fallback when no override is configured.
  • Design adheres to the existing Material You system (ListItem, standard dialog patterns).

Compatibility

Scenario Behavior
Fresh install user_agent column created as part of initial schema.
Upgrade from prior version onUpgrade migration adds column; existing rows default to NULL (global UA).
No UA override configured Behavior is identical to pre-patch; global default UA is used.


feat: 每个配置文件独立 User-Agent 支持


概述

引入基于配置文件级别的 User-Agent 覆盖机制,允许对每个配置文件的下载和更新请求独立指定 HTTP User-Agent 头。此功能旨在解决对 UA 有强制要求的订阅服务商的兼容性问题。


背景

现有实现对所有出站配置文件请求统一使用全局 User-Agent。部分代理订阅服务商要求请求携带特定 UA 字符串(如 Clash.MetaClash for Windows 或标准浏览器标识),以返回有效的配置内容或通过访问控制。单一的全局 UA 无法同时满足多个配置文件的差异化需求。


变更详情

数据层

  • Profile 模型(由 freezed 管理)中新增可选字段 userAgent
  • 扩展 Drift SQLite 数据库 Schema,在 profiles 表中新增 user_agent 列。
  • 实现安全的 onUpgrade 迁移策略:捕获并忽略因重复列触发的 SqliteException,防止现有数据库在应用初始化时崩溃。
  • 同步更新 freezedjson_serializabledrift 的代码生成文件。

网络层(lib/common/request.dart

  • 修改 Request 工具类:当配置文件存在 UA 覆盖值时,在 dio 出站请求中动态注入 User-Agent 请求头。

状态管理(lib/providers/action.dart

  • 扩展 ProfilesAction,在通过 URL 创建配置文件和手动更新配置文件的流程中,接受并传递 userAgent 参数。
  • 将 URL 添加流程的返回类型由 String 重构为强类型元组 (String, String?),以同时携带 URL 与所选 UA。

UI 层(lib/views/config/general.dart

  • UaDialog — 新增可复用的 UA 选择对话框,支持:
    • 从内置预设列表中选择(Clash.Meta、Clash for Windows、Chrome、Safari 等)。
    • 手动输入自定义 UA 字符串。
  • URLFormDialog — 在"添加配置"流程中新增 UA 选择控件。
  • EditProfileView — 在"编辑配置"界面新增 UA 配置项;未设置覆盖值时,优雅回退并显示 Global(全局默认)标识。
  • UI 设计与现有 Material You 规范(ListItem、标准对话框样式)保持一致。

兼容性说明

场景 行为
全新安装 user_agent 列作为初始 Schema 的一部分创建。
从旧版本升级 onUpgrade 迁移自动添加该列;现有行默认为 NULL(使用全局 UA)。
未配置 UA 覆盖 行为与更新前完全一致,继续使用全局默认 User-Agent。

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.

1 participant