windows_core_api.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 ConnectionState.connectingVirtual:
  92. // 虚拟连接中
  93. break;
  94. case ConnectionState.connecting:
  95. _handleStateConnecting();
  96. break;
  97. case ConnectionState.connected:
  98. _handleStateConnected();
  99. break;
  100. case ConnectionState.error:
  101. final code = data != null ? data as int : -1;
  102. _handleStateError(code);
  103. break;
  104. case ConnectionState.disconnected:
  105. _handleStateDisconnected();
  106. break;
  107. case ConnectionState.disconnecting:
  108. // 断开中
  109. break;
  110. }
  111. });
  112. }
  113. void _handleStateConnecting() {
  114. _hasConnectedOnce = false;
  115. _sessionCountUp = 0;
  116. // 正在连接
  117. _eventController.add(
  118. '{"type":"vpn_status","status":1,"code":0,"message":""}',
  119. );
  120. }
  121. void _handleStateConnected() {
  122. // 只记录第一次连接成功的时间戳
  123. if (!_hasConnectedOnce) {
  124. _sessionCountUp = 0;
  125. }
  126. _hasConnectedOnce = true;
  127. // 创建检测定时器
  128. _checkTimer ??= Timer.periodic(const Duration(seconds: 1), (_) {
  129. // 累加1秒
  130. _sessionCountUp += 1000;
  131. // 累减1秒
  132. _remainTime -= 1000;
  133. // 检查用户会员剩余时间
  134. _checkMembershipRemaining();
  135. // 更新连接时长
  136. _updateSessionDuration();
  137. });
  138. // 通知 已经连接
  139. _eventController.add(
  140. '{"type":"vpn_status","status":2,"code":0,"message":""}',
  141. );
  142. // 获取统计
  143. _vpn.queryStat().then((stat) {
  144. log(_tag, 'stat: $stat');
  145. try {
  146. final ts = DateTime.now().millisecondsSinceEpoch;
  147. final connectionHistory = List<ConnectionHistory>.from(
  148. (stat?['connectionHistory'] ?? []).map(
  149. (x) => ConnectionHistory.fromJson(x),
  150. ),
  151. );
  152. final param = jsonEncode([
  153. StatLogEntry(
  154. id: Uuid().v4(),
  155. time: ts,
  156. level: 'info',
  157. module: 'NM_BoostResult',
  158. category: 'nomo',
  159. fields: Fields(
  160. code: 0,
  161. boostSessionId: _boostSessionId,
  162. success: true,
  163. locationId: _locationId,
  164. locationCode: _locationCode,
  165. generatedTime: ts,
  166. connectionHistory: connectionHistory,
  167. ),
  168. ),
  169. ]);
  170. final nodeId = connectionHistory.last.nodeId ?? '';
  171. final msg = jsonEncode({
  172. "type": "boost_result",
  173. "locationCode": _locationCode,
  174. "nodeId": nodeId,
  175. "success": true,
  176. "param": param,
  177. });
  178. log(_tag, msg);
  179. _eventController.add(msg);
  180. } catch (e) {
  181. log(_tag, 'parse stat json error: $e');
  182. }
  183. });
  184. }
  185. void _handleStateError(int code) {
  186. _eventController.add(
  187. '{"type":"vpn_status","status":3,"code":$code,"message":""}',
  188. );
  189. // 获取统计
  190. _vpn.queryStat().then((stat) {
  191. log(_tag, 'stat: $stat');
  192. try {
  193. final ts = DateTime.now().millisecondsSinceEpoch;
  194. final connectionHistory = List<ConnectionHistory>.from(
  195. (stat?['connectionHistory'] ?? []).map(
  196. (x) => ConnectionHistory.fromJson(x),
  197. ),
  198. );
  199. final param = jsonEncode([
  200. StatLogEntry(
  201. id: Uuid().v4(),
  202. time: ts,
  203. level: 'info',
  204. module: 'NM_BoostResult',
  205. category: 'nomo',
  206. fields: Fields(
  207. code: code,
  208. boostSessionId: _boostSessionId,
  209. success: false,
  210. locationId: _locationId,
  211. locationCode: _locationCode,
  212. generatedTime: ts,
  213. connectionHistory: connectionHistory,
  214. ),
  215. ),
  216. ]);
  217. final nodeId = connectionHistory.last.nodeId ?? '';
  218. final msg =
  219. '{"type":"boost_result","locationCode":"$_locationCode","nodeId":"$nodeId","success":false, "param": $param}';
  220. _eventController.add(msg);
  221. } catch (e) {
  222. log(_tag, 'parse stat json error: $e');
  223. }
  224. });
  225. _vpn.stop();
  226. }
  227. void _handleStateDisconnected() {
  228. _checkTimer?.cancel();
  229. _checkTimer = null;
  230. final isNoRemainTime = _remainTime <= 0;
  231. if (isNoRemainTime) {
  232. _eventController.add(
  233. '{"type":"vpn_status","status":3,"code":${Errors.ERROR_REMAIN_TIME},"message":""}',
  234. );
  235. } else {
  236. _eventController.add(
  237. '{"type":"vpn_status","status":0,"code":0,"message":""}',
  238. );
  239. }
  240. }
  241. void _checkMembershipRemaining() {
  242. // 没有会员时间
  243. if (_remainTime < 1000) {
  244. log(_tag, 'no remain time, need to disconnect.');
  245. // 断开vpn
  246. _vpn.stop();
  247. }
  248. }
  249. void _updateSessionDuration() {
  250. if (_isCountdown) {
  251. _eventController.add(
  252. '{"type":"timer_update","currentTime":$_remainTime,"mode":1}',
  253. );
  254. } else {
  255. _eventController.add(
  256. '{"type":"timer_update","currentTime":$_sessionCountUp,"mode":0}',
  257. );
  258. }
  259. }
  260. void _updatTrayIcon() {
  261. final isDark = Get.theme.brightness == Brightness.dark;
  262. // 更新提示栏
  263. _windowService.setSystemTrayIcon(
  264. _vpn.status == ConnectionState.connected,
  265. isDark,
  266. Configs.appName,
  267. );
  268. }
  269. void _setTrayMenu() {
  270. final trayMenu = Menu(
  271. items: [
  272. MenuItem(label: Strings.showWindow.tr, key: 'active'),
  273. MenuItem.separator(),
  274. MenuItem(label: Strings.quitApp.tr, key: 'quit'),
  275. ],
  276. );
  277. _windowService.setSystemTrayMenu(trayMenu, (menuItem) {
  278. if (menuItem.key == 'quit') {
  279. _windowService.quitApplication();
  280. } else if (menuItem.key == 'active') {
  281. _windowService.activeWindow();
  282. }
  283. });
  284. }
  285. @override
  286. Future<String?> getApps() async {
  287. // Windows 不需要获取应用列表
  288. return null;
  289. }
  290. @override
  291. Future<String?> getSystemLocale() async {
  292. return Platform.localeName;
  293. }
  294. @override
  295. Future<bool?> connect(
  296. String sessionId,
  297. int socksPort,
  298. String tunnelConfig,
  299. String configJson,
  300. int remainTime,
  301. bool isCountdown,
  302. List<String> allowVpnApps,
  303. List<String> disallowVpnApps,
  304. String accessToken,
  305. String aesKey,
  306. String aesIv,
  307. int locationId,
  308. String locationCode,
  309. List<String> baseUrls,
  310. String params,
  311. int peekTimeInterval,
  312. ) async {
  313. // 记录会员剩余时间
  314. _remainTime = remainTime;
  315. _isCountdown = isCountdown;
  316. _locationId = locationId;
  317. _locationCode = locationCode;
  318. _boostSessionId = sessionId;
  319. String geoPath = await _getGeoDirectory();
  320. final selfExecutable = Platform.resolvedExecutable;
  321. List<String> allowExes = [];
  322. List<String> disallowExes = [selfExecutable];
  323. // 连接参数
  324. Map<String, dynamic> params = {
  325. 'sessionId': sessionId,
  326. 'connectOptions': jsonEncode({
  327. 'geoPath': geoPath,
  328. 'nodesConfig': configJson,
  329. }),
  330. 'allowExes': allowExes,
  331. 'disallowExes': disallowExes,
  332. };
  333. // 连接vpn
  334. _vpn.start(params);
  335. return true;
  336. }
  337. @override
  338. Future<bool?> disconnect() async {
  339. // 实现 Windows 断开连接逻辑
  340. await _vpn.stop();
  341. return true;
  342. }
  343. @override
  344. Future<String?> getRemoteIp() async {
  345. // 实现 Windows 获取远程 IP
  346. return await _vpn.getRemoteAddress();
  347. }
  348. @override
  349. Future<String?> getAdvertisingId() async {
  350. // Windows 不支持广告 ID
  351. return null;
  352. }
  353. @override
  354. Future<bool?> moveTaskToBack() async {
  355. // Windows 不需要此功能
  356. return true;
  357. }
  358. @override
  359. Future<bool?> isConnected() async {
  360. return _vpn.isOnline && _vpn.status == ConnectionState.connected;
  361. }
  362. @override
  363. Future<String?> getSimInfo() async {
  364. // Windows 不支持 SIM 卡信息
  365. return null;
  366. }
  367. @override
  368. Future<String?> getChannel() async {
  369. return 'windows';
  370. }
  371. @override
  372. Future<void> openPackage(String packageName) async {
  373. // Windows 不支持打开应用
  374. }
  375. /// 发送事件(供 Windows 实现内部使用)
  376. ///
  377. /// Windows 原生端可以通过 MethodChannel 发送事件:
  378. /// ```cpp
  379. /// // C++ 示例
  380. /// flutter::MethodChannel<flutter::EncodableValue> channel(
  381. /// messenger, "app.xixi.nomo/core_api",
  382. /// &flutter::StandardMethodCodec::GetInstance());
  383. ///
  384. /// // 发送 VPN 状态变化
  385. /// channel.InvokeMethod("onEventChange",
  386. /// flutter::EncodableValue("{\"type\":\"vpn_status\",\"status\":2}"));
  387. /// ```
  388. ///
  389. /// 事件 JSON 格式:
  390. /// - vpn_status: {"type":"vpn_status","status":0|1|2|3,"code":0,"message":""}
  391. /// - status: 0=idle, 1=connecting, 2=connected, 3=error
  392. /// - timer_update: {"type":"timer_update","currentTime":123,"mode":"countdown"}
  393. /// - boost_result: {"type":"boost_result","locationCode":"US","nodeId":"xxx","success":true}
  394. static void sendEvent(String event) {
  395. _eventController.add(event);
  396. }
  397. /// 释放资源
  398. static void dispose() {
  399. _vpn.dispose();
  400. _windowService.dispose();
  401. _eventController.close();
  402. }
  403. /// 获取 geo 文件目录
  404. Future<String> _getGeoDirectory() async {
  405. try {
  406. final appDir = await getApplicationSupportDirectory();
  407. final geoDir = Directory(path.join(appDir.path, 'geo'));
  408. return geoDir.path;
  409. } catch (_) {
  410. return '';
  411. }
  412. }
  413. }