windows_core_api.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/services.dart';
  5. import 'package:get/get.dart';
  6. import 'package:nomo/app/controllers/windows/window_service.dart';
  7. import 'package:path/path.dart' as path;
  8. import 'package:path_provider/path_provider.dart';
  9. import 'package:uuid/uuid.dart';
  10. import '../../config/translations/strings_enum.dart';
  11. import '../../utils/log/logger.dart';
  12. import '../constants/configs.dart';
  13. import '../constants/enums.dart';
  14. import '../constants/errors.dart';
  15. import 'base_core_api.dart';
  16. import 'windows/menu_base/src/menu.dart';
  17. import 'windows/menu_base/src/menu_item.dart';
  18. import 'windows/stat_log_entry.dart';
  19. import 'windows/vpn_service.dart';
  20. /// Windows 实现
  21. class WindowsCoreApi implements BaseCoreApi {
  22. static const _tag = 'WindowsCoreApi';
  23. WindowsCoreApi._() {
  24. // 初始化事件通道
  25. _initEventChannel();
  26. // 初始化vpn服务
  27. _initVpnService();
  28. // 初始化窗口服务
  29. _initWindowService();
  30. }
  31. // 创建vpn服务
  32. static final _vpn = VpnService();
  33. // 创建窗口服务
  34. static final _windowService = WindowService();
  35. // 检查定时器
  36. Timer? _checkTimer;
  37. bool _hasConnectedOnce = false;
  38. bool _isVpnInited = false;
  39. int _remainTime = 0x7FFFFFFFFFFFFFFF; // 会员剩余时间
  40. int _sessionCountUp = 0; // session计时
  41. bool _isCountdown = false; // 汇报计时类型 倒计时还是正计时
  42. int _locationId = 0;
  43. String _locationCode = '';
  44. String _boostSessionId = '';
  45. /// 内部构造方法,供 BaseCoreApi 工厂使用
  46. factory WindowsCoreApi.create() => WindowsCoreApi._();
  47. // Windows Method Channel
  48. static const MethodChannel _channel = MethodChannel('app.xixi.nomo/core_api');
  49. // Windows 事件流控制器
  50. static final StreamController<String> _eventController =
  51. StreamController<String>.broadcast();
  52. // Windows 事件流
  53. static Stream<String> get eventStream => _eventController.stream;
  54. // 初始化事件监听
  55. void _initEventChannel() {
  56. // 监听来自 Windows 原生端的方法调用
  57. _channel.setMethodCallHandler(_handleMethodCall);
  58. }
  59. // 处理来自 Windows 原生端的方法调用
  60. Future<dynamic> _handleMethodCall(MethodCall call) async {
  61. switch (call.method) {
  62. case 'onEventChange':
  63. // 原生端发送事件,转发到事件流
  64. final String event = call.arguments as String;
  65. _eventController.add(event);
  66. return null;
  67. default:
  68. throw PlatformException(
  69. code: 'Unimplemented',
  70. message: 'Method ${call.method} not implemented',
  71. );
  72. }
  73. }
  74. void _initWindowService() {
  75. _windowService.initialize();
  76. _updatTrayIcon();
  77. _setTrayMenu();
  78. }
  79. void _initVpnService() {
  80. if (_isVpnInited) {
  81. return;
  82. }
  83. _isVpnInited = true;
  84. // 初始化vpn服务 10秒超时
  85. _vpn.initialize(10, false);
  86. // 监听VPN服务状态
  87. _vpn.onStatusChanged.listen((event) async {
  88. final (status, data) = event;
  89. // 处理VPN连接状态
  90. switch (status) {
  91. case VpnStatus.connecting:
  92. _handleStateConnecting();
  93. break;
  94. case VpnStatus.connected:
  95. _handleStateConnected();
  96. break;
  97. case VpnStatus.error:
  98. final code = data != null ? data as int : -1;
  99. _handleStateError(code);
  100. break;
  101. case VpnStatus.idle:
  102. _handleStateDisconnected();
  103. break;
  104. }
  105. });
  106. }
  107. void _handleStateConnecting() {
  108. _hasConnectedOnce = false;
  109. _sessionCountUp = 0;
  110. // 正在连接
  111. _eventController.add(
  112. '{"type":"vpn_status","status":1,"code":0,"message":""}',
  113. );
  114. }
  115. void _handleStateConnected() {
  116. // 只记录第一次连接成功的时间戳
  117. if (!_hasConnectedOnce) {
  118. _sessionCountUp = 0;
  119. }
  120. _hasConnectedOnce = true;
  121. // 创建检测定时器
  122. _checkTimer ??= Timer.periodic(const Duration(seconds: 1), (_) {
  123. // 累加1秒
  124. _sessionCountUp += 1000;
  125. // 累减1秒
  126. _remainTime -= 1000;
  127. // 检查用户会员剩余时间
  128. _checkMembershipRemaining();
  129. // 更新连接时长
  130. _updateSessionDuration();
  131. });
  132. // 通知 已经连接
  133. _eventController.add(
  134. '{"type":"vpn_status","status":2,"code":0,"message":""}',
  135. );
  136. // 获取统计
  137. _vpn.queryStat().then((stat) {
  138. log(_tag, 'stat: $stat');
  139. try {
  140. final ts = DateTime.now().millisecondsSinceEpoch;
  141. final connectionHistory = List<ConnectionHistory>.from(
  142. (stat?['connectionHistory'] ?? []).map(
  143. (x) => ConnectionHistory.fromJson(x),
  144. ),
  145. );
  146. final param = jsonEncode([
  147. StatLogEntry(
  148. id: Uuid().v4(),
  149. time: ts,
  150. level: 'info',
  151. module: 'NM_BoostResult',
  152. category: 'nomo',
  153. fields: Fields(
  154. code: 0,
  155. boostSessionId: _boostSessionId,
  156. success: true,
  157. locationId: _locationId,
  158. locationCode: _locationCode,
  159. generatedTime: ts,
  160. connectionHistory: connectionHistory,
  161. ),
  162. ),
  163. ]);
  164. final nodeId = connectionHistory.last.nodeId ?? '';
  165. final msg = jsonEncode({
  166. "type": "boost_result",
  167. "locationCode": _locationCode,
  168. "nodeId": nodeId,
  169. "success": true,
  170. "param": param,
  171. });
  172. log(_tag, msg);
  173. _eventController.add(msg);
  174. } catch (e) {
  175. log(_tag, 'parse stat json error: $e');
  176. }
  177. });
  178. }
  179. void _handleStateError(int code) {
  180. _eventController.add(
  181. '{"type":"vpn_status","status":3,"code":$code,"message":""}',
  182. );
  183. // 获取统计
  184. _vpn.queryStat().then((stat) {
  185. log(_tag, 'stat: $stat');
  186. try {
  187. final ts = DateTime.now().millisecondsSinceEpoch;
  188. final connectionHistory = List<ConnectionHistory>.from(
  189. (stat?['connectionHistory'] ?? []).map(
  190. (x) => ConnectionHistory.fromJson(x),
  191. ),
  192. );
  193. final param = jsonEncode([
  194. StatLogEntry(
  195. id: Uuid().v4(),
  196. time: ts,
  197. level: 'info',
  198. module: 'NM_BoostResult',
  199. category: 'nomo',
  200. fields: Fields(
  201. code: code,
  202. boostSessionId: _boostSessionId,
  203. success: false,
  204. locationId: _locationId,
  205. locationCode: _locationCode,
  206. generatedTime: ts,
  207. connectionHistory: connectionHistory,
  208. ),
  209. ),
  210. ]);
  211. final nodeId = connectionHistory.last.nodeId ?? '';
  212. final msg =
  213. '{"type":"boost_result","locationCode":"$_locationCode","nodeId":"$nodeId","success":false, "param": $param}';
  214. _eventController.add(msg);
  215. } catch (e) {
  216. log(_tag, 'parse stat json error: $e');
  217. }
  218. });
  219. _vpn.stop();
  220. }
  221. void _handleStateDisconnected() {
  222. _checkTimer?.cancel();
  223. _checkTimer = null;
  224. final isNoRemainTime = _remainTime <= 0;
  225. if (isNoRemainTime) {
  226. _eventController.add(
  227. '{"type":"vpn_status","status":3,"code":${Errors.ERROR_REMAIN_TIME},"message":""}',
  228. );
  229. } else {
  230. _eventController.add(
  231. '{"type":"vpn_status","status":0,"code":0,"message":""}',
  232. );
  233. }
  234. }
  235. void _checkMembershipRemaining() {
  236. // 没有会员时间
  237. if (_remainTime < 1000) {
  238. log(_tag, 'no remain time, need to disconnect.');
  239. // 断开vpn
  240. _vpn.stop();
  241. }
  242. }
  243. void _updateSessionDuration() {
  244. if (_isCountdown) {
  245. _eventController.add(
  246. '{"type":"timer_update","currentTime":$_remainTime,"mode":1}',
  247. );
  248. } else {
  249. _eventController.add(
  250. '{"type":"timer_update","currentTime":$_sessionCountUp,"mode":0}',
  251. );
  252. }
  253. }
  254. void _updatTrayIcon() {
  255. final isDark = Get.theme.brightness == Brightness.dark;
  256. // 更新提示栏
  257. _windowService.setSystemTrayIcon(
  258. _vpn.status == ConnectionState.connected,
  259. isDark,
  260. Configs.appName,
  261. );
  262. }
  263. void _setTrayMenu() {
  264. final trayMenu = Menu(
  265. items: [
  266. MenuItem(label: Strings.showWindow.tr, key: 'active'),
  267. MenuItem.separator(),
  268. MenuItem(label: Strings.quitApp.tr, key: 'quit'),
  269. ],
  270. );
  271. _windowService.setSystemTrayMenu(trayMenu, (menuItem) {
  272. if (menuItem.key == 'quit') {
  273. _windowService.quitApplication();
  274. } else if (menuItem.key == 'active') {
  275. _windowService.activeWindow();
  276. }
  277. });
  278. }
  279. @override
  280. Future<String?> getApps() async {
  281. // Windows 不需要获取应用列表
  282. return null;
  283. }
  284. @override
  285. Future<String?> getSystemLocale() async {
  286. return Platform.localeName;
  287. }
  288. @override
  289. Future<bool?> connect(
  290. String sessionId,
  291. int socksPort,
  292. String tunnelConfig,
  293. String configJson,
  294. int remainTime,
  295. bool isCountdown,
  296. List<String> allowVpnApps,
  297. List<String> disallowVpnApps,
  298. String accessToken,
  299. String aesKey,
  300. String aesIv,
  301. int locationId,
  302. String locationCode,
  303. List<String> baseUrls,
  304. String params,
  305. int peekTimeInterval,
  306. ) async {
  307. // 记录会员剩余时间
  308. _remainTime = remainTime;
  309. _isCountdown = isCountdown;
  310. _locationId = locationId;
  311. _locationCode = locationCode;
  312. _boostSessionId = sessionId;
  313. String geoPath = await _getGeoDirectory();
  314. final selfExecutable = Platform.resolvedExecutable;
  315. List<String> allowExes = [];
  316. List<String> disallowExes = [selfExecutable];
  317. // 连接参数
  318. Map<String, dynamic> params = {
  319. 'sessionId': sessionId,
  320. 'connectOptions': jsonEncode({
  321. 'geoPath': geoPath,
  322. 'nodesConfig': configJson,
  323. }),
  324. 'allowExes': allowExes,
  325. 'disallowExes': disallowExes,
  326. };
  327. // 连接vpn
  328. _vpn.start(params);
  329. return true;
  330. }
  331. @override
  332. Future<bool?> disconnect() async {
  333. // 实现 Windows 断开连接逻辑
  334. await _vpn.stop();
  335. return true;
  336. }
  337. @override
  338. Future<String?> getRemoteIp() async {
  339. // 实现 Windows 获取远程 IP
  340. return await _vpn.getRemoteAddress();
  341. }
  342. @override
  343. Future<String?> getAdvertisingId() async {
  344. // Windows 不支持广告 ID
  345. return null;
  346. }
  347. @override
  348. Future<bool?> moveTaskToBack() async {
  349. // Windows 不需要此功能
  350. return true;
  351. }
  352. @override
  353. Future<bool?> isConnected() async {
  354. return _vpn.isOnline && _vpn.status == ConnectionState.connected;
  355. }
  356. @override
  357. Future<String?> getSimInfo() async {
  358. // Windows 不支持 SIM 卡信息
  359. return null;
  360. }
  361. @override
  362. Future<String?> getChannel() async {
  363. return 'windows';
  364. }
  365. @override
  366. Future<void> openPackage(String packageName) async {
  367. // Windows 不支持打开应用
  368. }
  369. /// 发送事件(供 Windows 实现内部使用)
  370. ///
  371. /// Windows 原生端可以通过 MethodChannel 发送事件:
  372. /// ```cpp
  373. /// // C++ 示例
  374. /// flutter::MethodChannel<flutter::EncodableValue> channel(
  375. /// messenger, "app.xixi.nomo/core_api",
  376. /// &flutter::StandardMethodCodec::GetInstance());
  377. ///
  378. /// // 发送 VPN 状态变化
  379. /// channel.InvokeMethod("onEventChange",
  380. /// flutter::EncodableValue("{\"type\":\"vpn_status\",\"status\":2}"));
  381. /// ```
  382. ///
  383. /// 事件 JSON 格式:
  384. /// - vpn_status: {"type":"vpn_status","status":0|1|2|3,"code":0,"message":""}
  385. /// - status: 0=idle, 1=connecting, 2=connected, 3=error
  386. /// - timer_update: {"type":"timer_update","currentTime":123,"mode":"countdown"}
  387. /// - boost_result: {"type":"boost_result","locationCode":"US","nodeId":"xxx","success":true}
  388. static void sendEvent(String event) {
  389. _eventController.add(event);
  390. }
  391. /// 释放资源
  392. static void dispose() {
  393. _vpn.dispose();
  394. _windowService.dispose();
  395. _eventController.close();
  396. }
  397. /// 获取 geo 文件目录
  398. Future<String> _getGeoDirectory() async {
  399. try {
  400. final appDir = await getApplicationSupportDirectory();
  401. final geoDir = Directory(path.join(appDir.path, 'geo'));
  402. return geoDir.path;
  403. } catch (_) {
  404. return '';
  405. }
  406. }
  407. }