connection_service.dart 8.2 KB

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