connection_service.dart 8.5 KB

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