4992d916d0
MethodChannel pangolin/vpn (start/stop/getStatus/selectOutbound/ getActiveOutbound/setKillSwitch) + 双 EventChannel (status/stats) 契约定义在 lib/bridge/vpn_bridge.dart 头部注释冻结。 - Dart: VpnBridge 抽象接口 + VpnNativeBridge 原生实现 + VpnBridgeMock 假内核(含 connectDelay/statsInterval 可配参数) - Dart 桌面: kernel_process.dart KernelProcess/ClashApiClient 接口骨架 - Android: PangolinVpnService (VpnService 骨架 + 前台通知占位) + VpnEventBus 解耦总线 + MainActivity MethodChannel/EventChannel 注册 - iOS: PacketTunnelProvider (NEPacketTunnelProvider 骨架) + VpnManager (NETunnelProviderManager + NEVPNStatus 订阅) + AppDelegate 通道注册 + Xcode project.pbxproj 添加 PacketTunnel extension target - Widget: VpnStatus enum 迁移至 bridge/vpn_bridge.dart (+error 态), connect_button/home_shell 更新穷举匹配 - Test: test/bridge/vpn_bridge_mock_test.dart 覆盖 start→connecting→on →stop→off 序列、stats 周期推帧、selectOutbound 重连序列、schema 回测 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
99 lines
3.9 KiB
Swift
99 lines
3.9 KiB
Swift
// AppDelegate.swift — Flutter ↔ 原生通道注册
|
||
//
|
||
// 通道契约(见 lib/bridge/vpn_bridge.dart 头部注释):
|
||
// MethodChannel : pangolin/vpn
|
||
// EventChannel : pangolin/vpn/status → VpnStatusStreamHandler
|
||
// EventChannel : pangolin/vpn/stats → VpnStatsStreamHandler
|
||
//
|
||
// VpnManager.shared 持有 NETunnelProviderManager 与 NEVPNStatus 订阅。
|
||
|
||
import UIKit
|
||
import Flutter
|
||
import NetworkExtension
|
||
|
||
@UIApplicationMain
|
||
@objc class AppDelegate: FlutterAppDelegate {
|
||
|
||
override func application(
|
||
_ application: UIApplication,
|
||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
||
) -> Bool {
|
||
GeneratedPluginRegistrant.register(with: self)
|
||
|
||
guard let controller = window?.rootViewController as? FlutterViewController else {
|
||
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
||
}
|
||
let messenger = controller.binaryMessenger
|
||
|
||
// ── MethodChannel ────────────────────────────────────────
|
||
let vpnChannel = FlutterMethodChannel(name: "pangolin/vpn", binaryMessenger: messenger)
|
||
vpnChannel.setMethodCallHandler { [weak self] call, result in
|
||
self?.handleVpnMethod(call: call, result: result)
|
||
}
|
||
|
||
// ── EventChannel: status ─────────────────────────────────
|
||
FlutterEventChannel(name: "pangolin/vpn/status", binaryMessenger: messenger)
|
||
.setStreamHandler(VpnStatusStreamHandler.shared)
|
||
|
||
// ── EventChannel: stats ──────────────────────────────────
|
||
FlutterEventChannel(name: "pangolin/vpn/stats", binaryMessenger: messenger)
|
||
.setStreamHandler(VpnStatsStreamHandler.shared)
|
||
|
||
// 预加载 VPN 偏好设置(首次为空,静默失败)
|
||
VpnManager.shared.loadFromPreferences { error in
|
||
if let error = error {
|
||
NSLog("VpnManager: loadFromPreferences error: %@", error.localizedDescription)
|
||
}
|
||
}
|
||
|
||
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
||
}
|
||
|
||
// ── MethodChannel handler ─────────────────────────────────────
|
||
|
||
private func handleVpnMethod(call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||
switch call.method {
|
||
|
||
case "start":
|
||
let configJson = call.arguments as? String ?? "{}"
|
||
VpnManager.shared.start(configJson: configJson) { error in
|
||
if let error = error {
|
||
result(FlutterError(
|
||
code: "START_FAILED",
|
||
message: error.localizedDescription,
|
||
details: nil
|
||
))
|
||
} else {
|
||
result(nil)
|
||
}
|
||
}
|
||
|
||
case "stop":
|
||
VpnManager.shared.stop()
|
||
result(nil)
|
||
|
||
case "getStatus":
|
||
result(VpnManager.shared.statusString)
|
||
|
||
case "selectOutbound":
|
||
let tag = call.arguments as? String ?? "auto"
|
||
NSLog("VPN: selectOutbound tag=%@", tag)
|
||
// TODO(11F): sendProviderMessage → PacketTunnelProvider outbound selector
|
||
result(nil)
|
||
|
||
case "getActiveOutbound":
|
||
// TODO(11F): query from PacketTunnelProvider via sendProviderMessage
|
||
result("auto")
|
||
|
||
case "setKillSwitch":
|
||
let on = call.arguments as? Bool ?? false
|
||
NSLog("VPN: setKillSwitch on=%d", on ? 1 : 0)
|
||
// TODO(11F): 通过 include/excludeRoutes 实现 Kill Switch
|
||
result(nil)
|
||
|
||
default:
|
||
result(FlutterMethodNotImplemented)
|
||
}
|
||
}
|
||
}
|