Files
ShadowsocksX-NG/ShadowsocksX-NG2/App/ProxyStatusMenu.swift
T
Qiu Yuzhou 6bfd3c0d35 feat(ng2): proxy runtime LaunchAgent, wrapper and sslocal lifecycle (#27)
GUI → LaunchAgent → wrapper → sslocal full chain (spec #21 D2/D5):

- ShadowsocksX-NG2Agent: Swift BundleProgram tool target embedded at
  Contents/MacOS, supervised by launchd via SMAppService agent plist
  (Contents/Library/LaunchAgents, bundle-relative ProgramArguments,
  KeepAlive={SuccessfulExit:false}). Wrapper reads the cross-process
  contract v2/sslocal-active.json, spawns sslocal -c <absolute path>
  and supervises it: SIGTERM chain stop, SIGUSR1 forward for
  server-only changes vs graceful restart on listen fingerprint
  changes, non-zero exit on unexpected child loss for KeepAlive
  replay, invalid/missing contract → clean exit with plain unlink.
- Runtime file protocol: RuntimeFileStore reuses AtomicFileWriter
  (0700 dir self-heal, 0600 temp at creation, atomic replace);
  explicit-stop cleanup unlinks contract, agent pid file and stale
  temporaries. Shared read-side validation via
  SslocalRuntimeDocument.decodeValidated so GUI and wrapper cannot
  drift. Wrapper pid file bridges SMAppService's missing pid.
- GUI: ProxyRuntimeController consumes ActivationStateMachine effects
  through pure ProxyRuntimePlan decision table (ordered actions,
  idempotent skip of identical contract); proxy toggle and launch
  failure presentation (naming endpoint:port) in the status menu;
  launch resync reconciles agent registration, wrapper liveness and
  on-disk contract; catalog commits re-expand immediately.
- Log sanitization baseline: closed RuntimeLogEvent enum plus Redactor,
  wrapper/sslocal output converged to a 0600 agent.log with RUST_LOG
  defaulting to warn.

Tests (126 total, all green): stub-sslocal driven wrapper lifecycle
(start/stop order, SIGTERM chain, crash replay, cleanup, reload
protocol), real sslocal smoke (SOCKS port binding + full SOCKS5
handshake), plan decision table, controller flows with fakes,
runtime file permissions/atomic replace, endpoint probe, redaction.

Manual release-gate items (real launchd/SMAppService): agent
registration approval flow, GUI-quit persistence, bootout behaviors.

Closes #27
2026-09-20 20:26:54 +08:00

82 lines
2.0 KiB
Swift

import AppKit
import SwiftUI
/// 状态菜单的代理控制区(spec #21 D11 白名单中本票的子集):头部状态摘要、
/// 代理开关与「启动失败」呈现。模式选择、活动目标级联等项由后续工单接入。
struct ProxyStatusMenu: View {
@Environment(\.openWindow) private var openWindow
@ObservedObject var controller: ProxyRuntimeController
var body: some View {
Text(statusSummary)
Divider()
Button(proxyOn ? "停止代理" : "启动代理") {
Task { await controller.setProxyEnabled(!proxyOn) }
}
if let failure = failureSummary {
Text(failure)
.font(.caption)
.foregroundStyle(.secondary)
}
Divider()
Button("打开主窗口…") {
NSApp.activate()
openWindow(id: "main")
}
Divider()
Button("退出 ShadowsocksX-NG 2.0") {
NSApp.terminate(nil)
}
}
private var proxyOn: Bool {
switch controller.state {
case .off, .launchFailed, .activationFailed, .serviceFailed:
return false
case .starting, .running, .requiresApproval:
return true
}
}
private var statusSummary: String {
switch controller.state {
case .off:
return "代理未运行"
case .starting:
return "正在启动代理…"
case .running:
return "代理运行中"
case .launchFailed:
return "启动失败"
case .activationFailed:
return "无法启动"
case .requiresApproval:
return "等待允许后台代理"
case .serviceFailed:
return "服务管理失败"
}
}
private var failureSummary: String? {
switch controller.state {
case .launchFailed(let detail):
return detail
case .activationFailed(let reason):
return reason
case .serviceFailed(let detail):
return detail
case .requiresApproval:
return "请在系统设置-登录项中允许 ShadowsocksX-NG 后台项"
case .off, .starting, .running:
return nil
}
}
}