windows_core_api.dart 12 KB

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