connection_service.dart 9.2 KB

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