connection_service.dart 7.4 KB

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