connection_service.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // // 检查所有内核是否已停止
  20. // bool allCoresStopped = await globalController.checkAllCoresStopped();
  21. //
  22. // if (!allCoresStopped) {
  23. // // 检查并停止占用端口
  24. // await globalController.checkAndStopIfPortsOccupied();
  25. //
  26. // // 如果端口被占用,尝试更换端口
  27. // if (globalController.msgStatus.value == '端口被占用,已停止所有内核') {
  28. // // 尝试为每个端口找到新的可用端口
  29. // int newMixedPort = await findAvailablePort(controllers.config.mixedPort.value);
  30. // int newApiPort = await findAvailablePort(controllers.config.apiAddressPort.value);
  31. // int newServicePort = await findAvailablePort(controllers.config.servicePort.value);
  32. //
  33. // // 更新配置
  34. // controllers.config.mixedPort.value = newMixedPort;
  35. // controllers.config.apiAddressPort.value = newApiPort;
  36. // controllers.config.servicePort.value = newServicePort;
  37. //
  38. // // 保存新的配置
  39. // await controllers.config.saveConfig();
  40. //
  41. // globalController.updateMsg("端口已更新,正在重新启动...");
  42. // }
  43. // }
  44. updateStatus(ConnectionStatus.connecting);
  45. globalController.updateMsg("正在启动连接...");
  46. if(Platform.isMacOS){
  47. if (serviceController.serviceIsRuning) {
  48. await globalController.makeProxy();
  49. await serviceController.reloadClashCore();
  50. // await serviceController.fetchSetProxy();
  51. } else {
  52. if (controllers.service.clashServiceIsRuning) {
  53. await globalController.makeProxy();
  54. await reloadClashCore();
  55. }
  56. }
  57. } else {
  58. if (controllers.service.clashServiceIsRuning) {
  59. await globalController.makeProxy();
  60. await reloadClashCore();
  61. }
  62. }
  63. await globalController.updateNode();
  64. // 等待连接建立
  65. await Future.delayed(Duration(seconds: 2));
  66. if(Platform.isMacOS){
  67. if (serviceController.serviceIsRuning) {
  68. //await serviceController.fetchStartInit();
  69. await serviceController.fetchSetProxy();
  70. } else {
  71. if (controllers.service.clashServiceIsRuning) {
  72. globalController.connectStatus.value = true;
  73. await globalController.openProxy();
  74. } else {
  75. throw Exception("内核启动失败");
  76. }
  77. }
  78. } else {
  79. if (controllers.service.clashServiceIsRuning) {
  80. updateStatus(ConnectionStatus.connected);
  81. globalController.connectStatus.value = true;
  82. globalController.updateMsg("连接成功");
  83. } else {
  84. throw Exception("内核启动失败");
  85. }
  86. }
  87. updateStatus(ConnectionStatus.connected);
  88. globalController.updateMsg("连接成功");
  89. controllers.global.connectStatus.value = true;
  90. } catch (e) {
  91. updateStatus(ConnectionStatus.disconnected);
  92. globalController.handleApiError("连接失败: $e");
  93. }
  94. }
  95. Future<void> stopConnection() async {
  96. try {
  97. updateStatus(ConnectionStatus.disconnected);
  98. globalController.updateMsg("正在断开连接...");
  99. if(Platform.isMacOS){
  100. if (serviceController.serviceIsRuning) {
  101. await serviceController.stopClash();
  102. await serviceController.fetchSetProxyStop();
  103. } else {
  104. if (controllers.service.clashServiceIsRuning){
  105. await controllers.service.stopClash();
  106. await globalController.closeProxy();
  107. globalController.updateMsg("内核已重新加载");
  108. } else {
  109. globalController.updateMsg("内核启动失败");
  110. }
  111. }
  112. } else{
  113. if (controllers.service.clashServiceIsRuning){
  114. await controllers.service.stopClash();
  115. await globalController.closeProxy();
  116. globalController.updateMsg("内核已重新加载");
  117. } else {
  118. globalController.updateMsg("内核启动失败");
  119. }
  120. }
  121. globalController.connectStatus.value = false;
  122. updateStatus(ConnectionStatus.disconnected);
  123. globalController.updateMsg("已断开连接");
  124. controllers.global.connectStatus.value = false;
  125. } catch (e) {
  126. globalController.handleApiError("断开连接失败: $e");
  127. }
  128. }
  129. Future<void> reloadClashCore() async {
  130. try {
  131. globalController.updateMsg("正在重新加载内核...");
  132. if (serviceController.serviceIsRuning) {
  133. await serviceController.reloadClashCore();
  134. globalController.updateMsg("内核已重新加载");
  135. globalController.swift(globalController.selectedNode.value?.name ?? "");
  136. return;
  137. }
  138. if (controllers.service.clashServiceIsRuning){
  139. await controllers.service.reloadClashCore();
  140. globalController.updateMsg("内核已重新加载");
  141. globalController.swift(globalController.selectedNode.value?.name ?? "");
  142. } else {
  143. globalController.updateMsg("内核启动失败");
  144. }
  145. } catch (e) {
  146. globalController.handleApiError("重新加载内核失败: $e");
  147. }
  148. }
  149. Future<void> coreInit() async {
  150. try {
  151. globalController.updateMsg("正在初始化核心...");
  152. if (serviceController.installStatus.value) {
  153. //每次启动多要初始化
  154. await controllers.cc_service.fetchStartInit();
  155. } else {
  156. await controllers.service.initClashCoreConfig();
  157. }
  158. globalController.updateMsg("核心初始化完成");
  159. } catch (e) {
  160. globalController.updateMsg("失败...");
  161. globalController.handleApiError("核心初始化失败: $e");
  162. }
  163. }
  164. Future<void> installService() async{
  165. if(serviceController.serviceStatus.value == RunningState.stoped){
  166. controllers.service.stopClashCore();
  167. globalController.updateMsg("正在安装服务");
  168. await serviceController.serviceModeSwitch(true);
  169. }
  170. }
  171. Future<void> UninstallService() async{
  172. if(serviceController.serviceStatus.value == RunningState.running){
  173. controllers.global.updateMsg("正在卸载服务");
  174. await serviceController.serviceModeSwitch(false);
  175. }
  176. }
  177. }