connection_service.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. } else {
  109. controllers.global.systemProxySwitch(true);
  110. }
  111. updateStatus(ConnectionStatus.connected);
  112. globalController.updateMsg("连接成功");
  113. controllers.global.connectStatus.value = true;
  114. //检测系统代理并切换成网卡模式
  115. if(Platform.isWindows){
  116. if(!await proxyManager.isProxyEnabled()){
  117. globalController.updateMsg("当前没有设置系统代理,20秒后切换成网卡模式");
  118. await Future.delayed(Duration(seconds: 20)); // 等待核心状态更新
  119. // await globalController.TunProxySwitch(true);
  120. // await globalController.makeProxy();
  121. // await reloadClashCore();
  122. LogHelper().d("重置网卡模式");
  123. }
  124. }
  125. } catch (e) {
  126. updateStatus(ConnectionStatus.disconnected);
  127. globalController.handleApiError("连接失败: $e");
  128. }
  129. }
  130. Future<void> stopConnection() async {
  131. try {
  132. updateStatus(ConnectionStatus.disconnected);
  133. globalController.updateMsg("正在断开连接...");
  134. if(Platform.isMacOS){
  135. if (serviceController.serviceIsRuning) {
  136. await serviceController.stopClash();
  137. await serviceController.fetchSetProxyStop();
  138. } else {
  139. if (controllers.service.clashServiceIsRuning){
  140. await controllers.service.stopClash();
  141. await globalController.closeProxy();
  142. globalController.updateMsg("内核已重新加载");
  143. } else {
  144. globalController.updateMsg("内核启动失败");
  145. }
  146. }
  147. } else{
  148. if (controllers.service.clashServiceIsRuning){
  149. await controllers.service.stopClash();
  150. final proxyManager = WlBaseHelp();
  151. proxyManager.stopProxy();
  152. await globalController.closeProxy();
  153. globalController.updateMsg("内核已重新加载");
  154. } else {
  155. globalController.updateMsg("内核启动失败");
  156. }
  157. }
  158. globalController.connectStatus.value = false;
  159. updateStatus(ConnectionStatus.disconnected);
  160. globalController.updateMsg("已断开连接");
  161. controllers.global.connectStatus.value = false;
  162. } catch (e) {
  163. globalController.handleApiError("断开连接失败: $e");
  164. }
  165. }
  166. Future<void> reloadClashCore() async {
  167. try {
  168. globalController.updateMsg("正在重新加载内核...");
  169. if (serviceController.serviceIsRuning) {
  170. await serviceController.reloadClashCore();
  171. globalController.updateMsg("内核已重新加载");
  172. globalController.swift(globalController.selectedNode.value?.name ?? "");
  173. return;
  174. }
  175. if (controllers.service.clashServiceIsRuning){
  176. await controllers.service.reloadClashCore();
  177. globalController.updateMsg("内核已重新加载");
  178. globalController.swift(globalController.selectedNode.value?.name ?? "");
  179. } else {
  180. globalController.updateMsg("内核启动失败");
  181. }
  182. } catch (e) {
  183. globalController.handleApiError("重新加载内核失败: $e");
  184. }
  185. }
  186. Future<void> coreInit() async {
  187. try {
  188. globalController.updateMsg("正在初始化核心...");
  189. if (serviceController.installStatus.value) {
  190. //每次启动多要初始化
  191. await controllers.cc_service.fetchStartInit();
  192. } else {
  193. await controllers.service.initClashCoreConfig();
  194. }
  195. globalController.updateMsg("核心初始化完成");
  196. } catch (e) {
  197. globalController.updateMsg("失败...");
  198. globalController.handleApiError("核心初始化失败: $e");
  199. }
  200. }
  201. Future<void> installService() async{
  202. if(serviceController.serviceStatus.value == RunningState.stoped){
  203. controllers.service.stopClashCore();
  204. globalController.updateMsg("正在安装服务");
  205. await serviceController.serviceModeSwitch(true);
  206. }
  207. }
  208. Future<void> UninstallService() async{
  209. if(serviceController.serviceStatus.value == RunningState.running){
  210. controllers.global.updateMsg("正在卸载服务");
  211. await serviceController.serviceModeSwitch(false);
  212. }
  213. }
  214. }