connection_service.dart 8.6 KB

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