Files
pangolin/client/lib/state/connection_provider.dart
T
wangjia 43b25c8aa0
ci-pangolin / Lint — shellcheck (push) Has been cancelled
ci-pangolin / OpenAPI Sync Check (push) Has been cancelled
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Has been cancelled
ci-pangolin / Flutter — analyze + test (push) Has been cancelled
feat: 国内流量直连分流(geoip-cn / geosite-cn)— #5
国内 IP/域名直连(不走隧道)→ 省流量 + 国内访问快;非国内走代理。

- clientconfig.go: BuildClientConfig 加 ClientConfigOpts{SplitCN,RulesBaseURL};
  开启时 route 加 {rule_set:[geoip-cn,geosite-cn]→direct} + 定义 rule_set
  (remote .srs,download_detour:direct 直连下载)
- rules.go: 控制面静态服务 /v1/rules/{name}.srs(白名单防穿越)——自托管避免
  GitHub 在国内被墙的鸡生蛋;客户端反正连控制面,可达性有保证
- nodes.go ConnectNode: 读 ?split_cn → opts;NodeAPI 加 rulesBaseURL
  (PANGOLIN_PUBLIC_URL);main.go 挂 /v1/rules 路由 + RulesHandler
- 客户端: connect_api splitCN→?split_cn=1;connection_provider 传 smartRoute 偏好
- deploy/single-node: 拉 geoip-cn/geosite-cn.srs 到 $DATA_DIR/rules +
  设 PANGOLIN_PUBLIC_URL/PANGOLIN_RULES_DIR

验证:go test(splitCN 开/关渲染 + RulesHandler 白名单/404)+ flutter analyze +
shellcheck;不需要节点。订阅链接暂用默认 opts、DNS 分流为后续增强。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-19 20:34:00 +08:00

203 lines
7.0 KiB
Dart
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.
// connection_provider.dart — 连接状态机(严格三态,状态由内核事件驱动)
//
// 设计约定:
// - UI 调用 toggle(),控制器内部读取有效节点 + 认证令牌,调用 ConnectApi
// 并启动 VpnBridge 子进程。
// - 严禁乐观翻转:VpnPhase.on 必须由 bridge.statusStream 确认后才置。
// - 状态来源:bridge.statusStream(来自内核回调),非 Timer 模拟。
import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../bridge/vpn_bridge.dart';
import '../bridge/vpn_bridge_provider.dart';
import '../l10n/app_text.dart';
import '../services/connect_api.dart';
import 'app_providers.dart';
import 'auth_provider.dart';
import 'nodes_provider.dart';
import 'settings_provider.dart';
// ── 设备 IDMVP 常量;后续由 device_info_plus 取真实 ID)──────────
const _kDeviceId = String.fromEnvironment(
'PANGOLIN_DEVICE_ID',
defaultValue: 'mac-001',
);
// ── API base URL ─────────────────────────────────────────────────
const _kApiUrl = String.fromEnvironment(
'PANGOLIN_API_URL',
defaultValue: 'http://localhost:8080',
);
// ── 连接阶段枚举 ──────────────────────────────────────────────────
/// 连接阶段。严格三态,与设计稿一致。
enum VpnPhase { off, connecting, on }
// ── 连接状态快照 ──────────────────────────────────────────────────
class ConnectionState {
const ConnectionState({required this.phase, this.elapsed = Duration.zero, this.error});
final VpnPhase phase;
final Duration elapsed;
/// 连接失败原因(已本地化);null = 无错误。供 UI 提示,不再静默吞掉。
final String? error;
ConnectionState copyWith({VpnPhase? phase, Duration? elapsed}) =>
ConnectionState(phase: phase ?? this.phase, elapsed: elapsed ?? this.elapsed);
@override
bool operator ==(Object other) =>
other is ConnectionState &&
other.phase == phase &&
other.elapsed == elapsed &&
other.error == error;
@override
int get hashCode => Object.hash(phase, elapsed, error);
}
// ── 状态机 ───────────────────────────────────────────────────────
class ConnectionController extends StateNotifier<ConnectionState> {
ConnectionController(this._ref, this._bridge)
: super(const ConnectionState(phase: VpnPhase.off)) {
// 订阅桥状态流:状态由内核事件驱动,严禁 UI 乐观翻转。
_statusSub = _bridge.statusStream.listen(_onKernelStatus);
}
final Ref _ref;
final VpnBridge _bridge;
StreamSubscription<VpnStatus>? _statusSub;
Timer? _elapsed;
ConnectApi? _api;
// ── 公有 API ───────────────────────────────────────────────────
/// 用户点击连接键:按当前状态决定动作,握手中忽略(禁乐观)。
void toggle() {
switch (state.phase) {
case VpnPhase.off:
_connect();
case VpnPhase.on:
_disconnect();
case VpnPhase.connecting:
break; // 握手进行中,不响应
}
}
/// 节点切换时触发:已连接则重连。
void onNodeChanged() {
if (state.phase == VpnPhase.on || state.phase == VpnPhase.connecting) {
_disconnect().then((_) => _connect());
}
}
// ── 内部 ─────────────────────────────────────────────────────
Future<void> _connect() async {
state = const ConnectionState(phase: VpnPhase.connecting);
final token = _ref.read(authProvider).accessToken ?? '';
final node = _ref.read(effectiveNodeProvider);
final zh = _ref.read(localeProvider) == AppLang.zh;
// 节点未就绪(列表加载中/为空):不连接,提示用户。
if (node.uuid.isEmpty) {
if (mounted) {
state = ConnectionState(
phase: VpnPhase.off,
error: zh ? '节点尚未就绪,请稍候重试' : 'Nodes not ready, please retry',
);
}
return;
}
try {
_api?.dispose();
_api = ConnectApi(baseUrl: _kApiUrl, authToken: token);
final configJson = await _api!.fetchConfig(
nodeId: node.uuid,
deviceId: _kDeviceId,
// smartRoute 偏好 → 国内分流(#5):国内 IP/域名直连,不走隧道。
splitCN: _ref.read(settingsProvider).smartRoute,
);
// bridge.start() 不阻塞至连接建立;on 状态由 statusStream 回调驱动。
await _bridge.start(configJson);
} on ConnectApiException catch (e) {
// 把后端/网络错误冒泡到 UI(原静默回 off,用户不知所以)。
if (mounted) state = ConnectionState(phase: VpnPhase.off, error: zh ? e.messageZh : e.messageEn);
} catch (e) {
if (mounted) {
state = ConnectionState(
phase: VpnPhase.off,
error: zh ? '连接失败,请重试' : 'Connection failed, please retry',
);
}
}
}
Future<void> _disconnect() async {
_stopElapsed();
try {
await _bridge.stop();
} catch (_) {}
if (mounted) state = const ConnectionState(phase: VpnPhase.off);
}
void _onKernelStatus(VpnStatus s) {
if (!mounted) return;
switch (s) {
case VpnStatus.on:
state = state.copyWith(phase: VpnPhase.on);
_startElapsed();
case VpnStatus.connecting:
state = state.copyWith(phase: VpnPhase.connecting);
_stopElapsed();
case VpnStatus.off:
case VpnStatus.error:
state = const ConnectionState(phase: VpnPhase.off);
_stopElapsed();
}
}
void _startElapsed() {
_elapsed?.cancel();
_elapsed = Timer.periodic(const Duration(seconds: 1), (_) {
if (mounted && state.phase == VpnPhase.on) {
state = state.copyWith(elapsed: state.elapsed + const Duration(seconds: 1));
}
});
}
void _stopElapsed() {
_elapsed?.cancel();
_elapsed = null;
}
@override
void dispose() {
_statusSub?.cancel();
_stopElapsed();
_api?.dispose();
super.dispose();
}
}
// ── Provider ──────────────────────────────────────────────────────
final connectionProvider =
StateNotifierProvider<ConnectionController, ConnectionState>(
(ref) => ConnectionController(ref, ref.watch(vpnBridgeProvider)),
);
/// 内核实时统计流(上/下行瞬时速率、字节数)。连接页速度行的真实数据源。
final vpnStatsProvider = StreamProvider<VpnStatsEvent>(
(ref) => ref.watch(vpnBridgeProvider).statsStream,
);