node_provider.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import 'dart:async';
  2. import 'dart:developer' as developer;
  3. import 'package:dio/dio.dart';
  4. import 'package:flutter_riverpod/flutter_riverpod.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. import '../core/config/app_config.dart';
  7. import '../data/models/node/service_node.dart';
  8. import 'app_provider.dart';
  9. // ── State ─────────────────────────────────────────────────
  10. class NodeState {
  11. final List<ServiceNode> nodes;
  12. final ServiceNode? currentNode;
  13. final bool isLoading;
  14. final bool isSpeedTesting;
  15. /// nodeId → latency(ms)。null 表示测速中或超时
  16. final Map<int, int?> latencyMap;
  17. const NodeState({
  18. this.nodes = const [],
  19. this.currentNode,
  20. this.isLoading = false,
  21. this.isSpeedTesting = false,
  22. this.latencyMap = const {},
  23. });
  24. NodeState copyWith({
  25. List<ServiceNode>? nodes,
  26. ServiceNode? currentNode,
  27. bool? isLoading,
  28. bool? isSpeedTesting,
  29. Map<int, int?>? latencyMap,
  30. bool clearCurrentNode = false,
  31. }) {
  32. return NodeState(
  33. nodes: nodes ?? this.nodes,
  34. currentNode: clearCurrentNode ? null : (currentNode ?? this.currentNode),
  35. isLoading: isLoading ?? this.isLoading,
  36. isSpeedTesting: isSpeedTesting ?? this.isSpeedTesting,
  37. latencyMap: latencyMap ?? this.latencyMap,
  38. );
  39. }
  40. }
  41. // ── Notifier ──────────────────────────────────────────────
  42. class NodeNotifier extends Notifier<NodeState> {
  43. // 熔断内部状态(不暴露给 UI)
  44. final Map<int, int> _failCounts = {};
  45. final Map<int, DateTime> _circuitOpenTimes = {};
  46. @override
  47. NodeState build() {
  48. Future.microtask(_init);
  49. return const NodeState(isLoading: true);
  50. }
  51. SharedPreferences get _prefs => ref.read(sharedPreferencesProvider);
  52. /// Debug / Release 分 key 缓存,避免 debug 构建复用 release 写入的生产节点缓存
  53. String get _nodeListCacheKey =>
  54. AppConfig.isDebug ? 'node_list_cache_debug' : 'node_list_cache';
  55. String get _selectedNodeIdKey =>
  56. AppConfig.isDebug ? 'node_selected_id_debug' : 'node_selected_id';
  57. // ── 初始化 ────────────────────────────────────────────
  58. Future<void> _init() async {
  59. // 1. 加载本地缓存;若缓存为空,用编译期 fallbackHosts 构造虚拟节点垫底,
  60. // 避免首屏在拉到节点列表前出现"无节点可切"的窗口
  61. final cachedNodes = _loadCachedNodes();
  62. final savedId = _prefs.getInt(_selectedNodeIdKey);
  63. final initialNodes =
  64. cachedNodes.isNotEmpty ? cachedNodes : _buildFallbackNodes();
  65. ServiceNode? selected;
  66. if (savedId != null) {
  67. selected = initialNodes.where((n) => n.id == savedId).firstOrNull;
  68. }
  69. selected ??= initialNodes.firstOrNull;
  70. state = state.copyWith(
  71. nodes: initialNodes,
  72. currentNode: selected,
  73. isLoading: false,
  74. );
  75. // 1.5 主动探活:缓存的 currentNode 可能已下线,先 ping 一次。
  76. // 失败立即 failover,避免首屏业务请求承担探活成本(用户看到红 toast)。
  77. // 不 await——与 fetchNodeList/测速 并行,最终都会收敛到正确节点。
  78. unawaited(_probeCurrentNode());
  79. // 2. 从远程 CDN 拉取最新兜底列表,合并到 fallbackHosts 头部
  80. final remoteFallbacks = await _fetchRemoteFallbackHosts();
  81. // 3. 后台拉取最新节点列表(优先使用远程兜底 + 本地硬编码兜底)
  82. await fetchNodeList(extraHosts: remoteFallbacks);
  83. // 4. 测速
  84. if (state.nodes.isNotEmpty) {
  85. await speedTestAll();
  86. }
  87. }
  88. /// 主动探活当前节点:用一次轻量 GET 请求验证 currentNode 是否可达,
  89. /// 失败立即触发 immediate failover 切下一个候选。
  90. /// 与 _init 中的 fetchNodeList / speedTestAll 并发执行,竞争状态下最后写入者胜。
  91. Future<void> _probeCurrentNode() async {
  92. final node = state.currentNode;
  93. if (node == null) return;
  94. final dio = Dio(BaseOptions(
  95. connectTimeout:
  96. const Duration(milliseconds: AppConfig.speedTestTimeoutMs),
  97. receiveTimeout:
  98. const Duration(milliseconds: AppConfig.speedTestTimeoutMs),
  99. ));
  100. try {
  101. await dio.get('${node.normalizedApiHost}contract/watchlist/test');
  102. developer.log('Node probe ok: ${node.name}', name: 'Node');
  103. } catch (e) {
  104. developer.log(
  105. 'Node probe failed: ${node.name} → $e, switching node',
  106. name: 'Node',
  107. );
  108. reportFailure(immediate: true);
  109. }
  110. }
  111. /// 用 AppConfig.fallbackHosts 构造一组兜底虚拟节点,id 用负数与真节点隔离。
  112. /// 仅在没有缓存节点 / 节点列表 API 不可达时作为应急节点池使用。
  113. List<ServiceNode> _buildFallbackNodes() {
  114. final hosts = AppConfig.fallbackHosts;
  115. return List<ServiceNode>.generate(hosts.length, (i) {
  116. final api = hosts[i];
  117. var ws = api.replaceFirst(RegExp(r'^https?://'), 'wss://');
  118. if (ws.endsWith('/')) ws = ws.substring(0, ws.length - 1);
  119. ws = '$ws/market-new';
  120. return ServiceNode(
  121. id: -(i + 1),
  122. name: 'Fallback ${i + 1}',
  123. apiHost: api,
  124. wsHost: ws,
  125. status: 1,
  126. );
  127. });
  128. }
  129. /// 从远程 CDN 拉取兜底 Host 列表,失败时静默返回空列表
  130. Future<List<String>> _fetchRemoteFallbackHosts() async {
  131. final dio = Dio(BaseOptions(
  132. connectTimeout:
  133. const Duration(milliseconds: AppConfig.speedTestTimeoutMs),
  134. receiveTimeout:
  135. const Duration(milliseconds: AppConfig.speedTestTimeoutMs),
  136. ));
  137. for (final url in AppConfig.remoteConfigUrls) {
  138. try {
  139. final response = await dio.get<Map<String, dynamic>>(url);
  140. final hosts = (response.data?['fallbackHosts'] as List<dynamic>?)
  141. ?.whereType<String>()
  142. .where((h) => h.startsWith('http'))
  143. .toList();
  144. if (hosts != null && hosts.isNotEmpty) {
  145. developer.log('Remote config loaded from $url: ${hosts.length} hosts',
  146. name: 'Node');
  147. return hosts;
  148. }
  149. } catch (e) {
  150. developer.log('Remote config fetch failed from $url: $e', name: 'Node');
  151. }
  152. }
  153. return [];
  154. }
  155. List<ServiceNode> _loadCachedNodes() {
  156. final json = _prefs.getString(_nodeListCacheKey);
  157. if (json == null || json.isEmpty) return [];
  158. try {
  159. return ServiceNode.listFromJson(json);
  160. } catch (_) {
  161. return [];
  162. }
  163. }
  164. void _saveCachedNodes(List<ServiceNode> nodes) {
  165. _prefs.setString(_nodeListCacheKey, ServiceNode.listToJson(nodes));
  166. }
  167. // ── 拉取节点列表 ─────────────────────────────────────
  168. Future<void> fetchNodeList({List<String> extraHosts = const []}) async {
  169. final dio = Dio(BaseOptions(
  170. connectTimeout:
  171. const Duration(milliseconds: AppConfig.speedTestTimeoutMs),
  172. receiveTimeout:
  173. const Duration(milliseconds: AppConfig.speedTestTimeoutMs),
  174. headers: {'Content-Type': 'application/json'},
  175. ));
  176. // 远程兜底列表优先,再用硬编码列表;去重保留顺序
  177. final seen = <String>{};
  178. final hostsToTry = [...extraHosts, ...AppConfig.fallbackHosts]
  179. .where((h) => seen.add(h))
  180. .toList();
  181. for (final host in hostsToTry) {
  182. try {
  183. final response =
  184. await dio.get<Map<String, dynamic>>('${host}contract/node/list');
  185. final body = response.data;
  186. if (body == null) continue;
  187. final code = body['code'] as int? ?? -1;
  188. if (code != 0) continue;
  189. final list = (body['data'] as List<dynamic>)
  190. .map((e) => ServiceNode.fromJson(e as Map<String, dynamic>))
  191. .where((n) => n.isValid)
  192. .toList();
  193. if (list.isEmpty) continue;
  194. // 更新缓存
  195. _saveCachedNodes(list);
  196. // 保持当前选择(如果仍在新列表中)
  197. final currentId = state.currentNode?.id;
  198. ServiceNode? selected;
  199. if (currentId != null) {
  200. selected = list.where((n) => n.id == currentId).firstOrNull;
  201. }
  202. selected ??= list.first;
  203. state = state.copyWith(
  204. nodes: list,
  205. currentNode: selected,
  206. isLoading: false,
  207. );
  208. return;
  209. } catch (e) {
  210. developer.log('fetchNodeList from $host failed: $e', name: 'Node');
  211. continue;
  212. }
  213. }
  214. // 全部失败,保持当前状态
  215. state = state.copyWith(isLoading: false);
  216. }
  217. // ── 测速 ──────────────────────────────────────────────
  218. Future<void> speedTestAll() async {
  219. if (state.nodes.isEmpty) return;
  220. state = state.copyWith(isSpeedTesting: true, latencyMap: {});
  221. final results = <int, int?>{};
  222. final futures = <Future<void>>[];
  223. for (final node in state.nodes) {
  224. futures.add(
  225. _speedTest(node).then((ms) => results[node.id] = ms),
  226. );
  227. }
  228. await Future.wait(futures);
  229. state = state.copyWith(
  230. isSpeedTesting: false,
  231. latencyMap: results,
  232. );
  233. // 如果没有用户手动选择,自动切到最快节点
  234. final savedId = _prefs.getInt(_selectedNodeIdKey);
  235. if (savedId == null) {
  236. _autoSelectBestNode(results);
  237. }
  238. }
  239. Future<int?> _speedTest(ServiceNode node) async {
  240. final dio = Dio(BaseOptions(
  241. connectTimeout:
  242. const Duration(milliseconds: AppConfig.speedTestTimeoutMs),
  243. receiveTimeout:
  244. const Duration(milliseconds: AppConfig.speedTestTimeoutMs),
  245. ));
  246. final sw = Stopwatch()..start();
  247. try {
  248. await dio.get('${node.normalizedApiHost}contract/watchlist/test');
  249. sw.stop();
  250. return sw.elapsedMilliseconds;
  251. } catch (_) {
  252. sw.stop();
  253. return null; // 超时或不可达
  254. }
  255. }
  256. void _autoSelectBestNode(Map<int, int?> latencyMap) {
  257. final reachable = state.nodes.where((n) {
  258. final ms = latencyMap[n.id];
  259. return ms != null;
  260. }).toList();
  261. if (reachable.isEmpty) return;
  262. reachable.sort((a, b) {
  263. final la = latencyMap[a.id]!;
  264. final lb = latencyMap[b.id]!;
  265. return la.compareTo(lb);
  266. });
  267. final best = reachable.first;
  268. if (best.id != state.currentNode?.id) {
  269. state = state.copyWith(currentNode: best);
  270. }
  271. }
  272. // ── 用户手动选择 ─────────────────────────────────────
  273. void selectNode(ServiceNode node) {
  274. _prefs.setInt(_selectedNodeIdKey, node.id);
  275. _failCounts.remove(node.id);
  276. state = state.copyWith(currentNode: node);
  277. }
  278. // ── 故障转移(由 Dio 拦截器调用)────────────────────
  279. /// 报告请求失败,返回 true 表示已切换节点。
  280. ///
  281. /// [immediate] 为 true 时第一次失败就熔断当前节点切下一个;
  282. /// 用于 DNS / connect / timeout 等"硬"网络错误,避免连续等 N 次超时才切。
  283. /// 5xx 等服务端错误仍走累积阈值,避免单点抖动导致频繁切换。
  284. bool reportFailure({bool immediate = false}) {
  285. final node = state.currentNode;
  286. if (node == null || state.nodes.length <= 1) return false;
  287. final count = (_failCounts[node.id] ?? 0) + 1;
  288. _failCounts[node.id] = count;
  289. final shouldTrip = immediate || count >= AppConfig.failoverThreshold;
  290. if (shouldTrip) {
  291. // 熔断当前节点
  292. _circuitOpenTimes[node.id] = DateTime.now();
  293. final next = _getNextAvailableNode();
  294. if (next != null) {
  295. developer.log(
  296. 'Failover: ${node.name} → ${next.name} (immediate=$immediate)',
  297. name: 'Node',
  298. );
  299. _failCounts.remove(next.id);
  300. state = state.copyWith(currentNode: next);
  301. return true;
  302. }
  303. // 所有节点都熔断了,重置全部
  304. _circuitOpenTimes.clear();
  305. _failCounts.clear();
  306. }
  307. return false;
  308. }
  309. /// 报告请求成功,重置失败计数
  310. void reportSuccess() {
  311. final node = state.currentNode;
  312. if (node == null) return;
  313. if (_failCounts.containsKey(node.id)) {
  314. _failCounts.remove(node.id);
  315. }
  316. }
  317. ServiceNode? _getNextAvailableNode() {
  318. final currentId = state.currentNode?.id;
  319. final available = state.nodes.where((n) {
  320. if (n.id == currentId) return false;
  321. return !_isCircuitOpen(n.id);
  322. }).toList();
  323. if (available.isEmpty) return null;
  324. // 优先选延迟最低的
  325. available.sort((a, b) {
  326. final la = state.latencyMap[a.id];
  327. final lb = state.latencyMap[b.id];
  328. if (la == null && lb == null) return 0;
  329. if (la == null) return 1;
  330. if (lb == null) return -1;
  331. return la.compareTo(lb);
  332. });
  333. return available.first;
  334. }
  335. bool _isCircuitOpen(int nodeId) {
  336. final openTime = _circuitOpenTimes[nodeId];
  337. if (openTime == null) return false;
  338. if (DateTime.now().difference(openTime).inSeconds >
  339. AppConfig.circuitBreakDurationSec) {
  340. // 半开:超过冷却期,允许重试
  341. _circuitOpenTimes.remove(nodeId);
  342. _failCounts.remove(nodeId);
  343. return false;
  344. }
  345. return true;
  346. }
  347. }
  348. // ── Provider ──────────────────────────────────────────────
  349. final nodeProvider = NotifierProvider<NodeNotifier, NodeState>(
  350. NodeNotifier.new,
  351. );