Files
ShadowsocksX-NG/ShadowsocksX-NG2/App/HomeView.swift
T
Qiu Yuzhou d2a8d6105c feat(system-proxy): route writes through privileged LaunchDaemon helper
Implements issue #71 with ADR-0020 (supersedes ADR-0019's endpoint-signature
cleanup, startup cleanup, and health-gate cleanup).

- New ShadowsocksX-NG2SystemProxyHelper target: app-bundled root LaunchDaemon
  (SMAppService.daemon, on-demand via MachServices) that mechanically applies
  a closed typed configuration or executes an unconditional clear. Clients are
  accepted by code-signing bundle identifier only; requests run serially on a
  shared queue (last-writer-wins).
- SystemProxyConfiguration is now the closed typed apply payload (Codable over
  JSON XPC): explicit SOCKS/HTTP/HTTPS enable + endpoints, PAC and
  auto-discovery disabled with URL/JS removed, explicit simple-hostname
  exclusion, complete ExceptionsList. Unmodeled Proxies keys are preserved and
  equivalent values skip the commit.
- Cleanup is a side effect of a persisted system-proxy intent on-to-off
  transition only: the switch itself, or the agent-off cascade (persist both
  intents off, clear, then stop listeners; failed cascade persistence records
  no transition and clears nothing). Launch with the switch off, health-gate
  closure, invalid targets, and passive events never clear. Failed clears are
  surfaced without deferred retry.
- Endpoint-signature store, ownership migration, and direct Authorization
  Services writes are removed; old signature files stay inert. Helper
  unavailability or pending approval keeps an enabled intent pending and
  surfaces the Login Items approval path (HomeView button + menu label).
2026-09-30 12:26:40 +08:00

216 lines
7.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import SwiftUI
/// 首页分区(地图 #52,票 #54):代理模式切换、运行控制、当前服务器目标树
/// 与终端代理环境变量命令。模式与两个开关(agent/系统代理,issue #60)走代理
/// 控制工作流的整体 snapshot;目标树与激活走目录工作流 projection 与 `activate`;
/// 命令复制是 UI 副作用。三种模式(规则/全局/直连)与规则子选项共用同一快照。
struct HomeView: View {
@ObservedObject var workflow: CatalogWorkflow
@ObservedObject var control: ProxyControlWorkflow
let clipboard: any TextClipboard
let onManageServers: () -> Void
let errors: ErrorAlertPresenter
var body: some View {
ScrollView {
HStack(alignment: .top, spacing: 20) {
VStack(spacing: 20) {
ModeCard(control: control)
TargetTreeCard(
workflow: workflow,
control: control,
onManageServers: onManageServers,
errors: errors)
}
.frame(maxWidth: .infinity, alignment: .leading)
VStack(spacing: 20) {
RuntimeControlCard(control: control)
TerminalProxyEnvironmentCard(control: control, clipboard: clipboard, errors: errors)
QuickActionCard(workflow: workflow)
}
.frame(width: 300)
}
.padding(.leading, 28)
.padding(.trailing, 32)
.padding(.top, 24)
.padding(.bottom, 36)
}
}
}
/// 「运行控制」卡(issue #60/#71):agent 与系统代理两个开关并排,绑定同一
/// snapshot 的持久化意图;各自呈现运行状态/实际应用与点名原因。两个开关
/// 互不代替:关闭系统代理清除全部系统代理配置,关闭 agent 会级联关闭仍开启
/// 的系统代理意图并先清理再停止监听;helper 待批准时呈现登录项批准路径。
private struct RuntimeControlCard: View {
@ObservedObject var control: ProxyControlWorkflow
var body: some View {
let summary = StatusMenuModel.summary(from: control.snapshot)
HomeCard(
title: "运行控制",
subtitle: "后台代理与系统代理相互独立",
trailing: { EmptyView() },
content: {
VStack(alignment: .leading, spacing: 14) {
VStack(alignment: .leading, spacing: 6) {
HStack {
Toggle("后台代理", isOn: agentBinding)
.toggleStyle(.switch)
Spacer(minLength: 0)
Text(summary.status)
.font(.callout.weight(.medium))
.foregroundStyle(.secondary)
.lineLimit(1)
}
if let detail = summary.detail {
Text(detail)
.font(.caption)
.foregroundStyle(.orange)
.fixedSize(horizontal: false, vertical: true)
}
}
Divider()
VStack(alignment: .leading, spacing: 6) {
HStack {
Toggle("设置系统代理", isOn: systemProxyBinding)
.toggleStyle(.switch)
Spacer(minLength: 0)
Text(summary.systemProxyStateLabel)
.font(.callout.weight(.medium))
.foregroundStyle(.secondary)
.lineLimit(1)
}
Text(systemProxyHint)
.font(.caption)
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
if systemProxyApprovalNeeded {
Button("在登录项中批准…") {
Task { await control.openSystemProxyHelperApproval() }
}
.controlSize(.small)
}
if let systemProxyDetail = summary.systemProxyDetail {
Text(systemProxyDetail)
.font(.caption)
.foregroundStyle(.orange)
.fixedSize(horizontal: false, vertical: true)
}
}
}
.padding(.top, 6)
})
}
private var agentBinding: Binding<Bool> {
Binding(
get: { control.snapshot.agentIntentEnabled },
set: { enabled in Task { await control.setAgentEnabled(enabled) } })
}
private var systemProxyBinding: Binding<Bool> {
Binding(
get: { control.snapshot.systemProxyIntentEnabled },
set: { enabled in Task { await control.setSystemProxyEnabled(enabled) } })
}
private var systemProxyApprovalNeeded: Bool {
control.snapshot.systemProxyIntentEnabled && control.snapshot.systemProxyApprovalRequired
}
private var systemProxyHint: String {
let exitRequirement =
control.snapshot.proxyMode == .direct
? "直连模式不依赖活动服务器"
: "其他模式需要可用的活动服务器"
if systemProxyApprovalNeeded {
return "系统代理助手等待批准:在系统设置 → 登录项与扩展中允许后重试"
}
return switch control.snapshot.systemProxyApplication {
case .idle:
"开启后让 macOS 系统代理指向本地入口;\(exitRequirement)"
case .pending:
"已请求应用系统代理,等待本地入口就绪;\(exitRequirement)"
case .applied:
"系统代理已指向本地入口"
case .failed:
"应用失败,原因见下方"
}
}
}
/// 「代理模式」卡:可用模式切换(Domain 单点策略投影)+ 规则子选项 + 模式说明 + 当前徽标。
private struct ModeCard: View {
@ObservedObject var control: ProxyControlWorkflow
var body: some View {
HomeCard(
title: "代理模式",
subtitle: "选择系统代理应用方式",
trailing: { ModeBadge(label: modeBadgeLabel) },
content: {
VStack(alignment: .leading, spacing: 14) {
Picker("代理模式", selection: modeBinding) {
ForEach(control.snapshot.availableModes, id: \.self) { mode in
Text(mode.label).tag(mode)
}
}
.pickerStyle(.segmented)
.labelsHidden()
.frame(maxWidth: 480, alignment: .leading)
if control.snapshot.proxyMode == .rule {
Picker("未匹配默认动作", selection: ruleDefaultActionBinding) {
ForEach(RuleDefaultAction.allCases, id: \.self) { action in
Text(action.label).tag(action)
}
}
.pickerStyle(.segmented)
.labelsHidden()
.frame(maxWidth: 480, alignment: .leading)
}
Text(modeDescription(control.snapshot.proxyMode))
.font(.callout)
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.top, 6)
})
}
private var modeBadgeLabel: String {
let mode = control.snapshot.proxyMode
if mode == .rule {
return "\(mode.label) · \(control.snapshot.ruleDefaultAction.label)"
}
return mode.label
}
private var modeBinding: Binding<ProxyMode> {
Binding(
get: { control.snapshot.proxyMode },
set: { mode in
Task { await control.setProxyMode(mode) }
})
}
private var ruleDefaultActionBinding: Binding<RuleDefaultAction> {
Binding(
get: { control.snapshot.ruleDefaultAction },
set: { action in
Task { await control.setRuleDefaultAction(action) }
})
}
private func modeDescription(_ mode: ProxyMode) -> String {
switch mode {
case .rule:
"按内置规则在代理与直连间选择;未匹配目标走默认动作。"
case .global:
"所有系统代理流量通过本地 SOCKS 与 HTTP 入口转发,不使用规则分流。"
case .direct:
"通过本地 SOCKS 与 HTTP 入口直连,不使用 Shadowsocks 服务器。"
}
}
}