connection_service.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:get/get.dart';
  4. import 'package:naiyouwl/app/controller/controllers.dart';
  5. import 'package:naiyouwl/app/component/connection_status.dart';
  6. import 'package:naiyouwl/app/clash/service/clash_service.dart';
  7. import 'package:naiyouwl/app/controller/service.dart';
  8. import '../controller/GlobalController.dart';
  9. import '../modules/home/controllers/home_controller.dart';
  10. import '../utils/utils.dart';
  11. class ConnectionService {
  12. final GlobalController globalController;
  13. final Function(ConnectionStatus) updateStatus;
  14. final ClashService clashService = Get.find<ClashService>();
  15. final ServiceController serviceController = Get.find<ServiceController>();
  16. ConnectionService(this.globalController, this.updateStatus);
  17. Future<void> startConnection() async {
  18. try {
  19. updateStatus(ConnectionStatus.connecting);
  20. globalController.updateMsg("正在启动连接...");
  21. if(Platform.isMacOS){
  22. if (serviceController.serviceIsRuning) {
  23. await globalController.makeProxy();
  24. await serviceController.reloadClashCore();
  25. // await serviceController.fetchSetProxy();
  26. } else {
  27. if (controllers.service.clashServiceIsRuning) {
  28. await globalController.makeProxy();
  29. await reloadClashCore();
  30. }
  31. }
  32. } else {
  33. if (controllers.service.clashServiceIsRuning) {
  34. await globalController.makeProxy();
  35. await reloadClashCore();
  36. }
  37. }
  38. await globalController.updateNode();
  39. // 等待连接建立
  40. await Future.delayed(Duration(seconds: 2));
  41. if(Platform.isMacOS){
  42. if (serviceController.serviceIsRuning) {
  43. //await serviceController.fetchStartInit();
  44. await serviceController.fetchSetProxy();
  45. } else {
  46. if (controllers.service.clashServiceIsRuning) {
  47. globalController.connectStatus.value = true;
  48. await globalController.openProxy();
  49. } else {
  50. throw Exception("内核启动失败");
  51. }
  52. }
  53. } else {
  54. if (controllers.service.clashServiceIsRuning) {
  55. updateStatus(ConnectionStatus.connected);
  56. globalController.connectStatus.value = true;
  57. globalController.updateMsg("连接成功");
  58. } else {
  59. throw Exception("内核启动失败");
  60. }
  61. }
  62. updateStatus(ConnectionStatus.connected);
  63. globalController.updateMsg("连接成功");
  64. controllers.global.connectStatus.value = true;
  65. } catch (e) {
  66. updateStatus(ConnectionStatus.disconnected);
  67. globalController.handleApiError("连接失败: $e");
  68. }
  69. }
  70. Future<void> stopConnection() async {
  71. try {
  72. updateStatus(ConnectionStatus.disconnected);
  73. globalController.updateMsg("正在断开连接...");
  74. if(Platform.isMacOS){
  75. if (serviceController.serviceIsRuning) {
  76. await serviceController.stopClash();
  77. await serviceController.fetchSetProxyStop();
  78. } else {
  79. if (controllers.service.clashServiceIsRuning){
  80. await controllers.service.stopClash();
  81. await globalController.closeProxy();
  82. globalController.updateMsg("内核已重新加载");
  83. } else {
  84. globalController.updateMsg("内核启动失败");
  85. }
  86. }
  87. } else{
  88. if (controllers.service.clashServiceIsRuning){
  89. await controllers.service.stopClash();
  90. await globalController.closeProxy();
  91. globalController.updateMsg("内核已重新加载");
  92. } else {
  93. globalController.updateMsg("内核启动失败");
  94. }
  95. }
  96. globalController.connectStatus.value = false;
  97. updateStatus(ConnectionStatus.disconnected);
  98. globalController.updateMsg("已断开连接");
  99. controllers.global.connectStatus.value = false;
  100. } catch (e) {
  101. globalController.handleApiError("断开连接失败: $e");
  102. }
  103. }
  104. Future<void> reloadClashCore() async {
  105. try {
  106. globalController.updateMsg("正在重新加载内核...");
  107. if (serviceController.serviceIsRuning) {
  108. await serviceController.reloadClashCore();
  109. globalController.updateMsg("内核已重新加载");
  110. globalController.swift(globalController.selectedNode.value?.name ?? "");
  111. return;
  112. }
  113. if (controllers.service.clashServiceIsRuning){
  114. await controllers.service.reloadClashCore();
  115. globalController.updateMsg("内核已重新加载");
  116. globalController.swift(globalController.selectedNode.value?.name ?? "");
  117. } else {
  118. globalController.updateMsg("内核启动失败");
  119. }
  120. } catch (e) {
  121. globalController.handleApiError("重新加载内核失败: $e");
  122. }
  123. }
  124. Future<void> coreInit() async {
  125. try {
  126. globalController.updateMsg("正在初始化核心...");
  127. if (serviceController.installStatus.value) {
  128. //每次启动多要初始化
  129. await controllers.cc_service.fetchStartInit();
  130. } else {
  131. await controllers.service.initClashCoreConfig();
  132. }
  133. globalController.updateMsg("核心初始化完成");
  134. } catch (e) {
  135. globalController.updateMsg("失败...");
  136. globalController.handleApiError("核心初始化失败: $e");
  137. }
  138. }
  139. Future<void> installService() async{
  140. if(serviceController.serviceStatus.value == RunningState.stoped){
  141. controllers.service.stopClashCore();
  142. globalController.updateMsg("正在安装服务");
  143. await serviceController.serviceModeSwitch(true);
  144. }
  145. }
  146. Future<void> UninstallService() async{
  147. if(serviceController.serviceStatus.value == RunningState.running){
  148. controllers.global.updateMsg("正在卸载服务");
  149. await serviceController.serviceModeSwitch(false);
  150. }
  151. }
  152. }