connection_service.dart 7.3 KB

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